1use rustc_abi::FieldIdx;
4use rustc_ast::{AsmMacro, InlineAsmOptions};
5use rustc_data_structures::fx::FxHashMap;
6use rustc_hir as hir;
7use rustc_hir::attrs::lang_items::LangItem;
8use rustc_middle::mir::*;
9use rustc_middle::span_bug;
10use rustc_middle::thir::*;
11use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty};
12use rustc_span::{DUMMY_SP, Spanned, sym};
13use rustc_trait_selection::infer::InferCtxtExt;
14use tracing::{debug, instrument};
15
16use crate::builder::expr::category::{Category, RvalueFunc};
17use crate::builder::matches::{DeclareLetBindings, Exhaustive, HasMatchGuard};
18use crate::builder::scope::LintLevel;
19use crate::builder::{BlockAnd, BlockAndExtension, BlockFrame, Builder, NeedsTemporary};
20use crate::diagnostics::{LoopMatchArmWithGuard, LoopMatchUnsupportedType};
21
22impl<'a, 'tcx> Builder<'a, 'tcx> {
23 #[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(25u32),
::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::expr::into"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("destination")
}> =
::tracing::__macro_support::FieldName::new("destination");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("block")
}> =
::tracing::__macro_support::FieldName::new("block");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("expr_id")
}> =
::tracing::__macro_support::FieldName::new("expr_id");
NAME.as_str()
}], ::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&destination)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&block)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expr_id)
as &dyn ::tracing::field::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);
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();
let goto =
this.cfg.goto(body_block_end, source_info, loop_block);
if let Some(attrs) = this.thir.attributes.get(&expr_id) {
goto.attributes = attrs.clone();
}
None
})
}
ExprKind::LoopMatch {
state,
region_scope,
match_data: LoopMatchMatchData {
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,
Exhaustive::Yes);
let state_place = scrutinee_place_builder.to_place(this);
let state_result = this.temp(state_ty, expr_span);
let state_result_place = Place::from(state_result);
{
let BlockAnd(b, v) =
this.in_scope((region_scope, source_info),
LintLevel::Inherited,
move |this|
{
this.in_const_continuable_scope(arms.clone(),
built_tree.clone(), state_place, expr_span,
|this|
{
let block =
this.in_breakable_scope(None, state_result_place, expr_span,
|this|
{
Some(this.lower_match_arms(state_result_place,
scrutinee_place_builder, scrutinee_span, arms, built_tree,
this.source_info(match_span)))
}).into_block();
this.cfg.push_assign(block, source_info, state_place,
Rvalue::Use(this.consume_by_copy_or_move(state_result_place),
WithRetag::Yes));
block.unit()
})
});
body_block = b;
v
};
this.cfg.goto(body_block, source_info, loop_block);
None
})
}
ExprKind::Call { ty, fun, ref args, .. } if
let ty::FnDef(def_id, generic_args) = *ty.kind() &&
let Some(intrinsic) = this.tcx.intrinsic(def_id) &&
#[allow(non_exhaustive_omitted_patterns)] match intrinsic.name
{
sym::write_via_move | sym::write_box_via_move => true,
_ => false,
} => {
let generic_args = generic_args.no_bound_vars().unwrap();
let _fun =
{
let BlockAnd(b, v) = this.as_local_operand(block, fun);
block = b;
v
};
match intrinsic.name {
sym::write_via_move => {
if !destination.ty(&this.local_decls, this.tcx).ty.is_unit()
{
::core::panicking::panic("assertion failed: destination.ty(&this.local_decls, this.tcx).ty.is_unit()")
};
let [ptr, val] =
**args else {
::rustc_middle::util::bug::span_bug_fmt(expr_span,
format_args!("invalid write_via_move call"))
};
let Some(ptr) =
{
let BlockAnd(b, v) = this.as_local_operand(block, ptr);
block = b;
v
}.place() else {
::rustc_middle::util::bug::span_bug_fmt(expr_span,
format_args!("invalid write_via_move call"))
};
let ptr_deref =
ptr.project_deeper(&[ProjectionElem::Deref], this.tcx);
this.expr_into_dest(ptr_deref, block, val)
}
sym::write_box_via_move => {
let [b, val] =
**args else {
::rustc_middle::util::bug::span_bug_fmt(expr_span,
format_args!("invalid init_box_via_move call"))
};
let Some(b) =
{
let BlockAnd(b, v) = this.as_local_operand(block, b);
block = b;
v
}.place() else {
::rustc_middle::util::bug::span_bug_fmt(expr_span,
format_args!("invalid init_box_via_move call"))
};
let tcx = this.tcx;
let decls = &this.local_decls;
let place = b.project_deeper(&[ProjectionElem::Deref], tcx);
let place =
place.project_to_field(FieldIdx::from_u32(1), decls, tcx);
let place =
place.project_to_field(FieldIdx::ZERO, decls, tcx);
let place =
place.project_to_field(FieldIdx::ZERO, decls, tcx);
{
match (&place.ty(decls, tcx).ty, &generic_args.type_at(0)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
{
let BlockAnd(b, v) = this.expr_into_dest(place, block, val);
block = b;
v
};
this.cfg.push_assign(block, source_info, destination,
Rvalue::Use(Operand::Move(b), WithRetag::Yes));
block.unit()
}
_ =>
::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached")),
}
}
ExprKind::Call {
ty: _, fun, ref args, from_hir_call, fn_span } => {
let fun =
{
let BlockAnd(b, v) = this.as_local_operand(block, fun);
block = b;
v
};
let args: Box<[_]> =
args.into_iter().copied().map(|arg|
Spanned {
node: {
let BlockAnd(b, v) = this.as_local_call_operand(block, arg);
block = b;
v
},
span: this.thir.exprs[arg].span,
}).collect();
let success = this.cfg.start_new_block();
this.record_operands_moved(&args);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/expr/into.rs:493",
"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(493u32),
::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};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("expr_into_dest: fn_span={0:?}",
fn_span) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
this.cfg.terminate(block, source_info,
TerminatorKind::Call {
func: fun,
args,
unwind: UnwindAction::Continue,
destination,
target: Some(success),
call_source: if from_hir_call {
CallSource::Normal
} else { CallSource::OverloadedOperator },
fn_span,
});
this.diverge_from(block);
success.unit()
}
ExprKind::ByUse { expr, span } => {
let place =
{
let BlockAnd(b, v) = this.as_place(block, expr);
block = b;
v
};
let ty = place.ty(&this.local_decls, this.tcx).ty;
if this.tcx.type_is_copy_modulo_regions(this.infcx.typing_env(this.param_env),
ty) {
this.cfg.push_assign(block, source_info, destination,
Rvalue::Use(Operand::Copy(place), WithRetag::Yes));
block.unit()
} else if this.infcx.type_is_use_cloned_modulo_regions(this.param_env,
ty) {
let success = this.cfg.start_new_block();
let clone_trait =
this.tcx.require_lang_item(LangItem::Clone, span);
let clone_fn =
this.tcx.associated_item_def_ids(clone_trait)[0];
let func =
Operand::function_handle(this.tcx, clone_fn, &[ty.into()],
expr_span);
let ref_ty =
Ty::new_imm_ref(this.tcx, this.tcx.lifetimes.re_erased, ty);
let ref_place = this.temp(ref_ty, span);
this.cfg.push_assign(block, source_info, ref_place,
Rvalue::Ref(this.tcx.lifetimes.re_erased,
BorrowKind::Shared, place));
this.cfg.terminate(block, source_info,
TerminatorKind::Call {
func,
args: [Spanned {
node: Operand::Move(ref_place),
span: DUMMY_SP,
}].into(),
destination,
target: Some(success),
unwind: UnwindAction::Unreachable,
call_source: CallSource::Use,
fn_span: expr_span,
});
success.unit()
} else {
this.cfg.push_assign(block, source_info, destination,
Rvalue::Use(Operand::Move(place), WithRetag::Yes));
block.unit()
}
}
ExprKind::Use { source } =>
this.expr_into_dest(destination, block, source),
ExprKind::Borrow { arg, borrow_kind } => {
let arg_place =
match borrow_kind {
BorrowKind::Shared => {
{
let BlockAnd(b, v) = this.as_read_only_place(block, arg);
block = b;
v
}
}
_ => {
let BlockAnd(b, v) = this.as_place(block, arg);
block = b;
v
}
};
let borrow =
Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind,
arg_place);
this.cfg.push_assign(block, source_info, destination,
borrow);
block.unit()
}
ExprKind::RawBorrow { mutability, arg } => {
let place =
match mutability {
hir::Mutability::Not => this.as_read_only_place(block, arg),
hir::Mutability::Mut => this.as_place(block, arg),
};
let address_of =
Rvalue::RawPtr(mutability.into(),
{ let BlockAnd(b, v) = place; block = b; v });
this.cfg.push_assign(block, source_info, destination,
address_of);
block.unit()
}
ExprKind::Adt(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(InlineAsmExpr {
asm_macro, template, ref operands, options, line_spans }) =>
{
use rustc_middle::{mir, thir};
let destination_block = this.cfg.start_new_block();
let mut targets =
if asm_macro.diverges(options) {
::alloc::vec::Vec::new()
} else {
::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[destination_block]))
};
let operands =
operands.into_iter().map(|op|
match *op {
thir::InlineAsmOperand::In { reg, expr } =>
mir::InlineAsmOperand::In {
reg,
value: {
let BlockAnd(b, v) = this.as_local_operand(block, expr);
block = b;
v
},
},
thir::InlineAsmOperand::Out { reg, late, expr } => {
mir::InlineAsmOperand::Out {
reg,
late,
place: expr.map(|expr|
{
let BlockAnd(b, v) = this.as_place(block, expr);
block = b;
v
}),
}
}
thir::InlineAsmOperand::InOut { reg, late, expr } => {
let place =
{
let BlockAnd(b, v) = this.as_place(block, expr);
block = b;
v
};
mir::InlineAsmOperand::InOut {
reg,
late,
in_value: Operand::Copy(place),
out_place: Some(place),
}
}
thir::InlineAsmOperand::SplitInOut {
reg, late, in_expr, out_expr } => {
mir::InlineAsmOperand::InOut {
reg,
late,
in_value: {
let BlockAnd(b, v) = this.as_local_operand(block, in_expr);
block = b;
v
},
out_place: out_expr.map(|out_expr|
{
{
let BlockAnd(b, v) = this.as_place(block, out_expr);
block = b;
v
}
}),
}
}
thir::InlineAsmOperand::Const { value, span } => {
mir::InlineAsmOperand::Const {
value: Box::new(ConstOperand {
span,
user_ty: None,
const_: value,
}),
}
}
thir::InlineAsmOperand::SymFn { value } =>
mir::InlineAsmOperand::SymFn {
value: Box::new(this.as_constant(&this.thir[value])),
},
thir::InlineAsmOperand::SymStatic { def_id } => {
mir::InlineAsmOperand::SymStatic { def_id }
}
thir::InlineAsmOperand::Label { block } => {
let target = this.cfg.start_new_block();
let target_index = targets.len();
targets.push(target);
let tmp = this.get_unit_temp();
let target =
this.ast_block(tmp, target, block,
source_info).into_block();
this.cfg.terminate(target, source_info,
TerminatorKind::Goto { target: destination_block });
mir::InlineAsmOperand::Label { target_index }
}
}).collect();
if !expr.ty.is_never() {
this.cfg.push_assign_unit(block, source_info, destination,
this.tcx);
}
let asm_macro =
match asm_macro {
AsmMacro::Asm | AsmMacro::GlobalAsm => InlineAsmMacro::Asm,
AsmMacro::NakedAsm => InlineAsmMacro::NakedAsm,
};
this.cfg.terminate(block, source_info,
TerminatorKind::InlineAsm {
asm_macro,
template,
operands,
options,
line_spans,
targets: targets.into_boxed_slice(),
unwind: if options.contains(InlineAsmOptions::MAY_UNWIND) {
UnwindAction::Continue
} else { UnwindAction::Unreachable },
});
if options.contains(InlineAsmOptions::MAY_UNWIND) {
this.diverge_from(block);
}
destination_block.unit()
}
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
block = this.stmt_expr(block, expr_id, None).into_block();
this.cfg.push_assign_unit(block, source_info, destination,
this.tcx);
block.unit()
}
ExprKind::Continue { .. } | ExprKind::ConstContinue { .. } |
ExprKind::Break { .. } | ExprKind::Return { .. } |
ExprKind::Become { .. } => {
block = this.stmt_expr(block, expr_id, None).into_block();
block.unit()
}
ExprKind::VarRef { .. } | ExprKind::UpvarRef { .. } |
ExprKind::PlaceTypeAscription { .. } |
ExprKind::ValueTypeAscription { .. } |
ExprKind::PlaceUnwrapUnsafeBinder { .. } |
ExprKind::ValueUnwrapUnsafeBinder { .. } => {
if true {
if !(Category::of(&expr.kind) == Some(Category::Place)) {
::core::panicking::panic("assertion failed: Category::of(&expr.kind) == Some(Category::Place)")
};
};
let place =
{
let BlockAnd(b, v) = this.as_place(block, expr_id);
block = b;
v
};
let rvalue =
Rvalue::Use(this.consume_by_copy_or_move(place),
WithRetag::Yes);
this.cfg.push_assign(block, source_info, destination,
rvalue);
block.unit()
}
ExprKind::Index { .. } | ExprKind::Deref { .. } |
ExprKind::Field { .. } => {
if true {
{
match (&Category::of(&expr.kind), &Some(Category::Place)) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
if !destination.projection.is_empty() {
this.local_decls.push(LocalDecl::new(expr.ty, expr.span));
}
let place =
{
let BlockAnd(b, v) = this.as_place(block, expr_id);
block = b;
v
};
let rvalue =
Rvalue::Use(this.consume_by_copy_or_move(place),
WithRetag::Yes);
this.cfg.push_assign(block, source_info, destination,
rvalue);
block.unit()
}
ExprKind::Yield { value } => {
let scope = this.local_temp_lifetime();
let value =
{
let BlockAnd(b, v) =
this.as_operand(block, scope, value, LocalInfo::Boring,
NeedsTemporary::No);
block = b;
v
};
let resume = this.cfg.start_new_block();
this.cfg.terminate(block, source_info,
TerminatorKind::Yield {
value,
resume,
resume_arg: destination,
drop: None,
});
this.coroutine_drop_cleanup(block);
resume.unit()
}
ExprKind::Unary { .. } | ExprKind::Binary { .. } |
ExprKind::Cast { .. } | ExprKind::PointerCoercion { .. } |
ExprKind::Repeat { .. } | ExprKind::Array { .. } |
ExprKind::Tuple { .. } | ExprKind::Closure { .. } |
ExprKind::ConstBlock { .. } | ExprKind::Literal { .. } |
ExprKind::NamedConst { .. } | ExprKind::NonHirLiteral { .. }
| ExprKind::ZstLiteral { .. } | ExprKind::ConstParam { .. }
| ExprKind::ThreadLocalRef(_) | ExprKind::StaticRef { .. } |
ExprKind::WrapUnsafeBinder { .. } => {
if true {
if !match Category::of(&expr.kind).unwrap() {
Category::Rvalue(RvalueFunc::Into) => false,
Category::Place => false,
_ => true,
} {
::core::panicking::panic("assertion failed: match Category::of(&expr.kind).unwrap() {\n Category::Rvalue(RvalueFunc::Into) => false,\n Category::Place => false,\n _ => true,\n}")
};
};
let rvalue =
{
let BlockAnd(b, v) = this.as_local_rvalue(block, expr_id);
block = b;
v
};
this.cfg.push_assign(block, source_info, destination,
rvalue);
block.unit()
}
ExprKind::Reborrow { source, mutability, target } => {
let place =
{
let BlockAnd(b, v) = this.as_place(block, source);
block = b;
v
};
this.cfg.push_assign(block, source_info, destination,
Rvalue::Reborrow(target, mutability, place));
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))]
26 pub(crate) fn expr_into_dest(
27 &mut self,
28 destination: Place<'tcx>,
29 mut block: BasicBlock,
30 expr_id: ExprId,
31 ) -> BlockAnd<()> {
32 let this = self; let expr = &this.thir[expr_id];
37 let expr_span = expr.span;
38 let source_info = this.source_info(expr_span);
39
40 let expr_is_block_or_scope =
41 matches!(expr.kind, ExprKind::Block { .. } | ExprKind::Scope { .. });
42
43 if !expr_is_block_or_scope {
44 this.block_context.push(BlockFrame::SubExpr);
45 }
46
47 let block_and = match expr.kind {
48 ExprKind::Scope { region_scope, hir_id, value } => {
49 let region_scope = (region_scope, source_info);
50 this.in_scope(region_scope, LintLevel::Explicit(hir_id), |this| {
51 this.expr_into_dest(destination, block, value)
52 })
53 }
54 ExprKind::Block { block: ast_block } => {
55 this.ast_block(destination, block, ast_block, source_info)
56 }
57 ExprKind::Match { scrutinee, ref arms, .. } => this.match_expr(
58 destination,
59 block,
60 scrutinee,
61 arms,
62 expr_span,
63 this.thir[scrutinee].span,
64 ),
65 ExprKind::If { cond, then, else_opt, if_then_scope } => {
66 let then_span = this.thir[then].span;
67 let then_source_info = this.source_info(then_span);
68 let condition_scope = this.local_scope();
69
70 let then_and_else_blocks = this.in_scope(
71 (if_then_scope, then_source_info),
72 LintLevel::Inherited,
73 |this| {
74 let source_info = if this.is_let(cond) {
76 let variable_scope =
77 this.new_source_scope(then_span, LintLevel::Inherited);
78 this.source_scope = variable_scope;
79 SourceInfo { span: then_span, scope: variable_scope }
80 } else {
81 this.source_info(then_span)
82 };
83
84 let (then_block, else_block) =
86 this.in_if_then_scope(condition_scope, then_span, |this| {
87 let then_blk = this
88 .then_else_break(
89 block,
90 cond,
91 Some(condition_scope), source_info,
93 DeclareLetBindings::Yes, )
95 .into_block();
96
97 this.expr_into_dest(destination, then_blk, then)
99 });
100
101 then_block.and(else_block)
103 },
104 );
105
106 let (then_blk, mut else_blk);
108 else_blk = unpack!(then_blk = then_and_else_blocks);
109
110 if let Some(else_expr) = else_opt {
112 else_blk = this.expr_into_dest(destination, else_blk, else_expr).into_block();
113 } else {
114 let correct_si = this.source_info(expr_span.shrink_to_hi());
117 this.cfg.push_assign_unit(else_blk, correct_si, destination, this.tcx);
118 }
119
120 let join_block = this.cfg.start_new_block();
123 this.cfg.goto(then_blk, source_info, join_block);
124 this.cfg.goto(else_blk, source_info, join_block);
125 join_block.unit()
126 }
127 ExprKind::Let { .. } => {
128 span_bug!(expr_span, "unexpected let expression outside of if or match-guard");
133 }
134 ExprKind::NeverToAny { source } => {
135 let source_expr = &this.thir[source];
136 let is_call =
137 matches!(source_expr.kind, ExprKind::Call { .. } | ExprKind::InlineAsm { .. });
138
139 unpack!(
142 block =
143 this.as_temp(block, this.local_temp_lifetime(), source, Mutability::Mut)
144 );
145
146 if is_call {
149 block.unit()
150 } else {
151 this.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
152 let end_block = this.cfg.start_new_block();
153 end_block.unit()
154 }
155 }
156 ExprKind::LogicalOp { op, lhs, rhs } => {
157 let condition_scope = this.local_scope();
158 let source_info = this.source_info(expr.span);
159
160 let (then_block, else_block) =
162 this.in_if_then_scope(condition_scope, expr.span, |this| {
163 this.then_else_break(
164 block,
165 lhs,
166 Some(condition_scope), source_info,
168 DeclareLetBindings::LetNotPermitted,
171 )
172 });
173 let (short_circuit, continuation, constant) = match op {
174 LogicalOp::And => (else_block, then_block, false),
175 LogicalOp::Or => (then_block, else_block, true),
176 };
177 this.cfg.push_assign_constant(
184 short_circuit,
185 source_info,
186 destination,
187 ConstOperand {
188 span: expr.span,
189 user_ty: None,
190 const_: Const::from_bool(this.tcx, constant),
191 },
192 );
193 let mut rhs_block =
194 this.expr_into_dest(destination, continuation, rhs).into_block();
195 this.visit_coverage_standalone_condition(rhs, destination, &mut rhs_block);
198
199 let target = this.cfg.start_new_block();
200 this.cfg.goto(rhs_block, source_info, target);
201 this.cfg.goto(short_circuit, source_info, target);
202 target.unit()
203 }
204 ExprKind::Loop { body } => {
205 let loop_block = this.cfg.start_new_block();
216
217 this.cfg.goto(block, source_info, loop_block);
219
220 this.in_breakable_scope(Some(loop_block), destination, expr_span, move |this| {
221 let body_block = this.cfg.start_new_block();
223 this.cfg.terminate(
224 loop_block,
225 source_info,
226 TerminatorKind::FalseUnwind {
227 real_target: body_block,
228 unwind: UnwindAction::Continue,
229 },
230 );
231 this.diverge_from(loop_block);
232
233 let tmp = this.get_unit_temp();
236 let body_block_end = this.expr_into_dest(tmp, body_block, body).into_block();
238
239 let goto = this.cfg.goto(body_block_end, source_info, loop_block);
240 if let Some(attrs) = this.thir.attributes.get(&expr_id) {
241 goto.attributes = attrs.clone();
242 }
243
244 None
246 })
247 }
248 ExprKind::LoopMatch {
249 state,
250 region_scope,
251 match_data: LoopMatchMatchData { ref arms, span: match_span, scrutinee },
252 } => {
253 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 this.cfg.goto(block, source_info, loop_block);
283
284 this.in_breakable_scope(Some(loop_block), destination, expr_span, |this| {
285 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 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 let built_tree = this.lower_match_tree(
321 body_block,
322 scrutinee_span,
323 &scrutinee_place_builder,
324 match_start_span,
325 patterns,
326 Exhaustive::Yes,
327 );
328
329 let state_place = scrutinee_place_builder.to_place(this);
330 let state_result = this.temp(state_ty, expr_span);
331 let state_result_place = Place::from(state_result);
332
333 unpack!(
346 body_block = this.in_scope(
347 (region_scope, source_info),
348 LintLevel::Inherited,
349 move |this| {
350 this.in_const_continuable_scope(
351 arms.clone(),
352 built_tree.clone(),
353 state_place,
354 expr_span,
355 |this| {
356 let block = this
357 .in_breakable_scope(
358 None,
359 state_result_place,
360 expr_span,
361 |this| {
362 Some(this.lower_match_arms(
363 state_result_place,
364 scrutinee_place_builder,
365 scrutinee_span,
366 arms,
367 built_tree,
368 this.source_info(match_span),
369 ))
370 },
371 )
372 .into_block();
373
374 this.cfg.push_assign(
375 block,
376 source_info,
377 state_place,
378 Rvalue::Use(
379 this.consume_by_copy_or_move(state_result_place),
380 WithRetag::Yes,
381 ),
382 );
383 block.unit()
384 },
385 )
386 }
387 )
388 );
389
390 this.cfg.goto(body_block, source_info, loop_block);
391
392 None
394 })
395 }
396 ExprKind::Call { ty, fun, ref args, .. }
399 if let ty::FnDef(def_id, generic_args) = *ty.kind()
400 && let Some(intrinsic) = this.tcx.intrinsic(def_id)
401 && matches!(intrinsic.name, sym::write_via_move | sym::write_box_via_move) =>
402 {
403 let generic_args = generic_args.no_bound_vars().unwrap();
404 let _fun = unpack!(block = this.as_local_operand(block, fun));
407
408 match intrinsic.name {
409 sym::write_via_move => {
410 assert!(destination.ty(&this.local_decls, this.tcx).ty.is_unit());
415
416 let [ptr, val] = **args else {
418 span_bug!(expr_span, "invalid write_via_move call")
419 };
420 let Some(ptr) = unpack!(block = this.as_local_operand(block, ptr)).place()
421 else {
422 span_bug!(expr_span, "invalid write_via_move call")
423 };
424 let ptr_deref = ptr.project_deeper(&[ProjectionElem::Deref], this.tcx);
425 this.expr_into_dest(ptr_deref, block, val)
426 }
427 sym::write_box_via_move => {
428 let [b, val] = **args else {
442 span_bug!(expr_span, "invalid init_box_via_move call")
443 };
444 let Some(b) = unpack!(block = this.as_local_operand(block, b)).place()
445 else {
446 span_bug!(expr_span, "invalid init_box_via_move call")
447 };
448 let tcx = this.tcx;
449 let decls = &this.local_decls;
450
451 let place = b.project_deeper(&[ProjectionElem::Deref], tcx);
453 let place = place.project_to_field(FieldIdx::from_u32(1), decls, tcx);
455 let place = place.project_to_field(FieldIdx::ZERO, decls, tcx);
457 let place = place.project_to_field(FieldIdx::ZERO, decls, tcx);
459 assert_eq!(place.ty(decls, tcx).ty, generic_args.type_at(0));
461
462 unpack!(block = this.expr_into_dest(place, block, val));
464
465 this.cfg.push_assign(
467 block,
468 source_info,
469 destination,
470 Rvalue::Use(Operand::Move(b), WithRetag::Yes),
472 );
473 block.unit()
474 }
475 _ => rustc_middle::bug!(),
476 }
477 }
478 ExprKind::Call { ty: _, fun, ref args, from_hir_call, fn_span } => {
479 let fun = unpack!(block = this.as_local_operand(block, fun));
480 let args: Box<[_]> = args
481 .into_iter()
482 .copied()
483 .map(|arg| Spanned {
484 node: unpack!(block = this.as_local_call_operand(block, arg)),
485 span: this.thir.exprs[arg].span,
486 })
487 .collect();
488
489 let success = this.cfg.start_new_block();
490
491 this.record_operands_moved(&args);
492
493 debug!("expr_into_dest: fn_span={:?}", fn_span);
494
495 this.cfg.terminate(
496 block,
497 source_info,
498 TerminatorKind::Call {
499 func: fun,
500 args,
501 unwind: UnwindAction::Continue,
502 destination,
503 target: Some(success),
504 call_source: if from_hir_call {
505 CallSource::Normal
506 } else {
507 CallSource::OverloadedOperator
508 },
509 fn_span,
510 },
511 );
512 this.diverge_from(block);
513 success.unit()
514 }
515 ExprKind::ByUse { expr, span } => {
516 let place = unpack!(block = this.as_place(block, expr));
517 let ty = place.ty(&this.local_decls, this.tcx).ty;
518
519 if this.tcx.type_is_copy_modulo_regions(this.infcx.typing_env(this.param_env), ty) {
520 this.cfg.push_assign(
521 block,
522 source_info,
523 destination,
524 Rvalue::Use(Operand::Copy(place), WithRetag::Yes),
525 );
526 block.unit()
527 } else if this.infcx.type_is_use_cloned_modulo_regions(this.param_env, ty) {
528 let success = this.cfg.start_new_block();
530 let clone_trait = this.tcx.require_lang_item(LangItem::Clone, span);
531 let clone_fn = this.tcx.associated_item_def_ids(clone_trait)[0];
532 let func =
533 Operand::function_handle(this.tcx, clone_fn, &[ty.into()], expr_span);
534 let ref_ty = Ty::new_imm_ref(this.tcx, this.tcx.lifetimes.re_erased, ty);
535 let ref_place = this.temp(ref_ty, span);
536 this.cfg.push_assign(
537 block,
538 source_info,
539 ref_place,
540 Rvalue::Ref(this.tcx.lifetimes.re_erased, BorrowKind::Shared, place),
541 );
542 this.cfg.terminate(
543 block,
544 source_info,
545 TerminatorKind::Call {
546 func,
547 args: [Spanned { node: Operand::Move(ref_place), span: DUMMY_SP }]
548 .into(),
549 destination,
550 target: Some(success),
551 unwind: UnwindAction::Unreachable,
552 call_source: CallSource::Use,
553 fn_span: expr_span,
554 },
555 );
556 success.unit()
557 } else {
558 this.cfg.push_assign(
559 block,
560 source_info,
561 destination,
562 Rvalue::Use(Operand::Move(place), WithRetag::Yes),
563 );
564 block.unit()
565 }
566 }
567 ExprKind::Use { source } => this.expr_into_dest(destination, block, source),
568 ExprKind::Borrow { arg, borrow_kind } => {
569 let arg_place = match borrow_kind {
575 BorrowKind::Shared => {
576 unpack!(block = this.as_read_only_place(block, arg))
577 }
578 _ => unpack!(block = this.as_place(block, arg)),
579 };
580 let borrow = Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place);
581 this.cfg.push_assign(block, source_info, destination, borrow);
582 block.unit()
583 }
584 ExprKind::RawBorrow { mutability, arg } => {
585 let place = match mutability {
586 hir::Mutability::Not => this.as_read_only_place(block, arg),
587 hir::Mutability::Mut => this.as_place(block, arg),
588 };
589 let address_of = Rvalue::RawPtr(mutability.into(), unpack!(block = place));
590 this.cfg.push_assign(block, source_info, destination, address_of);
591 block.unit()
592 }
593 ExprKind::Adt(AdtExpr {
594 adt_def,
595 variant_index,
596 args,
597 ref user_ty,
598 ref fields,
599 ref base,
600 }) => {
601 let is_union = adt_def.is_union();
604 let active_field_index = is_union.then(|| fields[0].name);
605
606 let scope = this.local_temp_lifetime();
607
608 let fields_map: FxHashMap<_, _> = fields
611 .into_iter()
612 .map(|f| {
613 (
614 f.name,
615 unpack!(
616 block = this.as_operand(
617 block,
618 scope,
619 f.expr,
620 LocalInfo::AggregateTemp,
621 NeedsTemporary::Maybe,
622 )
623 ),
624 )
625 })
626 .collect();
627
628 let variant = adt_def.variant(variant_index);
629 let field_names = variant.fields.indices();
630
631 let fields = match base {
632 AdtExprBase::None => {
633 field_names.filter_map(|n| fields_map.get(&n).cloned()).collect()
634 }
635 AdtExprBase::Base(FruInfo { base, field_types }) => {
636 let place_builder = unpack!(block = this.as_place_builder(block, *base));
637
638 itertools::zip_eq(field_names, &**field_types)
642 .map(|(n, ty)| match fields_map.get(&n) {
643 Some(v) => v.clone(),
644 None => {
645 let place =
646 place_builder.clone_project(PlaceElem::Field(n, *ty));
647 this.consume_by_copy_or_move(place.to_place(this))
648 }
649 })
650 .collect()
651 }
652 AdtExprBase::DefaultFields(field_types) => {
653 itertools::zip_eq(field_names, field_types)
654 .map(|(n, &ty)| match fields_map.get(&n) {
655 Some(v) => v.clone(),
656 None => match variant.fields[n].value {
657 Some(def) => {
658 let value = Const::Unevaluated(
659 UnevaluatedConst::new(def, args),
660 ty,
661 );
662 Operand::Constant(Box::new(ConstOperand {
663 span: expr_span,
664 user_ty: None,
665 const_: value,
666 }))
667 }
668 None => {
669 let name = variant.fields[n].name;
670 span_bug!(
671 expr_span,
672 "missing mandatory field `{name}` of type `{ty}`",
673 );
674 }
675 },
676 })
677 .collect()
678 }
679 };
680
681 let inferred_ty = expr.ty;
682 let user_ty = user_ty.as_ref().map(|user_ty| {
683 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
684 span: source_info.span,
685 user_ty: user_ty.clone(),
686 inferred_ty,
687 })
688 });
689 let adt = Box::new(AggregateKind::Adt(
690 adt_def.did(),
691 variant_index,
692 args,
693 user_ty,
694 active_field_index,
695 ));
696 this.cfg.push_assign(
697 block,
698 source_info,
699 destination,
700 Rvalue::Aggregate(adt, fields),
701 );
702 block.unit()
703 }
704 ExprKind::InlineAsm(InlineAsmExpr {
705 asm_macro,
706 template,
707 ref operands,
708 options,
709 line_spans,
710 }) => {
711 use rustc_middle::{mir, thir};
712
713 let destination_block = this.cfg.start_new_block();
714 let mut targets =
715 if asm_macro.diverges(options) { vec![] } else { vec![destination_block] };
716
717 let operands = operands
718 .into_iter()
719 .map(|op| match *op {
720 thir::InlineAsmOperand::In { reg, expr } => mir::InlineAsmOperand::In {
721 reg,
722 value: unpack!(block = this.as_local_operand(block, expr)),
723 },
724 thir::InlineAsmOperand::Out { reg, late, expr } => {
725 mir::InlineAsmOperand::Out {
726 reg,
727 late,
728 place: expr.map(|expr| unpack!(block = this.as_place(block, expr))),
729 }
730 }
731 thir::InlineAsmOperand::InOut { reg, late, expr } => {
732 let place = unpack!(block = this.as_place(block, expr));
733 mir::InlineAsmOperand::InOut {
734 reg,
735 late,
736 in_value: Operand::Copy(place),
738 out_place: Some(place),
739 }
740 }
741 thir::InlineAsmOperand::SplitInOut { reg, late, in_expr, out_expr } => {
742 mir::InlineAsmOperand::InOut {
743 reg,
744 late,
745 in_value: unpack!(block = this.as_local_operand(block, in_expr)),
746 out_place: out_expr.map(|out_expr| {
747 unpack!(block = this.as_place(block, out_expr))
748 }),
749 }
750 }
751 thir::InlineAsmOperand::Const { value, span } => {
752 mir::InlineAsmOperand::Const {
753 value: Box::new(ConstOperand {
754 span,
755 user_ty: None,
756 const_: value,
757 }),
758 }
759 }
760 thir::InlineAsmOperand::SymFn { value } => mir::InlineAsmOperand::SymFn {
761 value: Box::new(this.as_constant(&this.thir[value])),
762 },
763 thir::InlineAsmOperand::SymStatic { def_id } => {
764 mir::InlineAsmOperand::SymStatic { def_id }
765 }
766 thir::InlineAsmOperand::Label { block } => {
767 let target = this.cfg.start_new_block();
768 let target_index = targets.len();
769 targets.push(target);
770
771 let tmp = this.get_unit_temp();
772 let target =
773 this.ast_block(tmp, target, block, source_info).into_block();
774 this.cfg.terminate(
775 target,
776 source_info,
777 TerminatorKind::Goto { target: destination_block },
778 );
779
780 mir::InlineAsmOperand::Label { target_index }
781 }
782 })
783 .collect();
784
785 if !expr.ty.is_never() {
786 this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
787 }
788
789 let asm_macro = match asm_macro {
790 AsmMacro::Asm | AsmMacro::GlobalAsm => InlineAsmMacro::Asm,
791 AsmMacro::NakedAsm => InlineAsmMacro::NakedAsm,
792 };
793
794 this.cfg.terminate(
795 block,
796 source_info,
797 TerminatorKind::InlineAsm {
798 asm_macro,
799 template,
800 operands,
801 options,
802 line_spans,
803 targets: targets.into_boxed_slice(),
804 unwind: if options.contains(InlineAsmOptions::MAY_UNWIND) {
805 UnwindAction::Continue
806 } else {
807 UnwindAction::Unreachable
808 },
809 },
810 );
811 if options.contains(InlineAsmOptions::MAY_UNWIND) {
812 this.diverge_from(block);
813 }
814 destination_block.unit()
815 }
816
817 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
819 block = this.stmt_expr(block, expr_id, None).into_block();
820 this.cfg.push_assign_unit(block, source_info, destination, this.tcx);
821 block.unit()
822 }
823
824 ExprKind::Continue { .. }
825 | ExprKind::ConstContinue { .. }
826 | ExprKind::Break { .. }
827 | ExprKind::Return { .. }
828 | ExprKind::Become { .. } => {
829 block = this.stmt_expr(block, expr_id, None).into_block();
830 block.unit()
832 }
833
834 ExprKind::VarRef { .. }
836 | ExprKind::UpvarRef { .. }
837 | ExprKind::PlaceTypeAscription { .. }
838 | ExprKind::ValueTypeAscription { .. }
839 | ExprKind::PlaceUnwrapUnsafeBinder { .. }
840 | ExprKind::ValueUnwrapUnsafeBinder { .. } => {
841 debug_assert!(Category::of(&expr.kind) == Some(Category::Place));
842
843 let place = unpack!(block = this.as_place(block, expr_id));
844 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place), WithRetag::Yes);
845 this.cfg.push_assign(block, source_info, destination, rvalue);
846 block.unit()
847 }
848 ExprKind::Index { .. } | ExprKind::Deref { .. } | ExprKind::Field { .. } => {
849 debug_assert_eq!(Category::of(&expr.kind), Some(Category::Place));
850
851 if !destination.projection.is_empty() {
855 this.local_decls.push(LocalDecl::new(expr.ty, expr.span));
856 }
857
858 let place = unpack!(block = this.as_place(block, expr_id));
859 let rvalue = Rvalue::Use(this.consume_by_copy_or_move(place), WithRetag::Yes);
860 this.cfg.push_assign(block, source_info, destination, rvalue);
861 block.unit()
862 }
863
864 ExprKind::Yield { value } => {
865 let scope = this.local_temp_lifetime();
866 let value = unpack!(
867 block =
868 this.as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No)
869 );
870 let resume = this.cfg.start_new_block();
871 this.cfg.terminate(
872 block,
873 source_info,
874 TerminatorKind::Yield { value, resume, resume_arg: destination, drop: None },
875 );
876 this.coroutine_drop_cleanup(block);
877 resume.unit()
878 }
879
880 ExprKind::Unary { .. }
882 | ExprKind::Binary { .. }
883 | ExprKind::Cast { .. }
884 | ExprKind::PointerCoercion { .. }
885 | ExprKind::Repeat { .. }
886 | ExprKind::Array { .. }
887 | ExprKind::Tuple { .. }
888 | ExprKind::Closure { .. }
889 | ExprKind::ConstBlock { .. }
890 | ExprKind::Literal { .. }
891 | ExprKind::NamedConst { .. }
892 | ExprKind::NonHirLiteral { .. }
893 | ExprKind::ZstLiteral { .. }
894 | ExprKind::ConstParam { .. }
895 | ExprKind::ThreadLocalRef(_)
896 | ExprKind::StaticRef { .. }
897 | ExprKind::WrapUnsafeBinder { .. } => {
898 debug_assert!(match Category::of(&expr.kind).unwrap() {
899 Category::Rvalue(RvalueFunc::Into) => false,
901
902 Category::Place => false,
906
907 _ => true,
908 });
909
910 let rvalue = unpack!(block = this.as_local_rvalue(block, expr_id));
911 this.cfg.push_assign(block, source_info, destination, rvalue);
912 block.unit()
913 }
914 ExprKind::Reborrow { source, mutability, target } => {
915 let place = unpack!(block = this.as_place(block, source));
916 this.cfg.push_assign(
917 block,
918 source_info,
919 destination,
920 Rvalue::Reborrow(target, mutability, place),
921 );
922 block.unit()
923 }
924 };
925
926 if !expr_is_block_or_scope {
927 let popped = this.block_context.pop();
928 assert!(popped.is_some());
929 }
930
931 block_and
932 }
933
934 fn is_let(&self, expr: ExprId) -> bool {
935 match self.thir[expr].kind {
936 ExprKind::Let { .. } => true,
937 ExprKind::Scope { value, .. } => self.is_let(value),
938 _ => false,
939 }
940 }
941}