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_middle::middle::region;
9 use rustc_middle::mir::AssertKind;
10 use rustc_middle::mir::Place;
11 use rustc_middle::mir::*;
12 use rustc_middle::thir::*;
13 use rustc_middle::ty::{self, Ty, UpvarSubsts};
16 impl<'a, 'tcx> Builder<'a, 'tcx> {
17 /// Returns an rvalue suitable for use until the end of the current
20 /// The operand returned from this function will *not be valid* after
21 /// an ExprKind::Scope is passed, so please do *not* return it from
22 /// functions to avoid bad miscompiles.
23 crate fn as_local_rvalue(
27 ) -> BlockAnd<Rvalue<'tcx>> {
28 let local_scope = self.local_scope();
29 self.as_rvalue(block, Some(local_scope), expr)
32 /// Compile `expr`, yielding an rvalue.
35 mut block: BasicBlock,
36 scope: Option<region::Scope>,
38 ) -> BlockAnd<Rvalue<'tcx>> {
39 debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
42 let expr_span = expr.span;
43 let source_info = this.source_info(expr_span);
46 ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
47 ExprKind::Scope { region_scope, lint_level, value } => {
48 let region_scope = (region_scope, source_info);
49 this.in_scope(region_scope, lint_level, |this| {
50 this.as_rvalue(block, scope, &this.thir[value])
53 ExprKind::Repeat { value, count } => {
55 unpack!(block = this.as_operand(block, scope, &this.thir[value]));
56 block.and(Rvalue::Repeat(value_operand, count))
58 ExprKind::Binary { op, lhs, rhs } => {
59 let lhs = unpack!(block = this.as_operand(block, scope, &this.thir[lhs]));
60 let rhs = unpack!(block = this.as_operand(block, scope, &this.thir[rhs]));
61 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
63 ExprKind::Unary { op, arg } => {
64 let arg = unpack!(block = this.as_operand(block, scope, &this.thir[arg]));
65 // Check for -MIN on signed integers
66 if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() {
67 let bool_ty = this.tcx.types.bool;
69 let minval = this.minval_literal(expr_span, expr.ty);
70 let is_min = this.temp(bool_ty, expr_span);
76 Rvalue::BinaryOp(BinOp::Eq, box (arg.to_copy(), minval)),
81 Operand::Move(is_min),
83 AssertKind::OverflowNeg(arg.to_copy()),
87 block.and(Rvalue::UnaryOp(op, arg))
89 ExprKind::Box { value } => {
90 let value = &this.thir[value];
91 // The `Box<T>` temporary created here is not a part of the HIR,
92 // and therefore is not considered during generator auto-trait
93 // determination. See the comment about `box` at `yield_in_scope`.
94 let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
97 Statement { source_info, kind: StatementKind::StorageLive(result) },
99 if let Some(scope) = scope {
100 // schedule a shallow free of that memory, lest we unwind:
101 this.schedule_drop_storage_and_value(expr_span, scope, result);
104 // malloc some memory of suitable type (thus far, uninitialized):
105 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
106 this.cfg.push_assign(block, source_info, Place::from(result), box_);
108 // initialize the box contents:
110 block = this.expr_into_dest(
111 this.tcx.mk_place_deref(Place::from(result)),
116 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
118 ExprKind::Cast { source } => {
119 let source = unpack!(block = this.as_operand(block, scope, &this.thir[source]));
120 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
122 ExprKind::Pointer { cast, source } => {
123 let source = unpack!(block = this.as_operand(block, scope, &this.thir[source]));
124 block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
126 ExprKind::Array { ref fields } => {
127 // (*) We would (maybe) be closer to codegen if we
128 // handled this and other aggregate cases via
129 // `into()`, not `as_rvalue` -- in that case, instead
134 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
136 // we could just generate
141 // The problem is that then we would need to:
143 // (a) have a more complex mechanism for handling
145 // (b) distinguish the case where the type `Foo` has a
146 // destructor, in which case creating an instance
147 // as a whole "arms" the destructor, and you can't
148 // write individual fields; and,
149 // (c) handle the case where the type Foo has no
150 // fields. We don't want `let x: ();` to compile
151 // to the same MIR as `let x = ();`.
153 // first process the set of fields
154 let el_ty = expr.ty.sequence_element_type(this.tcx);
155 let fields: Vec<_> = fields
158 .map(|f| unpack!(block = this.as_operand(block, scope, &this.thir[f])))
161 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
163 ExprKind::Tuple { ref fields } => {
165 // first process the set of fields
166 let fields: Vec<_> = fields
169 .map(|f| unpack!(block = this.as_operand(block, scope, &this.thir[f])))
172 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
174 ExprKind::Closure { closure_id, substs, ref upvars, movability, ref fake_reads } => {
175 // Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
176 // and push the fake reads.
177 // This must come before creating the operands. This is required in case
178 // there is a fake read and a borrow of the same path, since otherwise the
179 // fake read might interfere with the borrow. Consider an example like this
184 // &mut x; // mutable borrow of `x`
185 // match x { _ => () } // fake read of `x`
189 // FIXME(RFC2229, rust#85435): Remove feature gate once diagnostics are
190 // improved and unsafe checking works properly in closure bodies again.
191 if this.tcx.features().capture_disjoint_fields {
192 for (thir_place, cause, hir_id) in fake_reads.into_iter() {
194 unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
196 if let Ok(place_builder_resolved) =
197 place_builder.try_upvars_resolved(this.tcx, this.typeck_results)
200 place_builder_resolved.into_place(this.tcx, this.typeck_results);
201 this.cfg.push_fake_read(
203 this.source_info(this.tcx.hir().span(*hir_id)),
212 let operands: Vec<_> = upvars
216 let upvar = &this.thir[upvar];
217 match Category::of(&upvar.kind) {
218 // Use as_place to avoid creating a temporary when
219 // moving a variable into a closure, so that
220 // borrowck knows which variables to mark as being
221 // used as mut. This is OK here because the upvar
222 // expressions have no side effects and act on
224 // This occurs when capturing by copy/move, while
225 // by reference captures use as_operand
226 Some(Category::Place) => {
227 let place = unpack!(block = this.as_place(block, upvar));
228 this.consume_by_copy_or_move(place)
231 // Turn mutable borrow captures into unique
232 // borrow captures when capturing an immutable
233 // variable. This is sound because the mutation
234 // that caused the capture will cause an error.
238 BorrowKind::Mut { allow_two_phase_borrow: false },
241 block = this.limit_capture_mutability(
249 _ => unpack!(block = this.as_operand(block, scope, upvar)),
256 let result = match substs {
257 UpvarSubsts::Generator(substs) => {
258 // We implicitly set the discriminant to 0. See
259 // librustc_mir/transform/deaggregator.rs for details.
260 let movability = movability.unwrap();
261 box AggregateKind::Generator(closure_id, substs, movability)
263 UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
265 block.and(Rvalue::Aggregate(result, operands))
267 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
268 block = unpack!(this.stmt_expr(block, expr, None));
269 block.and(Rvalue::Use(Operand::Constant(box Constant {
272 literal: ty::Const::zero_sized(this.tcx, this.tcx.types.unit).into(),
275 ExprKind::Yield { .. }
276 | ExprKind::Literal { .. }
277 | ExprKind::ConstBlock { .. }
278 | ExprKind::StaticRef { .. }
279 | ExprKind::Block { .. }
280 | ExprKind::Match { .. }
281 | ExprKind::If { .. }
282 | ExprKind::NeverToAny { .. }
283 | ExprKind::Use { .. }
284 | ExprKind::Borrow { .. }
285 | ExprKind::AddressOf { .. }
286 | ExprKind::Adt { .. }
287 | ExprKind::Loop { .. }
288 | ExprKind::LogicalOp { .. }
289 | ExprKind::Call { .. }
290 | ExprKind::Field { .. }
291 | ExprKind::Deref { .. }
292 | ExprKind::Index { .. }
293 | ExprKind::VarRef { .. }
294 | ExprKind::UpvarRef { .. }
295 | ExprKind::Break { .. }
296 | ExprKind::Continue { .. }
297 | ExprKind::Return { .. }
298 | ExprKind::InlineAsm { .. }
299 | ExprKind::LlvmInlineAsm { .. }
300 | ExprKind::PlaceTypeAscription { .. }
301 | ExprKind::ValueTypeAscription { .. } => {
302 // these do not have corresponding `Rvalue` variants,
303 // so make an operand and then return that
304 debug_assert!(!matches!(
305 Category::of(&expr.kind),
306 Some(Category::Rvalue(RvalueFunc::AsRvalue))
308 let operand = unpack!(block = this.as_operand(block, scope, expr));
309 block.and(Rvalue::Use(operand))
314 crate fn build_binary_op(
316 mut block: BasicBlock,
322 ) -> BlockAnd<Rvalue<'tcx>> {
323 let source_info = self.source_info(span);
324 let bool_ty = self.tcx.types.bool;
325 if self.check_overflow && op.is_checkable() && ty.is_integral() {
326 let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
327 let result_value = self.temp(result_tup, span);
329 self.cfg.push_assign(
333 Rvalue::CheckedBinaryOp(op, box (lhs.to_copy(), rhs.to_copy())),
335 let val_fld = Field::new(0);
336 let of_fld = Field::new(1);
339 let val = tcx.mk_place_field(result_value, val_fld, ty);
340 let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
342 let err = AssertKind::Overflow(op, lhs, rhs);
344 block = self.assert(block, Operand::Move(of), false, err, span);
346 block.and(Rvalue::Use(Operand::Move(val)))
348 if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
349 // Checking division and remainder is more complex, since we 1. always check
350 // and 2. there are two possible failure cases, divide-by-zero and overflow.
352 let zero_err = if op == BinOp::Div {
353 AssertKind::DivisionByZero(lhs.to_copy())
355 AssertKind::RemainderByZero(lhs.to_copy())
357 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
360 let is_zero = self.temp(bool_ty, span);
361 let zero = self.zero_literal(span, ty);
362 self.cfg.push_assign(
366 Rvalue::BinaryOp(BinOp::Eq, box (rhs.to_copy(), zero)),
369 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
371 // We only need to check for the overflow in one case:
372 // MIN / -1, and only for signed values.
374 let neg_1 = self.neg_1_literal(span, ty);
375 let min = self.minval_literal(span, ty);
377 let is_neg_1 = self.temp(bool_ty, span);
378 let is_min = self.temp(bool_ty, span);
379 let of = self.temp(bool_ty, span);
381 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
383 self.cfg.push_assign(
387 Rvalue::BinaryOp(BinOp::Eq, box (rhs.to_copy(), neg_1)),
389 self.cfg.push_assign(
393 Rvalue::BinaryOp(BinOp::Eq, box (lhs.to_copy(), min)),
396 let is_neg_1 = Operand::Move(is_neg_1);
397 let is_min = Operand::Move(is_min);
398 self.cfg.push_assign(
402 Rvalue::BinaryOp(BinOp::BitAnd, box (is_neg_1, is_min)),
405 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
409 block.and(Rvalue::BinaryOp(op, box (lhs, rhs)))
413 fn limit_capture_mutability(
417 temp_lifetime: Option<region::Scope>,
418 mut block: BasicBlock,
420 ) -> BlockAnd<Operand<'tcx>> {
423 let source_info = this.source_info(upvar_span);
424 let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
426 this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
428 let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
430 let mutability = match arg_place_builder.base() {
431 // We are capturing a path that starts off a local variable in the parent.
432 // The mutability of the current capture is same as the mutability
433 // of the local declaration in the parent.
434 PlaceBase::Local(local) => this.local_decls[local].mutability,
435 // Parent is a closure and we are capturing a path that is captured
436 // by the parent itself. The mutability of the current capture
437 // is same as that of the capture in the parent closure.
438 PlaceBase::Upvar { .. } => {
439 let enclosing_upvars_resolved =
440 arg_place_builder.clone().into_place(this.tcx, this.typeck_results);
442 match enclosing_upvars_resolved.as_ref() {
445 projection: &[ProjectionElem::Field(upvar_index, _), ..],
450 &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
454 local == ty::CAPTURE_STRUCT_LOCAL,
455 "Expected local to be Local(1), found {:?}",
460 this.upvar_mutbls.len() > upvar_index.index(),
461 "Unexpected capture place, upvar_mutbls={:#?}, upvar_index={:?}",
465 this.upvar_mutbls[upvar_index.index()]
467 _ => bug!("Unexpected capture place"),
472 let borrow_kind = match mutability {
473 Mutability::Not => BorrowKind::Unique,
474 Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
477 let arg_place = arg_place_builder.into_place(this.tcx, this.typeck_results);
479 this.cfg.push_assign(
483 Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
486 // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
487 // this can be `None`.
488 if let Some(temp_lifetime) = temp_lifetime {
489 this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
492 block.and(Operand::Move(Place::from(temp)))
495 // Helper to get a `-1` value of the appropriate type
496 fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
497 let param_ty = ty::ParamEnv::empty().and(ty);
498 let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
499 let n = (!0u128) >> (128 - bits);
500 let literal = ty::Const::from_bits(self.tcx, n, param_ty);
502 self.literal_operand(span, literal)
505 // Helper to get the minimum value of the appropriate type
506 fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
507 assert!(ty.is_signed());
508 let param_ty = ty::ParamEnv::empty().and(ty);
509 let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
510 let n = 1 << (bits - 1);
511 let literal = ty::Const::from_bits(self.tcx, n, param_ty);
513 self.literal_operand(span, literal)