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