rustc_mir_build/builder/
mod.rs

1//! This module used to be named `build`, but that was causing GitHub's
2//! "Go to file" feature to silently ignore all files in the module, probably
3//! because it assumes that "build" is a build-output directory.
4//! See <https://github.com/rust-lang/rust/pull/134365>.
5
6use itertools::Itertools;
7use rustc_abi::{ExternAbi, FieldIdx};
8use rustc_apfloat::Float;
9use rustc_apfloat::ieee::{Double, Half, Quad, Single};
10use rustc_ast::attr;
11use rustc_data_structures::fx::FxHashMap;
12use rustc_data_structures::sorted_map::SortedIndexMultiMap;
13use rustc_errors::ErrorGuaranteed;
14use rustc_hir::def::DefKind;
15use rustc_hir::def_id::{DefId, LocalDefId};
16use rustc_hir::{self as hir, BindingMode, ByRef, HirId, Node};
17use rustc_index::bit_set::GrowableBitSet;
18use rustc_index::{Idx, IndexSlice, IndexVec};
19use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
20use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
21use rustc_middle::middle::region;
22use rustc_middle::mir::*;
23use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
24use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
25use rustc_middle::{bug, span_bug};
26use rustc_span::{Span, Symbol, sym};
27
28use crate::builder::expr::as_place::PlaceBuilder;
29use crate::builder::scope::DropKind;
30
31pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
32    tcx: TyCtxt<'tcx>,
33    def_id: LocalDefId,
34) -> IndexVec<FieldIdx, Symbol> {
35    tcx.closure_captures(def_id)
36        .iter()
37        .map(|captured_place| {
38            let name = captured_place.to_symbol();
39            match captured_place.info.capture_kind {
40                ty::UpvarCapture::ByValue => name,
41                ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
42            }
43        })
44        .collect()
45}
46
47/// Create the MIR for a given `DefId`, including unreachable code. Do not call
48/// this directly; instead use the cached version via `mir_built`.
49pub fn build_mir<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
50    tcx.ensure_done().thir_abstract_const(def);
51    if let Err(e) = tcx.check_match(def) {
52        return construct_error(tcx, def, e);
53    }
54
55    if let Err(err) = tcx.check_tail_calls(def) {
56        return construct_error(tcx, def, err);
57    }
58
59    let body = match tcx.thir_body(def) {
60        Err(error_reported) => construct_error(tcx, def, error_reported),
61        Ok((thir, expr)) => {
62            let build_mir = |thir: &Thir<'tcx>| match thir.body_type {
63                thir::BodyTy::Fn(fn_sig) => construct_fn(tcx, def, thir, expr, fn_sig),
64                thir::BodyTy::Const(ty) => construct_const(tcx, def, thir, expr, ty),
65            };
66
67            // this must run before MIR dump, because
68            // "not all control paths return a value" is reported here.
69            //
70            // maybe move the check to a MIR pass?
71            tcx.ensure_ok().check_liveness(def);
72
73            // Don't steal here, instead steal in unsafeck. This is so that
74            // pattern inline constants can be evaluated as part of building the
75            // THIR of the parent function without a cycle.
76            build_mir(&thir.borrow())
77        }
78    };
79
80    // The borrow checker will replace all the regions here with its own
81    // inference variables. There's no point having non-erased regions here.
82    // The exception is `body.user_type_annotations`, which is used unmodified
83    // by borrow checking.
84    debug_assert!(
85        !(body.local_decls.has_free_regions()
86            || body.basic_blocks.has_free_regions()
87            || body.var_debug_info.has_free_regions()
88            || body.yield_ty().has_free_regions()),
89        "Unexpected free regions in MIR: {body:?}",
90    );
91
92    body
93}
94
95///////////////////////////////////////////////////////////////////////////
96// BuildMir -- walks a crate, looking for fn items and methods to build MIR from
97
98#[derive(Debug, PartialEq, Eq)]
99enum BlockFrame {
100    /// Evaluation is currently within a statement.
101    ///
102    /// Examples include:
103    /// 1. `EXPR;`
104    /// 2. `let _ = EXPR;`
105    /// 3. `let x = EXPR;`
106    Statement {
107        /// If true, then statement discards result from evaluating
108        /// the expression (such as examples 1 and 2 above).
109        ignores_expr_result: bool,
110    },
111
112    /// Evaluation is currently within the tail expression of a block.
113    ///
114    /// Example: `{ STMT_1; STMT_2; EXPR }`
115    TailExpr {
116        /// If true, then the surrounding context of the block ignores
117        /// the result of evaluating the block's tail expression.
118        ///
119        /// Example: `let _ = { STMT_1; EXPR };`
120        tail_result_is_ignored: bool,
121
122        /// `Span` of the tail expression.
123        span: Span,
124    },
125
126    /// Generic mark meaning that the block occurred as a subexpression
127    /// where the result might be used.
128    ///
129    /// Examples: `foo(EXPR)`, `match EXPR { ... }`
130    SubExpr,
131}
132
133impl BlockFrame {
134    fn is_tail_expr(&self) -> bool {
135        match *self {
136            BlockFrame::TailExpr { .. } => true,
137
138            BlockFrame::Statement { .. } | BlockFrame::SubExpr => false,
139        }
140    }
141    fn is_statement(&self) -> bool {
142        match *self {
143            BlockFrame::Statement { .. } => true,
144
145            BlockFrame::TailExpr { .. } | BlockFrame::SubExpr => false,
146        }
147    }
148}
149
150#[derive(Debug)]
151struct BlockContext(Vec<BlockFrame>);
152
153struct Builder<'a, 'tcx> {
154    tcx: TyCtxt<'tcx>,
155    // FIXME(@lcnr): Why does this use an `infcx`, there should be
156    // no shared type inference going on here. I feel like it would
157    // clearer to manually construct one where necessary or to provide
158    // a nice API for non-type inference trait system checks.
159    infcx: InferCtxt<'tcx>,
160    region_scope_tree: &'tcx region::ScopeTree,
161    param_env: ty::ParamEnv<'tcx>,
162
163    thir: &'a Thir<'tcx>,
164    cfg: CFG<'tcx>,
165
166    def_id: LocalDefId,
167    hir_id: HirId,
168    parent_module: DefId,
169    check_overflow: bool,
170    fn_span: Span,
171    arg_count: usize,
172    coroutine: Option<Box<CoroutineInfo<'tcx>>>,
173
174    /// The current set of scopes, updated as we traverse;
175    /// see the `scope` module for more details.
176    scopes: scope::Scopes<'tcx>,
177
178    /// The block-context: each time we build the code within an thir::Block,
179    /// we push a frame here tracking whether we are building a statement or
180    /// if we are pushing the tail expression of the block. This is used to
181    /// embed information in generated temps about whether they were created
182    /// for a block tail expression or not.
183    ///
184    /// It would be great if we could fold this into `self.scopes`
185    /// somehow, but right now I think that is very tightly tied to
186    /// the code generation in ways that we cannot (or should not)
187    /// start just throwing new entries onto that vector in order to
188    /// distinguish the context of EXPR1 from the context of EXPR2 in
189    /// `{ STMTS; EXPR1 } + EXPR2`.
190    block_context: BlockContext,
191
192    /// The vector of all scopes that we have created thus far;
193    /// we track this for debuginfo later.
194    source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
195    source_scope: SourceScope,
196
197    /// The guard-context: each time we build the guard expression for
198    /// a match arm, we push onto this stack, and then pop when we
199    /// finish building it.
200    guard_context: Vec<GuardFrame>,
201
202    /// Temporaries with fixed indexes. Used so that if-let guards on arms
203    /// with an or-pattern are only created once.
204    fixed_temps: FxHashMap<ExprId, Local>,
205    /// Scope of temporaries that should be deduplicated using [Self::fixed_temps].
206    fixed_temps_scope: Option<region::Scope>,
207
208    /// Maps `HirId`s of variable bindings to the `Local`s created for them.
209    /// (A match binding can have two locals; the 2nd is for the arm's guard.)
210    var_indices: FxHashMap<LocalVarId, LocalsForNode>,
211    local_decls: IndexVec<Local, LocalDecl<'tcx>>,
212    canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
213    upvars: CaptureMap<'tcx>,
214    unit_temp: Option<Place<'tcx>>,
215
216    var_debug_info: Vec<VarDebugInfo<'tcx>>,
217
218    // A cache for `maybe_lint_level_roots_bounded`. That function is called
219    // repeatedly, and each time it effectively traces a path through a tree
220    // structure from a node towards the root, doing an attribute check on each
221    // node along the way. This cache records which nodes trace all the way to
222    // the root (most of them do) and saves us from retracing many sub-paths
223    // many times, and rechecking many nodes.
224    lint_level_roots_cache: GrowableBitSet<hir::ItemLocalId>,
225
226    /// Collects additional coverage information during MIR building.
227    /// Only present if coverage is enabled and this function is eligible.
228    coverage_info: Option<coverageinfo::CoverageInfoBuilder>,
229}
230
231type CaptureMap<'tcx> = SortedIndexMultiMap<usize, HirId, Capture<'tcx>>;
232
233#[derive(Debug)]
234struct Capture<'tcx> {
235    captured_place: &'tcx ty::CapturedPlace<'tcx>,
236    use_place: Place<'tcx>,
237    mutability: Mutability,
238}
239
240impl<'a, 'tcx> Builder<'a, 'tcx> {
241    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
242        self.infcx.typing_env(self.param_env)
243    }
244
245    fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
246        self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
247    }
248
249    fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
250        self.var_indices[&id].local_id(for_guard)
251    }
252}
253
254impl BlockContext {
255    fn new() -> Self {
256        BlockContext(vec![])
257    }
258    fn push(&mut self, bf: BlockFrame) {
259        self.0.push(bf);
260    }
261    fn pop(&mut self) -> Option<BlockFrame> {
262        self.0.pop()
263    }
264
265    /// Traverses the frames on the `BlockContext`, searching for either
266    /// the first block-tail expression frame with no intervening
267    /// statement frame.
268    ///
269    /// Notably, this skips over `SubExpr` frames; this method is
270    /// meant to be used in the context of understanding the
271    /// relationship of a temp (created within some complicated
272    /// expression) with its containing expression, and whether the
273    /// value of that *containing expression* (not the temp!) is
274    /// ignored.
275    fn currently_in_block_tail(&self) -> Option<BlockTailInfo> {
276        for bf in self.0.iter().rev() {
277            match bf {
278                BlockFrame::SubExpr => continue,
279                BlockFrame::Statement { .. } => break,
280                &BlockFrame::TailExpr { tail_result_is_ignored, span } => {
281                    return Some(BlockTailInfo { tail_result_is_ignored, span });
282                }
283            }
284        }
285
286        None
287    }
288
289    /// Looks at the topmost frame on the BlockContext and reports
290    /// whether its one that would discard a block tail result.
291    ///
292    /// Unlike `currently_within_ignored_tail_expression`, this does
293    /// *not* skip over `SubExpr` frames: here, we want to know
294    /// whether the block result itself is discarded.
295    fn currently_ignores_tail_results(&self) -> bool {
296        match self.0.last() {
297            // no context: conservatively assume result is read
298            None => false,
299
300            // sub-expression: block result feeds into some computation
301            Some(BlockFrame::SubExpr) => false,
302
303            // otherwise: use accumulated is_ignored state.
304            Some(
305                BlockFrame::TailExpr { tail_result_is_ignored: ignored, .. }
306                | BlockFrame::Statement { ignores_expr_result: ignored },
307            ) => *ignored,
308        }
309    }
310}
311
312#[derive(Debug)]
313enum LocalsForNode {
314    /// In the usual case, a `HirId` for an identifier maps to at most
315    /// one `Local` declaration.
316    One(Local),
317
318    /// The exceptional case is identifiers in a match arm's pattern
319    /// that are referenced in a guard of that match arm. For these,
320    /// we have `2` Locals.
321    ///
322    /// * `for_arm_body` is the Local used in the arm body (which is
323    ///   just like the `One` case above),
324    ///
325    /// * `ref_for_guard` is the Local used in the arm's guard (which
326    ///   is a reference to a temp that is an alias of
327    ///   `for_arm_body`).
328    ForGuard { ref_for_guard: Local, for_arm_body: Local },
329}
330
331#[derive(Debug)]
332struct GuardFrameLocal {
333    id: LocalVarId,
334}
335
336impl GuardFrameLocal {
337    fn new(id: LocalVarId) -> Self {
338        GuardFrameLocal { id }
339    }
340}
341
342#[derive(Debug)]
343struct GuardFrame {
344    /// These are the id's of names that are bound by patterns of the
345    /// arm of *this* guard.
346    ///
347    /// (Frames higher up the stack will have the id's bound in arms
348    /// further out, such as in a case like:
349    ///
350    /// match E1 {
351    ///      P1(id1) if (... (match E2 { P2(id2) if ... => B2 })) => B1,
352    /// }
353    ///
354    /// here, when building for FIXME.
355    locals: Vec<GuardFrameLocal>,
356}
357
358/// `ForGuard` indicates whether we are talking about:
359///   1. The variable for use outside of guard expressions, or
360///   2. The temp that holds reference to (1.), which is actually what the
361///      guard expressions see.
362#[derive(Copy, Clone, Debug, PartialEq, Eq)]
363enum ForGuard {
364    RefWithinGuard,
365    OutsideGuard,
366}
367
368impl LocalsForNode {
369    fn local_id(&self, for_guard: ForGuard) -> Local {
370        match (self, for_guard) {
371            (&LocalsForNode::One(local_id), ForGuard::OutsideGuard)
372            | (
373                &LocalsForNode::ForGuard { ref_for_guard: local_id, .. },
374                ForGuard::RefWithinGuard,
375            )
376            | (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) => {
377                local_id
378            }
379
380            (&LocalsForNode::One(_), ForGuard::RefWithinGuard) => {
381                bug!("anything with one local should never be within a guard.")
382            }
383        }
384    }
385}
386
387struct CFG<'tcx> {
388    basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
389}
390
391rustc_index::newtype_index! {
392    struct ScopeId {}
393}
394
395#[derive(Debug)]
396enum NeedsTemporary {
397    /// Use this variant when whatever you are converting with `as_operand`
398    /// is the last thing you are converting. This means that if we introduced
399    /// an intermediate temporary, we'd only read it immediately after, so we can
400    /// also avoid it.
401    No,
402    /// For all cases where you aren't sure or that are too expensive to compute
403    /// for now. It is always safe to fall back to this.
404    Maybe,
405}
406
407///////////////////////////////////////////////////////////////////////////
408/// The `BlockAnd` "monad" packages up the new basic block along with a
409/// produced value (sometimes just unit, of course). The `unpack!`
410/// macro (and methods below) makes working with `BlockAnd` much more
411/// convenient.
412
413#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
414struct BlockAnd<T>(BasicBlock, T);
415
416impl BlockAnd<()> {
417    /// Unpacks `BlockAnd<()>` into a [`BasicBlock`].
418    #[must_use]
419    fn into_block(self) -> BasicBlock {
420        let Self(block, ()) = self;
421        block
422    }
423}
424
425trait BlockAndExtension {
426    fn and<T>(self, v: T) -> BlockAnd<T>;
427    fn unit(self) -> BlockAnd<()>;
428}
429
430impl BlockAndExtension for BasicBlock {
431    fn and<T>(self, v: T) -> BlockAnd<T> {
432        BlockAnd(self, v)
433    }
434
435    fn unit(self) -> BlockAnd<()> {
436        BlockAnd(self, ())
437    }
438}
439
440/// Update a block pointer and return the value.
441/// Use it like `let x = unpack!(block = self.foo(block, foo))`.
442macro_rules! unpack {
443    ($x:ident = $c:expr) => {{
444        let BlockAnd(b, v) = $c;
445        $x = b;
446        v
447    }};
448}
449
450///////////////////////////////////////////////////////////////////////////
451/// the main entry point for building MIR for a function
452
453fn construct_fn<'tcx>(
454    tcx: TyCtxt<'tcx>,
455    fn_def: LocalDefId,
456    thir: &Thir<'tcx>,
457    expr: ExprId,
458    fn_sig: ty::FnSig<'tcx>,
459) -> Body<'tcx> {
460    let span = tcx.def_span(fn_def);
461    let fn_id = tcx.local_def_id_to_hir_id(fn_def);
462
463    // The representation of thir for `-Zunpretty=thir-tree` relies on
464    // the entry expression being the last element of `thir.exprs`.
465    assert_eq!(expr.as_usize(), thir.exprs.len() - 1);
466
467    // Figure out what primary body this item has.
468    let body = tcx.hir().body_owned_by(fn_def);
469    let span_with_body = tcx.hir().span_with_body(fn_id);
470    let return_ty_span = tcx
471        .hir()
472        .fn_decl_by_hir_id(fn_id)
473        .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def))
474        .output
475        .span();
476
477    let mut abi = fn_sig.abi;
478    if let DefKind::Closure = tcx.def_kind(fn_def) {
479        // HACK(eddyb) Avoid having RustCall on closures,
480        // as it adds unnecessary (and wrong) auto-tupling.
481        abi = ExternAbi::Rust;
482    }
483
484    let arguments = &thir.params;
485
486    let return_ty = fn_sig.output();
487    let coroutine = match tcx.type_of(fn_def).instantiate_identity().kind() {
488        ty::Coroutine(_, args) => Some(Box::new(CoroutineInfo::initial(
489            tcx.coroutine_kind(fn_def).unwrap(),
490            args.as_coroutine().yield_ty(),
491            args.as_coroutine().resume_ty(),
492        ))),
493        ty::Closure(..) | ty::CoroutineClosure(..) | ty::FnDef(..) => None,
494        ty => span_bug!(span_with_body, "unexpected type of body: {ty:?}"),
495    };
496
497    if let Some(custom_mir_attr) =
498        tcx.hir().attrs(fn_id).iter().find(|attr| attr.name_or_empty() == sym::custom_mir)
499    {
500        return custom::build_custom_mir(
501            tcx,
502            fn_def.to_def_id(),
503            fn_id,
504            thir,
505            expr,
506            arguments,
507            return_ty,
508            return_ty_span,
509            span_with_body,
510            custom_mir_attr,
511        );
512    }
513
514    // FIXME(#132279): This should be able to reveal opaque
515    // types defined during HIR typeck.
516    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
517    let mut builder = Builder::new(
518        thir,
519        infcx,
520        fn_def,
521        fn_id,
522        span_with_body,
523        arguments.len(),
524        return_ty,
525        return_ty_span,
526        coroutine,
527    );
528
529    let call_site_scope =
530        region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
531    let arg_scope =
532        region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
533    let source_info = builder.source_info(span);
534    let call_site_s = (call_site_scope, source_info);
535    let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
536        let arg_scope_s = (arg_scope, source_info);
537        // Attribute epilogue to function's closing brace
538        let fn_end = span_with_body.shrink_to_hi();
539        let return_block = builder
540            .in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
541                Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
542                    builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
543                }))
544            })
545            .into_block();
546        let source_info = builder.source_info(fn_end);
547        builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
548        builder.build_drop_trees();
549        return_block.unit()
550    });
551
552    let mut body = builder.finish();
553
554    body.spread_arg = if abi == ExternAbi::RustCall {
555        // RustCall pseudo-ABI untuples the last argument.
556        Some(Local::new(arguments.len()))
557    } else {
558        None
559    };
560
561    body
562}
563
564fn construct_const<'a, 'tcx>(
565    tcx: TyCtxt<'tcx>,
566    def: LocalDefId,
567    thir: &'a Thir<'tcx>,
568    expr: ExprId,
569    const_ty: Ty<'tcx>,
570) -> Body<'tcx> {
571    let hir_id = tcx.local_def_id_to_hir_id(def);
572
573    // Figure out what primary body this item has.
574    let (span, const_ty_span) = match tcx.hir_node(hir_id) {
575        Node::Item(hir::Item {
576            kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _, _),
577            span,
578            ..
579        })
580        | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
581        | Node::TraitItem(hir::TraitItem {
582            kind: hir::TraitItemKind::Const(ty, Some(_)),
583            span,
584            ..
585        }) => (*span, ty.span),
586        Node::AnonConst(ct) => (ct.span, ct.span),
587        Node::ConstBlock(_) => {
588            let span = tcx.def_span(def);
589            (span, span)
590        }
591        _ => span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def),
592    };
593
594    // FIXME(#132279): We likely want to be able to use the hidden types of
595    // opaques used by this function here.
596    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
597    let mut builder =
598        Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);
599
600    let mut block = START_BLOCK;
601    block = builder.expr_into_dest(Place::return_place(), block, expr).into_block();
602
603    let source_info = builder.source_info(span);
604    builder.cfg.terminate(block, source_info, TerminatorKind::Return);
605
606    builder.build_drop_trees();
607
608    builder.finish()
609}
610
611/// Construct MIR for an item that has had errors in type checking.
612///
613/// This is required because we may still want to run MIR passes on an item
614/// with type errors, but normal MIR construction can't handle that in general.
615fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -> Body<'_> {
616    let span = tcx.def_span(def_id);
617    let hir_id = tcx.local_def_id_to_hir_id(def_id);
618
619    let (inputs, output, coroutine) = match tcx.def_kind(def_id) {
620        DefKind::Const
621        | DefKind::AssocConst
622        | DefKind::AnonConst
623        | DefKind::InlineConst
624        | DefKind::Static { .. } => (vec![], tcx.type_of(def_id).instantiate_identity(), None),
625        DefKind::Ctor(..) | DefKind::Fn | DefKind::AssocFn => {
626            let sig = tcx.liberate_late_bound_regions(
627                def_id.to_def_id(),
628                tcx.fn_sig(def_id).instantiate_identity(),
629            );
630            (sig.inputs().to_vec(), sig.output(), None)
631        }
632        DefKind::Closure => {
633            let closure_ty = tcx.type_of(def_id).instantiate_identity();
634            match closure_ty.kind() {
635                ty::Closure(_, args) => {
636                    let args = args.as_closure();
637                    let sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), args.sig());
638                    let self_ty = match args.kind() {
639                        ty::ClosureKind::Fn => {
640                            Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
641                        }
642                        ty::ClosureKind::FnMut => {
643                            Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
644                        }
645                        ty::ClosureKind::FnOnce => closure_ty,
646                    };
647                    (
648                        [self_ty].into_iter().chain(sig.inputs()[0].tuple_fields()).collect(),
649                        sig.output(),
650                        None,
651                    )
652                }
653                ty::Coroutine(_, args) => {
654                    let args = args.as_coroutine();
655                    let resume_ty = args.resume_ty();
656                    let yield_ty = args.yield_ty();
657                    let return_ty = args.return_ty();
658                    (
659                        vec![closure_ty, resume_ty],
660                        return_ty,
661                        Some(Box::new(CoroutineInfo::initial(
662                            tcx.coroutine_kind(def_id).unwrap(),
663                            yield_ty,
664                            resume_ty,
665                        ))),
666                    )
667                }
668                ty::CoroutineClosure(did, args) => {
669                    let args = args.as_coroutine_closure();
670                    let sig = tcx.liberate_late_bound_regions(
671                        def_id.to_def_id(),
672                        args.coroutine_closure_sig(),
673                    );
674                    let self_ty = match args.kind() {
675                        ty::ClosureKind::Fn => {
676                            Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
677                        }
678                        ty::ClosureKind::FnMut => {
679                            Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, closure_ty)
680                        }
681                        ty::ClosureKind::FnOnce => closure_ty,
682                    };
683                    (
684                        [self_ty].into_iter().chain(sig.tupled_inputs_ty.tuple_fields()).collect(),
685                        sig.to_coroutine(
686                            tcx,
687                            args.parent_args(),
688                            args.kind_ty(),
689                            tcx.coroutine_for_closure(*did),
690                            Ty::new_error(tcx, guar),
691                        ),
692                        None,
693                    )
694                }
695                ty::Error(_) => (vec![closure_ty, closure_ty], closure_ty, None),
696                kind => {
697                    span_bug!(
698                        span,
699                        "expected type of closure body to be a closure or coroutine, got {kind:?}"
700                    );
701                }
702            }
703        }
704        dk => span_bug!(span, "{:?} is not a body: {:?}", def_id, dk),
705    };
706
707    let source_info = SourceInfo { span, scope: OUTERMOST_SOURCE_SCOPE };
708    let local_decls = IndexVec::from_iter(
709        [output].iter().chain(&inputs).map(|ty| LocalDecl::with_source_info(*ty, source_info)),
710    );
711    let mut cfg = CFG { basic_blocks: IndexVec::new() };
712    let mut source_scopes = IndexVec::new();
713
714    cfg.start_new_block();
715    source_scopes.push(SourceScopeData {
716        span,
717        parent_scope: None,
718        inlined: None,
719        inlined_parent_scope: None,
720        local_data: ClearCrossCrate::Set(SourceScopeLocalData { lint_root: hir_id }),
721    });
722
723    cfg.terminate(START_BLOCK, source_info, TerminatorKind::Unreachable);
724
725    Body::new(
726        MirSource::item(def_id.to_def_id()),
727        cfg.basic_blocks,
728        source_scopes,
729        local_decls,
730        IndexVec::new(),
731        inputs.len(),
732        vec![],
733        span,
734        coroutine,
735        Some(guar),
736    )
737}
738
739impl<'a, 'tcx> Builder<'a, 'tcx> {
740    fn new(
741        thir: &'a Thir<'tcx>,
742        infcx: InferCtxt<'tcx>,
743        def: LocalDefId,
744        hir_id: HirId,
745        span: Span,
746        arg_count: usize,
747        return_ty: Ty<'tcx>,
748        return_span: Span,
749        coroutine: Option<Box<CoroutineInfo<'tcx>>>,
750    ) -> Builder<'a, 'tcx> {
751        let tcx = infcx.tcx;
752        let attrs = tcx.hir().attrs(hir_id);
753        // Some functions always have overflow checks enabled,
754        // however, they may not get codegen'd, depending on
755        // the settings for the crate they are codegened in.
756        let mut check_overflow = attr::contains_name(attrs, sym::rustc_inherit_overflow_checks);
757        // Respect -C overflow-checks.
758        check_overflow |= tcx.sess.overflow_checks();
759        // Constants always need overflow checks.
760        check_overflow |= matches!(
761            tcx.hir().body_owner_kind(def),
762            hir::BodyOwnerKind::Const { .. } | hir::BodyOwnerKind::Static(_)
763        );
764
765        let lint_level = LintLevel::Explicit(hir_id);
766        let param_env = tcx.param_env(def);
767        let mut builder = Builder {
768            thir,
769            tcx,
770            infcx,
771            region_scope_tree: tcx.region_scope_tree(def),
772            param_env,
773            def_id: def,
774            hir_id,
775            parent_module: tcx.parent_module(hir_id).to_def_id(),
776            check_overflow,
777            cfg: CFG { basic_blocks: IndexVec::new() },
778            fn_span: span,
779            arg_count,
780            coroutine,
781            scopes: scope::Scopes::new(),
782            block_context: BlockContext::new(),
783            source_scopes: IndexVec::new(),
784            source_scope: OUTERMOST_SOURCE_SCOPE,
785            guard_context: vec![],
786            fixed_temps: Default::default(),
787            fixed_temps_scope: None,
788            local_decls: IndexVec::from_elem_n(LocalDecl::new(return_ty, return_span), 1),
789            canonical_user_type_annotations: IndexVec::new(),
790            upvars: CaptureMap::new(),
791            var_indices: Default::default(),
792            unit_temp: None,
793            var_debug_info: vec![],
794            lint_level_roots_cache: GrowableBitSet::new_empty(),
795            coverage_info: coverageinfo::CoverageInfoBuilder::new_if_enabled(tcx, def),
796        };
797
798        assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
799        assert_eq!(builder.new_source_scope(span, lint_level), OUTERMOST_SOURCE_SCOPE);
800        builder.source_scopes[OUTERMOST_SOURCE_SCOPE].parent_scope = None;
801
802        builder
803    }
804
805    fn finish(self) -> Body<'tcx> {
806        let mut body = Body::new(
807            MirSource::item(self.def_id.to_def_id()),
808            self.cfg.basic_blocks,
809            self.source_scopes,
810            self.local_decls,
811            self.canonical_user_type_annotations,
812            self.arg_count,
813            self.var_debug_info,
814            self.fn_span,
815            self.coroutine,
816            None,
817        );
818        body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
819
820        for (index, block) in body.basic_blocks.iter().enumerate() {
821            if block.terminator.is_none() {
822                use rustc_middle::mir::pretty;
823                let options = pretty::PrettyPrintMirOptions::from_cli(self.tcx);
824                pretty::write_mir_fn(
825                    self.tcx,
826                    &body,
827                    &mut |_, _| Ok(()),
828                    &mut std::io::stdout(),
829                    options,
830                )
831                .unwrap();
832                span_bug!(self.fn_span, "no terminator on block {:?}", index);
833            }
834        }
835
836        body
837    }
838
839    fn insert_upvar_arg(&mut self) {
840        let Some(closure_arg) = self.local_decls.get(ty::CAPTURE_STRUCT_LOCAL) else { return };
841
842        let mut closure_ty = closure_arg.ty;
843        let mut closure_env_projs = vec![];
844        if let ty::Ref(_, ty, _) = closure_ty.kind() {
845            closure_env_projs.push(ProjectionElem::Deref);
846            closure_ty = *ty;
847        }
848
849        let upvar_args = match closure_ty.kind() {
850            ty::Closure(_, args) => ty::UpvarArgs::Closure(args),
851            ty::Coroutine(_, args) => ty::UpvarArgs::Coroutine(args),
852            ty::CoroutineClosure(_, args) => ty::UpvarArgs::CoroutineClosure(args),
853            _ => return,
854        };
855
856        // In analyze_closure() in upvar.rs we gathered a list of upvars used by an
857        // indexed closure and we stored in a map called closure_min_captures in TypeckResults
858        // with the closure's DefId. Here, we run through that vec of UpvarIds for
859        // the given closure and use the necessary information to create upvar
860        // debuginfo and to fill `self.upvars`.
861        let capture_tys = upvar_args.upvar_tys();
862
863        let tcx = self.tcx;
864        self.upvars = tcx
865            .closure_captures(self.def_id)
866            .iter()
867            .zip_eq(capture_tys)
868            .enumerate()
869            .map(|(i, (captured_place, ty))| {
870                let name = captured_place.to_symbol();
871
872                let capture = captured_place.info.capture_kind;
873                let var_id = match captured_place.place.base {
874                    HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
875                    _ => bug!("Expected an upvar"),
876                };
877
878                let mutability = captured_place.mutability;
879
880                let mut projs = closure_env_projs.clone();
881                projs.push(ProjectionElem::Field(FieldIdx::new(i), ty));
882                match capture {
883                    ty::UpvarCapture::ByValue => {}
884                    ty::UpvarCapture::ByRef(..) => {
885                        projs.push(ProjectionElem::Deref);
886                    }
887                };
888
889                let use_place = Place {
890                    local: ty::CAPTURE_STRUCT_LOCAL,
891                    projection: tcx.mk_place_elems(&projs),
892                };
893                self.var_debug_info.push(VarDebugInfo {
894                    name,
895                    source_info: SourceInfo::outermost(captured_place.var_ident.span),
896                    value: VarDebugInfoContents::Place(use_place),
897                    composite: None,
898                    argument_index: None,
899                });
900
901                let capture = Capture { captured_place, use_place, mutability };
902                (var_id, capture)
903            })
904            .collect();
905    }
906
907    fn args_and_body(
908        &mut self,
909        mut block: BasicBlock,
910        arguments: &IndexSlice<ParamId, Param<'tcx>>,
911        argument_scope: region::Scope,
912        expr_id: ExprId,
913    ) -> BlockAnd<()> {
914        let expr_span = self.thir[expr_id].span;
915        // Allocate locals for the function arguments
916        for (argument_index, param) in arguments.iter().enumerate() {
917            let source_info =
918                SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
919            let arg_local =
920                self.local_decls.push(LocalDecl::with_source_info(param.ty, source_info));
921
922            // If this is a simple binding pattern, give debuginfo a nice name.
923            if let Some(ref pat) = param.pat
924                && let Some(name) = pat.simple_ident()
925            {
926                self.var_debug_info.push(VarDebugInfo {
927                    name,
928                    source_info,
929                    value: VarDebugInfoContents::Place(arg_local.into()),
930                    composite: None,
931                    argument_index: Some(argument_index as u16 + 1),
932                });
933            }
934        }
935
936        self.insert_upvar_arg();
937
938        let mut scope = None;
939        // Bind the argument patterns
940        for (index, param) in arguments.iter().enumerate() {
941            // Function arguments always get the first Local indices after the return place
942            let local = Local::new(index + 1);
943            let place = Place::from(local);
944
945            // Make sure we drop (parts of) the argument even when not matched on.
946            self.schedule_drop(
947                param.pat.as_ref().map_or(expr_span, |pat| pat.span),
948                argument_scope,
949                local,
950                DropKind::Value,
951            );
952
953            let Some(ref pat) = param.pat else {
954                continue;
955            };
956            let original_source_scope = self.source_scope;
957            let span = pat.span;
958            if let Some(arg_hir_id) = param.hir_id {
959                self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span);
960            }
961            match pat.kind {
962                // Don't introduce extra copies for simple bindings
963                PatKind::Binding {
964                    var,
965                    mode: BindingMode(ByRef::No, mutability),
966                    subpattern: None,
967                    ..
968                } => {
969                    self.local_decls[local].mutability = mutability;
970                    self.local_decls[local].source_info.scope = self.source_scope;
971                    **self.local_decls[local].local_info.as_mut().assert_crate_local() =
972                        if let Some(kind) = param.self_kind {
973                            LocalInfo::User(BindingForm::ImplicitSelf(kind))
974                        } else {
975                            let binding_mode = BindingMode(ByRef::No, mutability);
976                            LocalInfo::User(BindingForm::Var(VarBindingForm {
977                                binding_mode,
978                                opt_ty_info: param.ty_span,
979                                opt_match_place: Some((None, span)),
980                                pat_span: span,
981                            }))
982                        };
983                    self.var_indices.insert(var, LocalsForNode::One(local));
984                }
985                _ => {
986                    scope = self.declare_bindings(
987                        scope,
988                        expr_span,
989                        &pat,
990                        None,
991                        Some((Some(&place), span)),
992                    );
993                    let place_builder = PlaceBuilder::from(local);
994                    block = self.place_into_pattern(block, pat, place_builder, false).into_block();
995                }
996            }
997            self.source_scope = original_source_scope;
998        }
999
1000        // Enter the argument pattern bindings source scope, if it exists.
1001        if let Some(source_scope) = scope {
1002            self.source_scope = source_scope;
1003        }
1004
1005        if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden) {
1006            let source_info = self.source_info(rustc_span::DUMMY_SP);
1007            self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
1008            self.cfg.start_new_block().unit()
1009        } else {
1010            // Ensure we don't silently codegen functions with fake bodies.
1011            match self.tcx.hir_node(self.hir_id) {
1012                hir::Node::Item(hir::Item {
1013                    kind: hir::ItemKind::Fn { has_body: false, .. },
1014                    ..
1015                }) => {
1016                    self.tcx.dcx().span_delayed_bug(
1017                        expr_span,
1018                        format!("fn item without body has reached MIR building: {:?}", self.def_id),
1019                    );
1020                }
1021                _ => {}
1022            }
1023            self.expr_into_dest(Place::return_place(), block, expr_id)
1024        }
1025    }
1026
1027    fn set_correct_source_scope_for_arg(
1028        &mut self,
1029        arg_hir_id: HirId,
1030        original_source_scope: SourceScope,
1031        pattern_span: Span,
1032    ) {
1033        let parent_id = self.source_scopes[original_source_scope]
1034            .local_data
1035            .as_ref()
1036            .assert_crate_local()
1037            .lint_root;
1038        self.maybe_new_source_scope(pattern_span, arg_hir_id, parent_id);
1039    }
1040
1041    fn get_unit_temp(&mut self) -> Place<'tcx> {
1042        match self.unit_temp {
1043            Some(tmp) => tmp,
1044            None => {
1045                let ty = self.tcx.types.unit;
1046                let fn_span = self.fn_span;
1047                let tmp = self.temp(ty, fn_span);
1048                self.unit_temp = Some(tmp);
1049                tmp
1050            }
1051        }
1052    }
1053}
1054
1055fn parse_float_into_constval<'tcx>(
1056    num: Symbol,
1057    float_ty: ty::FloatTy,
1058    neg: bool,
1059) -> Option<ConstValue<'tcx>> {
1060    parse_float_into_scalar(num, float_ty, neg).map(|s| ConstValue::Scalar(s.into()))
1061}
1062
1063pub(crate) fn parse_float_into_scalar(
1064    num: Symbol,
1065    float_ty: ty::FloatTy,
1066    neg: bool,
1067) -> Option<ScalarInt> {
1068    let num = num.as_str();
1069    match float_ty {
1070        // FIXME(f16_f128): When available, compare to the library parser as with `f32` and `f64`
1071        ty::FloatTy::F16 => {
1072            let mut f = num.parse::<Half>().ok()?;
1073            if neg {
1074                f = -f;
1075            }
1076            Some(ScalarInt::from(f))
1077        }
1078        ty::FloatTy::F32 => {
1079            let Ok(rust_f) = num.parse::<f32>() else { return None };
1080            let mut f = num
1081                .parse::<Single>()
1082                .unwrap_or_else(|e| panic!("apfloat::ieee::Single failed to parse `{num}`: {e:?}"));
1083
1084            assert!(
1085                u128::from(rust_f.to_bits()) == f.to_bits(),
1086                "apfloat::ieee::Single gave different result for `{}`: \
1087                 {}({:#x}) vs Rust's {}({:#x})",
1088                rust_f,
1089                f,
1090                f.to_bits(),
1091                Single::from_bits(rust_f.to_bits().into()),
1092                rust_f.to_bits()
1093            );
1094
1095            if neg {
1096                f = -f;
1097            }
1098
1099            Some(ScalarInt::from(f))
1100        }
1101        ty::FloatTy::F64 => {
1102            let Ok(rust_f) = num.parse::<f64>() else { return None };
1103            let mut f = num
1104                .parse::<Double>()
1105                .unwrap_or_else(|e| panic!("apfloat::ieee::Double failed to parse `{num}`: {e:?}"));
1106
1107            assert!(
1108                u128::from(rust_f.to_bits()) == f.to_bits(),
1109                "apfloat::ieee::Double gave different result for `{}`: \
1110                 {}({:#x}) vs Rust's {}({:#x})",
1111                rust_f,
1112                f,
1113                f.to_bits(),
1114                Double::from_bits(rust_f.to_bits().into()),
1115                rust_f.to_bits()
1116            );
1117
1118            if neg {
1119                f = -f;
1120            }
1121
1122            Some(ScalarInt::from(f))
1123        }
1124        // FIXME(f16_f128): When available, compare to the library parser as with `f32` and `f64`
1125        ty::FloatTy::F128 => {
1126            let mut f = num.parse::<Quad>().ok()?;
1127            if neg {
1128                f = -f;
1129            }
1130            Some(ScalarInt::from(f))
1131        }
1132    }
1133}
1134
1135///////////////////////////////////////////////////////////////////////////
1136// Builder methods are broken up into modules, depending on what kind
1137// of thing is being lowered. Note that they use the `unpack` macro
1138// above extensively.
1139
1140mod block;
1141mod cfg;
1142mod coverageinfo;
1143mod custom;
1144mod expr;
1145mod matches;
1146mod misc;
1147mod scope;
1148
1149pub(crate) use expr::category::Category as ExprCategory;