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//!
6//! ## The `let this = self;` idiom (LET_THIS_SELF)
7//!
8//! Throughout MIR building there are several places where a `Builder` method
9//! needs to borrow `self`, and then re-expose it to a closure as `|this|`.
10//!
11//! In complex builder methods, potentially with multiple levels of nesting, it
12//! would thus become necessary to mentally keep track of whether the builder
13//! is `self` (at the top level) or `this` (nested in a closure), or to replace
14//! one with the other when moving code in or out of a closure.
15//!
16//! (The borrow checker will prevent incorrect usage, but having to go back and
17//! satisfy the borrow checker still creates contributor friction.)
18//!
19//! To reduce that friction, some builder methods therefore start with
20//! `let this = self;` or similar, allowing subsequent code to uniformly refer
21//! to the builder as `this` (and never `self`), even when not nested.
22
23use itertools::Itertools;
24use rustc_abi::{ExternAbi, FieldIdx};
25use rustc_apfloat::Float;
26use rustc_apfloat::ieee::{Double, Half, Quad, Single};
27use rustc_ast::attr;
28use rustc_data_structures::fx::FxHashMap;
29use rustc_data_structures::sorted_map::SortedIndexMultiMap;
30use rustc_errors::ErrorGuaranteed;
31use rustc_hir::attrs::AttributeKind;
32use rustc_hir::def::DefKind;
33use rustc_hir::def_id::{DefId, LocalDefId};
34use rustc_hir::{self as hir, BindingMode, ByRef, HirId, ItemLocalId, Node, find_attr};
35use rustc_index::bit_set::GrowableBitSet;
36use rustc_index::{Idx, IndexSlice, IndexVec};
37use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
38use rustc_middle::hir::place::PlaceBase as HirPlaceBase;
39use rustc_middle::middle::region;
40use rustc_middle::mir::*;
41use rustc_middle::thir::{self, ExprId, LintLevel, LocalVarId, Param, ParamId, PatKind, Thir};
42use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt, TypeVisitableExt, TypingMode};
43use rustc_middle::{bug, span_bug};
44use rustc_session::lint;
45use rustc_span::{Span, Symbol, sym};
46
47use crate::builder::expr::as_place::PlaceBuilder;
48use crate::builder::scope::DropKind;
49use crate::errors;
50
51pub(crate) fn closure_saved_names_of_captured_variables<'tcx>(
52    tcx: TyCtxt<'tcx>,
53    def_id: LocalDefId,
54) -> IndexVec<FieldIdx, Symbol> {
55    tcx.closure_captures(def_id)
56        .iter()
57        .map(|captured_place| {
58            let name = captured_place.to_symbol();
59            match captured_place.info.capture_kind {
60                ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => name,
61                ty::UpvarCapture::ByRef(..) => Symbol::intern(&format!("_ref__{name}")),
62            }
63        })
64        .collect()
65}
66
67/// Create the MIR for a given `DefId`, including unreachable code. Do not call
68/// this directly; instead use the cached version via `mir_built`.
69pub fn build_mir<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> Body<'tcx> {
70    tcx.ensure_done().thir_abstract_const(def);
71    if let Err(e) = tcx.ensure_ok().check_match(def) {
72        return construct_error(tcx, def, e);
73    }
74
75    if let Err(err) = tcx.ensure_ok().check_tail_calls(def) {
76        return construct_error(tcx, def, err);
77    }
78
79    let body = match tcx.thir_body(def) {
80        Err(error_reported) => construct_error(tcx, def, error_reported),
81        Ok((thir, expr)) => {
82            let build_mir = |thir: &Thir<'tcx>| match thir.body_type {
83                thir::BodyTy::Fn(fn_sig) => construct_fn(tcx, def, thir, expr, fn_sig),
84                thir::BodyTy::Const(ty) | thir::BodyTy::GlobalAsm(ty) => {
85                    construct_const(tcx, def, thir, expr, ty)
86                }
87            };
88
89            // Don't steal here, instead steal in unsafeck. This is so that
90            // pattern inline constants can be evaluated as part of building the
91            // THIR of the parent function without a cycle.
92            build_mir(&thir.borrow())
93        }
94    };
95
96    // The borrow checker will replace all the regions here with its own
97    // inference variables. There's no point having non-erased regions here.
98    // The exception is `body.user_type_annotations`, which is used unmodified
99    // by borrow checking.
100    debug_assert!(
101        !(body.local_decls.has_free_regions()
102            || body.basic_blocks.has_free_regions()
103            || body.var_debug_info.has_free_regions()
104            || body.yield_ty().has_free_regions()),
105        "Unexpected free regions in MIR: {body:?}",
106    );
107
108    body
109}
110
111///////////////////////////////////////////////////////////////////////////
112// BuildMir -- walks a crate, looking for fn items and methods to build MIR from
113
114#[derive(Debug, PartialEq, Eq)]
115enum BlockFrame {
116    /// Evaluation is currently within a statement.
117    ///
118    /// Examples include:
119    /// 1. `EXPR;`
120    /// 2. `let _ = EXPR;`
121    /// 3. `let x = EXPR;`
122    Statement {
123        /// If true, then statement discards result from evaluating
124        /// the expression (such as examples 1 and 2 above).
125        ignores_expr_result: bool,
126    },
127
128    /// Evaluation is currently within the tail expression of a block.
129    ///
130    /// Example: `{ STMT_1; STMT_2; EXPR }`
131    TailExpr { info: BlockTailInfo },
132
133    /// Generic mark meaning that the block occurred as a subexpression
134    /// where the result might be used.
135    ///
136    /// Examples: `foo(EXPR)`, `match EXPR { ... }`
137    SubExpr,
138}
139
140impl BlockFrame {
141    fn is_tail_expr(&self) -> bool {
142        match *self {
143            BlockFrame::TailExpr { .. } => true,
144
145            BlockFrame::Statement { .. } | BlockFrame::SubExpr => false,
146        }
147    }
148    fn is_statement(&self) -> bool {
149        match *self {
150            BlockFrame::Statement { .. } => true,
151
152            BlockFrame::TailExpr { .. } | BlockFrame::SubExpr => false,
153        }
154    }
155}
156
157#[derive(Debug)]
158struct BlockContext(Vec<BlockFrame>);
159
160struct Builder<'a, 'tcx> {
161    tcx: TyCtxt<'tcx>,
162    // FIXME(@lcnr): Why does this use an `infcx`, there should be
163    // no shared type inference going on here. I feel like it would
164    // clearer to manually construct one where necessary or to provide
165    // a nice API for non-type inference trait system checks.
166    infcx: InferCtxt<'tcx>,
167    region_scope_tree: &'tcx region::ScopeTree,
168    param_env: ty::ParamEnv<'tcx>,
169
170    thir: &'a Thir<'tcx>,
171    cfg: CFG<'tcx>,
172
173    def_id: LocalDefId,
174    hir_id: HirId,
175    parent_module: DefId,
176    check_overflow: bool,
177    fn_span: Span,
178    arg_count: usize,
179    coroutine: Option<Box<CoroutineInfo<'tcx>>>,
180
181    /// The current set of scopes, updated as we traverse;
182    /// see the `scope` module for more details.
183    scopes: scope::Scopes<'tcx>,
184
185    /// The block-context: each time we build the code within an thir::Block,
186    /// we push a frame here tracking whether we are building a statement or
187    /// if we are pushing the tail expression of the block. This is used to
188    /// embed information in generated temps about whether they were created
189    /// for a block tail expression or not.
190    ///
191    /// It would be great if we could fold this into `self.scopes`
192    /// somehow, but right now I think that is very tightly tied to
193    /// the code generation in ways that we cannot (or should not)
194    /// start just throwing new entries onto that vector in order to
195    /// distinguish the context of EXPR1 from the context of EXPR2 in
196    /// `{ STMTS; EXPR1 } + EXPR2`.
197    block_context: BlockContext,
198
199    /// The vector of all scopes that we have created thus far;
200    /// we track this for debuginfo later.
201    source_scopes: IndexVec<SourceScope, SourceScopeData<'tcx>>,
202    source_scope: SourceScope,
203
204    /// The guard-context: each time we build the guard expression for
205    /// a match arm, we push onto this stack, and then pop when we
206    /// finish building it.
207    guard_context: Vec<GuardFrame>,
208
209    /// Temporaries with fixed indexes. Used so that if-let guards on arms
210    /// with an or-pattern are only created once.
211    fixed_temps: FxHashMap<ExprId, Local>,
212    /// Scope of temporaries that should be deduplicated using [Self::fixed_temps].
213    fixed_temps_scope: Option<region::Scope>,
214
215    /// Maps `HirId`s of variable bindings to the `Local`s created for them.
216    /// (A match binding can have two locals; the 2nd is for the arm's guard.)
217    var_indices: FxHashMap<LocalVarId, LocalsForNode>,
218    local_decls: IndexVec<Local, LocalDecl<'tcx>>,
219    canonical_user_type_annotations: ty::CanonicalUserTypeAnnotations<'tcx>,
220    upvars: CaptureMap<'tcx>,
221    unit_temp: Option<Place<'tcx>>,
222
223    var_debug_info: Vec<VarDebugInfo<'tcx>>,
224
225    // A cache for `maybe_lint_level_roots_bounded`. That function is called
226    // repeatedly, and each time it effectively traces a path through a tree
227    // structure from a node towards the root, doing an attribute check on each
228    // node along the way. This cache records which nodes trace all the way to
229    // the root (most of them do) and saves us from retracing many sub-paths
230    // many times, and rechecking many nodes.
231    lint_level_roots_cache: GrowableBitSet<hir::ItemLocalId>,
232
233    /// Collects additional coverage information during MIR building.
234    /// Only present if coverage is enabled and this function is eligible.
235    coverage_info: Option<coverageinfo::CoverageInfoBuilder>,
236}
237
238type CaptureMap<'tcx> = SortedIndexMultiMap<usize, ItemLocalId, Capture<'tcx>>;
239
240#[derive(Debug)]
241struct Capture<'tcx> {
242    captured_place: &'tcx ty::CapturedPlace<'tcx>,
243    use_place: Place<'tcx>,
244    mutability: Mutability,
245}
246
247impl<'a, 'tcx> Builder<'a, 'tcx> {
248    fn typing_env(&self) -> ty::TypingEnv<'tcx> {
249        self.infcx.typing_env(self.param_env)
250    }
251
252    fn is_bound_var_in_guard(&self, id: LocalVarId) -> bool {
253        self.guard_context.iter().any(|frame| frame.locals.iter().any(|local| local.id == id))
254    }
255
256    fn var_local_id(&self, id: LocalVarId, for_guard: ForGuard) -> Local {
257        self.var_indices[&id].local_id(for_guard)
258    }
259}
260
261impl BlockContext {
262    fn new() -> Self {
263        BlockContext(vec![])
264    }
265    fn push(&mut self, bf: BlockFrame) {
266        self.0.push(bf);
267    }
268    fn pop(&mut self) -> Option<BlockFrame> {
269        self.0.pop()
270    }
271
272    /// Traverses the frames on the `BlockContext`, searching for either
273    /// the first block-tail expression frame with no intervening
274    /// statement frame.
275    ///
276    /// Notably, this skips over `SubExpr` frames; this method is
277    /// meant to be used in the context of understanding the
278    /// relationship of a temp (created within some complicated
279    /// expression) with its containing expression, and whether the
280    /// value of that *containing expression* (not the temp!) is
281    /// ignored.
282    fn currently_in_block_tail(&self) -> Option<BlockTailInfo> {
283        for bf in self.0.iter().rev() {
284            match bf {
285                BlockFrame::SubExpr => continue,
286                BlockFrame::Statement { .. } => break,
287                &BlockFrame::TailExpr { info } => return Some(info),
288            }
289        }
290
291        None
292    }
293
294    /// Looks at the topmost frame on the BlockContext and reports
295    /// whether its one that would discard a block tail result.
296    ///
297    /// Unlike `currently_within_ignored_tail_expression`, this does
298    /// *not* skip over `SubExpr` frames: here, we want to know
299    /// whether the block result itself is discarded.
300    fn currently_ignores_tail_results(&self) -> bool {
301        match self.0.last() {
302            // no context: conservatively assume result is read
303            None => false,
304
305            // sub-expression: block result feeds into some computation
306            Some(BlockFrame::SubExpr) => false,
307
308            // otherwise: use accumulated is_ignored state.
309            Some(
310                BlockFrame::TailExpr { info: BlockTailInfo { tail_result_is_ignored: ign, .. } }
311                | BlockFrame::Statement { ignores_expr_result: ign },
312            ) => *ign,
313        }
314    }
315}
316
317#[derive(Debug)]
318enum LocalsForNode {
319    /// In the usual case, a `HirId` for an identifier maps to at most
320    /// one `Local` declaration.
321    One(Local),
322
323    /// The exceptional case is identifiers in a match arm's pattern
324    /// that are referenced in a guard of that match arm. For these,
325    /// we have `2` Locals.
326    ///
327    /// * `for_arm_body` is the Local used in the arm body (which is
328    ///   just like the `One` case above),
329    ///
330    /// * `ref_for_guard` is the Local used in the arm's guard (which
331    ///   is a reference to a temp that is an alias of
332    ///   `for_arm_body`).
333    ForGuard { ref_for_guard: Local, for_arm_body: Local },
334}
335
336#[derive(Debug)]
337struct GuardFrameLocal {
338    id: LocalVarId,
339}
340
341impl GuardFrameLocal {
342    fn new(id: LocalVarId) -> Self {
343        GuardFrameLocal { id }
344    }
345}
346
347#[derive(Debug)]
348struct GuardFrame {
349    /// These are the id's of names that are bound by patterns of the
350    /// arm of *this* guard.
351    ///
352    /// (Frames higher up the stack will have the id's bound in arms
353    /// further out, such as in a case like:
354    ///
355    /// match E1 {
356    ///      P1(id1) if (... (match E2 { P2(id2) if ... => B2 })) => B1,
357    /// }
358    ///
359    /// here, when building for FIXME.
360    locals: Vec<GuardFrameLocal>,
361}
362
363/// `ForGuard` indicates whether we are talking about:
364///   1. The variable for use outside of guard expressions, or
365///   2. The temp that holds reference to (1.), which is actually what the
366///      guard expressions see.
367#[derive(Copy, Clone, Debug, PartialEq, Eq)]
368enum ForGuard {
369    RefWithinGuard,
370    OutsideGuard,
371}
372
373impl LocalsForNode {
374    fn local_id(&self, for_guard: ForGuard) -> Local {
375        match (self, for_guard) {
376            (&LocalsForNode::One(local_id), ForGuard::OutsideGuard)
377            | (
378                &LocalsForNode::ForGuard { ref_for_guard: local_id, .. },
379                ForGuard::RefWithinGuard,
380            )
381            | (&LocalsForNode::ForGuard { for_arm_body: local_id, .. }, ForGuard::OutsideGuard) => {
382                local_id
383            }
384
385            (&LocalsForNode::One(_), ForGuard::RefWithinGuard) => {
386                bug!("anything with one local should never be within a guard.")
387            }
388        }
389    }
390}
391
392struct CFG<'tcx> {
393    basic_blocks: IndexVec<BasicBlock, BasicBlockData<'tcx>>,
394}
395
396rustc_index::newtype_index! {
397    struct ScopeId {}
398}
399
400#[derive(Debug)]
401enum NeedsTemporary {
402    /// Use this variant when whatever you are converting with `as_operand`
403    /// is the last thing you are converting. This means that if we introduced
404    /// an intermediate temporary, we'd only read it immediately after, so we can
405    /// also avoid it.
406    No,
407    /// For all cases where you aren't sure or that are too expensive to compute
408    /// for now. It is always safe to fall back to this.
409    Maybe,
410}
411
412/// The `BlockAnd` "monad" packages up the new basic block along with a
413/// produced value (sometimes just unit, of course). The `unpack!`
414/// macro (and methods below) makes working with `BlockAnd` much more
415/// convenient.
416#[must_use = "if you don't use one of these results, you're leaving a dangling edge"]
417struct BlockAnd<T>(BasicBlock, T);
418
419impl BlockAnd<()> {
420    /// Unpacks `BlockAnd<()>` into a [`BasicBlock`].
421    #[must_use]
422    fn into_block(self) -> BasicBlock {
423        let Self(block, ()) = self;
424        block
425    }
426}
427
428trait BlockAndExtension {
429    fn and<T>(self, v: T) -> BlockAnd<T>;
430    fn unit(self) -> BlockAnd<()>;
431}
432
433impl BlockAndExtension for BasicBlock {
434    fn and<T>(self, v: T) -> BlockAnd<T> {
435        BlockAnd(self, v)
436    }
437
438    fn unit(self) -> BlockAnd<()> {
439        BlockAnd(self, ())
440    }
441}
442
443/// Update a block pointer and return the value.
444/// Use it like `let x = unpack!(block = self.foo(block, foo))`.
445macro_rules! unpack {
446    ($x:ident = $c:expr) => {{
447        let BlockAnd(b, v) = $c;
448        $x = b;
449        v
450    }};
451}
452
453/// The main entry point for building MIR for a function.
454fn construct_fn<'tcx>(
455    tcx: TyCtxt<'tcx>,
456    fn_def: LocalDefId,
457    thir: &Thir<'tcx>,
458    expr: ExprId,
459    fn_sig: ty::FnSig<'tcx>,
460) -> Body<'tcx> {
461    let span = tcx.def_span(fn_def);
462    let fn_id = tcx.local_def_id_to_hir_id(fn_def);
463
464    // Figure out what primary body this item has.
465    let body = tcx.hir_body_owned_by(fn_def);
466    let span_with_body = tcx.hir_span_with_body(fn_id);
467    let return_ty_span = tcx
468        .hir_fn_decl_by_hir_id(fn_id)
469        .unwrap_or_else(|| span_bug!(span, "can't build MIR for {:?}", fn_def))
470        .output
471        .span();
472
473    let mut abi = fn_sig.abi;
474    if let DefKind::Closure = tcx.def_kind(fn_def) {
475        // HACK(eddyb) Avoid having RustCall on closures,
476        // as it adds unnecessary (and wrong) auto-tupling.
477        abi = ExternAbi::Rust;
478    }
479
480    let arguments = &thir.params;
481
482    let return_ty = fn_sig.output();
483    let coroutine = match tcx.type_of(fn_def).instantiate_identity().kind() {
484        ty::Coroutine(_, args) => Some(Box::new(CoroutineInfo::initial(
485            tcx.coroutine_kind(fn_def).unwrap(),
486            args.as_coroutine().yield_ty(),
487            args.as_coroutine().resume_ty(),
488        ))),
489        ty::Closure(..) | ty::CoroutineClosure(..) | ty::FnDef(..) => None,
490        ty => span_bug!(span_with_body, "unexpected type of body: {ty:?}"),
491    };
492
493    if let Some((dialect, phase)) = find_attr!(tcx.hir_attrs(fn_id), AttributeKind::CustomMir(dialect, phase, _) => (dialect, phase))
494    {
495        return custom::build_custom_mir(
496            tcx,
497            fn_def.to_def_id(),
498            fn_id,
499            thir,
500            expr,
501            arguments,
502            return_ty,
503            return_ty_span,
504            span_with_body,
505            dialect.as_ref().map(|(d, _)| *d),
506            phase.as_ref().map(|(p, _)| *p),
507        );
508    }
509
510    // FIXME(#132279): This should be able to reveal opaque
511    // types defined during HIR typeck.
512    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
513    let mut builder = Builder::new(
514        thir,
515        infcx,
516        fn_def,
517        fn_id,
518        span_with_body,
519        arguments.len(),
520        return_ty,
521        return_ty_span,
522        coroutine,
523    );
524
525    let call_site_scope =
526        region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::CallSite };
527    let arg_scope =
528        region::Scope { local_id: body.id().hir_id.local_id, data: region::ScopeData::Arguments };
529    let source_info = builder.source_info(span);
530    let call_site_s = (call_site_scope, source_info);
531    let _: BlockAnd<()> = builder.in_scope(call_site_s, LintLevel::Inherited, |builder| {
532        let arg_scope_s = (arg_scope, source_info);
533        // Attribute epilogue to function's closing brace
534        let fn_end = span_with_body.shrink_to_hi();
535        let return_block = builder
536            .in_breakable_scope(None, Place::return_place(), fn_end, |builder| {
537                Some(builder.in_scope(arg_scope_s, LintLevel::Inherited, |builder| {
538                    builder.args_and_body(START_BLOCK, arguments, arg_scope, expr)
539                }))
540            })
541            .into_block();
542        let source_info = builder.source_info(fn_end);
543        builder.cfg.terminate(return_block, source_info, TerminatorKind::Return);
544        builder.build_drop_trees();
545        return_block.unit()
546    });
547
548    builder.lint_and_remove_uninhabited();
549    let mut body = builder.finish();
550
551    body.spread_arg = if abi == ExternAbi::RustCall {
552        // RustCall pseudo-ABI untuples the last argument.
553        Some(Local::new(arguments.len()))
554    } else {
555        None
556    };
557
558    body
559}
560
561fn construct_const<'a, 'tcx>(
562    tcx: TyCtxt<'tcx>,
563    def: LocalDefId,
564    thir: &'a Thir<'tcx>,
565    expr: ExprId,
566    const_ty: Ty<'tcx>,
567) -> Body<'tcx> {
568    let hir_id = tcx.local_def_id_to_hir_id(def);
569
570    // Figure out what primary body this item has.
571    let (span, const_ty_span) = match tcx.hir_node(hir_id) {
572        Node::Item(hir::Item {
573            kind: hir::ItemKind::Static(_, _, ty, _) | hir::ItemKind::Const(_, _, ty, _),
574            span,
575            ..
576        })
577        | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(ty, _), span, .. })
578        | Node::TraitItem(hir::TraitItem {
579            kind: hir::TraitItemKind::Const(ty, Some(_)),
580            span,
581            ..
582        }) => (*span, ty.span),
583        Node::AnonConst(ct) => (ct.span, ct.span),
584        Node::ConstBlock(_) => {
585            let span = tcx.def_span(def);
586            (span, span)
587        }
588        Node::Item(hir::Item { kind: hir::ItemKind::GlobalAsm { .. }, span, .. }) => (*span, *span),
589        _ => span_bug!(tcx.def_span(def), "can't build MIR for {:?}", def),
590    };
591
592    // FIXME(#132279): We likely want to be able to use the hidden types of
593    // opaques used by this function here.
594    let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
595    let mut builder =
596        Builder::new(thir, infcx, def, hir_id, span, 0, const_ty, const_ty_span, None);
597
598    let mut block = START_BLOCK;
599    block = builder.expr_into_dest(Place::return_place(), block, expr).into_block();
600
601    let source_info = builder.source_info(span);
602    builder.cfg.terminate(block, source_info, TerminatorKind::Return);
603
604    builder.build_drop_trees();
605
606    builder.lint_and_remove_uninhabited();
607    builder.finish()
608}
609
610/// Construct MIR for an item that has had errors in type checking.
611///
612/// This is required because we may still want to run MIR passes on an item
613/// with type errors, but normal MIR construction can't handle that in general.
614fn construct_error(tcx: TyCtxt<'_>, def_id: LocalDefId, guar: ErrorGuaranteed) -> Body<'_> {
615    let span = tcx.def_span(def_id);
616    let hir_id = tcx.local_def_id_to_hir_id(def_id);
617
618    let (inputs, output, coroutine) = match tcx.def_kind(def_id) {
619        DefKind::Const
620        | DefKind::AssocConst
621        | DefKind::AnonConst
622        | DefKind::InlineConst
623        | DefKind::Static { .. }
624        | DefKind::GlobalAsm => (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    #[allow(dead_code)]
806    fn dump_for_debugging(&self) {
807        let mut body = Body::new(
808            MirSource::item(self.def_id.to_def_id()),
809            self.cfg.basic_blocks.clone(),
810            self.source_scopes.clone(),
811            self.local_decls.clone(),
812            self.canonical_user_type_annotations.clone(),
813            self.arg_count.clone(),
814            self.var_debug_info.clone(),
815            self.fn_span.clone(),
816            self.coroutine.clone(),
817            None,
818        );
819        body.coverage_info_hi = self.coverage_info.as_ref().map(|b| b.as_done());
820
821        let writer = pretty::MirWriter::new(self.tcx);
822        writer.write_mir_fn(&body, &mut std::io::stdout()).unwrap();
823    }
824
825    fn lint_and_remove_uninhabited(&mut self) {
826        let mut lints = vec![];
827
828        for bbdata in self.cfg.basic_blocks.iter_mut() {
829            let term = bbdata.terminator_mut();
830            let TerminatorKind::Call { ref mut target, destination, .. } = term.kind else {
831                continue;
832            };
833            let Some(target_bb) = *target else { continue };
834
835            let ty = destination.ty(&self.local_decls, self.tcx).ty;
836            let ty_is_inhabited = ty.is_inhabited_from(
837                self.tcx,
838                self.parent_module,
839                self.infcx.typing_env(self.param_env),
840            );
841
842            if !ty_is_inhabited {
843                // Unreachable code warnings are already emitted during type checking.
844                // However, during type checking, full type information is being
845                // calculated but not yet available, so the check for diverging
846                // expressions due to uninhabited result types is pretty crude and
847                // only checks whether ty.is_never(). Here, we have full type
848                // information available and can issue warnings for less obviously
849                // uninhabited types (e.g. empty enums). The check above is used so
850                // that we do not emit the same warning twice if the uninhabited type
851                // is indeed `!`.
852                if !ty.is_never() {
853                    lints.push((target_bb, ty, term.source_info.span));
854                }
855
856                // The presence or absence of a return edge affects control-flow sensitive
857                // MIR checks and ultimately whether code is accepted or not. We can only
858                // omit the return edge if a return type is visibly uninhabited to a module
859                // that makes the call.
860                *target = None;
861            }
862        }
863
864        /// Starting at a target unreachable block, find some user code to lint as unreachable
865        fn find_unreachable_code_from(
866            bb: BasicBlock,
867            bbs: &IndexVec<BasicBlock, BasicBlockData<'_>>,
868        ) -> Option<(SourceInfo, &'static str)> {
869            let bb = &bbs[bb];
870            for stmt in &bb.statements {
871                match &stmt.kind {
872                    // Ignore the implicit `()` return place assignment for unit functions/blocks
873                    StatementKind::Assign(box (_, Rvalue::Use(Operand::Constant(const_))))
874                        if const_.ty().is_unit() =>
875                    {
876                        continue;
877                    }
878                    StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {
879                        continue;
880                    }
881                    StatementKind::FakeRead(..) => return Some((stmt.source_info, "definition")),
882                    _ => return Some((stmt.source_info, "expression")),
883                }
884            }
885
886            let term = bb.terminator();
887            match term.kind {
888                // No user code in this bb, and our goto target may be reachable via other paths
889                TerminatorKind::Goto { .. } | TerminatorKind::Return => None,
890                _ => Some((term.source_info, "expression")),
891            }
892        }
893
894        for (target_bb, orig_ty, orig_span) in lints {
895            if orig_span.in_external_macro(self.tcx.sess.source_map()) {
896                continue;
897            }
898
899            let Some((target_loc, descr)) =
900                find_unreachable_code_from(target_bb, &self.cfg.basic_blocks)
901            else {
902                continue;
903            };
904            let lint_root = self.source_scopes[target_loc.scope]
905                .local_data
906                .as_ref()
907                .unwrap_crate_local()
908                .lint_root;
909            self.tcx.emit_node_span_lint(
910                lint::builtin::UNREACHABLE_CODE,
911                lint_root,
912                target_loc.span,
913                errors::UnreachableDueToUninhabited {
914                    expr: target_loc.span,
915                    orig: orig_span,
916                    descr,
917                    ty: orig_ty,
918                },
919            );
920        }
921    }
922
923    fn finish(self) -> Body<'tcx> {
924        let mut body = Body::new(
925            MirSource::item(self.def_id.to_def_id()),
926            self.cfg.basic_blocks,
927            self.source_scopes,
928            self.local_decls,
929            self.canonical_user_type_annotations,
930            self.arg_count,
931            self.var_debug_info,
932            self.fn_span,
933            self.coroutine,
934            None,
935        );
936        body.coverage_info_hi = self.coverage_info.map(|b| b.into_done());
937
938        let writer = pretty::MirWriter::new(self.tcx);
939        for (index, block) in body.basic_blocks.iter().enumerate() {
940            if block.terminator.is_none() {
941                writer.write_mir_fn(&body, &mut std::io::stdout()).unwrap();
942                span_bug!(self.fn_span, "no terminator on block {:?}", index);
943            }
944        }
945
946        body
947    }
948
949    fn insert_upvar_arg(&mut self) {
950        let Some(closure_arg) = self.local_decls.get(ty::CAPTURE_STRUCT_LOCAL) else { return };
951
952        let mut closure_ty = closure_arg.ty;
953        let mut closure_env_projs = vec![];
954        if let ty::Ref(_, ty, _) = closure_ty.kind() {
955            closure_env_projs.push(ProjectionElem::Deref);
956            closure_ty = *ty;
957        }
958
959        let upvar_args = match closure_ty.kind() {
960            ty::Closure(_, args) => ty::UpvarArgs::Closure(args),
961            ty::Coroutine(_, args) => ty::UpvarArgs::Coroutine(args),
962            ty::CoroutineClosure(_, args) => ty::UpvarArgs::CoroutineClosure(args),
963            _ => return,
964        };
965
966        // In analyze_closure() in upvar.rs we gathered a list of upvars used by an
967        // indexed closure and we stored in a map called closure_min_captures in TypeckResults
968        // with the closure's DefId. Here, we run through that vec of UpvarIds for
969        // the given closure and use the necessary information to create upvar
970        // debuginfo and to fill `self.upvars`.
971        let capture_tys = upvar_args.upvar_tys();
972
973        let tcx = self.tcx;
974        let mut upvar_owner = None;
975        self.upvars = tcx
976            .closure_captures(self.def_id)
977            .iter()
978            .zip_eq(capture_tys)
979            .enumerate()
980            .map(|(i, (captured_place, ty))| {
981                let name = captured_place.to_symbol();
982
983                let capture = captured_place.info.capture_kind;
984                let var_id = match captured_place.place.base {
985                    HirPlaceBase::Upvar(upvar_id) => upvar_id.var_path.hir_id,
986                    _ => bug!("Expected an upvar"),
987                };
988                let upvar_base = upvar_owner.get_or_insert(var_id.owner);
989                assert_eq!(*upvar_base, var_id.owner);
990                let var_id = var_id.local_id;
991
992                let mutability = captured_place.mutability;
993
994                let mut projs = closure_env_projs.clone();
995                projs.push(ProjectionElem::Field(FieldIdx::new(i), ty));
996                match capture {
997                    ty::UpvarCapture::ByValue | ty::UpvarCapture::ByUse => {}
998                    ty::UpvarCapture::ByRef(..) => {
999                        projs.push(ProjectionElem::Deref);
1000                    }
1001                };
1002
1003                let use_place = Place {
1004                    local: ty::CAPTURE_STRUCT_LOCAL,
1005                    projection: tcx.mk_place_elems(&projs),
1006                };
1007                self.var_debug_info.push(VarDebugInfo {
1008                    name,
1009                    source_info: SourceInfo::outermost(captured_place.var_ident.span),
1010                    value: VarDebugInfoContents::Place(use_place),
1011                    composite: None,
1012                    argument_index: None,
1013                });
1014
1015                let capture = Capture { captured_place, use_place, mutability };
1016                (var_id, capture)
1017            })
1018            .collect();
1019    }
1020
1021    fn args_and_body(
1022        &mut self,
1023        mut block: BasicBlock,
1024        arguments: &IndexSlice<ParamId, Param<'tcx>>,
1025        argument_scope: region::Scope,
1026        expr_id: ExprId,
1027    ) -> BlockAnd<()> {
1028        let expr_span = self.thir[expr_id].span;
1029        // Allocate locals for the function arguments
1030        for (argument_index, param) in arguments.iter().enumerate() {
1031            let source_info =
1032                SourceInfo::outermost(param.pat.as_ref().map_or(self.fn_span, |pat| pat.span));
1033            let arg_local =
1034                self.local_decls.push(LocalDecl::with_source_info(param.ty, source_info));
1035
1036            // If this is a simple binding pattern, give debuginfo a nice name.
1037            if let Some(ref pat) = param.pat
1038                && let Some(name) = pat.simple_ident()
1039            {
1040                self.var_debug_info.push(VarDebugInfo {
1041                    name,
1042                    source_info,
1043                    value: VarDebugInfoContents::Place(arg_local.into()),
1044                    composite: None,
1045                    argument_index: Some(argument_index as u16 + 1),
1046                });
1047            }
1048        }
1049
1050        self.insert_upvar_arg();
1051
1052        let mut scope = None;
1053        // Bind the argument patterns
1054        for (index, param) in arguments.iter().enumerate() {
1055            // Function arguments always get the first Local indices after the return place
1056            let local = Local::new(index + 1);
1057            let place = Place::from(local);
1058
1059            // Make sure we drop (parts of) the argument even when not matched on.
1060            self.schedule_drop(
1061                param.pat.as_ref().map_or(expr_span, |pat| pat.span),
1062                argument_scope,
1063                local,
1064                DropKind::Value,
1065            );
1066
1067            let Some(ref pat) = param.pat else {
1068                continue;
1069            };
1070            let original_source_scope = self.source_scope;
1071            let span = pat.span;
1072            if let Some(arg_hir_id) = param.hir_id {
1073                self.set_correct_source_scope_for_arg(arg_hir_id, original_source_scope, span);
1074            }
1075            match pat.kind {
1076                // Don't introduce extra copies for simple bindings
1077                PatKind::Binding {
1078                    var,
1079                    mode: BindingMode(ByRef::No, mutability),
1080                    subpattern: None,
1081                    ..
1082                } => {
1083                    self.local_decls[local].mutability = mutability;
1084                    self.local_decls[local].source_info.scope = self.source_scope;
1085                    **self.local_decls[local].local_info.as_mut().unwrap_crate_local() =
1086                        if let Some(kind) = param.self_kind {
1087                            LocalInfo::User(BindingForm::ImplicitSelf(kind))
1088                        } else {
1089                            let binding_mode = BindingMode(ByRef::No, mutability);
1090                            LocalInfo::User(BindingForm::Var(VarBindingForm {
1091                                binding_mode,
1092                                opt_ty_info: param.ty_span,
1093                                opt_match_place: Some((None, span)),
1094                                pat_span: span,
1095                                introductions: vec![VarBindingIntroduction {
1096                                    span,
1097                                    is_shorthand: false,
1098                                }],
1099                            }))
1100                        };
1101                    self.var_indices.insert(var, LocalsForNode::One(local));
1102                }
1103                _ => {
1104                    scope = self.declare_bindings(
1105                        scope,
1106                        expr_span,
1107                        &pat,
1108                        None,
1109                        Some((Some(&place), span)),
1110                    );
1111                    let place_builder = PlaceBuilder::from(local);
1112                    block = self.place_into_pattern(block, pat, place_builder, false).into_block();
1113                }
1114            }
1115            self.source_scope = original_source_scope;
1116        }
1117
1118        // Enter the argument pattern bindings source scope, if it exists.
1119        if let Some(source_scope) = scope {
1120            self.source_scope = source_scope;
1121        }
1122
1123        if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden)
1124            || self.tcx.is_sdylib_interface_build()
1125        {
1126            let source_info = self.source_info(rustc_span::DUMMY_SP);
1127            self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
1128            self.cfg.start_new_block().unit()
1129        } else {
1130            // Ensure we don't silently codegen functions with fake bodies.
1131            match self.tcx.hir_node(self.hir_id) {
1132                hir::Node::Item(hir::Item {
1133                    kind: hir::ItemKind::Fn { has_body: false, .. },
1134                    ..
1135                }) => {
1136                    self.tcx.dcx().span_delayed_bug(
1137                        expr_span,
1138                        format!("fn item without body has reached MIR building: {:?}", self.def_id),
1139                    );
1140                }
1141                _ => {}
1142            }
1143            self.expr_into_dest(Place::return_place(), block, expr_id)
1144        }
1145    }
1146
1147    fn set_correct_source_scope_for_arg(
1148        &mut self,
1149        arg_hir_id: HirId,
1150        original_source_scope: SourceScope,
1151        pattern_span: Span,
1152    ) {
1153        let parent_id = self.source_scopes[original_source_scope]
1154            .local_data
1155            .as_ref()
1156            .unwrap_crate_local()
1157            .lint_root;
1158        self.maybe_new_source_scope(pattern_span, arg_hir_id, parent_id);
1159    }
1160
1161    fn get_unit_temp(&mut self) -> Place<'tcx> {
1162        match self.unit_temp {
1163            Some(tmp) => tmp,
1164            None => {
1165                let ty = self.tcx.types.unit;
1166                let fn_span = self.fn_span;
1167                let tmp = self.temp(ty, fn_span);
1168                self.unit_temp = Some(tmp);
1169                tmp
1170            }
1171        }
1172    }
1173}
1174
1175fn parse_float_into_constval(num: Symbol, float_ty: ty::FloatTy, neg: bool) -> Option<ConstValue> {
1176    parse_float_into_scalar(num, float_ty, neg).map(|s| ConstValue::Scalar(s.into()))
1177}
1178
1179pub(crate) fn parse_float_into_scalar(
1180    num: Symbol,
1181    float_ty: ty::FloatTy,
1182    neg: bool,
1183) -> Option<ScalarInt> {
1184    let num = num.as_str();
1185    match float_ty {
1186        // FIXME(f16_f128): When available, compare to the library parser as with `f32` and `f64`
1187        ty::FloatTy::F16 => {
1188            let mut f = num.parse::<Half>().ok()?;
1189            if neg {
1190                f = -f;
1191            }
1192            Some(ScalarInt::from(f))
1193        }
1194        ty::FloatTy::F32 => {
1195            let Ok(rust_f) = num.parse::<f32>() else { return None };
1196            let mut f = num
1197                .parse::<Single>()
1198                .unwrap_or_else(|e| panic!("apfloat::ieee::Single failed to parse `{num}`: {e:?}"));
1199
1200            assert!(
1201                u128::from(rust_f.to_bits()) == f.to_bits(),
1202                "apfloat::ieee::Single gave different result for `{}`: \
1203                 {}({:#x}) vs Rust's {}({:#x})",
1204                rust_f,
1205                f,
1206                f.to_bits(),
1207                Single::from_bits(rust_f.to_bits().into()),
1208                rust_f.to_bits()
1209            );
1210
1211            if neg {
1212                f = -f;
1213            }
1214
1215            Some(ScalarInt::from(f))
1216        }
1217        ty::FloatTy::F64 => {
1218            let Ok(rust_f) = num.parse::<f64>() else { return None };
1219            let mut f = num
1220                .parse::<Double>()
1221                .unwrap_or_else(|e| panic!("apfloat::ieee::Double failed to parse `{num}`: {e:?}"));
1222
1223            assert!(
1224                u128::from(rust_f.to_bits()) == f.to_bits(),
1225                "apfloat::ieee::Double gave different result for `{}`: \
1226                 {}({:#x}) vs Rust's {}({:#x})",
1227                rust_f,
1228                f,
1229                f.to_bits(),
1230                Double::from_bits(rust_f.to_bits().into()),
1231                rust_f.to_bits()
1232            );
1233
1234            if neg {
1235                f = -f;
1236            }
1237
1238            Some(ScalarInt::from(f))
1239        }
1240        // FIXME(f16_f128): When available, compare to the library parser as with `f32` and `f64`
1241        ty::FloatTy::F128 => {
1242            let mut f = num.parse::<Quad>().ok()?;
1243            if neg {
1244                f = -f;
1245            }
1246            Some(ScalarInt::from(f))
1247        }
1248    }
1249}
1250
1251///////////////////////////////////////////////////////////////////////////
1252// Builder methods are broken up into modules, depending on what kind
1253// of thing is being lowered. Note that they use the `unpack` macro
1254// above extensively.
1255
1256mod block;
1257mod cfg;
1258mod coverageinfo;
1259mod custom;
1260mod expr;
1261mod matches;
1262mod misc;
1263mod scope;
1264
1265pub(crate) use expr::category::Category as ExprCategory;