Skip to main content

rustc_mir_build/builder/expr/
into.rs

1//! See docs in build/expr/mod.rs
2
3use rustc_abi::FieldIdx;
4use rustc_ast::{AsmMacro, InlineAsmOptions};
5use rustc_data_structures::fx::FxHashMap;
6use rustc_data_structures::stack::ensure_sufficient_stack;
7use rustc_hir as hir;
8use rustc_hir::lang_items::LangItem;
9use rustc_middle::mir::*;
10use rustc_middle::span_bug;
11use rustc_middle::thir::*;
12use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
13use rustc_span::{DUMMY_SP, Spanned, sym};
14use rustc_trait_selection::infer::InferCtxtExt;
15use tracing::{debug, instrument};
16
17use crate::builder::expr::category::{Category, RvalueFunc};
18use crate::builder::matches::{DeclareLetBindings, HasMatchGuard};
19use crate::builder::scope::LintLevel;
20use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, NeedsTemporary};
21use crate::errors::{LoopMatchArmWithGuard, LoopMatchUnsupportedType};
22
23impl<'a, 'tcx> Builder<'a, 'tcx> {
24    /// Compile `expr`, storing the result into `destination`, which
25    /// is assumed to be uninitialized.
26    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("expr_into_dest",
                                    "rustc_mir_build::builder::expr::into",
                                    ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/expr/into.rs"),
                                    ::tracing_core::__macro_support::Option::Some(26u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::expr::into"),
                                    ::tracing_core::field::FieldSet::new(&["destination",
                                                    "block", "expr_id"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::DEBUG <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&destination)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&block)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr_id)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: BlockAnd<()> = loop {};
            return __tracing_attr_fake_return;
        }
        {
            let this = self;
            let expr = &this.thir[expr_id];
            let expr_span = expr.span;
            let source_info = this.source_info(expr_span);
            let expr_is_block_or_scope =
                #[allow(non_exhaustive_omitted_patterns)] match expr.kind {
                    ExprKind::Block { .. } | ExprKind::Scope { .. } => true,
                    _ => false,
                };
            if !expr_is_block_or_scope {
                this.block_context.push(BlockFrame::SubExpr);
            }
            let block_and =
                match expr.kind {
                    ExprKind::Scope { region_scope, hir_id, value } => {
                        let region_scope = (region_scope, source_info);
                        ensure_sufficient_stack(||
                                {
                                    this.in_scope(region_scope, LintLevel::Explicit(hir_id),
                                        |this| { this.expr_into_dest(destination, block, value) })
                                })
                    }
                    ExprKind::Block { block: ast_block } => {
                        this.ast_block(destination, block, ast_block, source_info)
                    }
                    ExprKind::Match { scrutinee, ref arms, .. } =>
                        this.match_expr(destination, block, scrutinee, arms,
                            expr_span, this.thir[scrutinee].span),
                    ExprKind::If { cond, then, else_opt, if_then_scope } => {
                        let then_span = this.thir[then].span;
                        let then_source_info = this.source_info(then_span);
                        let condition_scope = this.local_scope();
                        let then_and_else_blocks =
                            this.in_scope((if_then_scope, then_source_info),
                                LintLevel::Inherited,
                                |this|
                                    {
                                        let source_info =
                                            if this.is_let(cond) {
                                                let variable_scope =
                                                    this.new_source_scope(then_span, LintLevel::Inherited);
                                                this.source_scope = variable_scope;
                                                SourceInfo { span: then_span, scope: variable_scope }
                                            } else { this.source_info(then_span) };
                                        let (then_block, else_block) =
                                            this.in_if_then_scope(condition_scope, then_span,
                                                |this|
                                                    {
                                                        let then_blk =
                                                            this.then_else_break(block, cond, Some(condition_scope),
                                                                    source_info, DeclareLetBindings::Yes).into_block();
                                                        this.expr_into_dest(destination, then_blk, then)
                                                    });
                                        then_block.and(else_block)
                                    });
                        let (then_blk, mut else_blk);
                        else_blk =
                            {
                                let BlockAnd(b, v) = then_and_else_blocks;
                                then_blk = b;
                                v
                            };
                        if let Some(else_expr) = else_opt {
                            else_blk =
                                this.expr_into_dest(destination, else_blk,
                                        else_expr).into_block();
                        } else {
                            let correct_si = this.source_info(expr_span.shrink_to_hi());
                            this.cfg.push_assign_unit(else_blk, correct_si, destination,
                                this.tcx);
                        }
                        let join_block = this.cfg.start_new_block();
                        this.cfg.goto(then_blk, source_info, join_block);
                        this.cfg.goto(else_blk, source_info, join_block);
                        join_block.unit()
                    }
                    ExprKind::Let { .. } => {
                        ::rustc_middle::util::bug::span_bug_fmt(expr_span,
                            format_args!("unexpected let expression outside of if or match-guard"));
                    }
                    ExprKind::NeverToAny { source } => {
                        let source_expr = &this.thir[source];
                        let is_call =
                            #[allow(non_exhaustive_omitted_patterns)] match source_expr.kind
                                {
                                ExprKind::Call { .. } | ExprKind::InlineAsm { .. } => true,
                                _ => false,
                            };
                        {
                            let BlockAnd(b, v) =
                                this.as_temp(block, this.local_temp_lifetime(), source,
                                    Mutability::Mut);
                            block = b;
                            v
                        };
                        if is_call {
                            block.unit()
                        } else {
                            this.cfg.terminate(block, source_info,
                                TerminatorKind::Unreachable);
                            let end_block = this.cfg.start_new_block();
                            end_block.unit()
                        }
                    }
                    ExprKind::LogicalOp { op, lhs, rhs } => {
                        let condition_scope = this.local_scope();
                        let source_info = this.source_info(expr.span);
                        let (then_block, else_block) =
                            this.in_if_then_scope(condition_scope, expr.span,
                                |this|
                                    {
                                        this.then_else_break(block, lhs, Some(condition_scope),
                                            source_info, DeclareLetBindings::LetNotPermitted)
                                    });
                        let (short_circuit, continuation, constant) =
                            match op {
                                LogicalOp::And => (else_block, then_block, false),
                                LogicalOp::Or => (then_block, else_block, true),
                            };
                        this.cfg.push_assign_constant(short_circuit, source_info,
                            destination,
                            ConstOperand {
                                span: expr.span,
                                user_ty: None,
                                const_: Const::from_bool(this.tcx, constant),
                            });
                        let mut rhs_block =
                            this.expr_into_dest(destination, continuation,
                                    rhs).into_block();
                        this.visit_coverage_standalone_condition(rhs, destination,
                            &mut rhs_block);
                        let target = this.cfg.start_new_block();
                        this.cfg.goto(rhs_block, source_info, target);
                        this.cfg.goto(short_circuit, source_info, target);
                        target.unit()
                    }
                    ExprKind::Loop { body } => {
                        let loop_block = this.cfg.start_new_block();
                        this.cfg.goto(block, source_info, loop_block);
                        this.in_breakable_scope(Some(loop_block), destination,
                            expr_span,
                            move |this|
                                {
                                    let body_block = this.cfg.start_new_block();
                                    this.cfg.terminate(loop_block, source_info,
                                        TerminatorKind::FalseUnwind {
                                            real_target: body_block,
                                            unwind: UnwindAction::Continue,
                                        });
                                    this.diverge_from(loop_block);
                                    let tmp = this.get_unit_temp();
                                    let body_block_end =
                                        this.expr_into_dest(tmp, body_block, body).into_block();
                                    this.cfg.goto(body_block_end, source_info, loop_block);
                                    None
                                })
                    }
                    ExprKind::LoopMatch {
                        state,
                        region_scope,
                        match_data: box LoopMatchMatchData {
                            box ref arms, span: match_span, scrutinee
                            } } => {
                        fn is_supported_loop_match_type(ty: Ty<'_>) -> bool {
                            match ty.kind() {
                                ty::Uint(_) | ty::Int(_) | ty::Float(_) | ty::Bool |
                                    ty::Char => true,
                                ty::Adt(adt_def, _) =>
                                    match adt_def.adt_kind() {
                                        ty::AdtKind::Struct | ty::AdtKind::Union => false,
                                        ty::AdtKind::Enum => {
                                            adt_def.variants().iter().all(|v| v.fields.is_empty())
                                        }
                                    },
                                _ => false,
                            }
                        }
                        let state_ty = this.thir.exprs[state].ty;
                        if !is_supported_loop_match_type(state_ty) {
                            let span = this.thir.exprs[state].span;
                            this.tcx.dcx().emit_fatal(LoopMatchUnsupportedType {
                                    span,
                                    ty: state_ty,
                                })
                        }
                        let loop_block = this.cfg.start_new_block();
                        this.cfg.goto(block, source_info, loop_block);
                        this.in_breakable_scope(Some(loop_block), destination,
                            expr_span,
                            |this|
                                {
                                    let mut body_block = this.cfg.start_new_block();
                                    this.cfg.terminate(loop_block, source_info,
                                        TerminatorKind::FalseUnwind {
                                            real_target: body_block,
                                            unwind: UnwindAction::Continue,
                                        });
                                    this.diverge_from(loop_block);
                                    let scrutinee_span = this.thir.exprs[scrutinee].span;
                                    let scrutinee_place_builder =
                                        {
                                            let BlockAnd(b, v) =
                                                this.lower_scrutinee(body_block, scrutinee, scrutinee_span);
                                            body_block = b;
                                            v
                                        };
                                    let match_start_span =
                                        match_span.shrink_to_lo().to(scrutinee_span);
                                    let mut patterns = Vec::with_capacity(arms.len());
                                    for &arm_id in arms.iter() {
                                        let arm = &this.thir[arm_id];
                                        if let Some(guard) = arm.guard {
                                            let span = this.thir.exprs[guard].span;
                                            this.tcx.dcx().emit_fatal(LoopMatchArmWithGuard { span })
                                        }
                                        patterns.push((&*arm.pattern, HasMatchGuard::No));
                                    }
                                    let built_tree =
                                        this.lower_match_tree(body_block, scrutinee_span,
                                            &scrutinee_place_builder, match_start_span, patterns,
                                            false);
                                    let state_place = scrutinee_place_builder.to_place(this);
                                    let state_result = this.temp(state_ty, expr_span);
                                    let state_result_place = Place::from(state_result);
                                    {
                                        let BlockAnd(b, v) =
                                            this.in_scope((region_scope, source_info),
                                                LintLevel::Inherited,
                                                move |this|
                                                    {
                                                        this.in_const_continuable_scope(Box::from(arms),
                                                            built_tree.clone(), state_place, expr_span,
                                                            |this|
                                                                {
                                                                    let block =
                                                                        this.in_breakable_scope(None, state_result_place, expr_span,
                                                                                |this|
                                                                                    {
                                                                                        Some(this.lower_match_arms(state_result_place,
                                                                                                scrutinee_place_builder, scrutinee_span, arms, built_tree,
                                                                                                this.source_info(match_span)))
                                                                                    }).into_block();
                                                                    this.cfg.push_assign(block, source_info, state_place,
                                                                        Rvalue::Use(this.consume_by_copy_or_move(state_result_place),
                                                                            WithRetag::Yes));
                                                                    block.unit()
                                                                })
                                                    });
                                        body_block = b;
                                        v
                                    };
                                    this.cfg.goto(body_block, source_info, loop_block);
                                    None
                                })
                    }
                    ExprKind::Call { ty, fun, ref args, .. } if
                        let ty::FnDef(def_id, generic_args) = *ty.kind() &&
                                let Some(intrinsic) = this.tcx.intrinsic(def_id) &&
                            #[allow(non_exhaustive_omitted_patterns)] match intrinsic.name
                                {
                                sym::write_via_move | sym::write_box_via_move => true,
                                _ => false,
                            } => {
                        let _fun =
                            {
                                let BlockAnd(b, v) = this.as_local_operand(block, fun);
                                block = b;
                                v
                            };
                        match intrinsic.name {
                            sym::write_via_move => {
                                if !destination.ty(&this.local_decls, this.tcx).ty.is_unit()
                                    {
                                    ::core::panicking::panic("assertion failed: destination.ty(&this.local_decls, this.tcx).ty.is_unit()")
                                };
                                let [ptr, val] =
                                    **args else {
                                        ::rustc_middle::util::bug::span_bug_fmt(expr_span,
                                            format_args!("invalid write_via_move call"))
                                    };
                                let Some(ptr) =
                                    {
                                            let BlockAnd(b, v) = this.as_local_operand(block, ptr);
                                            block = b;
                                            v
                                        }.place() else {
                                        ::rustc_middle::util::bug::span_bug_fmt(expr_span,
                                            format_args!("invalid write_via_move call"))
                                    };
                                let ptr_deref =
                                    ptr.project_deeper(&[ProjectionElem::Deref], this.tcx);
                                this.expr_into_dest(ptr_deref, block, val)
                            }
                            sym::write_box_via_move => {
                                let [b, val] =
                                    **args else {
                                        ::rustc_middle::util::bug::span_bug_fmt(expr_span,
                                            format_args!("invalid init_box_via_move call"))
                                    };
                                let Some(b) =
                                    {
                                            let BlockAnd(b, v) = this.as_local_operand(block, b);
                                            block = b;
                                            v
                                        }.place() else {
                                        ::rustc_middle::util::bug::span_bug_fmt(expr_span,
                                            format_args!("invalid init_box_via_move call"))
                                    };
                                let tcx = this.tcx;
                                let decls = &this.local_decls;
                                let place = b.project_deeper(&[ProjectionElem::Deref], tcx);
                                let place =
                                    place.project_to_field(FieldIdx::from_u32(1), decls, tcx);
                                let place =
                                    place.project_to_field(FieldIdx::ZERO, decls, tcx);
                                let place =
                                    place.project_to_field(FieldIdx::ZERO, decls, tcx);
                                match (&place.ty(decls, tcx).ty, &generic_args.type_at(0)) {
                                    (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);
                                        }
                                    }
                                };
                                {
                                    let BlockAnd(b, v) = this.expr_into_dest(place, block, val);
                                    block = b;
                                    v
                                };
                                this.cfg.push_assign(block, source_info, destination,
                                    Rvalue::Use(Operand::Move(b), WithRetag::Yes));
                                block.unit()
                            }
                            _ =>
                                ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached")),
                        }
                    }
                    ExprKind::Call {
                        ty: _, fun, ref args, from_hir_call, fn_span } => {
                        let fun =
                            {
                                let BlockAnd(b, v) = this.as_local_operand(block, fun);
                                block = b;
                                v
                            };
                        let args: Box<[_]> =
                            args.into_iter().copied().map(|arg|
                                        Spanned {
                                            node: {
                                                let BlockAnd(b, v) = this.as_local_call_operand(block, arg);
                                                block = b;
                                                v
                                            },
                                            span: this.thir.exprs[arg].span,
                                        }).collect();
                        let success = this.cfg.start_new_block();
                        this.record_operands_moved(&args);
                        {
                            use ::tracing::__macro_support::Callsite as _;
                            static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                                {
                                    static META: ::tracing::Metadata<'static> =
                                        {
                                            ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/expr/into.rs:491",
                                                "rustc_mir_build::builder::expr::into",
                                                ::tracing::Level::DEBUG,
                                                ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/expr/into.rs"),
                                                ::tracing_core::__macro_support::Option::Some(491u32),
                                                ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::expr::into"),
                                                ::tracing_core::field::FieldSet::new(&["message"],
                                                    ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                                ::tracing::metadata::Kind::EVENT)
                                        };
                                    ::tracing::callsite::DefaultCallsite::new(&META)
                                };
                            let enabled =
                                ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                        ::tracing::Level::DEBUG <=
                                            ::tracing::level_filters::LevelFilter::current() &&
                                    {
                                        let interest = __CALLSITE.interest();
                                        !interest.is_never() &&
                                            ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                                interest)
                                    };
                            if enabled {
                                (|value_set: ::tracing::field::ValueSet|
                                            {
                                                let meta = __CALLSITE.metadata();
                                                ::tracing::Event::dispatch(meta, &value_set);
                                                ;
                                            })({
                                        #[allow(unused_imports)]
                                        use ::tracing::field::{debug, display, Value};
                                        let mut iter = __CALLSITE.metadata().fields().iter();
                                        __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                            ::tracing::__macro_support::Option::Some(&format_args!("expr_into_dest: fn_span={0:?}",
                                                                            fn_span) as &dyn Value))])
                                    });
                            } else { ; }
                        };
                        this.cfg.terminate(block, source_info,
                            TerminatorKind::Call {
                                func: fun,
                                args,
                                unwind: UnwindAction::Continue,
                                destination,
                                target: Some(success),
                                call_source: if from_hir_call {
                                    CallSource::Normal
                                } else { CallSource::OverloadedOperator },
                                fn_span,
                            });
                        this.diverge_from(block);
                        success.unit()
                    }
                    ExprKind::ByUse { expr, span } => {
                        let place =
                            {
                                let BlockAnd(b, v) = this.as_place(block, expr);
                                block = b;
                                v
                            };
                        let ty = place.ty(&this.local_decls, this.tcx).ty;
                        if this.tcx.type_is_copy_modulo_regions(this.infcx.typing_env(this.param_env),
                                ty) {
                            this.cfg.push_assign(block, source_info, destination,
                                Rvalue::Use(Operand::Copy(place), WithRetag::Yes));
                            block.unit()
                        } else if this.infcx.type_is_use_cloned_modulo_regions(this.param_env,
                                ty) {
                            let success = this.cfg.start_new_block();
                            let clone_trait =
                                this.tcx.require_lang_item(LangItem::Clone, span);
                            let clone_fn =
                                this.tcx.associated_item_def_ids(clone_trait)[0];
                            let func =
                                Operand::function_handle(this.tcx, clone_fn, [ty.into()],
                                    expr_span);
                            let ref_ty =
                                Ty::new_imm_ref(this.tcx, this.tcx.lifetimes.re_erased, ty);
                            let ref_place = this.temp(ref_ty, span);
                            this.cfg.push_assign(block, source_info, ref_place,
                                Rvalue::Ref(this.tcx.lifetimes.re_erased,
                                    BorrowKind::Shared, place));
                            this.cfg.terminate(block, source_info,
                                TerminatorKind::Call {
                                    func,
                                    args: [Spanned {
                                                    node: Operand::Move(ref_place),
                                                    span: DUMMY_SP,
                                                }].into(),
                                    destination,
                                    target: Some(success),
                                    unwind: UnwindAction::Unreachable,
                                    call_source: CallSource::Use,
                                    fn_span: expr_span,
                                });
                            success.unit()
                        } else {
                            this.cfg.push_assign(block, source_info, destination,
                                Rvalue::Use(Operand::Move(place), WithRetag::Yes));
                            block.unit()
                        }
                    }
                    ExprKind::Use { source } =>
                        this.expr_into_dest(destination, block, source),
                    ExprKind::Borrow { arg, borrow_kind } => {
                        let arg_place =
                            match borrow_kind {
                                BorrowKind::Shared => {
                                    {
                                        let BlockAnd(b, v) = this.as_read_only_place(block, arg);
                                        block = b;
                                        v
                                    }
                                }
                                _ => {
                                    let BlockAnd(b, v) = this.as_place(block, arg);
                                    block = b;
                                    v
                                }
                            };
                        let borrow =
                            Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind,
                                arg_place);
                        this.cfg.push_assign(block, source_info, destination,
                            borrow);
                        block.unit()
                    }
                    ExprKind::RawBorrow { mutability, arg } => {
                        let place =
                            match mutability {
                                hir::Mutability::Not => this.as_read_only_place(block, arg),
                                hir::Mutability::Mut => this.as_place(block, arg),
                            };
                        let address_of =
                            Rvalue::RawPtr(mutability.into(),
                                { let BlockAnd(b, v) = place; block = b; v });
                        this.cfg.push_assign(block, source_info, destination,
                            address_of);
                        block.unit()
                    }
                    ExprKind::Adt(box AdtExpr {
                        adt_def,
                        variant_index,
                        args,
                        ref user_ty,
                        ref fields,
                        ref base }) => {
                        let is_union = adt_def.is_union();
                        let active_field_index = is_union.then(|| fields[0].name);
                        let scope = this.local_temp_lifetime();
                        let fields_map: FxHashMap<_, _> =
                            fields.into_iter().map(|f|
                                        {
                                            (f.name,
                                                {
                                                    let BlockAnd(b, v) =
                                                        this.as_operand(block, scope, f.expr,
                                                            LocalInfo::AggregateTemp, NeedsTemporary::Maybe);
                                                    block = b;
                                                    v
                                                })
                                        }).collect();
                        let variant = adt_def.variant(variant_index);
                        let field_names = variant.fields.indices();
                        let fields =
                            match base {
                                AdtExprBase::None => {
                                    field_names.filter_map(|n|
                                                fields_map.get(&n).cloned()).collect()
                                }
                                AdtExprBase::Base(FruInfo { base, field_types }) => {
                                    let place_builder =
                                        {
                                            let BlockAnd(b, v) = this.as_place_builder(block, *base);
                                            block = b;
                                            v
                                        };
                                    itertools::zip_eq(field_names,
                                                &**field_types).map(|(n, ty)|
                                                match fields_map.get(&n) {
                                                    Some(v) => v.clone(),
                                                    None => {
                                                        let place =
                                                            place_builder.clone_project(PlaceElem::Field(n, *ty));
                                                        this.consume_by_copy_or_move(place.to_place(this))
                                                    }
                                                }).collect()
                                }
                                AdtExprBase::DefaultFields(field_types) => {
                                    itertools::zip_eq(field_names,
                                                field_types).map(|(n, &ty)|
                                                match fields_map.get(&n) {
                                                    Some(v) => v.clone(),
                                                    None =>
                                                        match variant.fields[n].value {
                                                            Some(def) => {
                                                                let value =
                                                                    Const::Unevaluated(UnevaluatedConst::new(def, args), ty);
                                                                Operand::Constant(Box::new(ConstOperand {
                                                                            span: expr_span,
                                                                            user_ty: None,
                                                                            const_: value,
                                                                        }))
                                                            }
                                                            None => {
                                                                let name = variant.fields[n].name;
                                                                ::rustc_middle::util::bug::span_bug_fmt(expr_span,
                                                                    format_args!("missing mandatory field `{0}` of type `{1}`",
                                                                        name, ty));
                                                            }
                                                        },
                                                }).collect()
                                }
                            };
                        let inferred_ty = expr.ty;
                        let user_ty =
                            user_ty.as_ref().map(|user_ty|
                                    {
                                        this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
                                                span: source_info.span,
                                                user_ty: user_ty.clone(),
                                                inferred_ty,
                                            })
                                    });
                        let adt =
                            Box::new(AggregateKind::Adt(adt_def.did(), variant_index,
                                    args, user_ty, active_field_index));
                        this.cfg.push_assign(block, source_info, destination,
                            Rvalue::Aggregate(adt, fields));
                        block.unit()
                    }
                    ExprKind::InlineAsm(box InlineAsmExpr {
                        asm_macro, template, ref operands, options, line_spans }) =>
                        {
                        use rustc_middle::{mir, thir};
                        let destination_block = this.cfg.start_new_block();
                        let mut targets =
                            if asm_macro.diverges(options) {
                                ::alloc::vec::Vec::new()
                            } else {
                                ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                                        [destination_block]))
                            };
                        let operands =
                            operands.into_iter().map(|op|
                                        match *op {
                                            thir::InlineAsmOperand::In { reg, expr } =>
                                                mir::InlineAsmOperand::In {
                                                    reg,
                                                    value: {
                                                        let BlockAnd(b, v) = this.as_local_operand(block, expr);
                                                        block = b;
                                                        v
                                                    },
                                                },
                                            thir::InlineAsmOperand::Out { reg, late, expr } => {
                                                mir::InlineAsmOperand::Out {
                                                    reg,
                                                    late,
                                                    place: expr.map(|expr|
                                                            {
                                                                let BlockAnd(b, v) = this.as_place(block, expr);
                                                                block = b;
                                                                v
                                                            }),
                                                }
                                            }
                                            thir::InlineAsmOperand::InOut { reg, late, expr } => {
                                                let place =
                                                    {
                                                        let BlockAnd(b, v) = this.as_place(block, expr);
                                                        block = b;
                                                        v
                                                    };
                                                mir::InlineAsmOperand::InOut {
                                                    reg,
                                                    late,
                                                    in_value: Operand::Copy(place),
                                                    out_place: Some(place),
                                                }
                                            }
                                            thir::InlineAsmOperand::SplitInOut {
                                                reg, late, in_expr, out_expr } => {
                                                mir::InlineAsmOperand::InOut {
                                                    reg,
                                                    late,
                                                    in_value: {
                                                        let BlockAnd(b, v) = this.as_local_operand(block, in_expr);
                                                        block = b;
                                                        v
                                                    },
                                                    out_place: out_expr.map(|out_expr|
                                                            {
                                                                {
                                                                    let BlockAnd(b, v) = this.as_place(block, out_expr);
                                                                    block = b;
                                                                    v
                                                                }
                                                            }),
                                                }
                                            }
                                            thir::InlineAsmOperand::Const { value, span } => {
                                                mir::InlineAsmOperand::Const {
                                                    value: Box::new(ConstOperand {
                                                            span,
                                                            user_ty: None,
                                                            const_: value,
                                                        }),
                                                }
                                            }
                                            thir::InlineAsmOperand::SymFn { value } =>
                                                mir::InlineAsmOperand::SymFn {
                                                    value: Box::new(this.as_constant(&this.thir[value])),
                                                },
                                            thir::InlineAsmOperand::SymStatic { def_id } => {
                                                mir::InlineAsmOperand::SymStatic { def_id }
                                            }
                                            thir::InlineAsmOperand::Label { block } => {
                                                let target = this.cfg.start_new_block();
                                                let target_index = targets.len();
                                                targets.push(target);
                                                let tmp = this.get_unit_temp();
                                                let target =
                                                    this.ast_block(tmp, target, block,
                                                            source_info).into_block();
                                                this.cfg.terminate(target, source_info,
                                                    TerminatorKind::Goto { target: destination_block });
                                                mir::InlineAsmOperand::Label { target_index }
                                            }
                                        }).collect();
                        if !expr.ty.is_never() {
                            this.cfg.push_assign_unit(block, source_info, destination,
                                this.tcx);
                        }
                        let asm_macro =
                            match asm_macro {
                                AsmMacro::Asm | AsmMacro::GlobalAsm => InlineAsmMacro::Asm,
                                AsmMacro::NakedAsm => InlineAsmMacro::NakedAsm,
                            };
                        this.cfg.terminate(block, source_info,
                            TerminatorKind::InlineAsm {
                                asm_macro,
                                template,
                                operands,
                                options,
                                line_spans,
                                targets: targets.into_boxed_slice(),
                                unwind: if options.contains(InlineAsmOptions::MAY_UNWIND) {
                                    UnwindAction::Continue
                                } else { UnwindAction::Unreachable },
                            });
                        if options.contains(InlineAsmOptions::MAY_UNWIND) {
                            this.diverge_from(block);
                        }
                        destination_block.unit()
                    }
                    ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
                        block = this.stmt_expr(block, expr_id, None).into_block();
                        this.cfg.push_assign_unit(block, source_info, destination,
                            this.tcx);
                        block.unit()
                    }
                    ExprKind::Continue { .. } | ExprKind::ConstContinue { .. } |
                        ExprKind::Break { .. } | ExprKind::Return { .. } |
                        ExprKind::Become { .. } => {
                        block = this.stmt_expr(block, expr_id, None).into_block();
                        block.unit()
                    }
                    ExprKind::VarRef { .. } | ExprKind::UpvarRef { .. } |
                        ExprKind::PlaceTypeAscription { .. } |
                        ExprKind::ValueTypeAscription { .. } |
                        ExprKind::PlaceUnwrapUnsafeBinder { .. } |
                        ExprKind::ValueUnwrapUnsafeBinder { .. } => {
                        if true {
                            if !(Category::of(&expr.kind) == Some(Category::Place)) {
                                ::core::panicking::panic("assertion failed: Category::of(&expr.kind) == Some(Category::Place)")
                            };
                        };
                        let place =
                            {
                                let BlockAnd(b, v) = this.as_place(block, expr_id);
                                block = b;
                                v
                            };
                        let rvalue =
                            Rvalue::Use(this.consume_by_copy_or_move(place),
                                WithRetag::Yes);
                        this.cfg.push_assign(block, source_info, destination,
                            rvalue);
                        block.unit()
                    }
                    ExprKind::Index { .. } | ExprKind::Deref { .. } |
                        ExprKind::Field { .. } => {
                        if true {
                            match (&Category::of(&expr.kind), &Some(Category::Place)) {
                                (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);
                                    }
                                }
                            };
                        };
                        if !destination.projection.is_empty() {
                            this.local_decls.push(LocalDecl::new(expr.ty, expr.span));
                        }
                        let place =
                            {
                                let BlockAnd(b, v) = this.as_place(block, expr_id);
                                block = b;
                                v
                            };
                        let rvalue =
                            Rvalue::Use(this.consume_by_copy_or_move(place),
                                WithRetag::Yes);
                        this.cfg.push_assign(block, source_info, destination,
                            rvalue);
                        block.unit()
                    }
                    ExprKind::Yield { value } => {
                        let scope = this.local_temp_lifetime();
                        let value =
                            {
                                let BlockAnd(b, v) =
                                    this.as_operand(block, scope, value, LocalInfo::Boring,
                                        NeedsTemporary::No);
                                block = b;
                                v
                            };
                        let resume = this.cfg.start_new_block();
                        this.cfg.terminate(block, source_info,
                            TerminatorKind::Yield {
                                value,
                                resume,
                                resume_arg: destination,
                                drop: None,
                            });
                        this.coroutine_drop_cleanup(block);
                        resume.unit()
                    }
                    ExprKind::Unary { .. } | ExprKind::Binary { .. } |
                        ExprKind::Cast { .. } | ExprKind::PointerCoercion { .. } |
                        ExprKind::Repeat { .. } | ExprKind::Array { .. } |
                        ExprKind::Tuple { .. } | ExprKind::Closure { .. } |
                        ExprKind::ConstBlock { .. } | ExprKind::Literal { .. } |
                        ExprKind::NamedConst { .. } | ExprKind::NonHirLiteral { .. }
                        | ExprKind::ZstLiteral { .. } | ExprKind::ConstParam { .. }
                        | ExprKind::ThreadLocalRef(_) | ExprKind::StaticRef { .. } |
                        ExprKind::WrapUnsafeBinder { .. } => {
                        if true {
                            if !match Category::of(&expr.kind).unwrap() {
                                        Category::Rvalue(RvalueFunc::Into) => false,
                                        Category::Place => false,
                                        _ => true,
                                    } {
                                ::core::panicking::panic("assertion failed: match Category::of(&expr.kind).unwrap() {\n    Category::Rvalue(RvalueFunc::Into) => false,\n    Category::Place => false,\n    _ => true,\n}")
                            };
                        };
                        let rvalue =
                            {
                                let BlockAnd(b, v) = this.as_local_rvalue(block, expr_id);
                                block = b;
                                v
                            };
                        this.cfg.push_assign(block, source_info, destination,
                            rvalue);
                        block.unit()
                    }
                };
            if !expr_is_block_or_scope {
                let popped = this.block_context.pop();
                if !popped.is_some() {
                    ::core::panicking::panic("assertion failed: popped.is_some()")
                };
            }
            block_and
        }
    }
}#[instrument(level = "debug", skip(self))]
27    pub(crate) fn expr_into_dest(
28        &mut self,
29        destination: Place<'tcx>,
30        mut block: BasicBlock,
31        expr_id: ExprId,
32    ) -> BlockAnd<()> {
33        // since we frequently have to reference `self` from within a
34        // closure, where `self` would be shadowed, it's easier to
35        // just use the name `this` uniformly
36        let this = self; // See "LET_THIS_SELF".
37        let expr = &this.thir[expr_id];
38        let expr_span = expr.span;
39        let source_info = this.source_info(expr_span);
40
41        let expr_is_block_or_scope =
42            matches!(expr.kind, ExprKind::Block { .. } | ExprKind::Scope { .. });
43
44        if !expr_is_block_or_scope {
45            this.block_context.push(BlockFrame::SubExpr);
46        }
47
48        let block_and = match expr.kind {
49            ExprKind::Scope { region_scope, hir_id, value } => {
50                let region_scope = (region_scope, source_info);
51                ensure_sufficient_stack(|| {
52                    this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
53                        this.expr_into_dest(destination, block, value)
54                    })
55                })
56            }
57            ExprKind::Block { block: ast_block } => {
58                this.ast_block(destination, block, ast_block, source_info)
59            }
60            ExprKind::Match { scrutinee, ref arms, .. } => this.match_expr(
61                destination,
62                block,
63                scrutinee,
64                arms,
65                expr_span,
66                this.thir[scrutinee].span,
67            ),
68            ExprKind::If { cond, then, else_opt, if_then_scope } => {
69                let then_span = this.thir[then].span;
70                let then_source_info = this.source_info(then_span);
71                let condition_scope = this.local_scope();
72
73                let then_and_else_blocks = this.in_scope(
74                    (if_then_scope, then_source_info),
75                    LintLevel::Inherited,
76                    |this| {
77                        // FIXME: Does this need extra logic to handle let-chains?
78                        let source_info = if this.is_let(cond) {
79                            let variable_scope =
80                                this.new_source_scope(then_span, LintLevel::Inherited);
81                            this.source_scope = variable_scope;
82                            SourceInfo { span: then_span, scope: variable_scope }
83                        } else {
84                            this.source_info(then_span)
85                        };
86
87                        // Lower the condition, and have it branch into `then` and `else` blocks.
88                        let (then_block, else_block) =
89                            this.in_if_then_scope(condition_scope, then_span, |this| {
90                                let then_blk = this
91                                    .then_else_break(
92                                        block,
93                                        cond,
94                                        Some(condition_scope), // Temp scope
95                                        source_info,
96                                        DeclareLetBindings::Yes, // Declare `let` bindings normally
97                                    )
98                                    .into_block();
99
100                                // Lower the `then` arm into its block.
101                                this.expr_into_dest(destination, then_blk, then)
102                            });
103
104                        // Pack `(then_block, else_block)` into `BlockAnd<BasicBlock>`.
105                        then_block.and(else_block)
106                    },
107                );
108
109                // Unpack `BlockAnd<BasicBlock>` into `(then_blk, else_blk)`.
110                let (then_blk, mut else_blk);
111                else_blk = unpack!(then_blk = then_and_else_blocks);
112
113                // If there is an `else` arm, lower it into `else_blk`.
114                if let Some(else_expr) = else_opt {
115                    else_blk = this.expr_into_dest(destination, else_blk, else_expr).into_block();
116                } else {
117                    // There is no `else` arm, so we know both arms have type `()`.
118                    // Generate the implicit `else {}` by assigning unit.
119                    let correct_si = this.source_info(expr_span.shrink_to_hi());
120                    this.cfg.push_assign_unit(else_blk, correct_si, destination, this.tcx);
121                }
122
123                // The `then` and `else` arms have been lowered into their respective
124                // blocks, so make both of them meet up in a new block.
125                let join_block = this.cfg.start_new_block();
126                this.cfg.goto(then_blk, source_info, join_block);
127                this.cfg.goto(else_blk, source_info, join_block);
128                join_block.unit()
129            }
130            ExprKind::Let { .. } => {
131                // After desugaring, `let` expressions should only appear inside `if`
132                // expressions or `match` guards, possibly nested within a let-chain.
133                // In both cases they are specifically handled by the lowerings of
134                // those expressions, so this case is currently unreachable.
135                span_bug!(expr_span, "unexpected let expression outside of if or match-guard");
136            }
137            ExprKind::NeverToAny { source } => {
138                let source_expr = &this.thir[source];
139                let is_call =
140                    matches!(source_expr.kind, ExprKind::Call { .. } | ExprKind::InlineAsm { .. });
141
142                // (#66975) Source could be a const of type `!`, so has to
143                // exist in the generated MIR.
144                unpack!(
145                    block =
146                        this.as_temp(block, this.local_temp_lifetime(), source, Mutability::Mut)
147                );
148
149                // This is an optimization. If the expression was a call then we already have an
150                // unreachable block. Don't bother to terminate it and create a new one.
151                if is_call {
152                    block.unit()
153                } else {
154                    this.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
155                    let end_block = this.cfg.start_new_block();
156                    end_block.unit()
157                }
158            }
159            ExprKind::LogicalOp { op, lhs, rhs } => {
160                let condition_scope = this.local_scope();
161                let source_info = this.source_info(expr.span);
162
163                // We first evaluate the left-hand side of the predicate ...
164                let (then_block, else_block) =
165                    this.in_if_then_scope(condition_scope, expr.span, |this| {
166                        this.then_else_break(
167                            block,
168                            lhs,
169                            Some(condition_scope), // Temp scope
170                            source_info,
171                            // This flag controls how inner `let` expressions are lowered,
172                            // but either way there shouldn't be any of those in here.
173                            DeclareLetBindings::LetNotPermitted,
174                        )
175                    });
176                let (short_circuit, continuation, constant) = match op {
177                    LogicalOp::And => (else_block, then_block, false),
178                    LogicalOp::Or => (then_block, else_block, true),
179                };
180                // At this point, the control flow splits into a short-circuiting path
181                // and a continuation path.
182                // - If the operator is `&&`, passing `lhs` leads to continuation of evaluation on `rhs`;
183                //   failing it leads to the short-circuting path which assigns `false` to the place.
184                // - If the operator is `||`, failing `lhs` leads to continuation of evaluation on `rhs`;
185                //   passing it leads to the short-circuting path which assigns `true` to the place.
186                this.cfg.push_assign_constant(
187                    short_circuit,
188                    source_info,
189                    destination,
190                    ConstOperand {
191                        span: expr.span,
192                        user_ty: None,
193                        const_: Const::from_bool(this.tcx, constant),
194                    },
195                );
196                let mut rhs_block =
197                    this.expr_into_dest(destination, continuation, rhs).into_block();
198                // Instrument the lowered RHS's value for condition coverage.
199                // (Does nothing if condition coverage is not enabled.)
200                this.visit_coverage_standalone_condition(rhs, destination, &mut rhs_block);
201
202                let target = this.cfg.start_new_block();
203                this.cfg.goto(rhs_block, source_info, target);
204                this.cfg.goto(short_circuit, source_info, target);
205                target.unit()
206            }
207            ExprKind::Loop { body } => {
208                // [block]
209                //    |
210                //   [loop_block] -> [body_block] -/eval. body/-> [body_block_end]
211                //    |        ^                                         |
212                // false link  |                                         |
213                //    |        +-----------------------------------------+
214                //    +-> [diverge_cleanup]
215                // The false link is required to make sure borrowck considers unwinds through the
216                // body, even when the exact code in the body cannot unwind
217
218                let loop_block = this.cfg.start_new_block();
219
220                // Start the loop.
221                this.cfg.goto(block, source_info, loop_block);
222
223                this.in_breakable_scope(Some(loop_block), destination, expr_span, move |this| {
224                    // conduct the test, if necessary
225                    let body_block = this.cfg.start_new_block();
226                    this.cfg.terminate(
227                        loop_block,
228                        source_info,
229                        TerminatorKind::FalseUnwind {
230                            real_target: body_block,
231                            unwind: UnwindAction::Continue,
232                        },
233                    );
234                    this.diverge_from(loop_block);
235
236                    // The “return” value of the loop body must always be a unit. We therefore
237                    // introduce a unit temporary as the destination for the loop body.
238                    let tmp = this.get_unit_temp();
239                    // Execute the body, branching back to the test.
240                    let body_block_end = this.expr_into_dest(tmp, body_block, body).into_block();
241                    this.cfg.goto(body_block_end, source_info, loop_block);
242
243                    // Loops are only exited by `break` expressions.
244                    None
245                })
246            }
247            ExprKind::LoopMatch {
248                state,
249                region_scope,
250                match_data: box LoopMatchMatchData { box ref arms, span: match_span, scrutinee },
251            } => {
252                // Intuitively, this is a combination of a loop containing a labeled block
253                // containing a match.
254                //
255                // The only new bit here is that the lowering of the match is wrapped in a
256                // `in_const_continuable_scope`, which makes the match arms and their target basic
257                // block available to the lowering of `#[const_continue]`.
258
259                fn is_supported_loop_match_type(ty: Ty<'_>) -> bool {
260                    match ty.kind() {
261                        ty::Uint(_) | ty::Int(_) | ty::Float(_) | ty::Bool | ty::Char => true,
262                        ty::Adt(adt_def, _) => match adt_def.adt_kind() {
263                            ty::AdtKind::Struct | ty::AdtKind::Union => false,
264                            ty::AdtKind::Enum => {
265                                adt_def.variants().iter().all(|v| v.fields.is_empty())
266                            }
267                        },
268                        _ => false,
269                    }
270                }
271
272                let state_ty = this.thir.exprs[state].ty;
273                if !is_supported_loop_match_type(state_ty) {
274                    let span = this.thir.exprs[state].span;
275                    this.tcx.dcx().emit_fatal(LoopMatchUnsupportedType { span, ty: state_ty })
276                }
277
278                let loop_block = this.cfg.start_new_block();
279
280                // Start the loop.
281                this.cfg.goto(block, source_info, loop_block);
282
283                this.in_breakable_scope(Some(loop_block), destination, expr_span, |this| {
284                    // Logic for `loop`.
285                    let mut body_block = this.cfg.start_new_block();
286                    this.cfg.terminate(
287                        loop_block,
288                        source_info,
289                        TerminatorKind::FalseUnwind {
290                            real_target: body_block,
291                            unwind: UnwindAction::Continue,
292                        },
293                    );
294                    this.diverge_from(loop_block);
295
296                    // Logic for `match`.
297                    let scrutinee_span = this.thir.exprs[scrutinee].span;
298                    let scrutinee_place_builder = unpack!(
299                        body_block = this.lower_scrutinee(body_block, scrutinee, scrutinee_span)
300                    );
301
302                    let match_start_span = match_span.shrink_to_lo().to(scrutinee_span);
303
304                    let mut patterns = Vec::with_capacity(arms.len());
305                    for &arm_id in arms.iter() {
306                        let arm = &this.thir[arm_id];
307
308                        if let Some(guard) = arm.guard {
309                            let span = this.thir.exprs[guard].span;
310                            this.tcx.dcx().emit_fatal(LoopMatchArmWithGuard { span })
311                        }
312
313                        patterns.push((&*arm.pattern, HasMatchGuard::No));
314                    }
315
316                    // The `built_tree` maps match arms to their basic block (where control flow
317                    // jumps to when a value matches the arm). This structure is stored so that a
318                    // `#[const_continue]` can figure out what basic block to jump to.
319                    let built_tree = this.lower_match_tree(
320                        body_block,
321                        scrutinee_span,
322                        &scrutinee_place_builder,
323                        match_start_span,
324                        patterns,
325                        false,
326                    );
327
328                    let state_place = scrutinee_place_builder.to_place(this);
329                    let state_result = this.temp(state_ty, expr_span);
330                    let state_result_place = Place::from(state_result);
331
332                    // This is logic for the labeled block: a block is a drop scope, hence
333                    // `in_scope`, and a labeled block can be broken out of with a `break 'label`,
334                    // hence the `in_breakable_scope`.
335                    //
336                    // The state update is still modeled like `state = 'blk: { ... }`: normal
337                    // match arm results and ordinary breaks to the block are first written to
338                    // `state_result_place`, then written back to `state_place`. This avoids
339                    // building an overlapping assignment like `state = state`.
340                    //
341                    // Then `in_const_continuable_scope` stores information for the lowering of
342                    // `#[const_continue]`, which still updates the actual `state_place` directly
343                    // so it can jump to the statically known next match branch.
344                    unpack!(
345                        body_block = this.in_scope(
346                            (region_scope, source_info),
347                            LintLevel::Inherited,
348                            move |this| {
349                                this.in_const_continuable_scope(
350                                    Box::from(arms),
351                                    built_tree.clone(),
352                                    state_place,
353                                    expr_span,
354                                    |this| {
355                                        let block = this
356                                            .in_breakable_scope(
357                                                None,
358                                                state_result_place,
359                                                expr_span,
360                                                |this| {
361                                                    Some(this.lower_match_arms(
362                                                        state_result_place,
363                                                        scrutinee_place_builder,
364                                                        scrutinee_span,
365                                                        arms,
366                                                        built_tree,
367                                                        this.source_info(match_span),
368                                                    ))
369                                                },
370                                            )
371                                            .into_block();
372
373                                        this.cfg.push_assign(
374                                            block,
375                                            source_info,
376                                            state_place,
377                                            Rvalue::Use(
378                                                this.consume_by_copy_or_move(state_result_place),
379                                                WithRetag::Yes,
380                                            ),
381                                        );
382                                        block.unit()
383                                    },
384                                )
385                            }
386                        )
387                    );
388
389                    this.cfg.goto(body_block, source_info, loop_block);
390
391                    // Loops are only exited by `break` expressions.
392                    None
393                })
394            }
395            // Some intrinsics are handled here because they desperately want to avoid introducing
396            // unnecessary copies.
397            ExprKind::Call { ty, fun, ref args, .. }
398                if let ty::FnDef(def_id, generic_args) = *ty.kind()
399                    && let Some(intrinsic) = this.tcx.intrinsic(def_id)
400                    && matches!(intrinsic.name, sym::write_via_move | sym::write_box_via_move) =>
401            {
402                // We still have to evaluate the callee expression as normal (but we don't care
403                // about its result).
404                let _fun = unpack!(block = this.as_local_operand(block, fun));
405
406                match intrinsic.name {
407                    sym::write_via_move => {
408                        // `write_via_move(ptr, val)` becomes `*ptr = val` but without any dropping.
409
410                        // The destination must have unit type (so we don't actually have to store anything
411                        // into it).
412                        assert!(destination.ty(&this.local_decls, this.tcx).ty.is_unit());
413
414                        // Compile this to an assignment of the argument into the destination.
415                        let [ptr, val] = **args else {
416                            span_bug!(expr_span, "invalid write_via_move call")
417                        };
418                        let Some(ptr) = unpack!(block = this.as_local_operand(block, ptr)).place()
419                        else {
420                            span_bug!(expr_span, "invalid write_via_move call")
421                        };
422                        let ptr_deref = ptr.project_deeper(&[ProjectionElem::Deref], this.tcx);
423                        this.expr_into_dest(ptr_deref, block, val)
424                    }
425                    sym::write_box_via_move => {
426                        // The signature is:
427                        // `fn write_box_via_move<T>(b: Box<MaybeUninit<T>>, val: T) -> Box<MaybeUninit<T>>`.
428                        // `write_box_via_move(b, val)` becomes
429                        // ```
430                        // (*b).value.value.value = val;
431                        // b
432                        // ```
433                        // One crucial aspect of this lowering is that the generated code must
434                        // cause the borrow checker to enforce that `val` lives sufficiently
435                        // long to be stored in `b`. The above lowering does this; anything that
436                        // involves a `*const T` or a `NonNull<T>` does not as those are covariant.
437
438                        // Extract the operands, compile `b`.
439                        let [b, val] = **args else {
440                            span_bug!(expr_span, "invalid init_box_via_move call")
441                        };
442                        let Some(b) = unpack!(block = this.as_local_operand(block, b)).place()
443                        else {
444                            span_bug!(expr_span, "invalid init_box_via_move call")
445                        };
446                        let tcx = this.tcx;
447                        let decls = &this.local_decls;
448
449                        // `b` is a `Box<MaybeUninit<T>>`.
450                        let place = b.project_deeper(&[ProjectionElem::Deref], tcx);
451                        // Current type: `MaybeUninit<T>`. Field #1 is `ManuallyDrop<T>`.
452                        let place = place.project_to_field(FieldIdx::from_u32(1), decls, tcx);
453                        // Current type: `ManuallyDrop<T>`. Field #0 is `MaybeDangling<T>`.
454                        let place = place.project_to_field(FieldIdx::ZERO, decls, tcx);
455                        // Current type: `MaybeDangling<T>`. Field #0 is `T`.
456                        let place = place.project_to_field(FieldIdx::ZERO, decls, tcx);
457                        // Sanity check.
458                        assert_eq!(place.ty(decls, tcx).ty, generic_args.type_at(0));
459
460                        // Store `val` into place.
461                        unpack!(block = this.expr_into_dest(place, block, val));
462
463                        // Return `b`
464                        this.cfg.push_assign(
465                            block,
466                            source_info,
467                            destination,
468                            // Move from `b` so that does not get dropped any more.
469                            Rvalue::Use(Operand::Move(b), WithRetag::Yes),
470                        );
471                        block.unit()
472                    }
473                    _ => rustc_middle::bug!(),
474                }
475            }
476            ExprKind::Call { ty: _, fun, ref args, from_hir_call, fn_span } => {
477                let fun = unpack!(block = this.as_local_operand(block, fun));
478                let args: Box<[_]> = args
479                    .into_iter()
480                    .copied()
481                    .map(|arg| Spanned {
482                        node: unpack!(block = this.as_local_call_operand(block, arg)),
483                        span: this.thir.exprs[arg].span,
484                    })
485                    .collect();
486
487                let success = this.cfg.start_new_block();
488
489                this.record_operands_moved(&args);
490
491                debug!("expr_into_dest: fn_span={:?}", fn_span);
492
493                this.cfg.terminate(
494                    block,
495                    source_info,
496                    TerminatorKind::Call {
497                        func: fun,
498                        args,
499                        unwind: UnwindAction::Continue,
500                        destination,
501                        target: Some(success),
502                        call_source: if from_hir_call {
503                            CallSource::Normal
504                        } else {
505                            CallSource::OverloadedOperator
506                        },
507                        fn_span,
508                    },
509                );
510                this.diverge_from(block);
511                success.unit()
512            }
513            ExprKind::ByUse { expr, span } => {
514                let place = unpack!(block = this.as_place(block, expr));
515                let ty = place.ty(&this.local_decls, this.tcx).ty;
516
517                if this.tcx.type_is_copy_modulo_regions(this.infcx.typing_env(this.param_env), ty) {
518                    this.cfg.push_assign(
519                        block,
520                        source_info,
521                        destination,
522                        Rvalue::Use(Operand::Copy(place), WithRetag::Yes),
523                    );
524                    block.unit()
525                } else if this.infcx.type_is_use_cloned_modulo_regions(this.param_env, ty) {
526                    // Convert `expr.use` to a call like `Clone::clone(&expr)`
527                    let success = this.cfg.start_new_block();
528                    let clone_trait = this.tcx.require_lang_item(LangItem::Clone, span);
529                    let clone_fn = this.tcx.associated_item_def_ids(clone_trait)[0];
530                    let func = Operand::function_handle(this.tcx, clone_fn, [ty.into()], expr_span);
531                    let ref_ty = Ty::new_imm_ref(this.tcx, this.tcx.lifetimes.re_erased, ty);
532                    let ref_place = this.temp(ref_ty, span);
533                    this.cfg.push_assign(
534                        block,
535                        source_info,
536                        ref_place,
537                        Rvalue::Ref(this.tcx.lifetimes.re_erased, BorrowKind::Shared, place),
538                    );
539                    this.cfg.terminate(
540                        block,
541                        source_info,
542                        TerminatorKind::Call {
543                            func,
544                            args: [Spanned { node: Operand::Move(ref_place), span: DUMMY_SP }]
545                                .into(),
546                            destination,
547                            target: Some(success),
548                            unwind: UnwindAction::Unreachable,
549                            call_source: CallSource::Use,
550                            fn_span: expr_span,
551                        },
552                    );
553                    success.unit()
554                } else {
555                    this.cfg.push_assign(
556                        block,
557                        source_info,
558                        destination,
559                        Rvalue::Use(Operand::Move(place), WithRetag::Yes),
560                    );
561                    block.unit()
562                }
563            }
564            ExprKind::Use { source } => this.expr_into_dest(destination, block, source),
565            ExprKind::Borrow { arg, borrow_kind } => {
566                // We don't do this in `as_rvalue` because we use `as_place`
567                // for borrow expressions, so we cannot create an `RValue` that
568                // remains valid across user code. `as_rvalue` is usually called
569                // by this method anyway, so this shouldn't cause too many
570                // unnecessary temporaries.
571                let arg_place = match borrow_kind {
572                    BorrowKind::Shared => {
573                        unpack!(block = this.as_read_only_place(block, arg))
574                    }
575                    _ => unpack!(block = this.as_place(block, arg)),
576                };
577                let borrow = Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place);
578                this.cfg.push_assign(block, source_info, destination, borrow);
579                block.unit()
580            }
581            ExprKind::RawBorrow { mutability, arg } => {
582                let place = match mutability {
583                    hir::Mutability::Not => this.as_read_only_place(block, arg),
584                    hir::Mutability::Mut => this.as_place(block, arg),
585                };
586                let address_of = Rvalue::RawPtr(mutability.into(), unpack!(block = place));
587                this.cfg.push_assign(block, source_info, destination, address_of);
588                block.unit()
589            }
590            ExprKind::Adt(box AdtExpr {
591                adt_def,
592                variant_index,
593                args,
594                ref user_ty,
595                ref fields,
596                ref base,
597            }) => {
598                // See the notes for `ExprKind::Array` in `as_rvalue` and for
599                // `ExprKind::Borrow` above.
600                let is_union = adt_def.is_union();
601                let active_field_index = is_union.then(|| fields[0].name);
602
603                let scope = this.local_temp_lifetime();
604
605                // first process the set of fields that were provided
606                // (evaluating them in order given by user)
607                let fields_map: FxHashMap<_, _> = fields
608                    .into_iter()
609                    .map(|f| {
610                        (
611                            f.name,
612                            unpack!(
613                                block = this.as_operand(
614                                    block,
615                                    scope,
616                                    f.expr,
617                                    LocalInfo::AggregateTemp,
618                                    NeedsTemporary::Maybe,
619                                )
620                            ),
621                        )
622                    })
623                    .collect();
624
625                let variant = adt_def.variant(variant_index);
626                let field_names = variant.fields.indices();
627
628                let fields = match base {
629                    AdtExprBase::None => {
630                        field_names.filter_map(|n| fields_map.get(&n).cloned()).collect()
631                    }
632                    AdtExprBase::Base(FruInfo { base, field_types }) => {
633                        let place_builder = unpack!(block = this.as_place_builder(block, *base));
634
635                        // We desugar FRU as we lower to MIR, so for each
636                        // base-supplied field, generate an operand that
637                        // reads it from the base.
638                        itertools::zip_eq(field_names, &**field_types)
639                            .map(|(n, ty)| match fields_map.get(&n) {
640                                Some(v) => v.clone(),
641                                None => {
642                                    let place =
643                                        place_builder.clone_project(PlaceElem::Field(n, *ty));
644                                    this.consume_by_copy_or_move(place.to_place(this))
645                                }
646                            })
647                            .collect()
648                    }
649                    AdtExprBase::DefaultFields(field_types) => {
650                        itertools::zip_eq(field_names, field_types)
651                            .map(|(n, &ty)| match fields_map.get(&n) {
652                                Some(v) => v.clone(),
653                                None => match variant.fields[n].value {
654                                    Some(def) => {
655                                        let value = Const::Unevaluated(
656                                            UnevaluatedConst::new(def, args),
657                                            ty,
658                                        );
659                                        Operand::Constant(Box::new(ConstOperand {
660                                            span: expr_span,
661                                            user_ty: None,
662                                            const_: value,
663                                        }))
664                                    }
665                                    None => {
666                                        let name = variant.fields[n].name;
667                                        span_bug!(
668                                            expr_span,
669                                            "missing mandatory field `{name}` of type `{ty}`",
670                                        );
671                                    }
672                                },
673                            })
674                            .collect()
675                    }
676                };
677
678                let inferred_ty = expr.ty;
679                let user_ty = user_ty.as_ref().map(|user_ty| {
680                    this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
681                        span: source_info.span,
682                        user_ty: user_ty.clone(),
683                        inferred_ty,
684                    })
685                });
686                let adt = Box::new(AggregateKind::Adt(
687                    adt_def.did(),
688                    variant_index,
689                    args,
690                    user_ty,
691                    active_field_index,
692                ));
693                this.cfg.push_assign(
694                    block,
695                    source_info,
696                    destination,
697                    Rvalue::Aggregate(adt, fields),
698                );
699                block.unit()
700            }
701            ExprKind::InlineAsm(box InlineAsmExpr {
702                asm_macro,
703                template,
704                ref operands,
705                options,
706                line_spans,
707            }) => {
708                use rustc_middle::{mir, thir};
709
710                let destination_block = this.cfg.start_new_block();
711                let mut targets =
712                    if asm_macro.diverges(options) { vec![] } else { vec![destination_block] };
713
714                let operands = operands
715                    .into_iter()
716                    .map(|op| match *op {
717                        thir::InlineAsmOperand::In { reg, expr } => mir::InlineAsmOperand::In {
718                            reg,
719                            value: unpack!(block = this.as_local_operand(block, expr)),
720                        },
721                        thir::InlineAsmOperand::Out { reg, late, expr } => {
722                            mir::InlineAsmOperand::Out {
723                                reg,
724                                late,
725                                place: expr.map(|expr| unpack!(block = this.as_place(block, expr))),
726                            }
727                        }
728                        thir::InlineAsmOperand::InOut { reg, late, expr } => {
729                            let place = unpack!(block = this.as_place(block, expr));
730                            mir::InlineAsmOperand::InOut {
731                                reg,
732                                late,
733                                // This works because asm operands must be Copy
734                                in_value: Operand::Copy(place),
735                                out_place: Some(place),
736                            }
737                        }
738                        thir::InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
739                            mir::InlineAsmOperand::InOut {
740                                reg,
741                                late,
742                                in_value: unpack!(block = this.as_local_operand(block, in_expr)),
743                                out_place: out_expr.map(|out_expr| {
744                                    unpack!(block = this.as_place(block, out_expr))
745                                }),
746                            }
747                        }
748                        thir::InlineAsmOperand::Const { value, span } => {
749                            mir::InlineAsmOperand::Const {
750                                value: Box::new(ConstOperand {
751                                    span,
752                                    user_ty: None,
753                                    const_: value,
754                                }),
755                            }
756                        }
757                        thir::InlineAsmOperand::SymFn { value } => mir::InlineAsmOperand::SymFn {
758                            value: Box::new(this.as_constant(&this.thir[value])),
759                        },
760                        thir::InlineAsmOperand::SymStatic { def_id } => {
761                            mir::InlineAsmOperand::SymStatic { def_id }
762                        }
763                        thir::InlineAsmOperand::Label { block } => {
764                            let target = this.cfg.start_new_block();
765                            let target_index = targets.len();
766                            targets.push(target);
767
768                            let tmp = this.get_unit_temp();
769                            let target =
770                                this.ast_block(tmp, target, block, source_info).into_block();
771                            this.cfg.terminate(
772                                target,
773                                source_info,
774                                TerminatorKind::Goto { target: destination_block },
775                            );
776
777                            mir::InlineAsmOperand::Label { target_index }
778                        }
779                    })
780                    .collect();
781
782                if !expr.ty.is_never() {
783                    this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
784                }
785
786                let asm_macro = match asm_macro {
787                    AsmMacro::Asm | AsmMacro::GlobalAsm => InlineAsmMacro::Asm,
788                    AsmMacro::NakedAsm => InlineAsmMacro::NakedAsm,
789                };
790
791                this.cfg.terminate(
792                    block,
793                    source_info,
794                    TerminatorKind::InlineAsm {
795                        asm_macro,
796                        template,
797                        operands,
798                        options,
799                        line_spans,
800                        targets: targets.into_boxed_slice(),
801                        unwind: if options.contains(InlineAsmOptions::MAY_UNWIND) {
802                            UnwindAction::Continue
803                        } else {
804                            UnwindAction::Unreachable
805                        },
806                    },
807                );
808                if options.contains(InlineAsmOptions::MAY_UNWIND) {
809                    this.diverge_from(block);
810                }
811                destination_block.unit()
812            }
813
814            // These cases don't actually need a destination
815            ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
816                block = this.stmt_expr(block, expr_id, None).into_block();
817                this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
818                block.unit()
819            }
820
821            ExprKind::Continue { .. }
822            | ExprKind::ConstContinue { .. }
823            | ExprKind::Break { .. }
824            | ExprKind::Return { .. }
825            | ExprKind::Become { .. } => {
826                block = this.stmt_expr(block, expr_id, None).into_block();
827                // No assign, as these have type `!`.
828                block.unit()
829            }
830
831            // Avoid creating a temporary
832            ExprKind::VarRef { .. }
833            | ExprKind::UpvarRef { .. }
834            | ExprKind::PlaceTypeAscription { .. }
835            | ExprKind::ValueTypeAscription { .. }
836            | ExprKind::PlaceUnwrapUnsafeBinder { .. }
837            | ExprKind::ValueUnwrapUnsafeBinder { .. } => {
838                debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
839
840                let place = unpack!(block = this.as_place(block, expr_id));
841                let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place), WithRetag::Yes);
842                this.cfg.push_assign(block, source_info, destination, rvalue);
843                block.unit()
844            }
845            ExprKind::Index { .. } | ExprKind::Deref { .. } | ExprKind::Field { .. } => {
846                debug_assert_eq!(Category::of(&expr.kind), Some(Category::Place));
847
848                // Create a "fake" temporary variable so that we check that the
849                // value is Sized. Usually, this is caught in type checking, but
850                // in the case of box expr there is no such check.
851                if !destination.projection.is_empty() {
852                    this.local_decls.push(LocalDecl::new(expr.ty, expr.span));
853                }
854
855                let place = unpack!(block = this.as_place(block, expr_id));
856                let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place), WithRetag::Yes);
857                this.cfg.push_assign(block, source_info, destination, rvalue);
858                block.unit()
859            }
860
861            ExprKind::Yield { value } => {
862                let scope = this.local_temp_lifetime();
863                let value = unpack!(
864                    block =
865                        this.as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No)
866                );
867                let resume = this.cfg.start_new_block();
868                this.cfg.terminate(
869                    block,
870                    source_info,
871                    TerminatorKind::Yield { value, resume, resume_arg: destination, drop: None },
872                );
873                this.coroutine_drop_cleanup(block);
874                resume.unit()
875            }
876
877            // these are the cases that are more naturally handled by some other mode
878            ExprKind::Unary { .. }
879            | ExprKind::Binary { .. }
880            | ExprKind::Cast { .. }
881            | ExprKind::PointerCoercion { .. }
882            | ExprKind::Repeat { .. }
883            | ExprKind::Array { .. }
884            | ExprKind::Tuple { .. }
885            | ExprKind::Closure { .. }
886            | ExprKind::ConstBlock { .. }
887            | ExprKind::Literal { .. }
888            | ExprKind::NamedConst { .. }
889            | ExprKind::NonHirLiteral { .. }
890            | ExprKind::ZstLiteral { .. }
891            | ExprKind::ConstParam { .. }
892            | ExprKind::ThreadLocalRef(_)
893            | ExprKind::StaticRef { .. }
894            | ExprKind::WrapUnsafeBinder { .. } => {
895                debug_assert!(match Category::of(&expr.kind).unwrap() {
896                    // should be handled above
897                    Category::Rvalue(RvalueFunc::Into) => false,
898
899                    // must be handled above or else we get an
900                    // infinite loop in the builder; see
901                    // e.g., `ExprKind::VarRef` above
902                    Category::Place => false,
903
904                    _ => true,
905                });
906
907                let rvalue = unpack!(block = this.as_local_rvalue(block, expr_id));
908                this.cfg.push_assign(block, source_info, destination, rvalue);
909                block.unit()
910            }
911        };
912
913        if !expr_is_block_or_scope {
914            let popped = this.block_context.pop();
915            assert!(popped.is_some());
916        }
917
918        block_and
919    }
920
921    fn is_let(&self, expr: ExprId) -> bool {
922        match self.thir[expr].kind {
923            ExprKind::Let { .. } => true,
924            ExprKind::Scope { value, .. } => self.is_let(value),
925            _ => false,
926        }
927    }
928}