diff options
Diffstat (limited to '')
-rw-r--r-- | compiler/rustc_mir_build/src/build/mod.rs | 616 |
1 files changed, 246 insertions, 370 deletions
diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 12b8ceede..25c4e51cb 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -1,15 +1,14 @@ -use crate::build; 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_data_structures::sorted_map::SortedIndexMultiMap; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; +use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; -use rustc_hir::lang_items::LangItem; use rustc_hir::{GeneratorKind, Node}; use rustc_index::vec::{Idx, IndexVec}; use rustc_infer::infer::{InferCtxt, TyCtxtInferExt}; @@ -18,8 +17,9 @@ 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::{ + self, BindingMode, Expr, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir, +}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable, TypeckResults}; use rustc_span::symbol::sym; use rustc_span::Span; @@ -47,9 +47,7 @@ pub(crate) fn mir_built<'tcx>( /// Construct the MIR for a given `DefId`. fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> { - let id = tcx.hir().local_def_id_to_hir_id(def.did); let body_owner_kind = tcx.hir().body_owner_kind(def.did); - let typeck_results = tcx.typeck_opt_const_arg(def); // Ensure unsafeck and abstract const building is ran before we steal the THIR. // We can't use `ensure()` for `thir_abstract_const` as it doesn't compute the query @@ -66,235 +64,42 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_ } } - // Figure out what primary body this item has. - let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) { - Node::Expr(hir::Expr { - kind: hir::ExprKind::Closure(hir::Closure { fn_decl, body, .. }), - .. - }) => (*body, fn_decl.output.span(), None), - Node::Item(hir::Item { - kind: hir::ItemKind::Fn(hir::FnSig { decl, .. }, _, body_id), - span, - .. - }) - | Node::ImplItem(hir::ImplItem { - kind: hir::ImplItemKind::Fn(hir::FnSig { decl, .. }, body_id), - span, - .. - }) - | Node::TraitItem(hir::TraitItem { - kind: hir::TraitItemKind::Fn(hir::FnSig { decl, .. }, hir::TraitFn::Provided(body_id)), - span, - .. - }) => { - // Use the `Span` of the `Item/ImplItem/TraitItem` as the body span, - // since the def span of a function does not include the body - (*body_id, decl.output.span(), Some(*span)) - } - Node::Item(hir::Item { - kind: hir::ItemKind::Static(ty, _, body_id) | hir::ItemKind::Const(ty, body_id), - .. - }) - | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, body_id), .. }) - | Node::TraitItem(hir::TraitItem { - kind: hir::TraitItemKind::Const(ty, Some(body_id)), - .. - }) => (*body_id, ty.span, None), - Node::AnonConst(hir::AnonConst { body, hir_id, .. }) => { - (*body, tcx.hir().span(*hir_id), None) - } - - _ => span_bug!(tcx.hir().span(id), "can't build MIR for {:?}", def.did), - }; - - // If we don't have a specialized span for the body, just use the - // normal def span. - let span_with_body = span_with_body.unwrap_or_else(|| tcx.hir().span(id)); - - tcx.infer_ctxt().enter(|infcx| { - let body = if let Some(error_reported) = typeck_results.tainted_by_errors { - build::construct_error(&infcx, def, id, body_id, body_owner_kind, error_reported) - } else if body_owner_kind.is_fn_or_closure() { - // fetch the fully liberated fn signature (that is, all bound - // types/lifetimes replaced) - let fn_sig = typeck_results.liberated_fn_sigs()[id]; - let fn_def_id = tcx.hir().local_def_id(id); - - let safety = match fn_sig.unsafety { - hir::Unsafety::Normal => Safety::Safe, - hir::Unsafety::Unsafe => Safety::FnUnsafe, - }; - - let body = tcx.hir().body(body_id); - let (thir, expr) = tcx - .thir_body(def) - .unwrap_or_else(|_| (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0))); + let body = match tcx.thir_body(def) { + Err(error_reported) => construct_error(tcx, def.did, body_owner_kind, error_reported), + Ok((thir, expr)) => { // We ran all queries that depended on THIR at the beginning // of `mir_build`, so now we can steal it let thir = thir.steal(); - let ty = tcx.type_of(fn_def_id); - let mut abi = fn_sig.abi; - let implicit_argument = match ty.kind() { - ty::Closure(..) => { - // HACK(eddyb) Avoid having RustCall on closures, - // as it adds unnecessary (and wrong) auto-tupling. - abi = Abi::Rust; - vec![ArgInfo(liberated_closure_env_ty(tcx, id, body_id), None, None, None)] - } - ty::Generator(..) => { - let gen_ty = tcx.typeck_body(body_id).node_type(id); - - // The resume argument may be missing, in that case we need to provide it here. - // It will always be `()` in this case. - if body.params.is_empty() { - vec![ - ArgInfo(gen_ty, None, None, None), - ArgInfo(tcx.mk_unit(), None, None, None), - ] - } else { - vec![ArgInfo(gen_ty, None, None, None)] - } - } - _ => 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() { - match fn_decl.implicit_self { - hir::ImplicitSelfKind::Imm => Some(ImplicitSelfKind::Imm), - hir::ImplicitSelfKind::Mut => Some(ImplicitSelfKind::Mut), - hir::ImplicitSelfKind::ImmRef => Some(ImplicitSelfKind::ImmRef), - hir::ImplicitSelfKind::MutRef => Some(ImplicitSelfKind::MutRef), - _ => None, - } - } 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 arguments = implicit_argument.into_iter().chain(explicit_arguments); - - let (yield_ty, return_ty) = if body.generator_kind.is_some() { - let gen_ty = tcx.typeck_body(body_id).node_type(id); - let gen_sig = match gen_ty.kind() { - ty::Generator(_, gen_substs, ..) => gen_substs.as_generator().sig(), - _ => span_bug!(tcx.hir().span(id), "generator w/o generator type: {:?}", ty), - }; - (Some(gen_sig.yield_ty), gen_sig.return_ty) + if body_owner_kind.is_fn_or_closure() { + construct_fn(tcx, def, &thir, expr) } else { - (None, fn_sig.output()) - }; - - let mut mir = build::construct_fn( - &thir, - &infcx, - def, - id, - arguments, - safety, - abi, - return_ty, - return_ty_span, - body, - expr, - span_with_body, - ); - if yield_ty.is_some() { - mir.generator.as_mut().unwrap().yield_ty = yield_ty; + construct_const(tcx, def, &thir, expr) } - mir - } else { - // Get the revealed type of this const. This is *not* the adjusted - // type of its body, which may be a subtype of this type. For - // example: - // - // fn foo(_: &()) {} - // static X: fn(&'static ()) = foo; - // - // The adjusted type of the body of X is `for<'a> fn(&'a ())` which - // is not the same as the type of X. We need the type of the return - // place to be the type of the constant because NLL typeck will - // equate them. - - let return_ty = typeck_results.node_type(id); - - let (thir, expr) = tcx - .thir_body(def) - .unwrap_or_else(|_| (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0))); - // We ran all queries that depended on THIR at the beginning - // of `mir_build`, so now we can steal it - let thir = thir.steal(); - - build::construct_const(&thir, &infcx, expr, def, id, return_ty, return_ty_span) - }; + } + }; - lints::check(tcx, &body); - - // The borrow checker will replace all the regions here with its own - // inference variables. There's no point having non-erased regions here. - // The exception is `body.user_type_annotations`, which is used unmodified - // by borrow checking. - debug_assert!( - !(body.local_decls.has_free_regions() - || body.basic_blocks().has_free_regions() - || body.var_debug_info.has_free_regions() - || body.yield_ty().has_free_regions()), - "Unexpected free regions in MIR: {:?}", - body, - ); + lints::check(tcx, &body); + + // The borrow checker will replace all the regions here with its own + // inference variables. There's no point having non-erased regions here. + // The exception is `body.user_type_annotations`, which is used unmodified + // by borrow checking. + debug_assert!( + !(body.local_decls.has_free_regions() + || body.basic_blocks.has_free_regions() + || body.var_debug_info.has_free_regions() + || body.yield_ty().has_free_regions()), + "Unexpected free regions in MIR: {:?}", + body, + ); - body - }) + body } /////////////////////////////////////////////////////////////////////////// // BuildMir -- walks a crate, looking for fn items and methods to build MIR from -fn liberated_closure_env_ty( - tcx: TyCtxt<'_>, - closure_expr_id: hir::HirId, - body_id: hir::BodyId, -) -> Ty<'_> { - let closure_ty = tcx.typeck_body(body_id).node_type(closure_expr_id); - - let ty::Closure(closure_def_id, closure_substs) = *closure_ty.kind() else { - bug!("closure expr does not have closure type: {:?}", closure_ty); - }; - - let bound_vars = - tcx.mk_bound_variable_kinds(std::iter::once(ty::BoundVariableKind::Region(ty::BrEnv))); - let br = - ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind: ty::BrEnv }; - let env_region = ty::ReLateBound(ty::INNERMOST, br); - let closure_env_ty = tcx.closure_env_ty(closure_def_id, closure_substs, env_region).unwrap(); - tcx.erase_late_bound_regions(ty::Binder::bind_with_vars(closure_env_ty, bound_vars)) -} - #[derive(Debug, PartialEq, Eq)] enum BlockFrame { /// Evaluation is currently within a statement. @@ -352,7 +157,7 @@ struct BlockContext(Vec<BlockFrame>); struct Builder<'a, 'tcx> { tcx: TyCtxt<'tcx>, - infcx: &'a InferCtxt<'a, 'tcx>, + infcx: InferCtxt<'a, 'tcx>, typeck_results: &'tcx TypeckResults<'tcx>, region_scope_tree: &'tcx region::ScopeTree, param_env: ty::ParamEnv<'tcx>, @@ -404,12 +209,21 @@ struct Builder<'a, 'tcx> { var_indices: FxHashMap<LocalVarId, LocalsForNode>, local_decls: IndexVec<Local, LocalDecl<'tcx>>, canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>, - upvar_mutbls: Vec<Mutability>, + upvars: CaptureMap<'tcx>, unit_temp: Option<Place<'tcx>>, var_debug_info: Vec<VarDebugInfo<'tcx>>, } +type CaptureMap<'tcx> = SortedIndexMultiMap<usize, hir::HirId, Capture<'tcx>>; + +#[derive(Debug)] +struct Capture<'tcx> { + captured_place: &'tcx ty::CapturedPlace<'tcx>, + use_place: Place<'tcx>, + mutability: Mutability, +} + impl<'a, 'tcx> Builder<'a, 'tcx> { fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool { self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id)) @@ -615,149 +429,212 @@ macro_rules! unpack { /////////////////////////////////////////////////////////////////////////// /// the main entry point for building MIR for a function -struct ArgInfo<'tcx>( - Ty<'tcx>, - Option<Span>, - Option<&'tcx hir::Param<'tcx>>, - Option<ImplicitSelfKind>, -); - -fn construct_fn<'tcx, A>( - thir: &Thir<'tcx>, - infcx: &InferCtxt<'_, 'tcx>, +fn construct_fn<'tcx>( + tcx: TyCtxt<'tcx>, fn_def: ty::WithOptConstParam<LocalDefId>, - fn_id: hir::HirId, - arguments: A, - safety: Safety, - abi: Abi, - return_ty: Ty<'tcx>, - return_ty_span: Span, - body: &'tcx hir::Body<'tcx>, + thir: &Thir<'tcx>, expr: ExprId, - span_with_body: Span, -) -> Body<'tcx> -where - A: Iterator<Item = ArgInfo<'tcx>>, -{ - let arguments: Vec<_> = arguments.collect(); - - let tcx = infcx.tcx; - let span = tcx.hir().span(fn_id); - - let mut builder = Builder::new( - thir, - infcx, - fn_def, - fn_id, - span_with_body, - arguments.len(), - safety, - return_ty, - return_ty_span, - body.generator_kind, - ); +) -> Body<'tcx> { + let span = tcx.def_span(fn_def.did); + let fn_id = tcx.hir().local_def_id_to_hir_id(fn_def.did); + let generator_kind = tcx.generator_kind(fn_def.did); - let call_site_scope = - region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite }; - let arg_scope = - region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::Arguments }; - let source_info = builder.source_info(span); - let call_site_s = (call_site_scope, source_info); - unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| { - let arg_scope_s = (arg_scope, source_info); - // Attribute epilogue to function's closing brace - let fn_end = span_with_body.shrink_to_hi(); - let return_block = - unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| { - Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| { - builder.args_and_body( - START_BLOCK, - fn_def.did, - &arguments, - arg_scope, - &thir[expr], - ) - })) - })); - let source_info = builder.source_info(fn_end); - builder.cfg.terminate(return_block, source_info, TerminatorKind::Return); - builder.build_drop_trees(); - return_block.unit() - })); + // Figure out what primary body this item has. + let body_id = tcx.hir().body_owned_by(fn_def.did); + let span_with_body = tcx.hir().span_with_body(fn_id); + let return_ty_span = tcx + .hir() + .fn_decl_by_hir_id(fn_id) + .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def.did)) + .output + .span(); + + // fetch the fully liberated fn signature (that is, all bound + // types/lifetimes replaced) + let typeck_results = tcx.typeck_opt_const_arg(fn_def); + let fn_sig = typeck_results.liberated_fn_sigs()[fn_id]; + + let safety = match fn_sig.unsafety { + hir::Unsafety::Normal => Safety::Safe, + hir::Unsafety::Unsafe => Safety::FnUnsafe, + }; + + let mut abi = fn_sig.abi; + if let DefKind::Closure = tcx.def_kind(fn_def.did) { + // HACK(eddyb) Avoid having RustCall on closures, + // as it adds unnecessary (and wrong) auto-tupling. + abi = Abi::Rust; + } + + let arguments = &thir.params; + + let (yield_ty, return_ty) = if generator_kind.is_some() { + let gen_ty = arguments[thir::UPVAR_ENV_PARAM].ty; + let gen_sig = match gen_ty.kind() { + ty::Generator(_, gen_substs, ..) => gen_substs.as_generator().sig(), + _ => { + span_bug!(span, "generator w/o generator type: {:?}", gen_ty) + } + }; + (Some(gen_sig.yield_ty), gen_sig.return_ty) + } else { + (None, fn_sig.output()) + }; + + let mut body = tcx.infer_ctxt().enter(|infcx| { + let mut builder = Builder::new( + thir, + infcx, + fn_def, + fn_id, + span_with_body, + arguments.len(), + safety, + return_ty, + return_ty_span, + generator_kind, + ); + + let call_site_scope = + region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::CallSite }; + let arg_scope = + region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::Arguments }; + let source_info = builder.source_info(span); + let call_site_s = (call_site_scope, source_info); + unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| { + let arg_scope_s = (arg_scope, source_info); + // Attribute epilogue to function's closing brace + let fn_end = span_with_body.shrink_to_hi(); + let return_block = unpack!(builder.in_breakable_scope( + None, + Place::return_place(), + fn_end, + |builder| { + Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| { + builder.args_and_body( + START_BLOCK, + fn_def.did, + arguments, + arg_scope, + &thir[expr], + ) + })) + } + )); + let source_info = builder.source_info(fn_end); + builder.cfg.terminate(return_block, source_info, TerminatorKind::Return); + builder.build_drop_trees(); + return_block.unit() + })); + + builder.finish() + }); - let spread_arg = if abi == Abi::RustCall { + body.spread_arg = if abi == Abi::RustCall { // RustCall pseudo-ABI untuples the last argument. Some(Local::new(arguments.len())) } else { None }; - - let mut body = builder.finish(); - body.spread_arg = spread_arg; + if yield_ty.is_some() { + body.generator.as_mut().unwrap().yield_ty = yield_ty; + } body } fn construct_const<'a, 'tcx>( + tcx: TyCtxt<'tcx>, + def: ty::WithOptConstParam<LocalDefId>, thir: &'a Thir<'tcx>, - infcx: &'a InferCtxt<'a, 'tcx>, expr: ExprId, - def: ty::WithOptConstParam<LocalDefId>, - hir_id: hir::HirId, - const_ty: Ty<'tcx>, - const_ty_span: Span, ) -> Body<'tcx> { - let tcx = infcx.tcx; - let span = tcx.hir().span(hir_id); - let mut builder = Builder::new( - thir, - infcx, - def, - hir_id, - span, - 0, - Safety::Safe, - const_ty, - const_ty_span, - None, - ); + let hir_id = tcx.hir().local_def_id_to_hir_id(def.did); + + // Figure out what primary body this item has. + let (span, const_ty_span) = match tcx.hir().get(hir_id) { + Node::Item(hir::Item { + kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _), + span, + .. + }) + | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. }) + | Node::TraitItem(hir::TraitItem { + kind: hir::TraitItemKind::Const(ty, Some(_)), + span, + .. + }) => (*span, ty.span), + Node::AnonConst(_) => { + let span = tcx.def_span(def.did); + (span, span) + } + _ => span_bug!(tcx.def_span(def.did), "can't build MIR for {:?}", def.did), + }; + + // Get the revealed type of this const. This is *not* the adjusted + // type of its body, which may be a subtype of this type. For + // example: + // + // fn foo(_: &()) {} + // static X: fn(&'static ()) = foo; + // + // The adjusted type of the body of X is `for<'a> fn(&'a ())` which + // is not the same as the type of X. We need the type of the return + // place to be the type of the constant because NLL typeck will + // equate them. + let typeck_results = tcx.typeck_opt_const_arg(def); + let const_ty = typeck_results.node_type(hir_id); + + tcx.infer_ctxt().enter(|infcx| { + let mut builder = Builder::new( + thir, + infcx, + def, + hir_id, + span, + 0, + Safety::Safe, + const_ty, + const_ty_span, + None, + ); - let mut block = START_BLOCK; - unpack!(block = builder.expr_into_dest(Place::return_place(), block, &thir[expr])); + let mut block = START_BLOCK; + unpack!(block = builder.expr_into_dest(Place::return_place(), block, &thir[expr])); - let source_info = builder.source_info(span); - builder.cfg.terminate(block, source_info, TerminatorKind::Return); + let source_info = builder.source_info(span); + builder.cfg.terminate(block, source_info, TerminatorKind::Return); - builder.build_drop_trees(); + builder.build_drop_trees(); - builder.finish() + builder.finish() + }) } /// Construct MIR for an item that has had errors in type checking. /// /// This is required because we may still want to run MIR passes on an item /// with type errors, but normal MIR construction can't handle that in general. -fn construct_error<'a, 'tcx>( - infcx: &'a InferCtxt<'a, 'tcx>, - def: ty::WithOptConstParam<LocalDefId>, - hir_id: hir::HirId, - body_id: hir::BodyId, +fn construct_error<'tcx>( + tcx: TyCtxt<'tcx>, + def: LocalDefId, body_owner_kind: hir::BodyOwnerKind, err: ErrorGuaranteed, ) -> Body<'tcx> { - let tcx = infcx.tcx; - let span = tcx.hir().span(hir_id); + let span = tcx.def_span(def); + let hir_id = tcx.hir().local_def_id_to_hir_id(def); + let generator_kind = tcx.generator_kind(def); + let ty = tcx.ty_error(); - let generator_kind = tcx.hir().body(body_id).generator_kind; let num_params = match body_owner_kind { - hir::BodyOwnerKind::Fn => tcx.hir().fn_decl_by_hir_id(hir_id).unwrap().inputs.len(), + hir::BodyOwnerKind::Fn => tcx.fn_sig(def).inputs().skip_binder().len(), hir::BodyOwnerKind::Closure => { - if generator_kind.is_some() { - // Generators have an implicit `self` parameter *and* a possibly - // implicit resume parameter. - 2 - } else { - // The implicit self parameter adds another local in MIR. - 1 + tcx.hir().fn_decl_by_hir_id(hir_id).unwrap().inputs.len() + let ty = tcx.type_of(def); + match ty.kind() { + ty::Closure(_, substs) => { + 1 + substs.as_closure().sig().inputs().skip_binder().len() + } + ty::Generator(..) => 2, + _ => bug!("expected closure or generator, found {ty:?}"), } } hir::BodyOwnerKind::Const => 0, @@ -788,7 +665,7 @@ fn construct_error<'a, 'tcx>( cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable); let mut body = Body::new( - MirSource::item(def.did.to_def_id()), + MirSource::item(def.to_def_id()), cfg.basic_blocks, source_scopes, local_decls, @@ -806,7 +683,7 @@ fn construct_error<'a, 'tcx>( impl<'a, 'tcx> Builder<'a, 'tcx> { fn new( thir: &'a Thir<'tcx>, - infcx: &'a InferCtxt<'a, 'tcx>, + infcx: InferCtxt<'a, 'tcx>, def: ty::WithOptConstParam<LocalDefId>, hir_id: hir::HirId, span: Span, @@ -855,7 +732,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { in_scope_unsafe: safety, local_decls: IndexVec::from_elem_n(LocalDecl::new(return_ty, return_span), 1), canonical_user_type_annotations: IndexVec::new(), - upvar_mutbls: vec![], + upvars: CaptureMap::new(), var_indices: Default::default(), unit_temp: None, var_debug_info: vec![], @@ -896,20 +773,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { &mut self, mut block: BasicBlock, fn_def_id: LocalDefId, - arguments: &[ArgInfo<'tcx>], + arguments: &IndexVec<ParamId, Param<'tcx>>, argument_scope: region::Scope, expr: &Expr<'tcx>, ) -> BlockAnd<()> { // Allocate locals for the function arguments - for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() { + for param in arguments.iter() { let source_info = - SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span)); - let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info)); + SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span)); + let arg_local = + self.local_decls.push(LocalDecl::with_source_info(param.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(ref pat) = param.pat && let Some(name) = pat.simple_ident() { self.var_debug_info.push(VarDebugInfo { - name: ident.name, + name, source_info, value: VarDebugInfoContents::Place(arg_local.into()), }); @@ -924,7 +802,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { // indexed closure and we stored in a map called closure_min_captures in TypeckResults // with the closure's DefId. Here, we run through that vec of UpvarIds for // the given closure and use the necessary information to create upvar - // debuginfo and to fill `self.upvar_mutbls`. + // debuginfo and to fill `self.upvars`. if hir_typeck_results.closure_min_captures.get(&fn_def_id).is_some() { let mut closure_env_projs = vec![]; let mut closure_ty = self.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty; @@ -944,7 +822,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { .closure_min_captures_flattened(fn_def_id) .zip(capture_tys.zip(capture_syms)); - self.upvar_mutbls = captures_with_tys + self.upvars = captures_with_tys .enumerate() .map(|(i, (captured_place, (ty, sym)))| { let capture = captured_place.info.capture_kind; @@ -964,48 +842,46 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } }; + let use_place = Place { + local: ty::CAPTURE_STRUCT_LOCAL, + projection: tcx.intern_place_elems(&projs), + }; self.var_debug_info.push(VarDebugInfo { name: *sym, source_info: SourceInfo::outermost(tcx_hir.span(var_id)), - value: VarDebugInfoContents::Place(Place { - local: ty::CAPTURE_STRUCT_LOCAL, - projection: tcx.intern_place_elems(&projs), - }), + value: VarDebugInfoContents::Place(use_place), }); - mutability + let capture = Capture { captured_place, use_place, mutability }; + (var_id, capture) }) .collect(); } let mut scope = None; // Bind the argument patterns - for (index, arg_info) in arguments.iter().enumerate() { + for (index, param) in arguments.iter().enumerate() { // Function arguments always get the first Local indices after the return place let local = Local::new(index + 1); let place = Place::from(local); - let &ArgInfo(_, opt_ty_info, arg_opt, ref self_binding) = arg_info; // Make sure we drop (parts of) the argument even when not matched on. self.schedule_drop( - arg_opt.as_ref().map_or(expr.span, |arg| arg.pat.span), + param.pat.as_ref().map_or(expr.span, |pat| pat.span), argument_scope, local, DropKind::Value, ); - let Some(arg) = arg_opt else { + let Some(ref pat) = param.pat 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; - self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span); - match *pattern.kind { + let span = pat.span; + if let Some(arg_hir_id) = param.hir_id { + self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span); + } + match pat.kind { // Don't introduce extra copies for simple bindings PatKind::Binding { mutability, @@ -1016,17 +892,17 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { } => { self.local_decls[local].mutability = mutability; self.local_decls[local].source_info.scope = self.source_scope; - self.local_decls[local].local_info = if let Some(kind) = self_binding { + self.local_decls[local].local_info = if let Some(kind) = param.self_kind { Some(Box::new(LocalInfo::User(ClearCrossCrate::Set( - BindingForm::ImplicitSelf(*kind), + BindingForm::ImplicitSelf(kind), )))) } else { let binding_mode = ty::BindingMode::BindByValue(mutability); Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var( VarBindingForm { binding_mode, - opt_ty_info, - opt_match_place: Some((Some(place), span)), + opt_ty_info: param.ty_span, + opt_match_place: Some((None, span)), pat_span: span, }, ))))) @@ -1037,12 +913,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { scope = self.declare_bindings( scope, expr.span, - &pattern, + &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, &pat, place_builder, false)); } } self.source_scope = original_source_scope; |