Skip to main content

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