1 //! See docs in `build/expr/mod.rs`.
3 use rustc_index::vec::Idx;
5 use crate::build::expr::as_place::PlaceBase;
6 use crate::build::expr::category::{Category, RvalueFunc};
7 use crate::build::{BlockAnd, BlockAndExtension, Builder};
8 use rustc_hir::lang_items::LangItem;
9 use rustc_middle::middle::region;
10 use rustc_middle::mir::AssertKind;
11 use rustc_middle::mir::Place;
12 use rustc_middle::mir::*;
13 use rustc_middle::thir::*;
14 use rustc_middle::ty::{self, Ty, UpvarSubsts};
17 impl<'a, 'tcx> Builder<'a, 'tcx> {
18 /// Returns an rvalue suitable for use until the end of the current
21 /// The operand returned from this function will *not be valid* after
22 /// an ExprKind::Scope is passed, so please do *not* return it from
23 /// functions to avoid bad miscompiles.
24 crate fn as_local_rvalue(
28 ) -> BlockAnd<Rvalue<'tcx>> {
29 let local_scope = self.local_scope();
30 self.as_rvalue(block, Some(local_scope), expr)
33 /// Compile `expr`, yielding an rvalue.
36 mut block: BasicBlock,
37 scope: Option<region::Scope>,
39 ) -> BlockAnd<Rvalue<'tcx>> {
40 debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
43 let expr_span = expr.span;
44 let source_info = this.source_info(expr_span);
47 ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
48 ExprKind::Scope { region_scope, lint_level, value } => {
49 let region_scope = (region_scope, source_info);
50 this.in_scope(region_scope, lint_level, |this| {
51 this.as_rvalue(block, scope, &this.thir[value])
54 ExprKind::Repeat { value, count } => {
56 unpack!(block = this.as_operand(block, scope, &this.thir[value], None));
57 block.and(Rvalue::Repeat(value_operand, count))
59 ExprKind::Binary { op, lhs, rhs } => {
60 let lhs = unpack!(block = this.as_operand(block, scope, &this.thir[lhs], None));
61 let rhs = unpack!(block = this.as_operand(block, scope, &this.thir[rhs], None));
62 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
64 ExprKind::Unary { op, arg } => {
65 let arg = unpack!(block = this.as_operand(block, scope, &this.thir[arg], None));
66 // Check for -MIN on signed integers
67 if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() {
68 let bool_ty = this.tcx.types.bool;
70 let minval = this.minval_literal(expr_span, expr.ty);
71 let is_min = this.temp(bool_ty, expr_span);
77 Rvalue::BinaryOp(BinOp::Eq, Box::new((arg.to_copy(), minval))),
82 Operand::Move(is_min),
84 AssertKind::OverflowNeg(arg.to_copy()),
88 block.and(Rvalue::UnaryOp(op, arg))
90 ExprKind::Box { value } => {
91 let value = &this.thir[value];
94 // `exchange_malloc` is unsafe but box is safe, so need a new scope.
95 let synth_scope = this.new_source_scope(
98 Some(Safety::BuiltinUnsafe),
100 let synth_info = SourceInfo { span: expr_span, scope: synth_scope };
102 let size = this.temp(tcx.types.usize, expr_span);
103 this.cfg.push_assign(
107 Rvalue::NullaryOp(NullOp::SizeOf, value.ty),
110 let align = this.temp(tcx.types.usize, expr_span);
111 this.cfg.push_assign(
115 Rvalue::NullaryOp(NullOp::AlignOf, value.ty),
118 // malloc some memory of suitable size and align:
119 let exchange_malloc = Operand::function_handle(
121 tcx.require_lang_item(LangItem::ExchangeMalloc, Some(expr_span)),
125 let storage = this.temp(tcx.mk_mut_ptr(tcx.types.u8), expr_span);
126 let success = this.cfg.start_new_block();
130 TerminatorKind::Call {
131 func: exchange_malloc,
132 args: vec![Operand::Move(size), Operand::Move(align)],
133 destination: Some((storage, success)),
135 from_hir_call: false,
139 this.diverge_from(block);
142 // The `Box<T>` temporary created here is not a part of the HIR,
143 // and therefore is not considered during generator auto-trait
144 // determination. See the comment about `box` at `yield_in_scope`.
145 let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
148 Statement { source_info, kind: StatementKind::StorageLive(result) },
150 if let Some(scope) = scope {
151 // schedule a shallow free of that memory, lest we unwind:
152 this.schedule_drop_storage_and_value(expr_span, scope, result);
155 // Transmute `*mut u8` to the box (thus far, uninitialized):
156 let box_ = Rvalue::ShallowInitBox(Operand::Move(storage), value.ty);
157 this.cfg.push_assign(block, source_info, Place::from(result), box_);
159 // initialize the box contents:
161 block = this.expr_into_dest(
162 this.tcx.mk_place_deref(Place::from(result)),
167 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
169 ExprKind::Cast { source } => {
171 unpack!(block = this.as_operand(block, scope, &this.thir[source], None));
172 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
174 ExprKind::Pointer { cast, source } => {
176 unpack!(block = this.as_operand(block, scope, &this.thir[source], None));
177 block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
179 ExprKind::Array { ref fields } => {
180 // (*) We would (maybe) be closer to codegen if we
181 // handled this and other aggregate cases via
182 // `into()`, not `as_rvalue` -- in that case, instead
187 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
189 // we could just generate
194 // The problem is that then we would need to:
196 // (a) have a more complex mechanism for handling
198 // (b) distinguish the case where the type `Foo` has a
199 // destructor, in which case creating an instance
200 // as a whole "arms" the destructor, and you can't
201 // write individual fields; and,
202 // (c) handle the case where the type Foo has no
203 // fields. We don't want `let x: ();` to compile
204 // to the same MIR as `let x = ();`.
206 // first process the set of fields
207 let el_ty = expr.ty.sequence_element_type(this.tcx);
208 let fields: Vec<_> = fields
211 .map(|f| unpack!(block = this.as_operand(block, scope, &this.thir[f], None)))
214 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(el_ty)), fields))
216 ExprKind::Tuple { ref fields } => {
218 // first process the set of fields
219 let fields: Vec<_> = fields
222 .map(|f| unpack!(block = this.as_operand(block, scope, &this.thir[f], None)))
225 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Tuple), fields))
227 ExprKind::Closure { closure_id, substs, ref upvars, movability, ref fake_reads } => {
228 // Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
229 // and push the fake reads.
230 // This must come before creating the operands. This is required in case
231 // there is a fake read and a borrow of the same path, since otherwise the
232 // fake read might interfere with the borrow. Consider an example like this
237 // &mut x; // mutable borrow of `x`
238 // match x { _ => () } // fake read of `x`
242 for (thir_place, cause, hir_id) in fake_reads.into_iter() {
244 unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
246 if let Ok(place_builder_resolved) =
247 place_builder.try_upvars_resolved(this.tcx, this.typeck_results)
250 place_builder_resolved.into_place(this.tcx, this.typeck_results);
251 this.cfg.push_fake_read(
253 this.source_info(this.tcx.hir().span(*hir_id)),
261 let operands: Vec<_> = upvars
265 let upvar = &this.thir[upvar];
266 match Category::of(&upvar.kind) {
267 // Use as_place to avoid creating a temporary when
268 // moving a variable into a closure, so that
269 // borrowck knows which variables to mark as being
270 // used as mut. This is OK here because the upvar
271 // expressions have no side effects and act on
273 // This occurs when capturing by copy/move, while
274 // by reference captures use as_operand
275 Some(Category::Place) => {
276 let place = unpack!(block = this.as_place(block, upvar));
277 this.consume_by_copy_or_move(place)
280 // Turn mutable borrow captures into unique
281 // borrow captures when capturing an immutable
282 // variable. This is sound because the mutation
283 // that caused the capture will cause an error.
287 BorrowKind::Mut { allow_two_phase_borrow: false },
290 block = this.limit_capture_mutability(
299 unpack!(block = this.as_operand(block, scope, upvar, None))
307 let result = match substs {
308 UpvarSubsts::Generator(substs) => {
309 // We implicitly set the discriminant to 0. See
310 // librustc_mir/transform/deaggregator.rs for details.
311 let movability = movability.unwrap();
312 Box::new(AggregateKind::Generator(closure_id, substs, movability))
314 UpvarSubsts::Closure(substs) => {
315 Box::new(AggregateKind::Closure(closure_id, substs))
318 block.and(Rvalue::Aggregate(result, operands))
320 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
321 block = unpack!(this.stmt_expr(block, expr, None));
322 block.and(Rvalue::Use(Operand::Constant(Box::new(Constant {
325 literal: ty::Const::zero_sized(this.tcx, this.tcx.types.unit).into(),
328 ExprKind::Yield { .. }
329 | ExprKind::Literal { .. }
330 | ExprKind::NamedConst { .. }
331 | ExprKind::ScalarLiteral { .. }
332 | ExprKind::ConstParam { .. }
333 | ExprKind::ConstBlock { .. }
334 | ExprKind::StaticRef { .. }
335 | ExprKind::Block { .. }
336 | ExprKind::Match { .. }
337 | ExprKind::If { .. }
338 | ExprKind::NeverToAny { .. }
339 | ExprKind::Use { .. }
340 | ExprKind::Borrow { .. }
341 | ExprKind::AddressOf { .. }
342 | ExprKind::Adt { .. }
343 | ExprKind::Loop { .. }
344 | ExprKind::LogicalOp { .. }
345 | ExprKind::Call { .. }
346 | ExprKind::Field { .. }
347 | ExprKind::Let { .. }
348 | ExprKind::Deref { .. }
349 | ExprKind::Index { .. }
350 | ExprKind::VarRef { .. }
351 | ExprKind::UpvarRef { .. }
352 | ExprKind::Break { .. }
353 | ExprKind::Continue { .. }
354 | ExprKind::Return { .. }
355 | ExprKind::InlineAsm { .. }
356 | ExprKind::PlaceTypeAscription { .. }
357 | ExprKind::ValueTypeAscription { .. } => {
358 // these do not have corresponding `Rvalue` variants,
359 // so make an operand and then return that
360 debug_assert!(!matches!(
361 Category::of(&expr.kind),
362 Some(Category::Rvalue(RvalueFunc::AsRvalue))
364 let operand = unpack!(block = this.as_operand(block, scope, expr, None));
365 block.and(Rvalue::Use(operand))
370 crate fn build_binary_op(
372 mut block: BasicBlock,
378 ) -> BlockAnd<Rvalue<'tcx>> {
379 let source_info = self.source_info(span);
380 let bool_ty = self.tcx.types.bool;
381 if self.check_overflow && op.is_checkable() && ty.is_integral() {
382 let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
383 let result_value = self.temp(result_tup, span);
385 self.cfg.push_assign(
389 Rvalue::CheckedBinaryOp(op, Box::new((lhs.to_copy(), rhs.to_copy()))),
391 let val_fld = Field::new(0);
392 let of_fld = Field::new(1);
395 let val = tcx.mk_place_field(result_value, val_fld, ty);
396 let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
398 let err = AssertKind::Overflow(op, lhs, rhs);
400 block = self.assert(block, Operand::Move(of), false, err, span);
402 block.and(Rvalue::Use(Operand::Move(val)))
404 if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
405 // Checking division and remainder is more complex, since we 1. always check
406 // and 2. there are two possible failure cases, divide-by-zero and overflow.
408 let zero_err = if op == BinOp::Div {
409 AssertKind::DivisionByZero(lhs.to_copy())
411 AssertKind::RemainderByZero(lhs.to_copy())
413 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
416 let is_zero = self.temp(bool_ty, span);
417 let zero = self.zero_literal(span, ty);
418 self.cfg.push_assign(
422 Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), zero))),
425 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
427 // We only need to check for the overflow in one case:
428 // MIN / -1, and only for signed values.
430 let neg_1 = self.neg_1_literal(span, ty);
431 let min = self.minval_literal(span, ty);
433 let is_neg_1 = self.temp(bool_ty, span);
434 let is_min = self.temp(bool_ty, span);
435 let of = self.temp(bool_ty, span);
437 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
439 self.cfg.push_assign(
443 Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), neg_1))),
445 self.cfg.push_assign(
449 Rvalue::BinaryOp(BinOp::Eq, Box::new((lhs.to_copy(), min))),
452 let is_neg_1 = Operand::Move(is_neg_1);
453 let is_min = Operand::Move(is_min);
454 self.cfg.push_assign(
458 Rvalue::BinaryOp(BinOp::BitAnd, Box::new((is_neg_1, is_min))),
461 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
465 block.and(Rvalue::BinaryOp(op, Box::new((lhs, rhs))))
469 fn limit_capture_mutability(
473 temp_lifetime: Option<region::Scope>,
474 mut block: BasicBlock,
476 ) -> BlockAnd<Operand<'tcx>> {
479 let source_info = this.source_info(upvar_span);
480 let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
482 this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
484 let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
486 let mutability = match arg_place_builder.base() {
487 // We are capturing a path that starts off a local variable in the parent.
488 // The mutability of the current capture is same as the mutability
489 // of the local declaration in the parent.
490 PlaceBase::Local(local) => this.local_decls[local].mutability,
491 // Parent is a closure and we are capturing a path that is captured
492 // by the parent itself. The mutability of the current capture
493 // is same as that of the capture in the parent closure.
494 PlaceBase::Upvar { .. } => {
495 let enclosing_upvars_resolved =
496 arg_place_builder.clone().into_place(this.tcx, this.typeck_results);
498 match enclosing_upvars_resolved.as_ref() {
501 projection: &[ProjectionElem::Field(upvar_index, _), ..],
506 &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
510 local == ty::CAPTURE_STRUCT_LOCAL,
511 "Expected local to be Local(1), found {:?}",
516 this.upvar_mutbls.len() > upvar_index.index(),
517 "Unexpected capture place, upvar_mutbls={:#?}, upvar_index={:?}",
521 this.upvar_mutbls[upvar_index.index()]
523 _ => bug!("Unexpected capture place"),
528 let borrow_kind = match mutability {
529 Mutability::Not => BorrowKind::Unique,
530 Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
533 let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);
535 this.cfg.push_assign(
539 Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
542 // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
543 // this can be `None`.
544 if let Some(temp_lifetime) = temp_lifetime {
545 this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
548 block.and(Operand::Move(Place::from(temp)))
551 // Helper to get a `-1` value of the appropriate type
552 fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
553 let param_ty = ty::ParamEnv::empty().and(ty);
554 let size = self.tcx.layout_of(param_ty).unwrap().size;
555 let literal = ty::Const::from_bits(self.tcx, size.unsigned_int_max(), param_ty);
557 self.literal_operand(span, literal)
560 // Helper to get the minimum value of the appropriate type
561 fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
562 assert!(ty.is_signed());
563 let param_ty = ty::ParamEnv::empty().and(ty);
564 let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
565 let n = 1 << (bits - 1);
566 let literal = ty::Const::from_bits(self.tcx, n, param_ty);
568 self.literal_operand(span, literal)