]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/mod.rs
Auto merge of #100968 - cjgillot:mir-upvar-vec, r=wesleywiser
[rust.git] / compiler / rustc_mir_build / src / build / mod.rs
1 use crate::build;
2 pub(crate) use crate::build::expr::as_constant::lit_to_mir_constant;
3 use crate::build::expr::as_place::PlaceBuilder;
4 use crate::build::scope::DropKind;
5 use crate::thir::pattern::pat_from_hir;
6 use rustc_apfloat::ieee::{Double, Single};
7 use rustc_apfloat::Float;
8 use rustc_data_structures::fx::FxHashMap;
9 use rustc_data_structures::sorted_map::SortedIndexMultiMap;
10 use rustc_errors::ErrorGuaranteed;
11 use rustc_hir as hir;
12 use rustc_hir::def_id::{DefId, LocalDefId};
13 use rustc_hir::lang_items::LangItem;
14 use rustc_hir::{GeneratorKind, Node};
15 use rustc_index::vec::{Idx, IndexVec};
16 use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
17 use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
18 use rustc_middle::middle::region;
19 use rustc_middle::mir::interpret::ConstValue;
20 use rustc_middle::mir::interpret::Scalar;
21 use rustc_middle::mir::*;
22 use rustc_middle::thir::{BindingMode, Expr, ExprId, LintLevel, LocalVarId, PatKind, Thir};
23 use rustc_middle::ty::subst::Subst;
24 use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable, TypeckResults};
25 use rustc_span::symbol::sym;
26 use rustc_span::Span;
27 use rustc_span::Symbol;
28 use rustc_target::spec::abi::Abi;
29
30 use super::lints;
31
32 pub(crate) fn mir_built<'tcx>(
33     tcx: TyCtxt<'tcx>,
34     def: ty::WithOptConstParam<LocalDefId>,
35 ) -> &'tcx rustc_data_structures::steal::Steal<Body<'tcx>> {
36     if let Some(def) = def.try_upgrade(tcx) {
37         return tcx.mir_built(def);
38     }
39
40     let mut body = mir_build(tcx, def);
41     if def.const_param_did.is_some() {
42         assert!(matches!(body.source.instance, ty::InstanceDef::Item(_)));
43         body.source = MirSource::from_instance(ty::InstanceDef::Item(def.to_global()));
44     }
45
46     tcx.alloc_steal_mir(body)
47 }
48
49 /// Construct the MIR for a given `DefId`.
50 fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> Body<'_> {
51     let id = tcx.hir().local_def_id_to_hir_id(def.did);
52     let body_owner_kind = tcx.hir().body_owner_kind(def.did);
53     let typeck_results = tcx.typeck_opt_const_arg(def);
54
55     // Ensure unsafeck and abstract const building is ran before we steal the THIR.
56     // We can't use `ensure()` for `thir_abstract_const` as it doesn't compute the query
57     // if inputs are green. This can cause ICEs when calling `thir_abstract_const` after
58     // THIR has been stolen if we haven't computed this query yet.
59     match def {
60         ty::WithOptConstParam { did, const_param_did: Some(const_param_did) } => {
61             tcx.ensure().thir_check_unsafety_for_const_arg((did, const_param_did));
62             drop(tcx.thir_abstract_const_of_const_arg((did, const_param_did)));
63         }
64         ty::WithOptConstParam { did, const_param_did: None } => {
65             tcx.ensure().thir_check_unsafety(did);
66             drop(tcx.thir_abstract_const(did));
67         }
68     }
69
70     // Figure out what primary body this item has.
71     let (body_id, return_ty_span, span_with_body) = match tcx.hir().get(id) {
72         Node::Expr(hir::Expr {
73             kind: hir::ExprKind::Closure(hir::Closure { fn_decl, body, .. }),
74             ..
75         }) => (*body, fn_decl.output.span(), None),
76         Node::Item(hir::Item {
77             kind: hir::ItemKind::Fn(hir::FnSig { decl, .. }, _, body_id),
78             span,
79             ..
80         })
81         | Node::ImplItem(hir::ImplItem {
82             kind: hir::ImplItemKind::Fn(hir::FnSig { decl, .. }, body_id),
83             span,
84             ..
85         })
86         | Node::TraitItem(hir::TraitItem {
87             kind: hir::TraitItemKind::Fn(hir::FnSig { decl, .. }, hir::TraitFn::Provided(body_id)),
88             span,
89             ..
90         }) => {
91             // Use the `Span` of the `Item/ImplItem/TraitItem` as the body span,
92             // since the def span of a function does not include the body
93             (*body_id, decl.output.span(), Some(*span))
94         }
95         Node::Item(hir::Item {
96             kind: hir::ItemKind::Static(ty, _, body_id) | hir::ItemKind::Const(ty, body_id),
97             ..
98         })
99         | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, body_id), .. })
100         | Node::TraitItem(hir::TraitItem {
101             kind: hir::TraitItemKind::Const(ty, Some(body_id)),
102             ..
103         }) => (*body_id, ty.span, None),
104         Node::AnonConst(hir::AnonConst { body, hir_id, .. }) => {
105             (*body, tcx.hir().span(*hir_id), None)
106         }
107
108         _ => span_bug!(tcx.hir().span(id), "can't build MIR for {:?}", def.did),
109     };
110
111     // If we don't have a specialized span for the body, just use the
112     // normal def span.
113     let span_with_body = span_with_body.unwrap_or_else(|| tcx.hir().span(id));
114
115     tcx.infer_ctxt().enter(|infcx| {
116         let body = if let Some(error_reported) = typeck_results.tainted_by_errors {
117             build::construct_error(&infcx, def, id, body_id, body_owner_kind, error_reported)
118         } else if body_owner_kind.is_fn_or_closure() {
119             // fetch the fully liberated fn signature (that is, all bound
120             // types/lifetimes replaced)
121             let fn_sig = typeck_results.liberated_fn_sigs()[id];
122             let fn_def_id = tcx.hir().local_def_id(id);
123
124             let safety = match fn_sig.unsafety {
125                 hir::Unsafety::Normal => Safety::Safe,
126                 hir::Unsafety::Unsafe => Safety::FnUnsafe,
127             };
128
129             let body = tcx.hir().body(body_id);
130             let (thir, expr) = tcx
131                 .thir_body(def)
132                 .unwrap_or_else(|_| (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0)));
133             // We ran all queries that depended on THIR at the beginning
134             // of `mir_build`, so now we can steal it
135             let thir = thir.steal();
136             let ty = tcx.type_of(fn_def_id);
137             let mut abi = fn_sig.abi;
138             let implicit_argument = match ty.kind() {
139                 ty::Closure(..) => {
140                     // HACK(eddyb) Avoid having RustCall on closures,
141                     // as it adds unnecessary (and wrong) auto-tupling.
142                     abi = Abi::Rust;
143                     vec![ArgInfo(liberated_closure_env_ty(tcx, id, body_id), None, None, None)]
144                 }
145                 ty::Generator(..) => {
146                     let gen_ty = tcx.typeck_body(body_id).node_type(id);
147
148                     // The resume argument may be missing, in that case we need to provide it here.
149                     // It will always be `()` in this case.
150                     if body.params.is_empty() {
151                         vec![
152                             ArgInfo(gen_ty, None, None, None),
153                             ArgInfo(tcx.mk_unit(), None, None, None),
154                         ]
155                     } else {
156                         vec![ArgInfo(gen_ty, None, None, None)]
157                     }
158                 }
159                 _ => vec![],
160             };
161
162             let explicit_arguments = body.params.iter().enumerate().map(|(index, arg)| {
163                 let owner_id = tcx.hir().body_owner(body_id);
164                 let opt_ty_info;
165                 let self_arg;
166                 if let Some(ref fn_decl) = tcx.hir().fn_decl_by_hir_id(owner_id) {
167                     opt_ty_info = fn_decl
168                         .inputs
169                         .get(index)
170                         // Make sure that inferred closure args have no type span
171                         .and_then(|ty| if arg.pat.span != ty.span { Some(ty.span) } else { None });
172                     self_arg = if index == 0 && fn_decl.implicit_self.has_implicit_self() {
173                         match fn_decl.implicit_self {
174                             hir::ImplicitSelfKind::Imm => Some(ImplicitSelfKind::Imm),
175                             hir::ImplicitSelfKind::Mut => Some(ImplicitSelfKind::Mut),
176                             hir::ImplicitSelfKind::ImmRef => Some(ImplicitSelfKind::ImmRef),
177                             hir::ImplicitSelfKind::MutRef => Some(ImplicitSelfKind::MutRef),
178                             _ => None,
179                         }
180                     } else {
181                         None
182                     };
183                 } else {
184                     opt_ty_info = None;
185                     self_arg = None;
186                 }
187
188                 // C-variadic fns also have a `VaList` input that's not listed in `fn_sig`
189                 // (as it's created inside the body itself, not passed in from outside).
190                 let ty = if fn_sig.c_variadic && index == fn_sig.inputs().len() {
191                     let va_list_did = tcx.require_lang_item(LangItem::VaList, Some(arg.span));
192
193                     tcx.bound_type_of(va_list_did).subst(tcx, &[tcx.lifetimes.re_erased.into()])
194                 } else {
195                     fn_sig.inputs()[index]
196                 };
197
198                 ArgInfo(ty, opt_ty_info, Some(&arg), self_arg)
199             });
200
201             let arguments = implicit_argument.into_iter().chain(explicit_arguments);
202
203             let (yield_ty, return_ty) = if body.generator_kind.is_some() {
204                 let gen_ty = tcx.typeck_body(body_id).node_type(id);
205                 let gen_sig = match gen_ty.kind() {
206                     ty::Generator(_, gen_substs, ..) => gen_substs.as_generator().sig(),
207                     _ => span_bug!(tcx.hir().span(id), "generator w/o generator type: {:?}", ty),
208                 };
209                 (Some(gen_sig.yield_ty), gen_sig.return_ty)
210             } else {
211                 (None, fn_sig.output())
212             };
213
214             let mut mir = build::construct_fn(
215                 &thir,
216                 &infcx,
217                 def,
218                 id,
219                 arguments,
220                 safety,
221                 abi,
222                 return_ty,
223                 return_ty_span,
224                 body,
225                 expr,
226                 span_with_body,
227             );
228             if yield_ty.is_some() {
229                 mir.generator.as_mut().unwrap().yield_ty = yield_ty;
230             }
231             mir
232         } else {
233             // Get the revealed type of this const. This is *not* the adjusted
234             // type of its body, which may be a subtype of this type. For
235             // example:
236             //
237             // fn foo(_: &()) {}
238             // static X: fn(&'static ()) = foo;
239             //
240             // The adjusted type of the body of X is `for<'a> fn(&'a ())` which
241             // is not the same as the type of X. We need the type of the return
242             // place to be the type of the constant because NLL typeck will
243             // equate them.
244
245             let return_ty = typeck_results.node_type(id);
246
247             let (thir, expr) = tcx
248                 .thir_body(def)
249                 .unwrap_or_else(|_| (tcx.alloc_steal_thir(Thir::new()), ExprId::from_u32(0)));
250             // We ran all queries that depended on THIR at the beginning
251             // of `mir_build`, so now we can steal it
252             let thir = thir.steal();
253
254             let span_with_body = span_with_body.to(tcx.hir().span(body_id.hir_id));
255
256             build::construct_const(
257                 &thir,
258                 &infcx,
259                 expr,
260                 def,
261                 id,
262                 return_ty,
263                 return_ty_span,
264                 span_with_body,
265             )
266         };
267
268         lints::check(tcx, &body);
269
270         // The borrow checker will replace all the regions here with its own
271         // inference variables. There's no point having non-erased regions here.
272         // The exception is `body.user_type_annotations`, which is used unmodified
273         // by borrow checking.
274         debug_assert!(
275             !(body.local_decls.has_free_regions()
276                 || body.basic_blocks.has_free_regions()
277                 || body.var_debug_info.has_free_regions()
278                 || body.yield_ty().has_free_regions()),
279             "Unexpected free regions in MIR: {:?}",
280             body,
281         );
282
283         body
284     })
285 }
286
287 ///////////////////////////////////////////////////////////////////////////
288 // BuildMir -- walks a crate, looking for fn items and methods to build MIR from
289
290 fn liberated_closure_env_ty(
291     tcx: TyCtxt<'_>,
292     closure_expr_id: hir::HirId,
293     body_id: hir::BodyId,
294 ) -> Ty<'_> {
295     let closure_ty = tcx.typeck_body(body_id).node_type(closure_expr_id);
296
297     let ty::Closure(closure_def_id, closure_substs) = *closure_ty.kind() else {
298         bug!("closure expr does not have closure type: {:?}", closure_ty);
299     };
300
301     let bound_vars =
302         tcx.mk_bound_variable_kinds(std::iter::once(ty::BoundVariableKind::Region(ty::BrEnv)));
303     let br =
304         ty::BoundRegion { var: ty::BoundVar::from_usize(bound_vars.len() - 1), kind: ty::BrEnv };
305     let env_region = ty::ReLateBound(ty::INNERMOST, br);
306     let closure_env_ty = tcx.closure_env_ty(closure_def_id, closure_substs, env_region).unwrap();
307     tcx.erase_late_bound_regions(ty::Binder::bind_with_vars(closure_env_ty, bound_vars))
308 }
309
310 #[derive(Debug, PartialEq, Eq)]
311 enum BlockFrame {
312     /// Evaluation is currently within a statement.
313     ///
314     /// Examples include:
315     /// 1. `EXPR;`
316     /// 2. `let _ = EXPR;`
317     /// 3. `let x = EXPR;`
318     Statement {
319         /// If true, then statement discards result from evaluating
320         /// the expression (such as examples 1 and 2 above).
321         ignores_expr_result: bool,
322     },
323
324     /// Evaluation is currently within the tail expression of a block.
325     ///
326     /// Example: `{ STMT_1; STMT_2; EXPR }`
327     TailExpr {
328         /// If true, then the surrounding context of the block ignores
329         /// the result of evaluating the block's tail expression.
330         ///
331         /// Example: `let _ = { STMT_1; EXPR };`
332         tail_result_is_ignored: bool,
333
334         /// `Span` of the tail expression.
335         span: Span,
336     },
337
338     /// Generic mark meaning that the block occurred as a subexpression
339     /// where the result might be used.
340     ///
341     /// Examples: `foo(EXPR)`, `match EXPR { ... }`
342     SubExpr,
343 }
344
345 impl BlockFrame {
346     fn is_tail_expr(&self) -> bool {
347         match *self {
348             BlockFrame::TailExpr { .. } => true,
349
350             BlockFrame::Statement { .. } | BlockFrame::SubExpr => false,
351         }
352     }
353     fn is_statement(&self) -> bool {
354         match *self {
355             BlockFrame::Statement { .. } => true,
356
357             BlockFrame::TailExpr { .. } | BlockFrame::SubExpr => false,
358         }
359     }
360 }
361
362 #[derive(Debug)]
363 struct BlockContext(Vec<BlockFrame>);
364
365 struct Builder<'a, 'tcx> {
366     tcx: TyCtxt<'tcx>,
367     infcx: &'a InferCtxt<'a, 'tcx>,
368     typeck_results: &'tcx TypeckResults<'tcx>,
369     region_scope_tree: &'tcx region::ScopeTree,
370     param_env: ty::ParamEnv<'tcx>,
371
372     thir: &'a Thir<'tcx>,
373     cfg: CFG<'tcx>,
374
375     def_id: DefId,
376     hir_id: hir::HirId,
377     parent_module: DefId,
378     check_overflow: bool,
379     fn_span: Span,
380     arg_count: usize,
381     generator_kind: Option<GeneratorKind>,
382
383     /// The current set of scopes, updated as we traverse;
384     /// see the `scope` module for more details.
385     scopes: scope::Scopes<'tcx>,
386
387     /// The block-context: each time we build the code within an thir::Block,
388     /// we push a frame here tracking whether we are building a statement or
389     /// if we are pushing the tail expression of the block. This is used to
390     /// embed information in generated temps about whether they were created
391     /// for a block tail expression or not.
392     ///
393     /// It would be great if we could fold this into `self.scopes`
394     /// somehow, but right now I think that is very tightly tied to
395     /// the code generation in ways that we cannot (or should not)
396     /// start just throwing new entries onto that vector in order to
397     /// distinguish the context of EXPR1 from the context of EXPR2 in
398     /// `{ STMTS; EXPR1 } + EXPR2`.
399     block_context: BlockContext,
400
401     /// The current unsafe block in scope
402     in_scope_unsafe: Safety,
403
404     /// The vector of all scopes that we have created thus far;
405     /// we track this for debuginfo later.
406     source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
407     source_scope: SourceScope,
408
409     /// The guard-context: each time we build the guard expression for
410     /// a match arm, we push onto this stack, and then pop when we
411     /// finish building it.
412     guard_context: Vec<GuardFrame>,
413
414     /// Maps `HirId`s of variable bindings to the `Local`s created for them.
415     /// (A match binding can have two locals; the 2nd is for the arm's guard.)
416     var_indices: FxHashMap<LocalVarId, LocalsForNode>,
417     local_decls: IndexVec<Local, LocalDecl<'tcx>>,
418     canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
419     upvars: CaptureMap<'tcx>,
420     unit_temp: Option<Place<'tcx>>,
421
422     var_debug_info: Vec<VarDebugInfo<'tcx>>,
423 }
424
425 type CaptureMap<'tcx> = SortedIndexMultiMap<usize, hir::HirId, Capture<'tcx>>;
426
427 #[derive(Debug)]
428 struct Capture<'tcx> {
429     captured_place: &'tcx ty::CapturedPlace<'tcx>,
430     use_place: Place<'tcx>,
431     mutability: Mutability,
432 }
433
434 impl<'a, 'tcx> Builder<'a, 'tcx> {
435     fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
436         self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
437     }
438
439     fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
440         self.var_indices[&id].local_id(for_guard)
441     }
442 }
443
444 impl BlockContext {
445     fn new() -> Self {
446         BlockContext(vec![])
447     }
448     fn push(&mut self, bf: BlockFrame) {
449         self.0.push(bf);
450     }
451     fn pop(&mut self) -> Option<BlockFrame> {
452         self.0.pop()
453     }
454
455     /// Traverses the frames on the `BlockContext`, searching for either
456     /// the first block-tail expression frame with no intervening
457     /// statement frame.
458     ///
459     /// Notably, this skips over `SubExpr` frames; this method is
460     /// meant to be used in the context of understanding the
461     /// relationship of a temp (created within some complicated
462     /// expression) with its containing expression, and whether the
463     /// value of that *containing expression* (not the temp!) is
464     /// ignored.
465     fn currently_in_block_tail(&self) -> Option<BlockTailInfo> {
466         for bf in self.0.iter().rev() {
467             match bf {
468                 BlockFrame::SubExpr => continue,
469                 BlockFrame::Statement { .. } => break,
470                 &BlockFrame::TailExpr { tail_result_is_ignored, span } => {
471                     return Some(BlockTailInfo { tail_result_is_ignored, span });
472                 }
473             }
474         }
475
476         None
477     }
478
479     /// Looks at the topmost frame on the BlockContext and reports
480     /// whether its one that would discard a block tail result.
481     ///
482     /// Unlike `currently_within_ignored_tail_expression`, this does
483     /// *not* skip over `SubExpr` frames: here, we want to know
484     /// whether the block result itself is discarded.
485     fn currently_ignores_tail_results(&self) -> bool {
486         match self.0.last() {
487             // no context: conservatively assume result is read
488             None => false,
489
490             // sub-expression: block result feeds into some computation
491             Some(BlockFrame::SubExpr) => false,
492
493             // otherwise: use accumulated is_ignored state.
494             Some(
495                 BlockFrame::TailExpr { tail_result_is_ignored: ignored, .. }
496                 | BlockFrame::Statement { ignores_expr_result: ignored },
497             ) => *ignored,
498         }
499     }
500 }
501
502 #[derive(Debug)]
503 enum LocalsForNode {
504     /// In the usual case, a `HirId` for an identifier maps to at most
505     /// one `Local` declaration.
506     One(Local),
507
508     /// The exceptional case is identifiers in a match arm's pattern
509     /// that are referenced in a guard of that match arm. For these,
510     /// we have `2` Locals.
511     ///
512     /// * `for_arm_body` is the Local used in the arm body (which is
513     ///   just like the `One` case above),
514     ///
515     /// * `ref_for_guard` is the Local used in the arm's guard (which
516     ///   is a reference to a temp that is an alias of
517     ///   `for_arm_body`).
518     ForGuard { ref_for_guard: Local, for_arm_body: Local },
519 }
520
521 #[derive(Debug)]
522 struct GuardFrameLocal {
523     id: LocalVarId,
524 }
525
526 impl GuardFrameLocal {
527     fn new(id: LocalVarId, _binding_mode: BindingMode) -> Self {
528         GuardFrameLocal { id }
529     }
530 }
531
532 #[derive(Debug)]
533 struct GuardFrame {
534     /// These are the id's of names that are bound by patterns of the
535     /// arm of *this* guard.
536     ///
537     /// (Frames higher up the stack will have the id's bound in arms
538     /// further out, such as in a case like:
539     ///
540     /// match E1 {
541     ///      P1(id1) if (... (match E2 { P2(id2) if ... => B2 })) => B1,
542     /// }
543     ///
544     /// here, when building for FIXME.
545     locals: Vec<GuardFrameLocal>,
546 }
547
548 /// `ForGuard` indicates whether we are talking about:
549 ///   1. The variable for use outside of guard expressions, or
550 ///   2. The temp that holds reference to (1.), which is actually what the
551 ///      guard expressions see.
552 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
553 enum ForGuard {
554     RefWithinGuard,
555     OutsideGuard,
556 }
557
558 impl LocalsForNode {
559     fn local_id(&self, for_guard: ForGuard) -> Local {
560         match (self, for_guard) {
561             (&LocalsForNode::One(local_id), ForGuard::OutsideGuard)
562             | (
563                 &LocalsForNode::ForGuard { ref_for_guard: local_id, .. },
564                 ForGuard::RefWithinGuard,
565             )
566             | (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) => {
567                 local_id
568             }
569
570             (&LocalsForNode::One(_), ForGuard::RefWithinGuard) => {
571                 bug!("anything with one local should never be within a guard.")
572             }
573         }
574     }
575 }
576
577 struct CFG<'tcx> {
578     basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
579 }
580
581 rustc_index::newtype_index! {
582     struct ScopeId { .. }
583 }
584
585 #[derive(Debug)]
586 enum NeedsTemporary {
587     /// Use this variant when whatever you are converting with `as_operand`
588     /// is the last thing you are converting. This means that if we introduced
589     /// an intermediate temporary, we'd only read it immediately after, so we can
590     /// also avoid it.
591     No,
592     /// For all cases where you aren't sure or that are too expensive to compute
593     /// for now. It is always safe to fall back to this.
594     Maybe,
595 }
596
597 ///////////////////////////////////////////////////////////////////////////
598 /// The `BlockAnd` "monad" packages up the new basic block along with a
599 /// produced value (sometimes just unit, of course). The `unpack!`
600 /// macro (and methods below) makes working with `BlockAnd` much more
601 /// convenient.
602
603 #[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
604 struct BlockAnd<T>(BasicBlock, T);
605
606 trait BlockAndExtension {
607     fn and<T>(self, v: T) -> BlockAnd<T>;
608     fn unit(self) -> BlockAnd<()>;
609 }
610
611 impl BlockAndExtension for BasicBlock {
612     fn and<T>(self, v: T) -> BlockAnd<T> {
613         BlockAnd(self, v)
614     }
615
616     fn unit(self) -> BlockAnd<()> {
617         BlockAnd(self, ())
618     }
619 }
620
621 /// Update a block pointer and return the value.
622 /// Use it like `let x = unpack!(block = self.foo(block, foo))`.
623 macro_rules! unpack {
624     ($x:ident = $c:expr) => {{
625         let BlockAnd(b, v) = $c;
626         $x = b;
627         v
628     }};
629
630     ($c:expr) => {{
631         let BlockAnd(b, ()) = $c;
632         b
633     }};
634 }
635
636 ///////////////////////////////////////////////////////////////////////////
637 /// the main entry point for building MIR for a function
638
639 struct ArgInfo<'tcx>(
640     Ty<'tcx>,
641     Option<Span>,
642     Option<&'tcx hir::Param<'tcx>>,
643     Option<ImplicitSelfKind>,
644 );
645
646 fn construct_fn<'tcx, A>(
647     thir: &Thir<'tcx>,
648     infcx: &InferCtxt<'_, 'tcx>,
649     fn_def: ty::WithOptConstParam<LocalDefId>,
650     fn_id: hir::HirId,
651     arguments: A,
652     safety: Safety,
653     abi: Abi,
654     return_ty: Ty<'tcx>,
655     return_ty_span: Span,
656     body: &'tcx hir::Body<'tcx>,
657     expr: ExprId,
658     span_with_body: Span,
659 ) -> Body<'tcx>
660 where
661     A: Iterator<Item = ArgInfo<'tcx>>,
662 {
663     let arguments: Vec<_> = arguments.collect();
664
665     let tcx = infcx.tcx;
666     let span = tcx.hir().span(fn_id);
667
668     let mut builder = Builder::new(
669         thir,
670         infcx,
671         fn_def,
672         fn_id,
673         span_with_body,
674         arguments.len(),
675         safety,
676         return_ty,
677         return_ty_span,
678         body.generator_kind,
679     );
680
681     let call_site_scope =
682         region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::CallSite };
683     let arg_scope =
684         region::Scope { id: body.value.hir_id.local_id, data: region::ScopeData::Arguments };
685     let source_info = builder.source_info(span);
686     let call_site_s = (call_site_scope, source_info);
687     unpack!(builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
688         let arg_scope_s = (arg_scope, source_info);
689         // Attribute epilogue to function's closing brace
690         let fn_end = span_with_body.shrink_to_hi();
691         let return_block =
692             unpack!(builder.in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
693                 Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
694                     builder.args_and_body(
695                         START_BLOCK,
696                         fn_def.did,
697                         &arguments,
698                         arg_scope,
699                         &thir[expr],
700                     )
701                 }))
702             }));
703         let source_info = builder.source_info(fn_end);
704         builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
705         builder.build_drop_trees();
706         return_block.unit()
707     }));
708
709     let spread_arg = if abi == Abi::RustCall {
710         // RustCall pseudo-ABI untuples the last argument.
711         Some(Local::new(arguments.len()))
712     } else {
713         None
714     };
715
716     let mut body = builder.finish();
717     body.spread_arg = spread_arg;
718     body
719 }
720
721 fn construct_const<'a, 'tcx>(
722     thir: &'a Thir<'tcx>,
723     infcx: &'a InferCtxt<'a, 'tcx>,
724     expr: ExprId,
725     def: ty::WithOptConstParam<LocalDefId>,
726     hir_id: hir::HirId,
727     const_ty: Ty<'tcx>,
728     const_ty_span: Span,
729     span: Span,
730 ) -> Body<'tcx> {
731     let mut builder = Builder::new(
732         thir,
733         infcx,
734         def,
735         hir_id,
736         span,
737         0,
738         Safety::Safe,
739         const_ty,
740         const_ty_span,
741         None,
742     );
743
744     let mut block = START_BLOCK;
745     unpack!(block = builder.expr_into_dest(Place::return_place(), block, &thir[expr]));
746
747     let source_info = builder.source_info(span);
748     builder.cfg.terminate(block, source_info, TerminatorKind::Return);
749
750     builder.build_drop_trees();
751
752     builder.finish()
753 }
754
755 /// Construct MIR for an item that has had errors in type checking.
756 ///
757 /// This is required because we may still want to run MIR passes on an item
758 /// with type errors, but normal MIR construction can't handle that in general.
759 fn construct_error<'a, 'tcx>(
760     infcx: &'a InferCtxt<'a, 'tcx>,
761     def: ty::WithOptConstParam<LocalDefId>,
762     hir_id: hir::HirId,
763     body_id: hir::BodyId,
764     body_owner_kind: hir::BodyOwnerKind,
765     err: ErrorGuaranteed,
766 ) -> Body<'tcx> {
767     let tcx = infcx.tcx;
768     let span = tcx.hir().span(hir_id);
769     let ty = tcx.ty_error();
770     let generator_kind = tcx.hir().body(body_id).generator_kind;
771     let num_params = match body_owner_kind {
772         hir::BodyOwnerKind::Fn => tcx.hir().fn_decl_by_hir_id(hir_id).unwrap().inputs.len(),
773         hir::BodyOwnerKind::Closure => {
774             if generator_kind.is_some() {
775                 // Generators have an implicit `self` parameter *and* a possibly
776                 // implicit resume parameter.
777                 2
778             } else {
779                 // The implicit self parameter adds another local in MIR.
780                 1 + tcx.hir().fn_decl_by_hir_id(hir_id).unwrap().inputs.len()
781             }
782         }
783         hir::BodyOwnerKind::Const => 0,
784         hir::BodyOwnerKind::Static(_) => 0,
785     };
786     let mut cfg = CFG { basic_blocks: IndexVec::new() };
787     let mut source_scopes = IndexVec::new();
788     let mut local_decls = IndexVec::from_elem_n(LocalDecl::new(ty, span), 1);
789
790     cfg.start_new_block();
791     source_scopes.push(SourceScopeData {
792         span,
793         parent_scope: None,
794         inlined: None,
795         inlined_parent_scope: None,
796         local_data: ClearCrossCrate::Set(SourceScopeLocalData {
797             lint_root: hir_id,
798             safety: Safety::Safe,
799         }),
800     });
801     let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
802
803     // Some MIR passes will expect the number of parameters to match the
804     // function declaration.
805     for _ in 0..num_params {
806         local_decls.push(LocalDecl::with_source_info(ty, source_info));
807     }
808     cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
809
810     let mut body = Body::new(
811         MirSource::item(def.did.to_def_id()),
812         cfg.basic_blocks,
813         source_scopes,
814         local_decls,
815         IndexVec::new(),
816         num_params,
817         vec![],
818         span,
819         generator_kind,
820         Some(err),
821     );
822     body.generator.as_mut().map(|gen| gen.yield_ty = Some(ty));
823     body
824 }
825
826 impl<'a, 'tcx> Builder<'a, 'tcx> {
827     fn new(
828         thir: &'a Thir<'tcx>,
829         infcx: &'a InferCtxt<'a, 'tcx>,
830         def: ty::WithOptConstParam<LocalDefId>,
831         hir_id: hir::HirId,
832         span: Span,
833         arg_count: usize,
834         safety: Safety,
835         return_ty: Ty<'tcx>,
836         return_span: Span,
837         generator_kind: Option<GeneratorKind>,
838     ) -> Builder<'a, 'tcx> {
839         let tcx = infcx.tcx;
840         let attrs = tcx.hir().attrs(hir_id);
841         // Some functions always have overflow checks enabled,
842         // however, they may not get codegen'd, depending on
843         // the settings for the crate they are codegened in.
844         let mut check_overflow = tcx.sess.contains_name(attrs, sym::rustc_inherit_overflow_checks);
845         // Respect -C overflow-checks.
846         check_overflow |= tcx.sess.overflow_checks();
847         // Constants always need overflow checks.
848         check_overflow |= matches!(
849             tcx.hir().body_owner_kind(def.did),
850             hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_)
851         );
852
853         let lint_level = LintLevel::Explicit(hir_id);
854         let param_env = tcx.param_env(def.did);
855         let mut builder = Builder {
856             thir,
857             tcx,
858             infcx,
859             typeck_results: tcx.typeck_opt_const_arg(def),
860             region_scope_tree: tcx.region_scope_tree(def.did),
861             param_env,
862             def_id: def.did.to_def_id(),
863             hir_id,
864             parent_module: tcx.parent_module(hir_id).to_def_id(),
865             check_overflow,
866             cfg: CFG { basic_blocks: IndexVec::new() },
867             fn_span: span,
868             arg_count,
869             generator_kind,
870             scopes: scope::Scopes::new(),
871             block_context: BlockContext::new(),
872             source_scopes: IndexVec::new(),
873             source_scope: OUTERMOST_SOURCE_SCOPE,
874             guard_context: vec![],
875             in_scope_unsafe: safety,
876             local_decls: IndexVec::from_elem_n(LocalDecl::new(return_ty, return_span), 1),
877             canonical_user_type_annotations: IndexVec::new(),
878             upvars: CaptureMap::new(),
879             var_indices: Default::default(),
880             unit_temp: None,
881             var_debug_info: vec![],
882         };
883
884         assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
885         assert_eq!(
886             builder.new_source_scope(span, lint_level, Some(safety)),
887             OUTERMOST_SOURCE_SCOPE
888         );
889         builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
890
891         builder
892     }
893
894     fn finish(self) -> Body<'tcx> {
895         for (index, block) in self.cfg.basic_blocks.iter().enumerate() {
896             if block.terminator.is_none() {
897                 span_bug!(self.fn_span, "no terminator on block {:?}", index);
898             }
899         }
900
901         Body::new(
902             MirSource::item(self.def_id),
903             self.cfg.basic_blocks,
904             self.source_scopes,
905             self.local_decls,
906             self.canonical_user_type_annotations,
907             self.arg_count,
908             self.var_debug_info,
909             self.fn_span,
910             self.generator_kind,
911             self.typeck_results.tainted_by_errors,
912         )
913     }
914
915     fn args_and_body(
916         &mut self,
917         mut block: BasicBlock,
918         fn_def_id: LocalDefId,
919         arguments: &[ArgInfo<'tcx>],
920         argument_scope: region::Scope,
921         expr: &Expr<'tcx>,
922     ) -> BlockAnd<()> {
923         // Allocate locals for the function arguments
924         for &ArgInfo(ty, _, arg_opt, _) in arguments.iter() {
925             let source_info =
926                 SourceInfo::outermost(arg_opt.map_or(self.fn_span, |arg| arg.pat.span));
927             let arg_local = self.local_decls.push(LocalDecl::with_source_info(ty, source_info));
928
929             // If this is a simple binding pattern, give debuginfo a nice name.
930             if let Some(arg) = arg_opt && let Some(ident) = arg.pat.simple_ident() {
931                 self.var_debug_info.push(VarDebugInfo {
932                     name: ident.name,
933                     source_info,
934                     value: VarDebugInfoContents::Place(arg_local.into()),
935                 });
936             }
937         }
938
939         let tcx = self.tcx;
940         let tcx_hir = tcx.hir();
941         let hir_typeck_results = self.typeck_results;
942
943         // In analyze_closure() in upvar.rs we gathered a list of upvars used by an
944         // indexed closure and we stored in a map called closure_min_captures in TypeckResults
945         // with the closure's DefId. Here, we run through that vec of UpvarIds for
946         // the given closure and use the necessary information to create upvar
947         // debuginfo and to fill `self.upvars`.
948         if hir_typeck_results.closure_min_captures.get(&fn_def_id).is_some() {
949             let mut closure_env_projs = vec![];
950             let mut closure_ty = self.local_decls[ty::CAPTURE_STRUCT_LOCAL].ty;
951             if let ty::Ref(_, ty, _) = closure_ty.kind() {
952                 closure_env_projs.push(ProjectionElem::Deref);
953                 closure_ty = *ty;
954             }
955             let upvar_substs = match closure_ty.kind() {
956                 ty::Closure(_, substs) => ty::UpvarSubsts::Closure(substs),
957                 ty::Generator(_, substs, _) => ty::UpvarSubsts::Generator(substs),
958                 _ => span_bug!(self.fn_span, "upvars with non-closure env ty {:?}", closure_ty),
959             };
960             let def_id = self.def_id.as_local().unwrap();
961             let capture_syms = tcx.symbols_for_closure_captures((def_id, fn_def_id));
962             let capture_tys = upvar_substs.upvar_tys();
963             let captures_with_tys = hir_typeck_results
964                 .closure_min_captures_flattened(fn_def_id)
965                 .zip(capture_tys.zip(capture_syms));
966
967             self.upvars = captures_with_tys
968                 .enumerate()
969                 .map(|(i, (captured_place, (ty, sym)))| {
970                     let capture = captured_place.info.capture_kind;
971                     let var_id = match captured_place.place.base {
972                         HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
973                         _ => bug!("Expected an upvar"),
974                     };
975
976                     let mutability = captured_place.mutability;
977
978                     let mut projs = closure_env_projs.clone();
979                     projs.push(ProjectionElem::Field(Field::new(i), ty));
980                     match capture {
981                         ty::UpvarCapture::ByValue => {}
982                         ty::UpvarCapture::ByRef(..) => {
983                             projs.push(ProjectionElem::Deref);
984                         }
985                     };
986
987                     let use_place = Place {
988                         local: ty::CAPTURE_STRUCT_LOCAL,
989                         projection: tcx.intern_place_elems(&projs),
990                     };
991                     self.var_debug_info.push(VarDebugInfo {
992                         name: *sym,
993                         source_info: SourceInfo::outermost(tcx_hir.span(var_id)),
994                         value: VarDebugInfoContents::Place(use_place),
995                     });
996
997                     let capture = Capture { captured_place, use_place, mutability };
998                     (var_id, capture)
999                 })
1000                 .collect();
1001         }
1002
1003         let mut scope = None;
1004         // Bind the argument patterns
1005         for (index, arg_info) in arguments.iter().enumerate() {
1006             // Function arguments always get the first Local indices after the return place
1007             let local = Local::new(index + 1);
1008             let place = Place::from(local);
1009             let &ArgInfo(_, opt_ty_info, arg_opt, ref self_binding) = arg_info;
1010
1011             // Make sure we drop (parts of) the argument even when not matched on.
1012             self.schedule_drop(
1013                 arg_opt.as_ref().map_or(expr.span, |arg| arg.pat.span),
1014                 argument_scope,
1015                 local,
1016                 DropKind::Value,
1017             );
1018
1019             let Some(arg) = arg_opt else {
1020                 continue;
1021             };
1022             let pat = match tcx.hir().get(arg.pat.hir_id) {
1023                 Node::Pat(pat) => pat,
1024                 node => bug!("pattern became {:?}", node),
1025             };
1026             let pattern = pat_from_hir(tcx, self.param_env, self.typeck_results, pat);
1027             let original_source_scope = self.source_scope;
1028             let span = pattern.span;
1029             self.set_correct_source_scope_for_arg(arg.hir_id, original_source_scope, span);
1030             match pattern.kind {
1031                 // Don't introduce extra copies for simple bindings
1032                 PatKind::Binding {
1033                     mutability,
1034                     var,
1035                     mode: BindingMode::ByValue,
1036                     subpattern: None,
1037                     ..
1038                 } => {
1039                     self.local_decls[local].mutability = mutability;
1040                     self.local_decls[local].source_info.scope = self.source_scope;
1041                     self.local_decls[local].local_info = if let Some(kind) = self_binding {
1042                         Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(
1043                             BindingForm::ImplicitSelf(*kind),
1044                         ))))
1045                     } else {
1046                         let binding_mode = ty::BindingMode::BindByValue(mutability);
1047                         Some(Box::new(LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
1048                             VarBindingForm {
1049                                 binding_mode,
1050                                 opt_ty_info,
1051                                 opt_match_place: Some((None, span)),
1052                                 pat_span: span,
1053                             },
1054                         )))))
1055                     };
1056                     self.var_indices.insert(var, LocalsForNode::One(local));
1057                 }
1058                 _ => {
1059                     scope = self.declare_bindings(
1060                         scope,
1061                         expr.span,
1062                         &pattern,
1063                         matches::ArmHasGuard(false),
1064                         Some((Some(&place), span)),
1065                     );
1066                     let place_builder = PlaceBuilder::from(local);
1067                     unpack!(
1068                         block =
1069                             self.place_into_pattern(block, pattern.as_ref(), place_builder, false)
1070                     );
1071                 }
1072             }
1073             self.source_scope = original_source_scope;
1074         }
1075
1076         // Enter the argument pattern bindings source scope, if it exists.
1077         if let Some(source_scope) = scope {
1078             self.source_scope = source_scope;
1079         }
1080
1081         self.expr_into_dest(Place::return_place(), block, &expr)
1082     }
1083
1084     fn set_correct_source_scope_for_arg(
1085         &mut self,
1086         arg_hir_id: hir::HirId,
1087         original_source_scope: SourceScope,
1088         pattern_span: Span,
1089     ) {
1090         let tcx = self.tcx;
1091         let current_root = tcx.maybe_lint_level_root_bounded(arg_hir_id, self.hir_id);
1092         let parent_root = tcx.maybe_lint_level_root_bounded(
1093             self.source_scopes[original_source_scope]
1094                 .local_data
1095                 .as_ref()
1096                 .assert_crate_local()
1097                 .lint_root,
1098             self.hir_id,
1099         );
1100         if current_root != parent_root {
1101             self.source_scope =
1102                 self.new_source_scope(pattern_span, LintLevel::Explicit(current_root), None);
1103         }
1104     }
1105
1106     fn get_unit_temp(&mut self) -> Place<'tcx> {
1107         match self.unit_temp {
1108             Some(tmp) => tmp,
1109             None => {
1110                 let ty = self.tcx.mk_unit();
1111                 let fn_span = self.fn_span;
1112                 let tmp = self.temp(ty, fn_span);
1113                 self.unit_temp = Some(tmp);
1114                 tmp
1115             }
1116         }
1117     }
1118 }
1119
1120 fn parse_float_into_constval<'tcx>(
1121     num: Symbol,
1122     float_ty: ty::FloatTy,
1123     neg: bool,
1124 ) -> Option<ConstValue<'tcx>> {
1125     parse_float_into_scalar(num, float_ty, neg).map(ConstValue::Scalar)
1126 }
1127
1128 pub(crate) fn parse_float_into_scalar(
1129     num: Symbol,
1130     float_ty: ty::FloatTy,
1131     neg: bool,
1132 ) -> Option<Scalar> {
1133     let num = num.as_str();
1134     match float_ty {
1135         ty::FloatTy::F32 => {
1136             let Ok(rust_f) = num.parse::<f32>() else { return None };
1137             let mut f = num.parse::<Single>().unwrap_or_else(|e| {
1138                 panic!("apfloat::ieee::Single failed to parse `{}`: {:?}", num, e)
1139             });
1140
1141             assert!(
1142                 u128::from(rust_f.to_bits()) == f.to_bits(),
1143                 "apfloat::ieee::Single gave different result for `{}`: \
1144                  {}({:#x}) vs Rust's {}({:#x})",
1145                 rust_f,
1146                 f,
1147                 f.to_bits(),
1148                 Single::from_bits(rust_f.to_bits().into()),
1149                 rust_f.to_bits()
1150             );
1151
1152             if neg {
1153                 f = -f;
1154             }
1155
1156             Some(Scalar::from_f32(f))
1157         }
1158         ty::FloatTy::F64 => {
1159             let Ok(rust_f) = num.parse::<f64>() else { return None };
1160             let mut f = num.parse::<Double>().unwrap_or_else(|e| {
1161                 panic!("apfloat::ieee::Double failed to parse `{}`: {:?}", num, e)
1162             });
1163
1164             assert!(
1165                 u128::from(rust_f.to_bits()) == f.to_bits(),
1166                 "apfloat::ieee::Double gave different result for `{}`: \
1167                  {}({:#x}) vs Rust's {}({:#x})",
1168                 rust_f,
1169                 f,
1170                 f.to_bits(),
1171                 Double::from_bits(rust_f.to_bits().into()),
1172                 rust_f.to_bits()
1173             );
1174
1175             if neg {
1176                 f = -f;
1177             }
1178
1179             Some(Scalar::from_f64(f))
1180         }
1181     }
1182 }
1183
1184 ///////////////////////////////////////////////////////////////////////////
1185 // Builder methods are broken up into modules, depending on what kind
1186 // of thing is being lowered. Note that they use the `unpack` macro
1187 // above extensively.
1188
1189 mod block;
1190 mod cfg;
1191 mod expr;
1192 mod matches;
1193 mod misc;
1194 mod scope;
1195
1196 pub(crate) use expr::category::Category as ExprCategory;