Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #101355

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f46fffc
safe transmute: use `Assume` struct to provide analysis options
jswrenn Aug 18, 2022
1d844fe
safe transmute: use `FxIndex{Map,Set}` instead of `FxHash{Map,Set}`
jswrenn Aug 23, 2022
54645e8
set up rustc_metadata for SessionDiagnostics, port dependency_format.rs
CleanCut Aug 23, 2022
3ed9310
port native_libs.rs to SessionDiagnostics
CleanCut Aug 23, 2022
f7e462a
port encoder.rs to SessionDiagnostics
CleanCut Aug 23, 2022
32e1823
port creader.rs to SessionDiagnostics
CleanCut Aug 23, 2022
bd8e312
port fs.rs to SessionDiagnostics
CleanCut Aug 23, 2022
d0ba1fb
port of locator.rs to SessionDiagnostics, fix some of the errors
CleanCut Aug 24, 2022
0d65819
respond to review feedback: mainly eliminate as many conversions as p…
CleanCut Aug 26, 2022
30adfd6
port 5 new diagnostics that appeared in master
CleanCut Aug 27, 2022
fbcc038
safe transmute: use `to_valtree` to destructure const `Assume`
jswrenn Aug 31, 2022
e4e4114
Only keep one version of ImplicitSelfKind.
cjgillot Aug 27, 2022
f848d27
Use tcx.hir() utils for spans in MIR building.
cjgillot Aug 27, 2022
26037b1
Simplify MIR building entry.
cjgillot Aug 27, 2022
609a90d
Compute explicit MIR params on THIR.
cjgillot Aug 27, 2022
692064b
Also compute implicit params in THIR.
cjgillot Aug 22, 2022
8a594f0
Shrink some visibilities.
cjgillot Aug 29, 2022
d85dff7
Bless codegen test.
cjgillot Aug 30, 2022
b9e1806
Use def_span for external requirements.
cjgillot Aug 31, 2022
bc793c9
Use `BCRYPT_RNG_ALG_HANDLE` by default
ChrisDenton Sep 2, 2022
532d5f2
Fix `std::collections::HashSet::drain` documentation
wkordalski Sep 2, 2022
6fbc4d9
Fix unsupported syntax in .manifest file
diminishedprime Sep 2, 2022
df09047
rustdoc: remove unused CSS selector `.methods > .item-info`
notriddle Sep 2, 2022
6f95c89
rustdoc: remove incorrect CSS rule causing misaligned item-info
notriddle Sep 2, 2022
0f29824
rustdoc: put in rule to get alignment in desktop and mobile layouts
notriddle Sep 2, 2022
e135e37
Rollup merge of #100726 - jswrenn:transmute, r=oli-obk
matthiaskrgr Sep 3, 2022
6395082
Rollup merge of #100928 - CleanCut:rustc_metadata_diagnostics, r=davi…
matthiaskrgr Sep 3, 2022
2c0a225
Rollup merge of #101086 - cjgillot:thir-param, r=oli-obk
matthiaskrgr Sep 3, 2022
2739e70
Rollup merge of #101325 - ChrisDenton:BCRYPT_RNG_ALG_HANDLE, r=thomcc
matthiaskrgr Sep 3, 2022
afed4ff
Rollup merge of #101330 - wkordalski:hashset-drain-doc, r=jyn514
matthiaskrgr Sep 3, 2022
297793c
Rollup merge of #101335 - notriddle:notriddle/methods-stability, r=no…
matthiaskrgr Sep 3, 2022
f0fcbca
Rollup merge of #101338 - diminishedprime:patch-2, r=tmandry
matthiaskrgr Sep 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Compute explicit MIR params on THIR.
  • Loading branch information
cjgillot committed Sep 1, 2022
commit 609a90dcb65396ca26e45b051dbe20d9c3b4a17b
25 changes: 25 additions & 0 deletions compiler/rustc_middle/src/thir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ thir_with_elements! {
blocks: BlockId => Block => "b{}",
exprs: ExprId => Expr<'tcx> => "e{}",
stmts: StmtId => Stmt<'tcx> => "s{}",
params: ParamId => Param<'tcx> => "p{}",
}

/// Description of a type-checked function parameter.
#[derive(Clone, Debug, HashStable)]
pub struct Param<'tcx> {
/// The pattern that appears in the parameter list.
pub pat: Pat<'tcx>,
/// The possibly inferred type.
pub ty: Ty<'tcx>,
/// Span of the explicitly provided type, or None if inferred for closures.
pub ty_span: Option<Span>,
/// Whether this param is `self`, and how it is bound.
pub self_kind: Option<hir::ImplicitSelfKind>,
/// HirId for lints.
pub hir_id: hir::HirId,
}

#[derive(Copy, Clone, Debug, HashStable)]
Expand Down Expand Up @@ -548,6 +564,15 @@ impl<'tcx> Pat<'tcx> {
pub fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
}

pub fn simple_ident(&self) -> Option<Symbol> {
match &*self.kind {
PatKind::Binding { name, mode: BindingMode::ByValue, subpattern: None, .. } => {
Some(*name)
}
_ => None,
}
}
}

#[derive(Clone, Debug, HashStable)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,15 +573,15 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

_ => {
let place_builder = unpack!(block = self.as_place_builder(block, initializer));
self.place_into_pattern(block, irrefutable_pat, place_builder, true)
self.place_into_pattern(block, &irrefutable_pat, place_builder, true)
}
}
}

pub(crate) fn place_into_pattern(
&mut self,
block: BasicBlock,
irrefutable_pat: Pat<'tcx>,
irrefutable_pat: &Pat<'tcx>,
initializer: PlaceBuilder<'tcx>,
set_match_place: bool,
) -> BlockAnd<()> {
Expand Down
62 changes: 12 additions & 50 deletions compiler/rustc_mir_build/src/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
pub(crate) use crate::build::expr::as_constant::lit_to_mir_constant;
use crate::build::expr::as_place::PlaceBuilder;
use crate::build::scope::DropKind;
use crate::thir::pattern::pat_from_hir;
use rustc_apfloat::ieee::{Double, Single};
use rustc_apfloat::Float;
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{GeneratorKind, ImplicitSelfKind, Node};
use rustc_index::vec::{Idx, IndexVec};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
Expand All @@ -17,8 +15,7 @@ use rustc_middle::middle::region;
use rustc_middle::mir::interpret::ConstValue;
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::*;
use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, LocalVarId, PatKind, Thir};
use rustc_middle::ty::subst::Subst;
use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, LocalVarId, Param, PatKind, Thir};
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable, TypeckResults};
use rustc_span::symbol::sym;
use rustc_span::Span;
Expand Down Expand Up @@ -439,10 +436,10 @@ macro_rules! unpack {
///////////////////////////////////////////////////////////////////////////
/// the main entry point for building MIR for a function

struct ArgInfo<'tcx>(
struct ArgInfo<'thir, 'tcx>(
Ty<'tcx>,
Option<Span>,
Option<&'tcx hir::Param<'tcx>>,
Option<&'thir Param<'tcx>>,
Option<ImplicitSelfKind>,
);

Expand Down Expand Up @@ -500,38 +497,8 @@ fn construct_fn<'tcx>(
_ => vec![],
};

let explicit_arguments = body.params.iter().enumerate().map(|(index, arg)| {
let owner_id = tcx.hir().body_owner(body_id);
let opt_ty_info;
let self_arg;
if let Some(ref fn_decl) = tcx.hir().fn_decl_by_hir_id(owner_id) {
opt_ty_info = fn_decl
.inputs
.get(index)
// Make sure that inferred closure args have no type span
.and_then(|ty| if arg.pat.span != ty.span { Some(ty.span) } else { None });
self_arg = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
Some(fn_decl.implicit_self)
} else {
None
};
} else {
opt_ty_info = None;
self_arg = None;
}

// C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
// (as it's created inside the body itself, not passed in from outside).
let ty = if fn_sig.c_variadic && index == fn_sig.inputs().len() {
let va_list_did = tcx.require_lang_item(LangItem::VaList, Some(arg.span));

tcx.bound_type_of(va_list_did).subst(tcx, &[tcx.lifetimes.re_erased.into()])
} else {
fn_sig.inputs()[index]
};

ArgInfo(ty, opt_ty_info, Some(&arg), self_arg)
});
let explicit_arguments =
thir.params.iter().map(|arg| ArgInfo(arg.ty, arg.ty_span, Some(&arg), arg.self_kind));

let arguments = implicit_argument.into_iter().chain(explicit_arguments);

Expand Down Expand Up @@ -842,7 +809,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
mut block: BasicBlock,
fn_def_id: LocalDefId,
arguments: &[ArgInfo<'tcx>],
arguments: &[ArgInfo<'_, 'tcx>],
argument_scope: region::Scope,
expr: &Expr<'tcx>,
) -> BlockAnd<()> {
Expand All @@ -853,9 +820,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));

// If this is a simple binding pattern, give debuginfo a nice name.
if let Some(arg) = arg_opt && let Some(ident) = arg.pat.simple_ident() {
if let Some(arg) = arg_opt && let Some(name) = arg.pat.simple_ident() {
self.var_debug_info.push(VarDebugInfo {
name: ident.name,
name,
source_info,
value: VarDebugInfoContents::Place(arg_local.into()),
});
Expand Down Expand Up @@ -943,15 +910,10 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
let Some(arg) = arg_opt else {
continue;
};
let pat = match tcx.hir().get(arg.pat.hir_id) {
Node::Pat(pat) => pat,
node => bug!("pattern became {:?}", node),
};
let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
let original_source_scope = self.source_scope;
let span = pattern.span;
let span = arg.pat.span;
self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
match *pattern.kind {
match *arg.pat.kind {
// Don't introduce extra copies for simple bindings
PatKind::Binding {
mutability,
Expand Down Expand Up @@ -983,12 +945,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
scope = self.declare_bindings(
scope,
expr.span,
&pattern,
&arg.pat,
matches::ArmHasGuard(false),
Some((Some(&place), span)),
);
let place_builder = PlaceBuilder::from(local);
unpack!(block = self.place_into_pattern(block, pattern, place_builder, false));
unpack!(block = self.place_into_pattern(block, &arg.pat, place_builder, false));
}
}
self.source_scope = original_source_scope;
Expand Down
48 changes: 47 additions & 1 deletion compiler/rustc_mir_build/src/thir/cx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ use rustc_data_structures::steal::Steal;
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::HirId;
use rustc_hir::Node;
use rustc_middle::middle::region;
use rustc_middle::thir::*;
use rustc_middle::ty::{self, RvalueScopes, TyCtxt};
use rustc_middle::ty::{self, RvalueScopes, Subst, TyCtxt};
use rustc_span::Span;

pub(crate) fn thir_body<'tcx>(
Expand All @@ -27,6 +28,13 @@ pub(crate) fn thir_body<'tcx>(
return Err(reported);
}
let expr = cx.mirror_expr(&body.value);

let owner_id = hir.local_def_id_to_hir_id(owner_def.did);
if let Some(ref fn_decl) = hir.fn_decl_by_hir_id(owner_id) {
let explicit_params = cx.explicit_params(owner_id, fn_decl, body);
cx.thir.params = explicit_params.collect();
}

Ok((tcx.alloc_steal_thir(cx.thir), expr))
}

Expand Down Expand Up @@ -85,6 +93,44 @@ impl<'tcx> Cx<'tcx> {
};
pat_from_hir(self.tcx, self.param_env, self.typeck_results(), p)
}

fn explicit_params<'a>(
&'a mut self,
owner_id: HirId,
fn_decl: &'tcx hir::FnDecl<'tcx>,
body: &'tcx hir::Body<'tcx>,
) -> impl Iterator<Item = Param<'tcx>> + 'a {
let fn_sig = self.typeck_results.liberated_fn_sigs()[owner_id];

body.params.iter().enumerate().map(move |(index, param)| {
let ty_span = fn_decl
.inputs
.get(index)
// Make sure that inferred closure args have no type span
.and_then(|ty| if param.pat.span != ty.span { Some(ty.span) } else { None });

let self_kind = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
Some(fn_decl.implicit_self)
} else {
None
};

// C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
// (as it's created inside the body itself, not passed in from outside).
let ty = if fn_decl.c_variadic && index == fn_decl.inputs.len() {
let va_list_did = self.tcx.require_lang_item(LangItem::VaList, Some(param.span));

self.tcx
.bound_type_of(va_list_did)
.subst(self.tcx, &[self.tcx.lifetimes.re_erased.into()])
} else {
fn_sig.inputs()[index]
};

let pat = self.pattern_from_hir(param.pat);
Param { pat, ty, ty_span, self_kind, hir_id: param.hir_id }
})
}
}

impl<'tcx> UserAnnotatedTyHelpers<'tcx> for Cx<'tcx> {
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/thir-tree.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ Thir {
},
],
stmts: [],
params: [],
}