2018-11-27 02:59:49 +00:00
|
|
|
//! See docs in `build/expr/mod.rs`.
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2024-10-30 21:57:40 -07:00
|
|
|
use rustc_abi::{BackendRepr, FieldIdx, Primitive};
|
2021-09-11 23:24:55 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::{Idx, IndexVec};
|
2024-05-08 19:31:05 +10:00
|
|
|
use rustc_middle::bug;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::middle::region;
|
2023-02-13 21:08:15 +00:00
|
|
|
use rustc_middle::mir::interpret::Scalar;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
2021-04-04 18:42:17 +02:00
|
|
|
use rustc_middle::thir::*;
|
2022-10-04 21:39:43 +03:00
|
|
|
use rustc_middle::ty::cast::{CastTy, mir_cast_kind};
|
2023-05-14 22:29:26 -07:00
|
|
|
use rustc_middle::ty::layout::IntegerExt;
|
2022-06-29 14:18:55 +00:00
|
|
|
use rustc_middle::ty::util::IntTypeExt;
|
2023-07-11 22:35:29 +01:00
|
|
|
use rustc_middle::ty::{self, Ty, UpvarArgs};
|
2024-01-12 08:21:42 +01:00
|
|
|
use rustc_span::source_map::Spanned;
|
|
|
|
use rustc_span::{DUMMY_SP, Span};
|
2024-05-22 14:35:22 +10:00
|
|
|
use tracing::debug;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
use crate::build::expr::as_place::PlaceBase;
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::build::expr::category::{Category, RvalueFunc};
|
2022-12-19 15:30:43 +00:00
|
|
|
use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
|
2024-07-29 08:13:50 +10: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.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn as_local_rvalue(
|
2021-02-24 21:29:09 +01:00
|
|
|
&mut self,
|
|
|
|
block: BasicBlock,
|
2023-12-15 15:16:24 +00:00
|
|
|
expr_id: ExprId,
|
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();
|
2024-09-02 01:13:07 +08:00
|
|
|
self.as_rvalue(
|
|
|
|
block,
|
|
|
|
TempLifetime { temp_lifetime: Some(local_scope), backwards_incompatible: None },
|
|
|
|
expr_id,
|
|
|
|
)
|
2017-02-26 16:21:08 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:59:21 -04:00
|
|
|
/// Compile `expr`, yielding an rvalue.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn as_rvalue(
|
2018-09-06 22:34:26 +01:00
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
2024-09-02 01:13:07 +08:00
|
|
|
scope: TempLifetime,
|
2023-12-15 15:16:24 +00:00
|
|
|
expr_id: ExprId,
|
2018-09-06 22:34:26 +01:00
|
|
|
) -> BlockAnd<Rvalue<'tcx>> {
|
2023-12-15 15:16:24 +00:00
|
|
|
let this = self;
|
|
|
|
let expr = &this.thir[expr_id];
|
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 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);
|
2023-12-15 15:16:24 +00:00
|
|
|
this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Repeat { value, count } => {
|
2024-09-22 14:11:44 -04:00
|
|
|
if Some(0) == count.try_to_target_usize(this.tcx) {
|
2022-04-13 07:08:58 -04:00
|
|
|
this.build_zero_repeat(block, value, scope, source_info)
|
|
|
|
} else {
|
|
|
|
let value_operand = unpack!(
|
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
2023-12-15 15:16:24 +00:00
|
|
|
value,
|
2023-03-09 16:55:20 +00:00
|
|
|
LocalInfo::Boring,
|
2022-04-13 07:08:58 -04:00
|
|
|
NeedsTemporary::No
|
|
|
|
)
|
|
|
|
);
|
|
|
|
block.and(Rvalue::Repeat(value_operand, count))
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
ExprKind::Binary { op, lhs, rhs } => {
|
2022-03-09 17:10:48 +00:00
|
|
|
let lhs = unpack!(
|
2023-03-18 00:29:53 -05:00
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
2023-12-15 15:16:24 +00:00
|
|
|
lhs,
|
2023-03-18 00:29:53 -05:00
|
|
|
LocalInfo::Boring,
|
|
|
|
NeedsTemporary::Maybe
|
|
|
|
)
|
2022-03-09 17:10:48 +00:00
|
|
|
);
|
|
|
|
let rhs = unpack!(
|
2023-12-15 15:16:24 +00:00
|
|
|
block =
|
|
|
|
this.as_operand(block, scope, rhs, LocalInfo::Boring, NeedsTemporary::No)
|
2022-03-09 17:10:48 +00:00
|
|
|
);
|
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 } => {
|
2022-03-09 17:10:48 +00:00
|
|
|
let arg = unpack!(
|
2023-12-15 15:16:24 +00:00
|
|
|
block =
|
|
|
|
this.as_operand(block, scope, arg, LocalInfo::Boring, NeedsTemporary::No)
|
2022-03-09 17:10:48 +00:00
|
|
|
);
|
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-08-05 03:16:19 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, Box::new((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 } => {
|
2023-12-15 15:16:24 +00:00
|
|
|
let value_ty = this.thir[value].ty;
|
2021-09-11 23:24:55 +01:00
|
|
|
let tcx = this.tcx;
|
2024-02-27 11:57:52 +00:00
|
|
|
let source_info = this.source_info(expr_span);
|
2021-09-11 23:24:55 +01:00
|
|
|
|
|
|
|
let size = this.temp(tcx.types.usize, expr_span);
|
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
2024-02-27 11:57:52 +00:00
|
|
|
source_info,
|
2021-09-11 23:24:55 +01:00
|
|
|
size,
|
2023-12-15 15:16:24 +00:00
|
|
|
Rvalue::NullaryOp(NullOp::SizeOf, value_ty),
|
2021-09-11 23:24:55 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let align = this.temp(tcx.types.usize, expr_span);
|
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
2024-02-27 11:57:52 +00:00
|
|
|
source_info,
|
2021-09-11 23:24:55 +01:00
|
|
|
align,
|
2023-12-15 15:16:24 +00:00
|
|
|
Rvalue::NullaryOp(NullOp::AlignOf, value_ty),
|
2021-09-11 23:24:55 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// malloc some memory of suitable size and align:
|
|
|
|
let exchange_malloc = Operand::function_handle(
|
|
|
|
tcx,
|
|
|
|
tcx.require_lang_item(LangItem::ExchangeMalloc, Some(expr_span)),
|
2022-12-13 10:49:30 +00:00
|
|
|
[],
|
2021-09-11 23:24:55 +01:00
|
|
|
expr_span,
|
|
|
|
);
|
2023-07-05 20:13:26 +01:00
|
|
|
let storage = this.temp(Ty::new_mut_ptr(tcx, tcx.types.u8), expr_span);
|
2021-09-11 23:24:55 +01:00
|
|
|
let success = this.cfg.start_new_block();
|
|
|
|
this.cfg.terminate(block, source_info, TerminatorKind::Call {
|
|
|
|
func: exchange_malloc,
|
2024-01-12 08:21:42 +01:00
|
|
|
args: [Spanned { node: Operand::Move(size), span: DUMMY_SP }, Spanned {
|
|
|
|
node: Operand::Move(align),
|
|
|
|
span: DUMMY_SP,
|
|
|
|
}]
|
2024-06-21 02:20:18 -07:00
|
|
|
.into(),
|
2022-04-16 09:27:54 -04:00
|
|
|
destination: storage,
|
|
|
|
target: Some(success),
|
2022-10-08 23:47:59 +01:00
|
|
|
unwind: UnwindAction::Continue,
|
2023-06-18 05:24:38 +00:00
|
|
|
call_source: CallSource::Misc,
|
2021-09-11 23:24:55 +01:00
|
|
|
fn_span: expr_span,
|
|
|
|
});
|
|
|
|
this.diverge_from(block);
|
|
|
|
block = success;
|
|
|
|
|
2017-09-20 16:36:20 +03:00
|
|
|
// The `Box<T>` temporary created here is not a part of the HIR,
|
2023-10-19 21:46:28 +00:00
|
|
|
// and therefore is not considered during coroutine auto-trait
|
2017-09-20 16:36:20 +03:00
|
|
|
// determination. See the comment about `box` at `yield_in_scope`.
|
2023-10-04 17:50:03 +00:00
|
|
|
let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span));
|
2018-09-06 22:34:26 +01:00
|
|
|
this.cfg.push(block, Statement {
|
2019-12-24 17:38:22 -05:00
|
|
|
source_info,
|
|
|
|
kind: StatementKind::StorageLive(result),
|
2018-09-06 22:34:26 +01:00
|
|
|
});
|
2024-09-02 01:13:07 +08:00
|
|
|
if let Some(scope) = scope.temp_lifetime {
|
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
|
|
|
|
2021-09-11 23:24:55 +01:00
|
|
|
// Transmute `*mut u8` to the box (thus far, uninitialized):
|
2023-12-15 15:16:24 +00:00
|
|
|
let box_ = Rvalue::ShallowInitBox(Operand::Move(storage), 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:
|
2024-07-06 11:55:54 +10:00
|
|
|
block = this
|
|
|
|
.expr_into_dest(this.tcx.mk_place_deref(Place::from(result)), block, value)
|
|
|
|
.into_block();
|
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 } => {
|
2023-12-15 15:16:24 +00:00
|
|
|
let source_expr = &this.thir[source];
|
2022-06-29 14:18:55 +00:00
|
|
|
|
|
|
|
// Casting an enum to an integer is equivalent to computing the discriminant and casting the
|
|
|
|
// discriminant. Previously every backend had to repeat the logic for this operation. Now we
|
|
|
|
// create all the steps directly in MIR with operations all backends need to support anyway.
|
2023-12-15 15:16:24 +00:00
|
|
|
let (source, ty) = if let ty::Adt(adt_def, ..) = source_expr.ty.kind()
|
2022-06-29 14:18:55 +00:00
|
|
|
&& adt_def.is_enum()
|
|
|
|
{
|
|
|
|
let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx);
|
2022-10-13 16:44:31 +01:00
|
|
|
let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not));
|
2024-11-15 13:53:31 +01:00
|
|
|
let layout =
|
|
|
|
this.tcx.layout_of(this.typing_env().as_query_input(source_expr.ty));
|
2023-12-15 15:16:24 +00:00
|
|
|
let discr = this.temp(discr_ty, source_expr.span);
|
2022-06-29 14:18:55 +00:00
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
|
|
|
discr,
|
2022-10-13 16:44:31 +01:00
|
|
|
Rvalue::Discriminant(temp.into()),
|
2022-06-29 14:18:55 +00:00
|
|
|
);
|
2022-10-26 17:50:11 +03:00
|
|
|
let (op, ty) = (Operand::Move(discr), discr_ty);
|
|
|
|
|
2024-10-29 13:37:26 -07:00
|
|
|
if let BackendRepr::Scalar(scalar) = layout.unwrap().backend_repr
|
2023-05-14 22:29:26 -07:00
|
|
|
&& !scalar.is_always_valid(&this.tcx)
|
|
|
|
&& let Primitive::Int(int_width, _signed) = scalar.primitive()
|
|
|
|
{
|
|
|
|
let unsigned_ty = int_width.to_ty(this.tcx, false);
|
|
|
|
let unsigned_place = this.temp(unsigned_ty, expr_span);
|
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
|
|
|
unsigned_place,
|
|
|
|
Rvalue::Cast(CastKind::IntToInt, Operand::Copy(discr), unsigned_ty),
|
|
|
|
);
|
|
|
|
|
|
|
|
let bool_ty = this.tcx.types.bool;
|
|
|
|
let range = scalar.valid_range(&this.tcx);
|
|
|
|
let merge_op =
|
|
|
|
if range.start <= range.end { BinOp::BitAnd } else { BinOp::BitOr };
|
|
|
|
|
|
|
|
let mut comparer = |range: u128, bin_op: BinOp| -> Place<'tcx> {
|
2024-11-28 21:54:27 +01:00
|
|
|
// We can use `ty::TypingEnv::fully_monomorphized()` here
|
2024-11-18 10:50:14 +01:00
|
|
|
// as we only need it to compute the layout of a primitive.
|
2023-09-20 20:51:14 +02:00
|
|
|
let range_val = Const::from_bits(
|
|
|
|
this.tcx,
|
|
|
|
range,
|
2024-11-15 13:53:31 +01:00
|
|
|
ty::TypingEnv::fully_monomorphized(),
|
|
|
|
unsigned_ty,
|
2023-09-20 20:51:14 +02:00
|
|
|
);
|
2023-05-14 22:29:26 -07:00
|
|
|
let lit_op = this.literal_operand(expr.span, range_val);
|
|
|
|
let is_bin_op = this.temp(bool_ty, expr_span);
|
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
|
|
|
is_bin_op,
|
|
|
|
Rvalue::BinaryOp(
|
|
|
|
bin_op,
|
|
|
|
Box::new((Operand::Copy(unsigned_place), lit_op)),
|
2023-10-13 08:58:33 +00:00
|
|
|
),
|
2023-05-14 22:29:26 -07:00
|
|
|
);
|
|
|
|
is_bin_op
|
|
|
|
};
|
|
|
|
let assert_place = if range.start == 0 {
|
|
|
|
comparer(range.end, BinOp::Le)
|
|
|
|
} else {
|
|
|
|
let start_place = comparer(range.start, BinOp::Ge);
|
|
|
|
let end_place = comparer(range.end, BinOp::Le);
|
|
|
|
let merge_place = this.temp(bool_ty, expr_span);
|
|
|
|
this.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
|
|
|
merge_place,
|
|
|
|
Rvalue::BinaryOp(
|
|
|
|
merge_op,
|
|
|
|
Box::new((
|
|
|
|
Operand::Move(start_place),
|
|
|
|
Operand::Move(end_place),
|
|
|
|
)),
|
2023-10-13 08:58:33 +00:00
|
|
|
),
|
2023-05-14 22:29:26 -07:00
|
|
|
);
|
|
|
|
merge_place
|
|
|
|
};
|
|
|
|
this.cfg.push(block, Statement {
|
|
|
|
source_info,
|
|
|
|
kind: StatementKind::Intrinsic(Box::new(
|
|
|
|
NonDivergingIntrinsic::Assume(Operand::Move(assert_place)),
|
|
|
|
)),
|
|
|
|
});
|
2022-10-26 17:50:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
(op, ty)
|
2022-06-29 14:18:55 +00:00
|
|
|
} else {
|
2023-12-15 15:16:24 +00:00
|
|
|
let ty = source_expr.ty;
|
2022-06-29 14:18:55 +00:00
|
|
|
let source = unpack!(
|
2023-03-09 16:55:20 +00:00
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
|
|
|
source,
|
|
|
|
LocalInfo::Boring,
|
|
|
|
NeedsTemporary::No
|
|
|
|
)
|
2022-06-29 14:18:55 +00:00
|
|
|
);
|
|
|
|
(source, ty)
|
|
|
|
};
|
|
|
|
let from_ty = CastTy::from_ty(ty);
|
2022-05-31 00:00:00 +00:00
|
|
|
let cast_ty = CastTy::from_ty(expr.ty);
|
2023-12-15 15:16:24 +00:00
|
|
|
debug!("ExprKind::Cast from_ty={from_ty:?}, cast_ty={:?}/{cast_ty:?}", expr.ty);
|
2022-10-04 21:39:43 +03:00
|
|
|
let cast_kind = mir_cast_kind(ty, expr.ty);
|
2022-05-31 00:00:00 +00:00
|
|
|
block.and(Rvalue::Cast(cast_kind, source, expr.ty))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2024-09-15 19:35:06 +02:00
|
|
|
ExprKind::PointerCoercion { cast, source, is_from_as_cast } => {
|
2022-03-09 17:10:48 +00:00
|
|
|
let source = unpack!(
|
2023-03-18 00:29:53 -05:00
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
2023-12-15 15:16:24 +00:00
|
|
|
source,
|
2023-03-18 00:29:53 -05:00
|
|
|
LocalInfo::Boring,
|
|
|
|
NeedsTemporary::No
|
|
|
|
)
|
2022-03-09 17:10:48 +00:00
|
|
|
);
|
2024-09-15 19:35:06 +02:00
|
|
|
let origin =
|
|
|
|
if is_from_as_cast { CoercionSource::AsCast } else { CoercionSource::Implicit };
|
|
|
|
block.and(Rvalue::Cast(CastKind::PointerCoercion(cast, origin), 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);
|
2023-04-01 20:11:38 -07:00
|
|
|
let fields: IndexVec<FieldIdx, _> = fields
|
2018-09-06 22:34:26 +01:00
|
|
|
.into_iter()
|
2021-04-03 19:58:46 +02:00
|
|
|
.copied()
|
2022-03-09 17:10:48 +00:00
|
|
|
.map(|f| {
|
|
|
|
unpack!(
|
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
2023-12-15 15:16:24 +00:00
|
|
|
f,
|
2023-03-09 16:55:20 +00:00
|
|
|
LocalInfo::Boring,
|
2022-03-09 17:10:48 +00:00
|
|
|
NeedsTemporary::Maybe
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
2018-09-06 22:34:26 +01:00
|
|
|
.collect();
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2021-08-05 03:16:19 +02:00
|
|
|
block.and(Rvalue::Aggregate(Box::new(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
|
2023-04-01 20:11:38 -07:00
|
|
|
let fields: IndexVec<FieldIdx, _> = fields
|
2018-09-06 22:34:26 +01:00
|
|
|
.into_iter()
|
2021-04-03 19:58:46 +02:00
|
|
|
.copied()
|
2022-03-09 17:10:48 +00:00
|
|
|
.map(|f| {
|
|
|
|
unpack!(
|
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
2023-12-15 15:16:24 +00:00
|
|
|
f,
|
2023-03-09 16:55:20 +00:00
|
|
|
LocalInfo::Boring,
|
2022-03-09 17:10:48 +00:00
|
|
|
NeedsTemporary::Maybe
|
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|
2018-09-06 22:34:26 +01:00
|
|
|
.collect();
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2021-08-05 03:16:19 +02:00
|
|
|
block.and(Rvalue::Aggregate(Box::new(AggregateKind::Tuple), fields))
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
2022-08-24 11:28:05 +10:00
|
|
|
ExprKind::Closure(box ClosureExpr {
|
|
|
|
closure_id,
|
2023-07-11 22:35:29 +01:00
|
|
|
args,
|
2022-08-24 11:28:05 +10:00
|
|
|
ref upvars,
|
|
|
|
ref fake_reads,
|
2023-12-21 01:52:10 +00:00
|
|
|
movability: _,
|
2022-08-24 11:28:05 +10:00
|
|
|
}) => {
|
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-05-21 14:55:09 -04:00
|
|
|
//
|
2021-05-27 17:58:35 -04:00
|
|
|
for (thir_place, cause, hir_id) in fake_reads.into_iter() {
|
2023-12-15 15:16:24 +00:00
|
|
|
let place_builder = unpack!(block = this.as_place_builder(block, *thir_place));
|
2021-05-27 17:58:35 -04:00
|
|
|
|
2022-11-17 18:52:28 -06:00
|
|
|
if let Some(mir_place) = place_builder.try_to_place(this) {
|
2021-05-27 17:58:35 -04:00
|
|
|
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
|
2023-04-01 20:11:38 -07:00
|
|
|
let operands: IndexVec<FieldIdx, _> = 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| {
|
2023-12-15 15:16:24 +00:00
|
|
|
let upvar_expr = &this.thir[upvar];
|
|
|
|
match Category::of(&upvar_expr.kind) {
|
2018-07-03 20:12:09 +01:00
|
|
|
// 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.
|
2023-12-15 15:16:24 +00:00
|
|
|
match upvar_expr.kind {
|
2018-07-15 15:00:58 +01:00
|
|
|
ExprKind::Borrow {
|
2018-09-06 22:34:26 +01:00
|
|
|
borrow_kind:
|
2023-05-31 06:25:27 +08:00
|
|
|
BorrowKind::Mut { kind: MutBorrowKind::Default },
|
2018-07-15 15:00:58 +01:00
|
|
|
arg,
|
2018-09-06 22:34:26 +01:00
|
|
|
} => unpack!(
|
|
|
|
block = this.limit_capture_mutability(
|
2023-12-15 15:16:24 +00:00
|
|
|
upvar_expr.span,
|
|
|
|
upvar_expr.ty,
|
2024-09-02 01:13:07 +08:00
|
|
|
scope.temp_lifetime,
|
2021-04-03 19:58:46 +02:00
|
|
|
block,
|
2023-12-15 15:16:24 +00:00
|
|
|
arg,
|
2018-09-06 22:34:26 +01:00
|
|
|
)
|
|
|
|
),
|
2021-09-06 16:59:24 -05:00
|
|
|
_ => {
|
2022-03-09 17:10:48 +00:00
|
|
|
unpack!(
|
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
|
|
|
upvar,
|
2023-03-09 16:55:20 +00:00
|
|
|
LocalInfo::Boring,
|
2022-03-09 17:10:48 +00:00
|
|
|
NeedsTemporary::Maybe
|
|
|
|
)
|
|
|
|
)
|
2021-09-06 16:59:24 -05:00
|
|
|
}
|
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
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
let result = match args {
|
2023-10-19 16:06:43 +00:00
|
|
|
UpvarArgs::Coroutine(args) => {
|
2023-12-21 01:52:10 +00:00
|
|
|
Box::new(AggregateKind::Coroutine(closure_id.to_def_id(), args))
|
2021-08-05 03:16:19 +02:00
|
|
|
}
|
2023-07-11 22:35:29 +01:00
|
|
|
UpvarArgs::Closure(args) => {
|
|
|
|
Box::new(AggregateKind::Closure(closure_id.to_def_id(), args))
|
2018-05-02 13:14:30 +02:00
|
|
|
}
|
2024-01-24 18:01:56 +00:00
|
|
|
UpvarArgs::CoroutineClosure(args) => {
|
|
|
|
Box::new(AggregateKind::CoroutineClosure(closure_id.to_def_id(), args))
|
|
|
|
}
|
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 { .. } => {
|
2024-07-06 11:45:47 +10:00
|
|
|
block = this.stmt_expr(block, expr_id, None).into_block();
|
2023-09-20 20:51:14 +02:00
|
|
|
block.and(Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
|
2020-04-09 12:24:53 +02:00
|
|
|
span: expr_span,
|
|
|
|
user_ty: None,
|
2023-09-20 20:51:14 +02:00
|
|
|
const_: Const::zero_sized(this.tcx.types.unit),
|
2021-08-05 03:16:19 +02:00
|
|
|
}))))
|
2016-04-16 19:45:28 +12:00
|
|
|
}
|
2022-03-09 17:10:48 +00:00
|
|
|
|
2023-05-17 22:10:36 +02:00
|
|
|
ExprKind::OffsetOf { container, fields } => {
|
|
|
|
block.and(Rvalue::NullaryOp(NullOp::OffsetOf(fields), container))
|
|
|
|
}
|
2022-09-11 00:37:49 -07:00
|
|
|
|
2022-03-09 17:10:48 +00:00
|
|
|
ExprKind::Literal { .. }
|
2022-03-11 12:07:53 +01:00
|
|
|
| ExprKind::NamedConst { .. }
|
2022-03-23 08:47:11 +01:00
|
|
|
| ExprKind::NonHirLiteral { .. }
|
2022-07-03 11:17:23 -04:00
|
|
|
| ExprKind::ZstLiteral { .. }
|
2022-03-11 12:07:53 +01:00
|
|
|
| ExprKind::ConstParam { .. }
|
2020-10-06 17:51:15 -03:00
|
|
|
| ExprKind::ConstBlock { .. }
|
2022-03-09 17:10:48 +00:00
|
|
|
| ExprKind::StaticRef { .. } => {
|
|
|
|
let constant = this.as_constant(expr);
|
|
|
|
block.and(Rvalue::Use(Operand::Constant(Box::new(constant))))
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::Yield { .. }
|
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 { .. }
|
2024-08-12 10:57:57 +02:00
|
|
|
| ExprKind::RawBorrow { .. }
|
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 { .. }
|
2021-08-08 11:49:13 -03:00
|
|
|
| ExprKind::Let { .. }
|
2018-09-06 22:34:26 +01:00
|
|
|
| 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 { .. }
|
2022-11-21 12:47:42 +00:00
|
|
|
| ExprKind::Become { .. }
|
2020-02-14 18:17:50 +00:00
|
|
|
| ExprKind::InlineAsm { .. }
|
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),
|
2022-03-09 17:10:48 +00:00
|
|
|
Some(Category::Rvalue(RvalueFunc::AsRvalue) | Category::Constant)
|
2021-01-09 12:00:45 -05:00
|
|
|
));
|
2023-03-18 00:29:53 -05:00
|
|
|
let operand = unpack!(
|
2023-12-15 15:16:24 +00:00
|
|
|
block = this.as_operand(
|
|
|
|
block,
|
|
|
|
scope,
|
|
|
|
expr_id,
|
|
|
|
LocalInfo::Boring,
|
|
|
|
NeedsTemporary::No,
|
|
|
|
)
|
2023-03-18 00:29:53 -05:00
|
|
|
);
|
2015-08-18 17:59:21 -04:00
|
|
|
block.and(Rvalue::Use(operand))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(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;
|
2023-02-13 21:08:15 +00:00
|
|
|
let rvalue = match op {
|
|
|
|
BinOp::Add | BinOp::Sub | BinOp::Mul if self.check_overflow && ty.is_integral() => {
|
2023-07-05 20:13:26 +01:00
|
|
|
let result_tup = Ty::new_tup(self.tcx, &[ty, bool_ty]);
|
2023-02-13 21:08:15 +00:00
|
|
|
let result_value = self.temp(result_tup, span);
|
|
|
|
|
2024-05-16 02:07:31 -07:00
|
|
|
let op_with_overflow = op.wrapping_to_overflowing().unwrap();
|
|
|
|
|
2023-02-13 21:08:15 +00:00
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
|
|
|
result_value,
|
2024-05-16 02:07:31 -07:00
|
|
|
Rvalue::BinaryOp(op_with_overflow, Box::new((lhs.to_copy(), rhs.to_copy()))),
|
2023-02-13 21:08:15 +00:00
|
|
|
);
|
2024-04-03 17:49:59 +03:00
|
|
|
let val_fld = FieldIdx::ZERO;
|
2023-03-28 12:32:57 -07:00
|
|
|
let of_fld = FieldIdx::new(1);
|
2023-02-13 21:08:15 +00:00
|
|
|
|
|
|
|
let tcx = self.tcx;
|
|
|
|
let val = tcx.mk_place_field(result_value, val_fld, ty);
|
|
|
|
let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
|
|
|
|
|
|
|
|
let err = AssertKind::Overflow(op, lhs, rhs);
|
|
|
|
block = self.assert(block, Operand::Move(of), false, err, span);
|
|
|
|
|
|
|
|
Rvalue::Use(Operand::Move(val))
|
|
|
|
}
|
|
|
|
BinOp::Shl | BinOp::Shr if self.check_overflow && ty.is_integral() => {
|
2023-03-22 01:06:19 -07:00
|
|
|
// For an unsigned RHS, the shift is in-range for `rhs < bits`.
|
|
|
|
// For a signed RHS, `IntToInt` cast to the equivalent unsigned
|
2023-11-12 12:16:41 +01:00
|
|
|
// type and do that same comparison.
|
|
|
|
// A negative value will be *at least* 128 after the cast (that's i8::MIN),
|
|
|
|
// and 128 is an overflowing shift amount for all our currently existing types,
|
|
|
|
// so this cast can never make us miss an overflow.
|
2023-03-22 01:06:19 -07:00
|
|
|
let (lhs_size, _) = ty.int_size_and_signed(self.tcx);
|
2023-11-12 12:16:41 +01:00
|
|
|
assert!(lhs_size.bits() <= 128);
|
2023-02-13 21:08:15 +00:00
|
|
|
let rhs_ty = rhs.ty(&self.local_decls, self.tcx);
|
|
|
|
let (rhs_size, _) = rhs_ty.int_size_and_signed(self.tcx);
|
2023-03-22 01:06:19 -07:00
|
|
|
|
|
|
|
let (unsigned_rhs, unsigned_ty) = match rhs_ty.kind() {
|
|
|
|
ty::Uint(_) => (rhs.to_copy(), rhs_ty),
|
|
|
|
ty::Int(int_width) => {
|
2023-07-05 20:13:26 +01:00
|
|
|
let uint_ty = Ty::new_uint(self.tcx, int_width.to_unsigned());
|
2023-03-22 01:06:19 -07:00
|
|
|
let rhs_temp = self.temp(uint_ty, span);
|
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
|
|
|
rhs_temp,
|
|
|
|
Rvalue::Cast(CastKind::IntToInt, rhs.to_copy(), uint_ty),
|
|
|
|
);
|
|
|
|
(Operand::Move(rhs_temp), uint_ty)
|
|
|
|
}
|
|
|
|
_ => unreachable!("only integers are shiftable"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// This can't overflow because the largest shiftable types are 128-bit,
|
|
|
|
// which fits in `u8`, the smallest possible `unsigned_ty`.
|
|
|
|
let lhs_bits = Operand::const_from_scalar(
|
2023-02-13 21:08:15 +00:00
|
|
|
self.tcx,
|
2023-03-22 01:06:19 -07:00
|
|
|
unsigned_ty,
|
|
|
|
Scalar::from_uint(lhs_size.bits(), rhs_size),
|
2023-02-13 21:08:15 +00:00
|
|
|
span,
|
|
|
|
);
|
|
|
|
|
2023-03-22 01:06:19 -07:00
|
|
|
let inbounds = self.temp(bool_ty, span);
|
2023-02-13 21:08:15 +00:00
|
|
|
self.cfg.push_assign(
|
|
|
|
block,
|
|
|
|
source_info,
|
2023-03-22 01:06:19 -07:00
|
|
|
inbounds,
|
|
|
|
Rvalue::BinaryOp(BinOp::Lt, Box::new((unsigned_rhs, lhs_bits))),
|
2023-02-13 21:08:15 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
|
2023-03-22 01:06:19 -07:00
|
|
|
block = self.assert(block, Operand::Move(inbounds), true, overflow_err, span);
|
2023-02-13 21:08:15 +00:00
|
|
|
Rvalue::BinaryOp(op, Box::new((lhs, rhs)))
|
|
|
|
}
|
|
|
|
BinOp::Div | BinOp::Rem if ty.is_integral() => {
|
2016-03-31 18:50:07 +13:00
|
|
|
// 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-08-05 03:16:19 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, Box::new((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-08-05 03:16:19 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, Box::new((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-08-05 03:16:19 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::Eq, Box::new((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-08-05 03:16:19 +02:00
|
|
|
Rvalue::BinaryOp(BinOp::BitAnd, Box::new((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
|
|
|
}
|
|
|
|
|
2023-02-13 21:08:15 +00:00
|
|
|
Rvalue::BinaryOp(op, Box::new((lhs, rhs)))
|
|
|
|
}
|
|
|
|
_ => Rvalue::BinaryOp(op, Box::new((lhs, rhs))),
|
|
|
|
};
|
|
|
|
block.and(rvalue)
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
|
2022-04-13 07:08:58 -04:00
|
|
|
fn build_zero_repeat(
|
|
|
|
&mut self,
|
|
|
|
mut block: BasicBlock,
|
|
|
|
value: ExprId,
|
2024-09-02 01:13:07 +08:00
|
|
|
scope: TempLifetime,
|
2022-04-13 07:08:58 -04:00
|
|
|
outer_source_info: SourceInfo,
|
|
|
|
) -> BlockAnd<Rvalue<'tcx>> {
|
|
|
|
let this = self;
|
2023-12-15 15:16:24 +00:00
|
|
|
let value_expr = &this.thir[value];
|
|
|
|
let elem_ty = value_expr.ty;
|
|
|
|
if let Some(Category::Constant) = Category::of(&value_expr.kind) {
|
2022-04-13 07:08:58 -04:00
|
|
|
// Repeating a const does nothing
|
|
|
|
} else {
|
|
|
|
// For a non-const, we may need to generate an appropriate `Drop`
|
2023-03-18 00:29:53 -05:00
|
|
|
let value_operand = unpack!(
|
|
|
|
block = this.as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No)
|
|
|
|
);
|
2022-04-13 07:08:58 -04:00
|
|
|
if let Operand::Move(to_drop) = value_operand {
|
|
|
|
let success = this.cfg.start_new_block();
|
2022-10-08 23:47:59 +01:00
|
|
|
this.cfg.terminate(block, outer_source_info, TerminatorKind::Drop {
|
|
|
|
place: to_drop,
|
|
|
|
target: success,
|
|
|
|
unwind: UnwindAction::Continue,
|
2023-05-25 17:30:23 +00:00
|
|
|
replace: false,
|
2022-04-13 07:08:58 -04:00
|
|
|
});
|
|
|
|
this.diverge_from(block);
|
|
|
|
block = success;
|
|
|
|
}
|
2024-01-12 08:21:42 +01:00
|
|
|
this.record_operands_moved(&[Spanned { node: value_operand, span: DUMMY_SP }]);
|
2022-04-13 07:08:58 -04:00
|
|
|
}
|
2023-04-01 20:11:38 -07:00
|
|
|
block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(elem_ty)), IndexVec::new()))
|
2022-04-13 07:08:58 -04: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,
|
2023-12-15 15:16:24 +00:00
|
|
|
arg: ExprId,
|
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));
|
|
|
|
|
2022-12-19 15:30:43 +00:00
|
|
|
let mutability = match arg_place_builder.base() {
|
2020-11-26 00:07:41 -05:00
|
|
|
// 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.
|
2022-12-19 15:30:43 +00: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.
|
2022-12-19 15:30:43 +00:00
|
|
|
PlaceBase::Upvar { .. } => {
|
2022-11-17 18:55:06 -06:00
|
|
|
let enclosing_upvars_resolved = arg_place_builder.to_place(this);
|
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!(
|
2021-05-21 21:01:27 +02:00
|
|
|
local == ty::CAPTURE_STRUCT_LOCAL,
|
2023-07-25 23:17:39 +02:00
|
|
|
"Expected local to be Local(1), found {local:?}"
|
2021-01-09 12:00:45 -05:00
|
|
|
);
|
|
|
|
// Not in a closure
|
|
|
|
debug_assert!(
|
2022-08-23 22:22:15 +02:00
|
|
|
this.upvars.len() > upvar_index.index(),
|
|
|
|
"Unexpected capture place, upvars={:#?}, upvar_index={:?}",
|
|
|
|
this.upvars,
|
2021-01-09 12:00:45 -05:00
|
|
|
upvar_index
|
|
|
|
);
|
2022-08-23 22:22:15 +02:00
|
|
|
this.upvars[upvar_index.index()].mutability
|
2021-01-09 12:00:45 -05:00
|
|
|
}
|
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 {
|
2023-05-31 06:25:27 +08:00
|
|
|
Mutability::Not => BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture },
|
|
|
|
Mutability::Mut => BorrowKind::Mut { kind: MutBorrowKind::Default },
|
2018-07-15 15:00:58 +01:00
|
|
|
};
|
|
|
|
|
2022-11-17 18:55:06 -06:00
|
|
|
let arg_place = arg_place_builder.to_place(this);
|
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> {
|
2024-11-15 13:53:31 +01:00
|
|
|
let typing_env = ty::TypingEnv::fully_monomorphized();
|
|
|
|
let size = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size;
|
|
|
|
let literal = Const::from_bits(self.tcx, size.unsigned_int_max(), typing_env, 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());
|
2024-11-15 13:53:31 +01:00
|
|
|
let typing_env = ty::TypingEnv::fully_monomorphized();
|
|
|
|
let bits = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size.bits();
|
2018-02-21 22:02:52 +01:00
|
|
|
let n = 1 << (bits - 1);
|
2024-11-15 13:53:31 +01:00
|
|
|
let literal = Const::from_bits(self.tcx, n, typing_env, 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
|
|
|
}
|