2019-06-15 17:37:19 +01:00
|
|
|
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::*;
|
2016-04-16 17:38:18 +12:00
|
|
|
|
2019-06-01 13:38:36 +02:00
|
|
|
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`.
|
2018-11-08 11:37:27 +01:00
|
|
|
/// If the original expression was an AST statement,
|
2018-11-27 02:59:49 +00:00
|
|
|
/// (e.g., `some().code(&here());`) then `opt_stmt_span` is the
|
2018-11-08 11:37:27 +01:00
|
|
|
/// span of that statement (including its semicolon, if any).
|
2019-06-15 10:08:41 +01:00
|
|
|
/// The scope is used if a statement temporary must be dropped.
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn stmt_expr(
|
2019-06-15 10:08:41 +01:00
|
|
|
&mut self,
|
|
|
|
|
mut block: BasicBlock,
|
2021-02-24 21:29:09 +01:00
|
|
|
expr: &Expr<'tcx>,
|
2019-06-15 10:08:41 +01:00
|
|
|
statement_scope: Option<region::Scope>,
|
|
|
|
|
) -> BlockAnd<()> {
|
2016-04-16 17:38:18 +12:00
|
|
|
let this = self;
|
|
|
|
|
let expr_span = expr.span;
|
2016-06-07 19:21:56 +03:00
|
|
|
let source_info = this.source_info(expr.span);
|
2016-04-16 17:38:18 +12:00
|
|
|
// Handle a number of expressions that don't need a destination at all. This
|
|
|
|
|
// avoids needing a mountain of temporary `()` variables.
|
2018-09-28 15:25:02 +02:00
|
|
|
let expr2 = expr.clone();
|
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)
|
2017-08-31 21:37:38 +03:00
|
|
|
})
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
|
|
|
|
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].
|
|
|
|
|
|
2018-09-28 15:25:02 +02:00
|
|
|
debug!("stmt_expr Assign block_context.push(SubExpr) : {:?}", expr2);
|
2018-09-22 00:51:48 +02:00
|
|
|
this.block_context.push(BlockFrame::SubExpr);
|
|
|
|
|
|
2016-04-16 17:38:18 +12:00
|
|
|
// Generate better code for things that don't need to be
|
|
|
|
|
// dropped.
|
2016-05-17 01:06:52 +03:00
|
|
|
if this.hir.needs_drop(lhs.ty) {
|
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));
|
2016-04-16 17:38:18 +12:00
|
|
|
} 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));
|
2020-03-31 14:08:48 -03:00
|
|
|
this.cfg.push_assign(block, source_info, lhs, rhs);
|
2016-05-17 01:06:52 +03:00
|
|
|
}
|
2018-09-22 00:51:48 +02:00
|
|
|
|
|
|
|
|
this.block_context.pop();
|
|
|
|
|
block.unit()
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
|
|
|
|
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)`?
|
|
|
|
|
|
2016-03-31 18:50:07 +13:00
|
|
|
let lhs_ty = lhs.ty;
|
|
|
|
|
|
2018-09-28 15:25:02 +02:00
|
|
|
debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr2);
|
2018-09-22 00:51:48 +02:00
|
|
|
this.block_context.push(BlockFrame::SubExpr);
|
|
|
|
|
|
2016-04-16 17:38:18 +12:00
|
|
|
// 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));
|
2016-04-16 17:38:18 +12:00
|
|
|
|
|
|
|
|
// 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
|
|
|
);
|
2020-03-31 14:08:48 -03:00
|
|
|
this.cfg.push_assign(block, source_info, lhs, result);
|
2016-04-16 17:38:18 +12:00
|
|
|
|
2018-09-22 00:51:48 +02:00
|
|
|
this.block_context.pop();
|
2016-04-16 17:38:18 +12:00
|
|
|
block.unit()
|
|
|
|
|
}
|
|
|
|
|
ExprKind::Continue { label } => {
|
2021-02-24 21:29:09 +01:00
|
|
|
this.break_scope(block, None, BreakableTarget::Continue(*label), source_info)
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
2021-02-24 21:29:09 +01:00
|
|
|
ExprKind::Break { label, value } => this.break_scope(
|
|
|
|
|
block,
|
|
|
|
|
value.as_deref(),
|
|
|
|
|
BreakableTarget::Break(*label),
|
|
|
|
|
source_info,
|
|
|
|
|
),
|
2016-04-16 17:38:18 +12:00
|
|
|
ExprKind::Return { value } => {
|
2021-02-24 21:29:09 +01:00
|
|
|
this.break_scope(block, value.as_deref(), BreakableTarget::Return, source_info)
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
2020-01-14 13:40:42 +00:00
|
|
|
ExprKind::LlvmInlineAsm { asm, outputs, inputs } => {
|
|
|
|
|
debug!("stmt_expr LlvmInlineAsm block_context.push(SubExpr) : {:?}", expr2);
|
2018-09-22 00:51:48 +02:00
|
|
|
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)))
|
2018-09-24 12:13:23 +10:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.into_boxed_slice();
|
2018-09-06 22:34:26 +01:00
|
|
|
let inputs = inputs
|
|
|
|
|
.into_iter()
|
2018-10-15 00:00:53 +02:00
|
|
|
.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<_>>()
|
2018-09-24 12:13:23 +10:00
|
|
|
.into_boxed_slice();
|
2018-09-06 22:34:26 +01:00
|
|
|
this.cfg.push(
|
|
|
|
|
block,
|
|
|
|
|
Statement {
|
|
|
|
|
source_info,
|
2020-01-14 13:40:42 +00:00
|
|
|
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,
|
2019-04-02 20:07:09 +11:00
|
|
|
}),
|
2017-02-15 21:21:36 +02:00
|
|
|
},
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2018-09-22 00:51:48 +02:00
|
|
|
this.block_context.pop();
|
2017-02-15 21:21:36 +02:00
|
|
|
block.unit()
|
|
|
|
|
}
|
2016-04-16 17:38:18 +12:00
|
|
|
_ => {
|
2019-06-15 10:08:41 +01:00
|
|
|
assert!(
|
|
|
|
|
statement_scope.is_some(),
|
|
|
|
|
"Should not be calling `stmt_expr` on a general expression \
|
|
|
|
|
without a statement scope",
|
|
|
|
|
);
|
2018-11-08 12:39:54 +01:00
|
|
|
|
|
|
|
|
// 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.
|
2019-06-15 10:08:41 +01:00
|
|
|
let adjusted_span = (|| {
|
2021-02-24 21:29:09 +01:00
|
|
|
if let ExprKind::Block { body } = &expr.kind {
|
2019-06-15 10:08:41 +01:00
|
|
|
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 {
|
2019-06-15 10:08:41 +01:00
|
|
|
if let Some(subtail_expr) = &subblock.expr {
|
|
|
|
|
expr = subtail_expr
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-11-08 12:39:54 +01:00
|
|
|
}
|
2020-07-21 09:09:27 +00:00
|
|
|
this.block_context.push(BlockFrame::TailExpr {
|
|
|
|
|
tail_result_is_ignored: true,
|
|
|
|
|
span: expr.span,
|
|
|
|
|
});
|
2019-06-15 10:08:41 +01:00
|
|
|
return Some(expr.span);
|
2018-11-08 14:32:17 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-15 10:08:41 +01:00
|
|
|
None
|
|
|
|
|
})();
|
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
let temp =
|
|
|
|
|
unpack!(block = this.as_temp(block, statement_scope, expr, Mutability::Not));
|
2018-11-08 11:37:27 +01:00
|
|
|
|
2019-06-15 10:08:41 +01:00
|
|
|
if let Some(span) = adjusted_span {
|
|
|
|
|
this.local_decls[temp].source_info.span = span;
|
|
|
|
|
this.block_context.pop();
|
|
|
|
|
}
|
2018-11-08 11:37:27 +01:00
|
|
|
|
2016-04-16 17:38:18 +12:00
|
|
|
block.unit()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|