Files
rust/compiler/rustc_mir_build/src/build/expr/stmt.rs

180 lines
7.6 KiB
Rust
Raw Normal View History

use crate::build::scope::BreakableTarget;
2019-02-08 06:28:15 +09:00
use crate::build::{BlockAnd, BlockAndExtension, BlockFrame, Builder};
2020-07-21 09:09:27 +00:00
use crate::thir::*;
2020-03-29 16:41:09 +02:00
use rustc_middle::middle::region;
use rustc_middle::mir::*;
impl<'a, 'tcx> Builder<'a, 'tcx> {
2020-07-21 09:09:27 +00:00
/// Builds a block of MIR statements to evaluate the THIR `expr`.
/// If the original expression was an AST statement,
/// (e.g., `some().code(&here());`) then `opt_stmt_span` is the
/// span of that statement (including its semicolon, if any).
/// The scope is used if a statement temporary must be dropped.
crate fn stmt_expr(
&mut self,
mut block: BasicBlock,
expr: &Expr<'_, 'tcx>,
statement_scope: Option<region::Scope>,
) -> BlockAnd<()> {
let this = self;
let expr_span = expr.span;
let source_info = this.source_info(expr.span);
// Handle a number of expressions that don't need a destination at all. This
// avoids needing a mountain of temporary `()` variables.
2021-02-24 21:29:09 +01:00
match &expr.kind {
2019-12-22 17:42:04 -05:00
ExprKind::Scope { region_scope, lint_level, value } => {
2021-02-24 21:29:09 +01:00
this.in_scope((*region_scope, source_info), *lint_level, |this| {
this.stmt_expr(block, &value, statement_scope)
})
}
ExprKind::Assign { lhs, rhs } => {
let lhs_span = lhs.span;
// Note: we evaluate assignments right-to-left. This
// is better for borrowck interaction with overloaded
// operators like x[j] = x[i].
2021-03-03 18:07:57 +01:00
debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr);
this.block_context.push(BlockFrame::SubExpr);
// Generate better code for things that don't need to be
// dropped.
if lhs.ty.needs_drop(this.tcx, this.param_env) {
2021-02-24 21:29:09 +01:00
let rhs = unpack!(block = this.as_local_operand(block, &rhs));
let lhs = unpack!(block = this.as_place(block, &lhs));
2018-09-06 22:34:26 +01:00
unpack!(block = this.build_drop_and_replace(block, lhs_span, lhs, rhs));
} else {
2021-02-24 21:29:09 +01:00
let rhs = unpack!(block = this.as_local_rvalue(block, &rhs));
let lhs = unpack!(block = this.as_place(block, &lhs));
this.cfg.push_assign(block, source_info, lhs, rhs);
}
this.block_context.pop();
block.unit()
}
ExprKind::AssignOp { op, lhs, rhs } => {
// FIXME(#28160) there is an interesting semantics
// question raised here -- should we "freeze" the
// value of the lhs here? I'm inclined to think not,
// since it seems closer to the semantics of the
// overloaded version, which takes `&mut self`. This
// only affects weird things like `x += {x += 1; x}`
// -- is that equal to `x + (x + 1)` or `2*(x+1)`?
let lhs_ty = lhs.ty;
2021-03-03 18:07:57 +01:00
debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr);
this.block_context.push(BlockFrame::SubExpr);
// As above, RTL.
2021-02-24 21:29:09 +01:00
let rhs = unpack!(block = this.as_local_operand(block, &rhs));
let lhs = unpack!(block = this.as_place(block, &lhs));
// we don't have to drop prior contents or anything
// because AssignOp is only legal for Copy types
// (overloaded ops should be desugared into a call).
2018-09-06 22:34:26 +01:00
let result = unpack!(
2021-02-24 21:29:09 +01:00
block = this.build_binary_op(
block,
*op,
expr_span,
lhs_ty,
Operand::Copy(lhs),
rhs
)
2018-09-06 22:34:26 +01:00
);
this.cfg.push_assign(block, source_info, lhs, result);
this.block_context.pop();
block.unit()
}
ExprKind::Continue { label } => {
2021-02-24 21:29:09 +01:00
this.break_scope(block, None, BreakableTarget::Continue(*label), source_info)
}
2021-02-24 21:29:09 +01:00
ExprKind::Break { label, value } => this.break_scope(
block,
value.as_deref(),
BreakableTarget::Break(*label),
source_info,
),
ExprKind::Return { value } => {
2021-02-24 21:29:09 +01:00
this.break_scope(block, value.as_deref(), BreakableTarget::Return, source_info)
}
ExprKind::LlvmInlineAsm { asm, outputs, inputs } => {
2021-03-03 18:07:57 +01:00
debug!("stmt_expr LlvmInlineAsm block_context.push(SubExpr) : {:?}", expr);
this.block_context.push(BlockFrame::SubExpr);
2018-09-06 22:34:26 +01:00
let outputs = outputs
.into_iter()
2021-02-24 21:29:09 +01:00
.map(|output| unpack!(block = this.as_place(block, &output)))
.collect::<Vec<_>>()
.into_boxed_slice();
2018-09-06 22:34:26 +01:00
let inputs = inputs
.into_iter()
.map(|input| {
2021-02-24 21:29:09 +01:00
(input.span, unpack!(block = this.as_local_operand(block, &input)))
2019-12-22 17:42:04 -05:00
})
.collect::<Vec<_>>()
.into_boxed_slice();
2018-09-06 22:34:26 +01:00
this.cfg.push(
block,
Statement {
source_info,
kind: StatementKind::LlvmInlineAsm(box LlvmInlineAsm {
2021-02-24 21:29:09 +01:00
asm: (*asm).clone(),
2018-09-06 22:34:26 +01:00
outputs,
inputs,
}),
},
2018-09-06 22:34:26 +01:00
);
this.block_context.pop();
block.unit()
}
_ => {
assert!(
statement_scope.is_some(),
"Should not be calling `stmt_expr` on a general expression \
without a statement scope",
);
// Issue #54382: When creating temp for the value of
// expression like:
//
// `{ side_effects(); { let l = stuff(); the_value } }`
//
// it is usually better to focus on `the_value` rather
// than the entirety of block(s) surrounding it.
let adjusted_span = (|| {
2021-02-24 21:29:09 +01:00
if let ExprKind::Block { body } = &expr.kind {
if let Some(tail_expr) = &body.expr {
2021-02-24 21:29:09 +01:00
let mut expr = &*tail_expr;
while let ExprKind::Block { body: subblock } = &expr.kind {
if let Some(subtail_expr) = &subblock.expr {
expr = subtail_expr
} else {
break;
}
}
2020-07-21 09:09:27 +00:00
this.block_context.push(BlockFrame::TailExpr {
tail_result_is_ignored: true,
span: expr.span,
});
return Some(expr.span);
}
}
None
})();
2019-12-22 17:42:04 -05:00
let temp =
unpack!(block = this.as_temp(block, statement_scope, expr, Mutability::Not));
if let Some(span) = adjusted_span {
this.local_decls[temp].source_info.span = span;
this.block_context.pop();
}
block.unit()
}
}
}
}