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