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};
|
2021-04-04 02:24:02 +02:00
|
|
|
use rustc_middle::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-04-03 19:58:46 +02: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.
|
2021-03-06 22:24:04 +01:00
|
|
|
match expr.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
ExprKind::Scope { region_scope, lint_level, value } => {
|
2021-03-06 22:24:04 +01:00
|
|
|
this.in_scope((region_scope, source_info), lint_level, |this| {
|
2021-04-03 19:58:46 +02:00
|
|
|
this.stmt_expr(block, &this.thir[value], statement_scope)
|
2017-08-31 21:37:38 +03:00
|
|
|
})
|
2016-04-16 17:38:18 +12:00
|
|
|
}
|
|
|
|
|
ExprKind::Assign { lhs, rhs } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let lhs = &this.thir[lhs];
|
|
|
|
|
let rhs = &this.thir[rhs];
|
2016-04-16 17:38:18 +12:00
|
|
|
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);
|
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.
|
2021-03-03 16:35:54 +01:00
|
|
|
if lhs.ty.needs_drop(this.tcx, this.param_env) {
|
2021-03-06 22:24:04 +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-03-06 22:24:04 +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)`?
|
|
|
|
|
|
2021-04-03 19:58:46 +02:00
|
|
|
let lhs = &this.thir[lhs];
|
|
|
|
|
let rhs = &this.thir[rhs];
|
2016-03-31 18:50:07 +13:00
|
|
|
let lhs_ty = lhs.ty;
|
|
|
|
|
|
2021-03-03 18:07:57 +01:00
|
|
|
debug!("stmt_expr AssignOp block_context.push(SubExpr) : {:?}", expr);
|
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-03-06 22:24:04 +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-03-06 22:24:04 +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-03-06 22:24:04 +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,
|
2021-04-03 19:58:46 +02:00
|
|
|
value.map(|value| &this.thir[value]),
|
2021-03-06 22:24:04 +01:00
|
|
|
BreakableTarget::Break(label),
|
2021-02-24 21:29:09 +01:00
|
|
|
source_info,
|
|
|
|
|
),
|
2021-04-03 19:58:46 +02:00
|
|
|
ExprKind::Return { value } => this.break_scope(
|
|
|
|
|
block,
|
|
|
|
|
value.map(|value| &this.thir[value]),
|
|
|
|
|
BreakableTarget::Return,
|
|
|
|
|
source_info,
|
|
|
|
|
),
|
|
|
|
|
ExprKind::LlvmInlineAsm { asm, ref outputs, ref inputs } => {
|
2021-03-03 18:07:57 +01:00
|
|
|
debug!("stmt_expr LlvmInlineAsm block_context.push(SubExpr) : {:?}", expr);
|
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-04-03 19:58:46 +02:00
|
|
|
.copied()
|
|
|
|
|
.map(|output| unpack!(block = this.as_place(block, &this.thir[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()
|
2021-04-03 19:58:46 +02:00
|
|
|
.copied()
|
2018-10-15 00:00:53 +02:00
|
|
|
.map(|input| {
|
2021-04-03 19:58:46 +02:00
|
|
|
let input = &this.thir[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-03-06 22:24:04 +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 {
|
2021-04-03 19:58:46 +02:00
|
|
|
if let Some(tail_expr) = body.expr {
|
|
|
|
|
let mut expr = &this.thir[tail_expr];
|
2021-03-07 00:21:14 +01:00
|
|
|
while let ExprKind::Block {
|
|
|
|
|
body: Block { expr: Some(nested_expr), .. },
|
|
|
|
|
}
|
2021-04-03 19:58:46 +02:00
|
|
|
| ExprKind::Scope { value: nested_expr, .. } = expr.kind
|
2021-03-07 00:21:14 +01:00
|
|
|
{
|
2021-04-03 19:58:46 +02:00
|
|
|
expr = &this.thir[nested_expr];
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|