1 pub(crate) use crate::build::expr::as_constant::lit_to_mir_constant;
2 use crate::build::expr::as_place::PlaceBuilder;
3 use crate::build::scope::DropKind;
4 use rustc_apfloat::ieee::{Double, Single};
5 use rustc_apfloat::Float;
6 use rustc_data_structures::fx::FxHashMap;
7 use rustc_data_structures::sorted_map::SortedIndexMultiMap;
8 use rustc_errors::ErrorGuaranteed;
10 use rustc_hir::def::DefKind;
11 use rustc_hir::def_id::{DefId, LocalDefId};
12 use rustc_hir::{GeneratorKind, Node};
13 use rustc_index::vec::{Idx, IndexVec};
14 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
15 use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
16 use rustc_middle::middle::region;
17 use rustc_middle::mir::interpret::ConstValue;
18 use rustc_middle::mir::interpret::Scalar;
19 use rustc_middle::mir::*;
20 use rustc_middle::thir::{
21 self, BindingMode, Expr, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir,
23 use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable, TypeckResults};
24 use rustc_span::symbol::sym;
26 use rustc_span::Symbol;
27 use rustc_target::spec::abi::Abi;
31 pub(crate) fn mir_built(
33 def: ty::WithOptConstParam<LocalDefId>,
34 ) -> &rustc_data_structures::steal::Steal<Body<'_>> {
35 if let Some(def) = def.try_upgrade(tcx) {
36 return tcx.mir_built(def);
39 let mut body = mir_build(tcx, def);
40 if def.const_param_did.is_some() {
41 assert!(matches!(body.source.instance, ty::InstanceDef::Item(_)));
42 body.source = MirSource::from_instance(ty::InstanceDef::Item(def.to_global()));
45 tcx.alloc_steal_mir(body)
48 /// Construct the MIR for a given `DefId`.
49 fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
50 let body_owner_kind = tcx.hir().body_owner_kind(def.did);
52 // Ensure unsafeck and abstract const building is ran before we steal the THIR.
53 // We can't use `ensure()` for `thir_abstract_const` as it doesn't compute the query
54 // if inputs are green. This can cause ICEs when calling `thir_abstract_const` after
55 // THIR has been stolen if we haven't computed this query yet.
57 ty::WithOptConstParam { did, const_param_did: Some(const_param_did) } => {
58 tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did));
59 drop(tcx.thir_abstract_const_of_const_arg((did, const_param_did)));
61 ty::WithOptConstParam { did, const_param_did: None } => {
62 tcx.ensure().thir_check_unsafety(did);
63 drop(tcx.thir_abstract_const(did));
67 let body = match tcx.thir_body(def) {
68 Err(error_reported) => construct_error(tcx, def.did, body_owner_kind, error_reported),
70 // We ran all queries that depended on THIR at the beginning
71 // of `mir_build`, so now we can steal it
72 let thir = thir.steal();
74 if body_owner_kind.is_fn_or_closure() {
75 construct_fn(tcx, def, &thir, expr)
77 construct_const(tcx, def, &thir, expr)
82 lints::check(tcx, &body);
84 // The borrow checker will replace all the regions here with its own
85 // inference variables. There's no point having non-erased regions here.
86 // The exception is `body.user_type_annotations`, which is used unmodified
87 // by borrow checking.
89 !(body.local_decls.has_free_regions()
90 || body.basic_blocks.has_free_regions()
91 || body.var_debug_info.has_free_regions()
92 || body.yield_ty().has_free_regions()),
93 "Unexpected free regions in MIR: {:?}",
100 ///////////////////////////////////////////////////////////////////////////
101 // BuildMir -- walks a crate, looking for fn items and methods to build MIR from
103 #[derive(Debug, PartialEq, Eq)]
105 /// Evaluation is currently within a statement.
107 /// Examples include:
109 /// 2. `let _ = EXPR;`
110 /// 3. `let x = EXPR;`
112 /// If true, then statement discards result from evaluating
113 /// the expression (such as examples 1 and 2 above).
114 ignores_expr_result: bool,
117 /// Evaluation is currently within the tail expression of a block.
119 /// Example: `{ STMT_1; STMT_2; EXPR }`
121 /// If true, then the surrounding context of the block ignores
122 /// the result of evaluating the block's tail expression.
124 /// Example: `let _ = { STMT_1; EXPR };`
125 tail_result_is_ignored: bool,
127 /// `Span` of the tail expression.
131 /// Generic mark meaning that the block occurred as a subexpression
132 /// where the result might be used.
134 /// Examples: `foo(EXPR)`, `match EXPR { ... }`
139 fn is_tail_expr(&self) -> bool {
141 BlockFrame::TailExpr { .. } => true,
143 BlockFrame::Statement { .. } | BlockFrame::SubExpr => false,
146 fn is_statement(&self) -> bool {
148 BlockFrame::Statement { .. } => true,
150 BlockFrame::TailExpr { .. } | BlockFrame::SubExpr => false,
156 struct BlockContext(Vec<BlockFrame>);
158 struct Builder<'a, 'tcx> {
160 infcx: InferCtxt<'tcx>,
161 typeck_results: &'tcx TypeckResults<'tcx>,
162 region_scope_tree: &'tcx region::ScopeTree,
163 param_env: ty::ParamEnv<'tcx>,
165 thir: &'a Thir<'tcx>,
170 parent_module: DefId,
171 check_overflow: bool,
174 generator_kind: Option<GeneratorKind>,
176 /// The current set of scopes, updated as we traverse;
177 /// see the `scope` module for more details.
178 scopes: scope::Scopes<'tcx>,
180 /// The block-context: each time we build the code within an thir::Block,
181 /// we push a frame here tracking whether we are building a statement or
182 /// if we are pushing the tail expression of the block. This is used to
183 /// embed information in generated temps about whether they were created
184 /// for a block tail expression or not.
186 /// It would be great if we could fold this into `self.scopes`
187 /// somehow, but right now I think that is very tightly tied to
188 /// the code generation in ways that we cannot (or should not)
189 /// start just throwing new entries onto that vector in order to
190 /// distinguish the context of EXPR1 from the context of EXPR2 in
191 /// `{ STMTS; EXPR1 } + EXPR2`.
192 block_context: BlockContext,
194 /// The current unsafe block in scope
195 in_scope_unsafe: Safety,
197 /// The vector of all scopes that we have created thus far;
198 /// we track this for debuginfo later.
199 source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
200 source_scope: SourceScope,
202 /// The guard-context: each time we build the guard expression for
203 /// a match arm, we push onto this stack, and then pop when we
204 /// finish building it.
205 guard_context: Vec<GuardFrame>,
207 /// Maps `HirId`s of variable bindings to the `Local`s created for them.
208 /// (A match binding can have two locals; the 2nd is for the arm's guard.)
209 var_indices: FxHashMap<LocalVarId, LocalsForNode>,
210 local_decls: IndexVec<Local, LocalDecl<'tcx>>,
211 canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
212 upvars: CaptureMap<'tcx>,
213 unit_temp: Option<Place<'tcx>>,
215 var_debug_info: Vec<VarDebugInfo<'tcx>>,
218 type CaptureMap<'tcx> = SortedIndexMultiMap<usize, hir::HirId, Capture<'tcx>>;
221 struct Capture<'tcx> {
222 captured_place: &'tcx ty::CapturedPlace<'tcx>,
223 use_place: Place<'tcx>,
224 mutability: Mutability,
227 impl<'a, 'tcx> Builder<'a, 'tcx> {
228 fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
229 self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
232 fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
233 self.var_indices[&id].local_id(for_guard)
241 fn push(&mut self, bf: BlockFrame) {
244 fn pop(&mut self) -> Option<BlockFrame> {
248 /// Traverses the frames on the `BlockContext`, searching for either
249 /// the first block-tail expression frame with no intervening
252 /// Notably, this skips over `SubExpr` frames; this method is
253 /// meant to be used in the context of understanding the
254 /// relationship of a temp (created within some complicated
255 /// expression) with its containing expression, and whether the
256 /// value of that *containing expression* (not the temp!) is
258 fn currently_in_block_tail(&self) -> Option<BlockTailInfo> {
259 for bf in self.0.iter().rev() {
261 BlockFrame::SubExpr => continue,
262 BlockFrame::Statement { .. } => break,
263 &BlockFrame::TailExpr { tail_result_is_ignored, span } => {
264 return Some(BlockTailInfo { tail_result_is_ignored, span });
272 /// Looks at the topmost frame on the BlockContext and reports
273 /// whether its one that would discard a block tail result.
275 /// Unlike `currently_within_ignored_tail_expression`, this does
276 /// *not* skip over `SubExpr` frames: here, we want to know
277 /// whether the block result itself is discarded.
278 fn currently_ignores_tail_results(&self) -> bool {
279 match self.0.last() {
280 // no context: conservatively assume result is read
283 // sub-expression: block result feeds into some computation
284 Some(BlockFrame::SubExpr) => false,
286 // otherwise: use accumulated is_ignored state.
288 BlockFrame::TailExpr { tail_result_is_ignored: ignored, .. }
289 | BlockFrame::Statement { ignores_expr_result: ignored },
297 /// In the usual case, a `HirId` for an identifier maps to at most
298 /// one `Local` declaration.
301 /// The exceptional case is identifiers in a match arm's pattern
302 /// that are referenced in a guard of that match arm. For these,
303 /// we have `2` Locals.
305 /// * `for_arm_body` is the Local used in the arm body (which is
306 /// just like the `One` case above),
308 /// * `ref_for_guard` is the Local used in the arm's guard (which
309 /// is a reference to a temp that is an alias of
311 ForGuard { ref_for_guard: Local, for_arm_body: Local },
315 struct GuardFrameLocal {
319 impl GuardFrameLocal {
320 fn new(id: LocalVarId, _binding_mode: BindingMode) -> Self {
321 GuardFrameLocal { id }
327 /// These are the id's of names that are bound by patterns of the
328 /// arm of *this* guard.
330 /// (Frames higher up the stack will have the id's bound in arms
331 /// further out, such as in a case like:
334 /// P1(id1) if (... (match E2 { P2(id2) if ... => B2 })) => B1,
337 /// here, when building for FIXME.
338 locals: Vec<GuardFrameLocal>,
341 /// `ForGuard` indicates whether we are talking about:
342 /// 1. The variable for use outside of guard expressions, or
343 /// 2. The temp that holds reference to (1.), which is actually what the
344 /// guard expressions see.
345 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
352 fn local_id(&self, for_guard: ForGuard) -> Local {
353 match (self, for_guard) {
354 (&LocalsForNode::One(local_id), ForGuard::OutsideGuard)
356 &LocalsForNode::ForGuard { ref_for_guard: local_id, .. },
357 ForGuard::RefWithinGuard,
359 | (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) => {
363 (&LocalsForNode::One(_), ForGuard::RefWithinGuard) => {
364 bug!("anything with one local should never be within a guard.")
371 basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
374 rustc_index::newtype_index! {
379 enum NeedsTemporary {
380 /// Use this variant when whatever you are converting with `as_operand`
381 /// is the last thing you are converting. This means that if we introduced
382 /// an intermediate temporary, we'd only read it immediately after, so we can
385 /// For all cases where you aren't sure or that are too expensive to compute
386 /// for now. It is always safe to fall back to this.
390 ///////////////////////////////////////////////////////////////////////////
391 /// The `BlockAnd` "monad" packages up the new basic block along with a
392 /// produced value (sometimes just unit, of course). The `unpack!`
393 /// macro (and methods below) makes working with `BlockAnd` much more
396 #[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
397 struct BlockAnd<T>(BasicBlock, T);
399 trait BlockAndExtension {
400 fn and<T>(self, v: T) -> BlockAnd<T>;
401 fn unit(self) -> BlockAnd<()>;
404 impl BlockAndExtension for BasicBlock {
405 fn and<T>(self, v: T) -> BlockAnd<T> {
409 fn unit(self) -> BlockAnd<()> {
414 /// Update a block pointer and return the value.
415 /// Use it like `let x = unpack!(block = self.foo(block, foo))`.
416 macro_rules! unpack {
417 ($x:ident = $c:expr) => {{
418 let BlockAnd(b, v) = $c;
424 let BlockAnd(b, ()) = $c;
429 ///////////////////////////////////////////////////////////////////////////
430 /// the main entry point for building MIR for a function
432 fn construct_fn<'tcx>(
434 fn_def: ty::WithOptConstParam<LocalDefId>,
438 let span = tcx.def_span(fn_def.did);
439 let fn_id = tcx.hir().local_def_id_to_hir_id(fn_def.did);
440 let generator_kind = tcx.generator_kind(fn_def.did);
442 // The representation of thir for `-Zunpretty=thir-tree` relies on
443 // the entry expression being the last element of `thir.exprs`.
444 assert_eq!(expr.as_usize(), thir.exprs.len() - 1);
446 // Figure out what primary body this item has.
447 let body_id = tcx.hir().body_owned_by(fn_def.did);
448 let span_with_body = tcx.hir().span_with_body(fn_id);
449 let return_ty_span = tcx
451 .fn_decl_by_hir_id(fn_id)
452 .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def.did))
456 // fetch the fully liberated fn signature (that is, all bound
457 // types/lifetimes replaced)
458 let typeck_results = tcx.typeck_opt_const_arg(fn_def);
459 let fn_sig = typeck_results.liberated_fn_sigs()[fn_id];
461 let safety = match fn_sig.unsafety {
462 hir::Unsafety::Normal => Safety::Safe,
463 hir::Unsafety::Unsafe => Safety::FnUnsafe,
466 let mut abi = fn_sig.abi;
467 if let DefKind::Closure = tcx.def_kind(fn_def.did) {
468 // HACK(eddyb) Avoid having RustCall on closures,
469 // as it adds unnecessary (and wrong) auto-tupling.
473 let arguments = &thir.params;
475 let (yield_ty, return_ty) = if generator_kind.is_some() {
476 let gen_ty = arguments[thir::UPVAR_ENV_PARAM].ty;
477 let gen_sig = match gen_ty.kind() {
478 ty::Generator(_, gen_substs, ..) => gen_substs.as_generator().sig(),
480 span_bug!(span, "generator w/o generator type: {:?}", gen_ty)
483 (Some(gen_sig.yield_ty), gen_sig.return_ty)
485 (None, fn_sig.output())
488 if let Some(custom_mir_attr) =
489 tcx.hir().attrs(fn_id).iter().find(|attr| attr.name_or_empty() == sym::custom_mir)
491 return custom::build_custom_mir(
493 fn_def.did.to_def_id(),
505 let infcx = tcx.infer_ctxt().build();
506 let mut builder = Builder::new(
519 let call_site_scope =
520 region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::CallSite };
522 region::Scope { id: body_id.hir_id.local_id, data: region::ScopeData::Arguments };
523 let source_info = builder.source_info(span);
524 let call_site_s = (call_site_scope, source_info);
525 unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
526 let arg_scope_s = (arg_scope, source_info);
527 // Attribute epilogue to function's closing brace
528 let fn_end = span_with_body.shrink_to_hi();
530 unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
531 Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
532 builder.args_and_body(
541 let source_info = builder.source_info(fn_end);
542 builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
543 builder.build_drop_trees();
547 let mut body = builder.finish();
549 body.spread_arg = if abi == Abi::RustCall {
550 // RustCall pseudo-ABI untuples the last argument.
551 Some(Local::new(arguments.len()))
555 if yield_ty.is_some() {
556 body.generator.as_mut().unwrap().yield_ty = yield_ty;
561 fn construct_const<'a, 'tcx>(
563 def: ty::WithOptConstParam<LocalDefId>,
564 thir: &'a Thir<'tcx>,
567 let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
569 // Figure out what primary body this item has.
570 let (span, const_ty_span) = match tcx.hir().get(hir_id) {
571 Node::Item(hir::Item {
572 kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _),
576 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
577 | Node::TraitItem(hir::TraitItem {
578 kind: hir::TraitItemKind::Const(ty, Some(_)),
581 }) => (*span, ty.span),
582 Node::AnonConst(_) => {
583 let span = tcx.def_span(def.did);
586 _ => span_bug!(tcx.def_span(def.did), "can't build MIR for {:?}", def.did),
589 // Get the revealed type of this const. This is *not* the adjusted
590 // type of its body, which may be a subtype of this type. For
594 // static X: fn(&'static ()) = foo;
596 // The adjusted type of the body of X is `for<'a> fn(&'a ())` which
597 // is not the same as the type of X. We need the type of the return
598 // place to be the type of the constant because NLL typeck will
600 let typeck_results = tcx.typeck_opt_const_arg(def);
601 let const_ty = typeck_results.node_type(hir_id);
603 let infcx = tcx.infer_ctxt().build();
604 let mut builder = Builder::new(
617 let mut block = START_BLOCK;
618 unpack!(block = builder.expr_into_dest(Place::return_place(), block, &thir[expr]));
620 let source_info = builder.source_info(span);
621 builder.cfg.terminate(block, source_info, TerminatorKind::Return);
623 builder.build_drop_trees();
628 /// Construct MIR for an item that has had errors in type checking.
630 /// This is required because we may still want to run MIR passes on an item
631 /// with type errors, but normal MIR construction can't handle that in general.
635 body_owner_kind: hir::BodyOwnerKind,
636 err: ErrorGuaranteed,
638 let span = tcx.def_span(def);
639 let hir_id = tcx.hir().local_def_id_to_hir_id(def);
640 let generator_kind = tcx.generator_kind(def);
642 let ty = tcx.ty_error();
643 let num_params = match body_owner_kind {
644 hir::BodyOwnerKind::Fn => tcx.fn_sig(def).skip_binder().inputs().skip_binder().len(),
645 hir::BodyOwnerKind::Closure => {
646 let ty = tcx.type_of(def);
648 ty::Closure(_, substs) => {
649 1 + substs.as_closure().sig().inputs().skip_binder().len()
651 ty::Generator(..) => 2,
652 _ => bug!("expected closure or generator, found {ty:?}"),
655 hir::BodyOwnerKind::Const => 0,
656 hir::BodyOwnerKind::Static(_) => 0,
658 let mut cfg = CFG { basic_blocks: IndexVec::new() };
659 let mut source_scopes = IndexVec::new();
660 let mut local_decls = IndexVec::from_elem_n(LocalDecl::new(ty, span), 1);
662 cfg.start_new_block();
663 source_scopes.push(SourceScopeData {
667 inlined_parent_scope: None,
668 local_data: ClearCrossCrate::Set(SourceScopeLocalData {
670 safety: Safety::Safe,
673 let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
675 // Some MIR passes will expect the number of parameters to match the
676 // function declaration.
677 for _ in 0..num_params {
678 local_decls.push(LocalDecl::with_source_info(ty, source_info));
680 cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
682 let mut body = Body::new(
683 MirSource::item(def.to_def_id()),
694 body.generator.as_mut().map(|gen| gen.yield_ty = Some(ty));
698 impl<'a, 'tcx> Builder<'a, 'tcx> {
700 thir: &'a Thir<'tcx>,
701 infcx: InferCtxt<'tcx>,
702 def: ty::WithOptConstParam<LocalDefId>,
709 generator_kind: Option<GeneratorKind>,
710 ) -> Builder<'a, 'tcx> {
712 let attrs = tcx.hir().attrs(hir_id);
713 // Some functions always have overflow checks enabled,
714 // however, they may not get codegen'd, depending on
715 // the settings for the crate they are codegened in.
716 let mut check_overflow = tcx.sess.contains_name(attrs, sym::rustc_inherit_overflow_checks);
717 // Respect -C overflow-checks.
718 check_overflow |= tcx.sess.overflow_checks();
719 // Constants always need overflow checks.
720 check_overflow |= matches!(
721 tcx.hir().body_owner_kind(def.did),
722 hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_)
725 let lint_level = LintLevel::Explicit(hir_id);
726 let param_env = tcx.param_env(def.did);
727 let mut builder = Builder {
731 typeck_results: tcx.typeck_opt_const_arg(def),
732 region_scope_tree: tcx.region_scope_tree(def.did),
734 def_id: def.did.to_def_id(),
736 parent_module: tcx.parent_module(hir_id).to_def_id(),
738 cfg: CFG { basic_blocks: IndexVec::new() },
742 scopes: scope::Scopes::new(),
743 block_context: BlockContext::new(),
744 source_scopes: IndexVec::new(),
745 source_scope: OUTERMOST_SOURCE_SCOPE,
746 guard_context: vec![],
747 in_scope_unsafe: safety,
748 local_decls: IndexVec::from_elem_n(LocalDecl::new(return_ty, return_span), 1),
749 canonical_user_type_annotations: IndexVec::new(),
750 upvars: CaptureMap::new(),
751 var_indices: Default::default(),
753 var_debug_info: vec![],
756 assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
758 builder.new_source_scope(span, lint_level, Some(safety)),
759 OUTERMOST_SOURCE_SCOPE
761 builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
766 fn finish(self) -> Body<'tcx> {
767 for (index, block) in self.cfg.basic_blocks.iter().enumerate() {
768 if block.terminator.is_none() {
769 span_bug!(self.fn_span, "no terminator on block {:?}", index);
774 MirSource::item(self.def_id),
775 self.cfg.basic_blocks,
778 self.canonical_user_type_annotations,
783 self.typeck_results.tainted_by_errors,
789 mut block: BasicBlock,
790 fn_def_id: LocalDefId,
791 arguments: &IndexVec<ParamId, Param<'tcx>>,
792 argument_scope: region::Scope,
795 // Allocate locals for the function arguments
796 for param in arguments.iter() {
798 SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
800 self.local_decls.push(LocalDecl::with_source_info(param.ty, source_info));
802 // If this is a simple binding pattern, give debuginfo a nice name.
803 if let Some(ref pat) = param.pat && let Some(name) = pat.simple_ident() {
804 self.var_debug_info.push(VarDebugInfo {
807 value: VarDebugInfoContents::Place(arg_local.into()),
813 let tcx_hir = tcx.hir();
814 let hir_typeck_results = self.typeck_results;
816 // In analyze_closure() in upvar.rs we gathered a list of upvars used by an
817 // indexed closure and we stored in a map called closure_min_captures in TypeckResults
818 // with the closure's DefId. Here, we run through that vec of UpvarIds for
819 // the given closure and use the necessary information to create upvar
820 // debuginfo and to fill `self.upvars`.
821 if hir_typeck_results.closure_min_captures.get(&fn_def_id).is_some() {
822 let mut closure_env_projs = vec![];
823 let mut closure_ty = self.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty;
824 if let ty::Ref(_, ty, _) = closure_ty.kind() {
825 closure_env_projs.push(ProjectionElem::Deref);
828 let upvar_substs = match closure_ty.kind() {
829 ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
830 ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
831 _ => span_bug!(self.fn_span, "upvars with non-closure env ty {:?}", closure_ty),
833 let def_id = self.def_id.as_local().unwrap();
834 let capture_syms = tcx.symbols_for_closure_captures((def_id, fn_def_id));
835 let capture_tys = upvar_substs.upvar_tys();
836 let captures_with_tys = hir_typeck_results
837 .closure_min_captures_flattened(fn_def_id)
838 .zip(capture_tys.zip(capture_syms));
840 self.upvars = captures_with_tys
842 .map(|(i, (captured_place, (ty, sym)))| {
843 let capture = captured_place.info.capture_kind;
844 let var_id = match captured_place.place.base {
845 HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
846 _ => bug!("Expected an upvar"),
849 let mutability = captured_place.mutability;
851 let mut projs = closure_env_projs.clone();
852 projs.push(ProjectionElem::Field(Field::new(i), ty));
854 ty::UpvarCapture::ByValue => {}
855 ty::UpvarCapture::ByRef(..) => {
856 projs.push(ProjectionElem::Deref);
860 let use_place = Place {
861 local: ty::CAPTURE_STRUCT_LOCAL,
862 projection: tcx.intern_place_elems(&projs),
864 self.var_debug_info.push(VarDebugInfo {
866 source_info: SourceInfo::outermost(tcx_hir.span(var_id)),
867 value: VarDebugInfoContents::Place(use_place),
870 let capture = Capture { captured_place, use_place, mutability };
876 let mut scope = None;
877 // Bind the argument patterns
878 for (index, param) in arguments.iter().enumerate() {
879 // Function arguments always get the first Local indices after the return place
880 let local = Local::new(index + 1);
881 let place = Place::from(local);
883 // Make sure we drop (parts of) the argument even when not matched on.
885 param.pat.as_ref().map_or(expr.span, |pat| pat.span),
891 let Some(ref pat) = param.pat else {
894 let original_source_scope = self.source_scope;
896 if let Some(arg_hir_id) = param.hir_id {
897 self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span);
900 // Don't introduce extra copies for simple bindings
904 mode: BindingMode::ByValue,
908 self.local_decls[local].mutability = mutability;
909 self.local_decls[local].source_info.scope = self.source_scope;
910 self.local_decls[local].local_info = if let Some(kind) = param.self_kind {
911 Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
912 BindingForm::ImplicitSelf(kind),
915 let binding_mode = ty::BindingMode::BindByValue(mutability);
916 Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
919 opt_ty_info: param.ty_span,
920 opt_match_place: Some((None, span)),
925 self.var_indices.insert(var, LocalsForNode::One(local));
928 scope = self.declare_bindings(
933 Some((Some(&place), span)),
935 let place_builder = PlaceBuilder::from(local);
936 unpack!(block = self.place_into_pattern(block, &pat, place_builder, false));
939 self.source_scope = original_source_scope;
942 // Enter the argument pattern bindings source scope, if it exists.
943 if let Some(source_scope) = scope {
944 self.source_scope = source_scope;
947 self.expr_into_dest(Place::return_place(), block, &expr)
950 fn set_correct_source_scope_for_arg(
952 arg_hir_id: hir::HirId,
953 original_source_scope: SourceScope,
956 let parent_id = self.source_scopes[original_source_scope]
959 .assert_crate_local()
961 self.maybe_new_source_scope(pattern_span, None, arg_hir_id, parent_id);
964 fn get_unit_temp(&mut self) -> Place<'tcx> {
965 match self.unit_temp {
968 let ty = self.tcx.mk_unit();
969 let fn_span = self.fn_span;
970 let tmp = self.temp(ty, fn_span);
971 self.unit_temp = Some(tmp);
978 fn parse_float_into_constval<'tcx>(
980 float_ty: ty::FloatTy,
982 ) -> Option<ConstValue<'tcx>> {
983 parse_float_into_scalar(num, float_ty, neg).map(ConstValue::Scalar)
986 pub(crate) fn parse_float_into_scalar(
988 float_ty: ty::FloatTy,
990 ) -> Option<Scalar> {
991 let num = num.as_str();
993 ty::FloatTy::F32 => {
994 let Ok(rust_f) = num.parse::<f32>() else { return None };
995 let mut f = num.parse::<Single>().unwrap_or_else(|e| {
996 panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
1000 u128::from(rust_f.to_bits()) == f.to_bits(),
1001 "apfloat::ieee::Single gave different result for `{}`: \
1002 {}({:#x}) vs Rust's {}({:#x})",
1006 Single::from_bits(rust_f.to_bits().into()),
1014 Some(Scalar::from_f32(f))
1016 ty::FloatTy::F64 => {
1017 let Ok(rust_f) = num.parse::<f64>() else { return None };
1018 let mut f = num.parse::<Double>().unwrap_or_else(|e| {
1019 panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
1023 u128::from(rust_f.to_bits()) == f.to_bits(),
1024 "apfloat::ieee::Double gave different result for `{}`: \
1025 {}({:#x}) vs Rust's {}({:#x})",
1029 Double::from_bits(rust_f.to_bits().into()),
1037 Some(Scalar::from_f64(f))
1042 ///////////////////////////////////////////////////////////////////////////
1043 // Builder methods are broken up into modules, depending on what kind
1044 // of thing is being lowered. Note that they use the `unpack` macro
1045 // above extensively.
1055 pub(crate) use expr::category::Category as ExprCategory;