2018-11-27 02:59:49 +00:00
|
|
|
//! See docs in `build/expr/mod.rs`.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2019-09-26 05:38:33 +00:00
|
|
|
use rustc_index::vec::Idx;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2021-01-09 12:00:45 -05:00
|
|
|
use crate::build::expr::as_place::PlaceBase;
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::build::expr::category::{Category, RvalueFunc};
|
|
|
|
use crate::build::{BlockAnd, BlockAndExtension, 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::AssertKind;
|
2021-02-02 21:07:52 -05:00
|
|
|
use rustc_middle::mir::Place;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::{self, Ty, UpvarSubsts};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2019-06-01 13:38:36 +02:00
|
|
|
impl<'a, 'tcx> Builder<'a, 'tcx> {
|
2019-10-19 21:01:36 +01:00
|
|
|
/// Returns an rvalue suitable for use until the end of the current
|
|
|
|
/// scope expression.
|
|
|
|
///
|
|
|
|
/// The operand returned from this function will *not be valid* after
|
|
|
|
/// an ExprKind::Scope is passed, so please do *not* return it from
|
|
|
|
/// functions to avoid bad miscompiles.
|
2021-02-24 21:29:09 +01:00
|
|
|
crate fn as_local_rvalue(
|
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2021-02-24 21:29:09 +01:00
|
|
|
) -> BlockAnd<Rvalue<'tcx>> {
|
2017-05-15 21:09:01 -04:00
|
|
|
let local_scope = self.local_scope();
|
2020-12-09 10:50:34 +00:00
|
|
|
self.as_rvalue(block, Some(local_scope), expr)
|
2017-02-26 16:21:08 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
/// Compile `expr`, yielding an rvalue.
|
2021-02-24 21:29:09 +01:00
|
|
|
crate fn as_rvalue(
|
2018-09-06 22:34:26 +01:00
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
scope: Option<region::Scope>,
|
2021-04-03 19:58:46 +02:00
|
|
|
expr: &Expr<'tcx>,
|
2018-09-06 22:34:26 +01:00
|
|
|
) -> BlockAnd<Rvalue<'tcx>> {
|
2019-12-24 17:38:22 -05:00
|
|
|
debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
|
2015-08-18 17:59:21 -04: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);
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2021-03-06 22:24:04 +01:00
|
|
|
match expr.kind {
|
|
|
|
ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
|
2019-12-24 17:38:22 -05:00
|
|
|
ExprKind::Scope { region_scope, lint_level, value } => {
|
2021-03-06 22:24:04 +01:00
|
|
|
let region_scope = (region_scope, source_info);
|
2021-04-03 19:58:46 +02:00
|
|
|
this.in_scope(region_scope, lint_level, |this| {
|
|
|
|
this.as_rvalue(block, scope, &this.thir[value])
|
|
|
|
})
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Repeat { value, count } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let value_operand =
|
|
|
|
unpack!(block = this.as_operand(block, scope, &this.thir[value]));
|
2015-11-10 23:52:23 +02:00
|
|
|
block.and(Rvalue::Repeat(value_operand, count))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Binary { op, lhs, rhs } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let lhs = unpack!(block = this.as_operand(block, scope, &this.thir[lhs]));
|
|
|
|
let rhs = unpack!(block = this.as_operand(block, scope, &this.thir[rhs]));
|
2021-03-06 22:24:04 +01:00
|
|
|
this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Unary { op, arg } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let arg = unpack!(block = this.as_operand(block, scope, &this.thir[arg]));
|
2016-03-31 18:50:07 +13:00
|
|
|
// Check for -MIN on signed integers
|
2021-03-06 22:24:04 +01:00
|
|
|
if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() {
|
2021-03-03 16:35:54 +01:00
|
|
|
let bool_ty = this.tcx.types.bool;
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
|
|
let minval = this.minval_literal(expr_span, expr.ty);
|
2017-04-11 23:52:51 +03:00
|
|
|
let is_min = this.temp(bool_ty, expr_span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
is_min,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, box (arg.to_copy(), minval)),
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
block = this.assert(
|
|
|
|
block,
|
|
|
|
Operand::Move(is_min),
|
|
|
|
false,
|
2020-06-19 18:57:15 +02:00
|
|
|
AssertKind::OverflowNeg(arg.to_copy()),
|
2018-09-06 22:34:26 +01:00
|
|
|
expr_span,
|
|
|
|
);
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
2021-03-06 22:24:04 +01:00
|
|
|
block.and(Rvalue::UnaryOp(op, arg))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2017-08-09 22:23:27 +03:00
|
|
|
ExprKind::Box { value } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let value = &this.thir[value];
|
2017-09-20 16:36:20 +03:00
|
|
|
// The `Box<T>` temporary created here is not a part of the HIR,
|
2020-11-24 15:44:04 -08:00
|
|
|
// and therefore is not considered during generator auto-trait
|
2017-09-20 16:36:20 +03:00
|
|
|
// determination. See the comment about `box` at `yield_in_scope`.
|
2020-05-06 10:17:38 +10:00
|
|
|
let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
|
2018-09-06 22:34:26 +01:00
|
|
|
this.cfg.push(
|
|
|
|
block,
|
2019-12-24 17:38:22 -05:00
|
|
|
Statement { source_info, kind: StatementKind::StorageLive(result) },
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2017-08-09 22:23:27 +03:00
|
|
|
if let Some(scope) = scope {
|
2016-01-28 23:59:00 +02:00
|
|
|
// schedule a shallow free of that memory, lest we unwind:
|
2019-12-24 17:38:22 -05:00
|
|
|
this.schedule_drop_storage_and_value(expr_span, scope, result);
|
2017-08-09 22:23:27 +03:00
|
|
|
}
|
2017-08-14 14:10:05 +03:00
|
|
|
|
|
|
|
// malloc some memory of suitable type (thus far, uninitialized):
|
|
|
|
let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
|
2020-03-31 14:08:48 -03:00
|
|
|
this.cfg.push_assign(block, source_info, Place::from(result), box_);
|
2017-08-14 14:10:05 +03:00
|
|
|
|
2021-01-21 22:35:05 -05:00
|
|
|
// initialize the box contents:
|
2019-02-22 05:24:03 +01:00
|
|
|
unpack!(
|
2021-02-24 21:29:09 +01:00
|
|
|
block = this.expr_into_dest(
|
2021-03-03 16:35:54 +01:00
|
|
|
this.tcx.mk_place_deref(Place::from(result)),
|
2021-02-24 21:29:09 +01:00
|
|
|
block,
|
2021-03-06 22:24:04 +01:00
|
|
|
value
|
2021-02-24 21:29:09 +01:00
|
|
|
)
|
2019-02-22 05:24:03 +01:00
|
|
|
);
|
2021-01-21 22:38:58 -05:00
|
|
|
block.and(Rvalue::Use(Operand::Move(Place::from(result))))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Cast { source } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, &this.thir[source]));
|
2016-05-01 17:56:07 +12:00
|
|
|
block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2019-04-16 17:36:41 +05:30
|
|
|
ExprKind::Pointer { cast, source } => {
|
2021-04-03 19:58:46 +02:00
|
|
|
let source = unpack!(block = this.as_operand(block, scope, &this.thir[source]));
|
2021-03-06 22:24:04 +01:00
|
|
|
block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2021-04-03 19:58:46 +02:00
|
|
|
ExprKind::Array { ref fields } => {
|
2018-05-08 16:10:16 +03:00
|
|
|
// (*) We would (maybe) be closer to codegen if we
|
2015-08-18 17:59:21 -04:00
|
|
|
// handled this and other aggregate cases via
|
|
|
|
// `into()`, not `as_rvalue` -- in that case, instead
|
|
|
|
// of generating
|
|
|
|
//
|
|
|
|
// let tmp1 = ...1;
|
|
|
|
// let tmp2 = ...2;
|
|
|
|
// dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
|
|
|
|
//
|
|
|
|
// we could just generate
|
|
|
|
//
|
|
|
|
// dest.f = ...1;
|
|
|
|
// dest.g = ...2;
|
|
|
|
//
|
|
|
|
// The problem is that then we would need to:
|
|
|
|
//
|
|
|
|
// (a) have a more complex mechanism for handling
|
|
|
|
// partial cleanup;
|
|
|
|
// (b) distinguish the case where the type `Foo` has a
|
|
|
|
// destructor, in which case creating an instance
|
|
|
|
// as a whole "arms" the destructor, and you can't
|
|
|
|
// write individual fields; and,
|
|
|
|
// (c) handle the case where the type Foo has no
|
|
|
|
// fields. We don't want `let x: ();` to compile
|
|
|
|
// to the same MIR as `let x = ();`.
|
|
|
|
|
|
|
|
// first process the set of fields
|
2021-03-03 16:35:54 +01:00
|
|
|
let el_ty = expr.ty.sequence_element_type(this.tcx);
|
2018-09-06 22:34:26 +01:00
|
|
|
let fields: Vec<_> = fields
|
|
|
|
.into_iter()
|
2021-04-03 19:58:46 +02:00
|
|
|
.copied()
|
|
|
|
.map(|f| unpack!(block = this.as_operand(block, scope, &this.thir[f])))
|
2018-09-06 22:34:26 +01:00
|
|
|
.collect();
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2021-04-03 19:58:46 +02:00
|
|
|
ExprKind::Tuple { ref fields } => {
|
2018-09-06 22:34:26 +01:00
|
|
|
// see (*) above
|
2015-08-18 17:59:21 -04:00
|
|
|
// first process the set of fields
|
2018-09-06 22:34:26 +01:00
|
|
|
let fields: Vec<_> = fields
|
|
|
|
.into_iter()
|
2021-04-03 19:58:46 +02:00
|
|
|
.copied()
|
|
|
|
.map(|f| unpack!(block = this.as_operand(block, scope, &this.thir[f])))
|
2018-09-06 22:34:26 +01:00
|
|
|
.collect();
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2017-05-12 01:38:26 +03:00
|
|
|
block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2021-04-03 19:58:46 +02:00
|
|
|
ExprKind::Closure { closure_id, substs, ref upvars, movability, ref fake_reads } => {
|
2021-02-25 18:03:41 -05:00
|
|
|
// Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
|
|
|
|
// and push the fake reads.
|
|
|
|
// This must come before creating the operands. This is required in case
|
|
|
|
// there is a fake read and a borrow of the same path, since otherwise the
|
|
|
|
// fake read might interfere with the borrow. Consider an example like this
|
|
|
|
// one:
|
|
|
|
// ```
|
|
|
|
// let mut x = 0;
|
|
|
|
// let c = || {
|
|
|
|
// &mut x; // mutable borrow of `x`
|
|
|
|
// match x { _ => () } // fake read of `x`
|
|
|
|
// };
|
|
|
|
// ```
|
2021-03-25 23:03:12 -04:00
|
|
|
for (thir_place, cause, hir_id) in fake_reads.into_iter() {
|
2021-04-03 19:58:46 +02:00
|
|
|
let place_builder =
|
|
|
|
unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
|
2021-03-25 23:03:12 -04:00
|
|
|
|
|
|
|
if let Ok(place_builder_resolved) =
|
|
|
|
place_builder.try_upvars_resolved(this.tcx, this.typeck_results)
|
|
|
|
{
|
|
|
|
let mir_place =
|
|
|
|
place_builder_resolved.into_place(this.tcx, this.typeck_results);
|
|
|
|
this.cfg.push_fake_read(
|
|
|
|
block,
|
|
|
|
this.source_info(this.tcx.hir().span(*hir_id)),
|
|
|
|
*cause,
|
|
|
|
mir_place,
|
|
|
|
);
|
2021-02-25 18:03:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 10:17:12 +02:00
|
|
|
// see (*) above
|
2019-04-02 16:04:51 -07:00
|
|
|
let operands: Vec<_> = upvars
|
2018-07-03 20:12:09 +01:00
|
|
|
.into_iter()
|
2021-04-03 19:58:46 +02:00
|
|
|
.copied()
|
2018-07-03 20:12:09 +01:00
|
|
|
.map(|upvar| {
|
2021-04-03 19:58:46 +02:00
|
|
|
let upvar = &this.thir[upvar];
|
2018-07-03 20:12:09 +01:00
|
|
|
match Category::of(&upvar.kind) {
|
|
|
|
// Use as_place to avoid creating a temporary when
|
|
|
|
// moving a variable into a closure, so that
|
|
|
|
// borrowck knows which variables to mark as being
|
|
|
|
// used as mut. This is OK here because the upvar
|
|
|
|
// expressions have no side effects and act on
|
|
|
|
// disjoint places.
|
|
|
|
// This occurs when capturing by copy/move, while
|
|
|
|
// by reference captures use as_operand
|
|
|
|
Some(Category::Place) => {
|
2021-03-06 22:24:04 +01:00
|
|
|
let place = unpack!(block = this.as_place(block, upvar));
|
2018-07-03 20:12:09 +01:00
|
|
|
this.consume_by_copy_or_move(place)
|
|
|
|
}
|
|
|
|
_ => {
|
2018-07-15 15:00:58 +01:00
|
|
|
// Turn mutable borrow captures into unique
|
|
|
|
// borrow captures when capturing an immutable
|
|
|
|
// variable. This is sound because the mutation
|
|
|
|
// that caused the capture will cause an error.
|
2021-03-06 22:24:04 +01:00
|
|
|
match upvar.kind {
|
2018-07-15 15:00:58 +01:00
|
|
|
ExprKind::Borrow {
|
2018-09-06 22:34:26 +01:00
|
|
|
borrow_kind:
|
2019-12-24 17:38:22 -05:00
|
|
|
BorrowKind::Mut { allow_two_phase_borrow: false },
|
2018-07-15 15:00:58 +01:00
|
|
|
arg,
|
2018-09-06 22:34:26 +01:00
|
|
|
} => unpack!(
|
|
|
|
block = this.limit_capture_mutability(
|
2021-04-03 19:58:46 +02:00
|
|
|
upvar.span,
|
|
|
|
upvar.ty,
|
|
|
|
scope,
|
|
|
|
block,
|
|
|
|
&this.thir[arg],
|
2018-09-06 22:34:26 +01:00
|
|
|
)
|
|
|
|
),
|
2021-03-06 22:24:04 +01:00
|
|
|
_ => unpack!(block = this.as_operand(block, scope, upvar)),
|
2018-07-15 15:00:58 +01:00
|
|
|
}
|
2018-07-03 20:12:09 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-24 17:38:22 -05:00
|
|
|
})
|
|
|
|
.collect();
|
2021-02-02 21:07:52 -05:00
|
|
|
|
2018-05-02 13:14:30 +02:00
|
|
|
let result = match substs {
|
|
|
|
UpvarSubsts::Generator(substs) => {
|
2019-04-02 16:04:51 -07:00
|
|
|
// We implicitly set the discriminant to 0. See
|
|
|
|
// librustc_mir/transform/deaggregator.rs for details.
|
2018-05-02 13:14:30 +02:00
|
|
|
let movability = movability.unwrap();
|
2021-03-06 22:24:04 +01:00
|
|
|
box AggregateKind::Generator(closure_id, substs, movability)
|
2018-05-02 13:14:30 +02:00
|
|
|
}
|
2021-03-06 22:24:04 +01:00
|
|
|
UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
|
2016-12-26 14:34:03 +01:00
|
|
|
};
|
|
|
|
block.and(Rvalue::Aggregate(result, operands))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2018-09-06 22:34:26 +01:00
|
|
|
ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
|
2018-11-08 11:37:27 +01:00
|
|
|
block = unpack!(this.stmt_expr(block, expr, None));
|
2020-04-09 12:24:53 +02:00
|
|
|
block.and(Rvalue::Use(Operand::Constant(box Constant {
|
|
|
|
span: expr_span,
|
|
|
|
user_ty: None,
|
2021-03-08 16:18:03 +00:00
|
|
|
literal: ty::Const::zero_sized(this.tcx, this.tcx.types.unit).into(),
|
2020-04-09 12:24:53 +02:00
|
|
|
})))
|
2016-04-16 19:45:28 +12:00
|
|
|
}
|
2020-01-25 02:31:32 +01:00
|
|
|
ExprKind::Yield { .. }
|
|
|
|
| ExprKind::Literal { .. }
|
2020-10-06 17:51:15 -03:00
|
|
|
| ExprKind::ConstBlock { .. }
|
2019-11-18 23:04:06 +00:00
|
|
|
| ExprKind::StaticRef { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Block { .. }
|
|
|
|
| ExprKind::Match { .. }
|
2021-01-01 15:38:11 -03:00
|
|
|
| ExprKind::If { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::NeverToAny { .. }
|
2019-03-30 13:05:33 +00:00
|
|
|
| ExprKind::Use { .. }
|
2019-10-19 21:01:36 +01:00
|
|
|
| ExprKind::Borrow { .. }
|
2019-04-20 18:06:03 +01:00
|
|
|
| ExprKind::AddressOf { .. }
|
2019-10-19 21:01:36 +01:00
|
|
|
| ExprKind::Adt { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Loop { .. }
|
|
|
|
| ExprKind::LogicalOp { .. }
|
|
|
|
| ExprKind::Call { .. }
|
|
|
|
| ExprKind::Field { .. }
|
|
|
|
| ExprKind::Deref { .. }
|
|
|
|
| ExprKind::Index { .. }
|
|
|
|
| ExprKind::VarRef { .. }
|
2020-11-17 01:52:14 -05:00
|
|
|
| ExprKind::UpvarRef { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| ExprKind::Break { .. }
|
|
|
|
| ExprKind::Continue { .. }
|
|
|
|
| ExprKind::Return { .. }
|
2020-02-14 18:17:50 +00:00
|
|
|
| ExprKind::InlineAsm { .. }
|
2020-01-14 13:40:42 +00:00
|
|
|
| ExprKind::LlvmInlineAsm { .. }
|
2018-09-20 18:43:35 -07:00
|
|
|
| ExprKind::PlaceTypeAscription { .. }
|
|
|
|
| ExprKind::ValueTypeAscription { .. } => {
|
2015-08-18 17:59:21 -04:00
|
|
|
// these do not have corresponding `Rvalue` variants,
|
|
|
|
// so make an operand and then return that
|
2021-01-09 12:00:45 -05:00
|
|
|
debug_assert!(!matches!(
|
|
|
|
Category::of(&expr.kind),
|
|
|
|
Some(Category::Rvalue(RvalueFunc::AsRvalue))
|
|
|
|
));
|
2017-02-26 16:21:08 +02:00
|
|
|
let operand = unpack!(block = this.as_operand(block, scope, expr));
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::Use(operand))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn build_binary_op(
|
2018-09-06 22:34:26 +01:00
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
op: BinOp,
|
|
|
|
span: Span,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
lhs: Operand<'tcx>,
|
|
|
|
rhs: Operand<'tcx>,
|
|
|
|
) -> BlockAnd<Rvalue<'tcx>> {
|
2016-06-07 19:21:56 +03:00
|
|
|
let source_info = self.source_info(span);
|
2021-03-03 16:35:54 +01:00
|
|
|
let bool_ty = self.tcx.types.bool;
|
|
|
|
if self.check_overflow && op.is_checkable() && ty.is_integral() {
|
|
|
|
let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
|
2017-04-11 23:52:51 +03:00
|
|
|
let result_value = self.temp(result_tup, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
result_value,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::CheckedBinaryOp(op, box (lhs.to_copy(), rhs.to_copy())),
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2016-03-31 18:50:07 +13:00
|
|
|
let val_fld = Field::new(0);
|
|
|
|
let of_fld = Field::new(1);
|
|
|
|
|
2021-03-03 16:35:54 +01:00
|
|
|
let tcx = self.tcx;
|
2020-04-25 10:17:14 +02:00
|
|
|
let val = tcx.mk_place_field(result_value, val_fld, ty);
|
2019-10-20 21:04:59 -04:00
|
|
|
let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2020-06-19 18:57:15 +02:00
|
|
|
let err = AssertKind::Overflow(op, lhs, rhs);
|
2016-04-29 00:04:45 +12:00
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
block = self.assert(block, Operand::Move(of), false, err, span);
|
2016-04-29 00:04:45 +12:00
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
block.and(Rvalue::Use(Operand::Move(val)))
|
2016-03-31 18:50:07 +13:00
|
|
|
} else {
|
|
|
|
if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
|
|
|
|
// Checking division and remainder is more complex, since we 1. always check
|
|
|
|
// and 2. there are two possible failure cases, divide-by-zero and overflow.
|
|
|
|
|
2019-07-24 10:24:55 +02:00
|
|
|
let zero_err = if op == BinOp::Div {
|
2020-06-19 18:57:15 +02:00
|
|
|
AssertKind::DivisionByZero(lhs.to_copy())
|
2016-03-31 18:50:07 +13:00
|
|
|
} else {
|
2020-06-19 18:57:15 +02:00
|
|
|
AssertKind::RemainderByZero(lhs.to_copy())
|
2016-03-31 18:50:07 +13:00
|
|
|
};
|
2020-06-19 18:57:15 +02:00
|
|
|
let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
|
|
// Check for / 0
|
2017-04-11 23:52:51 +03:00
|
|
|
let is_zero = self.temp(bool_ty, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
let zero = self.zero_literal(span, ty);
|
2018-09-06 22:34:26 +01:00
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
is_zero,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, box (rhs.to_copy(), zero)),
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
|
|
// We only need to check for the overflow in one case:
|
|
|
|
// MIN / -1, and only for signed values.
|
|
|
|
if ty.is_signed() {
|
|
|
|
let neg_1 = self.neg_1_literal(span, ty);
|
|
|
|
let min = self.minval_literal(span, ty);
|
|
|
|
|
2017-04-11 23:52:51 +03:00
|
|
|
let is_neg_1 = self.temp(bool_ty, span);
|
2018-09-06 22:34:26 +01:00
|
|
|
let is_min = self.temp(bool_ty, span);
|
|
|
|
let of = self.temp(bool_ty, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
|
|
|
// this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
|
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
is_neg_1,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, box (rhs.to_copy(), neg_1)),
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
is_min,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, box (lhs.to_copy(), min)),
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2017-11-17 17:19:57 +02:00
|
|
|
let is_neg_1 = Operand::Move(is_neg_1);
|
|
|
|
let is_min = Operand::Move(is_min);
|
2018-09-06 22:34:26 +01:00
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
of,
|
2021-03-05 09:32:47 +00:00
|
|
|
Rvalue::BinaryOp(BinOp::BitAnd, box (is_neg_1, is_min)),
|
2018-09-06 22:34:26 +01:00
|
|
|
);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2018-09-06 22:34:26 +01:00
|
|
|
block = self.assert(block, Operand::Move(of), false, overflow_err, span);
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 09:32:47 +00:00
|
|
|
block.and(Rvalue::BinaryOp(op, box (lhs, rhs)))
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-15 15:00:58 +01:00
|
|
|
fn limit_capture_mutability(
|
|
|
|
&mut self,
|
|
|
|
upvar_span: Span,
|
|
|
|
upvar_ty: Ty<'tcx>,
|
|
|
|
temp_lifetime: Option<region::Scope>,
|
|
|
|
mut block: BasicBlock,
|
2021-04-03 19:58:46 +02:00
|
|
|
arg: &Expr<'tcx>,
|
2018-07-15 15:00:58 +01:00
|
|
|
) -> BlockAnd<Operand<'tcx>> {
|
|
|
|
let this = self;
|
|
|
|
|
|
|
|
let source_info = this.source_info(upvar_span);
|
2020-05-06 10:17:38 +10:00
|
|
|
let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
|
2018-07-15 15:00:58 +01:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
|
2018-07-15 15:00:58 +01:00
|
|
|
|
2020-11-26 00:07:41 -05:00
|
|
|
let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
|
|
|
|
|
|
|
|
let mutability = match arg_place_builder.base() {
|
|
|
|
// We are capturing a path that starts off a local variable in the parent.
|
|
|
|
// The mutability of the current capture is same as the mutability
|
|
|
|
// of the local declaration in the parent.
|
2021-01-09 12:00:45 -05:00
|
|
|
PlaceBase::Local(local) => this.local_decls[local].mutability,
|
2020-11-26 00:07:41 -05:00
|
|
|
// Parent is a closure and we are capturing a path that is captured
|
|
|
|
// by the parent itself. The mutability of the current capture
|
|
|
|
// is same as that of the capture in the parent closure.
|
|
|
|
PlaceBase::Upvar { .. } => {
|
2021-01-09 12:00:45 -05:00
|
|
|
let enclosing_upvars_resolved =
|
2021-03-03 16:35:54 +01:00
|
|
|
arg_place_builder.clone().into_place(this.tcx, this.typeck_results);
|
2020-11-26 00:07:41 -05:00
|
|
|
|
|
|
|
match enclosing_upvars_resolved.as_ref() {
|
2021-01-09 12:00:45 -05:00
|
|
|
PlaceRef {
|
|
|
|
local,
|
|
|
|
projection: &[ProjectionElem::Field(upvar_index, _), ..],
|
|
|
|
}
|
2020-11-26 00:07:41 -05:00
|
|
|
| PlaceRef {
|
|
|
|
local,
|
2021-01-09 12:00:45 -05:00
|
|
|
projection:
|
|
|
|
&[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
|
|
|
|
} => {
|
|
|
|
// Not in a closure
|
|
|
|
debug_assert!(
|
|
|
|
local == Local::new(1),
|
|
|
|
"Expected local to be Local(1), found {:?}",
|
|
|
|
local
|
|
|
|
);
|
|
|
|
// Not in a closure
|
|
|
|
debug_assert!(
|
|
|
|
this.upvar_mutbls.len() > upvar_index.index(),
|
|
|
|
"Unexpected capture place, upvar_mutbls={:#?}, upvar_index={:?}",
|
|
|
|
this.upvar_mutbls,
|
|
|
|
upvar_index
|
|
|
|
);
|
|
|
|
this.upvar_mutbls[upvar_index.index()]
|
|
|
|
}
|
2020-11-26 00:07:41 -05:00
|
|
|
_ => bug!("Unexpected capture place"),
|
|
|
|
}
|
2018-07-15 15:00:58 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let borrow_kind = match mutability {
|
|
|
|
Mutability::Not => BorrowKind::Unique,
|
2019-12-24 17:38:22 -05:00
|
|
|
Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
|
2018-07-15 15:00:58 +01:00
|
|
|
};
|
|
|
|
|
2021-03-03 16:35:54 +01:00
|
|
|
let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);
|
2020-11-26 00:07:41 -05:00
|
|
|
|
2018-07-15 15:00:58 +01:00
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
Place::from(temp),
|
2021-03-03 16:35:54 +01:00
|
|
|
Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
|
2018-07-15 15:00:58 +01:00
|
|
|
);
|
|
|
|
|
2020-12-09 10:50:34 +00:00
|
|
|
// See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
|
|
|
|
// this can be `None`.
|
2018-07-15 15:00:58 +01:00
|
|
|
if let Some(temp_lifetime) = temp_lifetime {
|
2019-12-24 17:38:22 -05:00
|
|
|
this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
|
2018-07-15 15:00:58 +01:00
|
|
|
}
|
|
|
|
|
2019-06-24 17:46:09 +02:00
|
|
|
block.and(Operand::Move(Place::from(temp)))
|
2018-07-15 15:00:58 +01:00
|
|
|
}
|
|
|
|
|
2016-03-31 18:50:07 +13:00
|
|
|
// Helper to get a `-1` value of the appropriate type
|
2017-09-14 21:44:23 -04:00
|
|
|
fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
2019-06-14 18:09:57 +02:00
|
|
|
let param_ty = ty::ParamEnv::empty().and(ty);
|
2021-03-03 16:35:54 +01:00
|
|
|
let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
|
2018-02-21 22:02:52 +01:00
|
|
|
let n = (!0u128) >> (128 - bits);
|
2021-03-03 16:35:54 +01:00
|
|
|
let literal = ty::Const::from_bits(self.tcx, n, param_ty);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2019-08-12 18:15:13 +03:00
|
|
|
self.literal_operand(span, literal)
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helper to get the minimum value of the appropriate type
|
2017-09-14 21:44:23 -04:00
|
|
|
fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
2018-02-21 22:02:52 +01:00
|
|
|
assert!(ty.is_signed());
|
2019-06-14 18:09:57 +02:00
|
|
|
let param_ty = ty::ParamEnv::empty().and(ty);
|
2021-03-03 16:35:54 +01:00
|
|
|
let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
|
2018-02-21 22:02:52 +01:00
|
|
|
let n = 1 << (bits - 1);
|
2021-03-03 16:35:54 +01:00
|
|
|
let literal = ty::Const::from_bits(self.tcx, n, param_ty);
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2019-08-12 18:15:13 +03:00
|
|
|
self.literal_operand(span, literal)
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|