1 //! See docs in `build/expr/mod.rs`.
3 use rustc_index::vec::Idx;
5 use crate::build::expr::category::{Category, RvalueFunc};
6 use crate::build::{BlockAnd, BlockAndExtension, Builder};
8 use rustc_middle::middle::region;
9 use rustc_middle::mir::AssertKind;
10 use rustc_middle::mir::*;
11 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<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Rvalue<'tcx>>
25 M: Mirror<'tcx, Output = Expr<'tcx>>,
27 let local_scope = self.local_scope();
28 self.as_rvalue(block, Some(local_scope), expr)
31 /// Compile `expr`, yielding an rvalue.
35 scope: Option<region::Scope>,
37 ) -> BlockAnd<Rvalue<'tcx>>
39 M: Mirror<'tcx, Output = Expr<'tcx>>,
41 let expr = self.hir.mirror(expr);
42 self.expr_as_rvalue(block, scope, expr)
47 mut block: BasicBlock,
48 scope: Option<region::Scope>,
50 ) -> BlockAnd<Rvalue<'tcx>> {
51 debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
54 let expr_span = expr.span;
55 let source_info = this.source_info(expr_span);
58 ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
59 ExprKind::Scope { region_scope, lint_level, value } => {
60 let region_scope = (region_scope, source_info);
61 this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
63 ExprKind::Repeat { value, count } => {
64 let value_operand = unpack!(block = this.as_operand(block, scope, value));
65 block.and(Rvalue::Repeat(value_operand, count))
67 ExprKind::Binary { op, lhs, rhs } => {
68 let lhs = unpack!(block = this.as_operand(block, scope, lhs));
69 let rhs = unpack!(block = this.as_operand(block, scope, rhs));
70 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
72 ExprKind::Unary { op, arg } => {
73 let arg = unpack!(block = this.as_operand(block, scope, arg));
74 // Check for -MIN on signed integers
75 if this.hir.check_overflow() && op == UnOp::Neg && expr.ty.is_signed() {
76 let bool_ty = this.hir.bool_ty();
78 let minval = this.minval_literal(expr_span, expr.ty);
79 let is_min = this.temp(bool_ty, expr_span);
85 Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval),
90 Operand::Move(is_min),
92 AssertKind::OverflowNeg(arg.to_copy()),
96 block.and(Rvalue::UnaryOp(op, arg))
98 ExprKind::Box { value } => {
99 let value = this.hir.mirror(value);
100 // The `Box<T>` temporary created here is not a part of the HIR,
101 // and therefore is not considered during generator auto-trait
102 // determination. See the comment about `box` at `yield_in_scope`.
103 let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
106 Statement { source_info, kind: StatementKind::StorageLive(result) },
108 if let Some(scope) = scope {
109 // schedule a shallow free of that memory, lest we unwind:
110 this.schedule_drop_storage_and_value(expr_span, scope, result);
113 // malloc some memory of suitable type (thus far, uninitialized):
114 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
115 this.cfg.push_assign(block, source_info, Place::from(result), box_);
117 // Initialize the box contents. No scope is needed since the
118 // `Box` is already scheduled to be dropped.
121 this.hir.tcx().mk_place_deref(Place::from(result)),
127 let result_operand = Operand::Move(Place::from(result));
128 this.record_operands_moved(slice::from_ref(&result_operand));
129 block.and(Rvalue::Use(result_operand))
131 ExprKind::Cast { source } => {
132 let source = unpack!(block = this.as_operand(block, scope, source));
133 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
135 ExprKind::Pointer { cast, source } => {
136 let source = unpack!(block = this.as_operand(block, scope, source));
137 block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
139 ExprKind::Array { fields } => {
140 // (*) We would (maybe) be closer to codegen if we
141 // handled this and other aggregate cases via
142 // `into()`, not `as_rvalue` -- in that case, instead
147 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
149 // we could just generate
154 // The problem is that then we would need to:
156 // (a) have a more complex mechanism for handling
158 // (b) distinguish the case where the type `Foo` has a
159 // destructor, in which case creating an instance
160 // as a whole "arms" the destructor, and you can't
161 // write individual fields; and,
162 // (c) handle the case where the type Foo has no
163 // fields. We don't want `let x: ();` to compile
164 // to the same MIR as `let x = ();`.
166 // first process the set of fields
167 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
168 let fields: Vec<_> = fields
170 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
173 this.record_operands_moved(&fields);
174 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
176 ExprKind::Tuple { fields } => {
178 // first process the set of fields
179 let fields: Vec<_> = fields
181 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
184 this.record_operands_moved(&fields);
185 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
187 ExprKind::Closure { closure_id, substs, upvars, movability } => {
189 let operands: Vec<_> = upvars
192 let upvar = this.hir.mirror(upvar);
193 match Category::of(&upvar.kind) {
194 // Use as_place to avoid creating a temporary when
195 // moving a variable into a closure, so that
196 // borrowck knows which variables to mark as being
197 // used as mut. This is OK here because the upvar
198 // expressions have no side effects and act on
200 // This occurs when capturing by copy/move, while
201 // by reference captures use as_operand
202 Some(Category::Place) => {
203 let place = unpack!(block = this.as_place(block, upvar));
204 this.consume_by_copy_or_move(place)
207 // Turn mutable borrow captures into unique
208 // borrow captures when capturing an immutable
209 // variable. This is sound because the mutation
210 // that caused the capture will cause an error.
214 BorrowKind::Mut { allow_two_phase_borrow: false },
217 block = this.limit_capture_mutability(
218 upvar.span, upvar.ty, scope, block, arg,
221 _ => unpack!(block = this.as_operand(block, scope, upvar)),
227 let result = match substs {
228 UpvarSubsts::Generator(substs) => {
229 // We implicitly set the discriminant to 0. See
230 // librustc_mir/transform/deaggregator.rs for details.
231 let movability = movability.unwrap();
232 box AggregateKind::Generator(closure_id, substs, movability)
234 UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
236 this.record_operands_moved(&operands);
237 block.and(Rvalue::Aggregate(result, operands))
239 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
240 block = unpack!(this.stmt_expr(block, expr, None));
241 block.and(Rvalue::Use(Operand::Constant(box Constant {
244 literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
247 ExprKind::Yield { .. }
248 | ExprKind::Literal { .. }
249 | ExprKind::ConstBlock { .. }
250 | ExprKind::StaticRef { .. }
251 | ExprKind::Block { .. }
252 | ExprKind::Match { .. }
253 | ExprKind::NeverToAny { .. }
254 | ExprKind::Use { .. }
255 | ExprKind::Borrow { .. }
256 | ExprKind::AddressOf { .. }
257 | ExprKind::Adt { .. }
258 | ExprKind::Loop { .. }
259 | ExprKind::LogicalOp { .. }
260 | ExprKind::Call { .. }
261 | ExprKind::Field { .. }
262 | ExprKind::Deref { .. }
263 | ExprKind::Index { .. }
264 | ExprKind::VarRef { .. }
265 | ExprKind::UpvarRef { .. }
266 | ExprKind::Break { .. }
267 | ExprKind::Continue { .. }
268 | ExprKind::Return { .. }
269 | ExprKind::InlineAsm { .. }
270 | ExprKind::LlvmInlineAsm { .. }
271 | ExprKind::PlaceTypeAscription { .. }
272 | ExprKind::ValueTypeAscription { .. } => {
273 // these do not have corresponding `Rvalue` variants,
274 // so make an operand and then return that
275 debug_assert!(!matches!(Category::of(&expr.kind), Some(Category::Rvalue(RvalueFunc::AsRvalue))));
276 let operand = unpack!(block = this.as_operand(block, scope, expr));
277 block.and(Rvalue::Use(operand))
282 crate fn build_binary_op(
284 mut block: BasicBlock,
290 ) -> BlockAnd<Rvalue<'tcx>> {
291 let source_info = self.source_info(span);
292 let bool_ty = self.hir.bool_ty();
293 if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
294 let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
295 let result_value = self.temp(result_tup, span);
297 self.cfg.push_assign(
301 Rvalue::CheckedBinaryOp(op, lhs.to_copy(), rhs.to_copy()),
303 let val_fld = Field::new(0);
304 let of_fld = Field::new(1);
306 let tcx = self.hir.tcx();
307 let val = tcx.mk_place_field(result_value, val_fld, ty);
308 let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
310 let err = AssertKind::Overflow(op, lhs, rhs);
312 block = self.assert(block, Operand::Move(of), false, err, span);
314 block.and(Rvalue::Use(Operand::Move(val)))
316 if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
317 // Checking division and remainder is more complex, since we 1. always check
318 // and 2. there are two possible failure cases, divide-by-zero and overflow.
320 let zero_err = if op == BinOp::Div {
321 AssertKind::DivisionByZero(lhs.to_copy())
323 AssertKind::RemainderByZero(lhs.to_copy())
325 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
328 let is_zero = self.temp(bool_ty, span);
329 let zero = self.zero_literal(span, ty);
330 self.cfg.push_assign(
334 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero),
337 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
339 // We only need to check for the overflow in one case:
340 // MIN / -1, and only for signed values.
342 let neg_1 = self.neg_1_literal(span, ty);
343 let min = self.minval_literal(span, ty);
345 let is_neg_1 = self.temp(bool_ty, span);
346 let is_min = self.temp(bool_ty, span);
347 let of = self.temp(bool_ty, span);
349 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
351 self.cfg.push_assign(
355 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1),
357 self.cfg.push_assign(
361 Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min),
364 let is_neg_1 = Operand::Move(is_neg_1);
365 let is_min = Operand::Move(is_min);
366 self.cfg.push_assign(
370 Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min),
373 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
377 block.and(Rvalue::BinaryOp(op, lhs, rhs))
381 fn limit_capture_mutability(
385 temp_lifetime: Option<region::Scope>,
386 mut block: BasicBlock,
388 ) -> BlockAnd<Operand<'tcx>> {
391 let source_info = this.source_info(upvar_span);
392 let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
394 this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
396 let arg_place = unpack!(block = this.as_place(block, arg));
398 let mutability = match arg_place.as_ref() {
399 PlaceRef { local, projection: &[] } => this.local_decls[local].mutability,
400 PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
402 this.local_decls[local].is_ref_for_guard(),
403 "Unexpected capture place",
405 this.local_decls[local].mutability
409 projection: &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _)],
414 &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
416 let place = PlaceRef { local, projection: proj_base };
418 // Not projected from the implicit `self` in a closure.
420 match place.local_or_deref_local() {
421 Some(local) => local == Local::new(1),
424 "Unexpected capture place"
428 this.upvar_mutbls.len() > upvar_index.index(),
429 "Unexpected capture place"
431 this.upvar_mutbls[upvar_index.index()]
433 _ => bug!("Unexpected capture place"),
436 let borrow_kind = match mutability {
437 Mutability::Not => BorrowKind::Unique,
438 Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
441 this.cfg.push_assign(
445 Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
448 // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
449 // this can be `None`.
450 if let Some(temp_lifetime) = temp_lifetime {
451 this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
454 block.and(Operand::Move(Place::from(temp)))
457 // Helper to get a `-1` value of the appropriate type
458 fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
459 let param_ty = ty::ParamEnv::empty().and(ty);
460 let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
461 let n = (!0u128) >> (128 - bits);
462 let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
464 self.literal_operand(span, literal)
467 // Helper to get the minimum value of the appropriate type
468 fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
469 assert!(ty.is_signed());
470 let param_ty = ty::ParamEnv::empty().and(ty);
471 let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
472 let n = 1 << (bits - 1);
473 let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
475 self.literal_operand(span, literal)