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::region;
9 use rustc::mir::interpret::PanicInfo;
11 use rustc::ty::{self, Ty, UpvarSubsts};
14 impl<'a, 'tcx> Builder<'a, 'tcx> {
15 /// Returns an rvalue suitable for use until the end of the current
18 /// The operand returned from this function will *not be valid* after
19 /// an ExprKind::Scope is passed, so please do *not* return it from
20 /// functions to avoid bad miscompiles.
21 pub fn as_local_rvalue<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Rvalue<'tcx>>
23 M: Mirror<'tcx, Output = Expr<'tcx>>,
25 let local_scope = self.local_scope();
26 self.as_rvalue(block, local_scope, expr)
29 /// Compile `expr`, yielding an rvalue.
33 scope: Option<region::Scope>,
35 ) -> BlockAnd<Rvalue<'tcx>>
37 M: Mirror<'tcx, Output = Expr<'tcx>>,
39 let expr = self.hir.mirror(expr);
40 self.expr_as_rvalue(block, scope, expr)
45 mut block: BasicBlock,
46 scope: Option<region::Scope>,
48 ) -> BlockAnd<Rvalue<'tcx>> {
49 debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
52 let expr_span = expr.span;
53 let source_info = this.source_info(expr_span);
56 ExprKind::Scope { region_scope, lint_level, value } => {
57 let region_scope = (region_scope, source_info);
58 this.in_scope(region_scope, lint_level, |this| this.as_rvalue(block, scope, value))
60 ExprKind::Repeat { value, count } => {
61 let value_operand = unpack!(block = this.as_operand(block, scope, value));
62 block.and(Rvalue::Repeat(value_operand, count))
64 ExprKind::Binary { op, lhs, rhs } => {
65 let lhs = unpack!(block = this.as_operand(block, scope, lhs));
66 let rhs = unpack!(block = this.as_operand(block, scope, rhs));
67 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
69 ExprKind::Unary { op, arg } => {
70 let arg = unpack!(block = this.as_operand(block, scope, arg));
71 // Check for -MIN on signed integers
72 if this.hir.check_overflow() && op == UnOp::Neg && expr.ty.is_signed() {
73 let bool_ty = this.hir.bool_ty();
75 let minval = this.minval_literal(expr_span, expr.ty);
76 let is_min = this.temp(bool_ty, expr_span);
82 Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval),
87 Operand::Move(is_min),
89 PanicInfo::OverflowNeg,
93 block.and(Rvalue::UnaryOp(op, arg))
95 ExprKind::Box { value } => {
96 let value = this.hir.mirror(value);
97 // The `Box<T>` temporary created here is not a part of the HIR,
98 // and therefore is not considered during generator OIBIT
99 // determination. See the comment about `box` at `yield_in_scope`.
100 let result = this.local_decls.push(LocalDecl::new_internal(expr.ty, expr_span));
103 Statement { source_info, kind: StatementKind::StorageLive(result) },
105 if let Some(scope) = scope {
106 // schedule a shallow free of that memory, lest we unwind:
107 this.schedule_drop_storage_and_value(expr_span, scope, result);
110 // malloc some memory of suitable type (thus far, uninitialized):
111 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
112 this.cfg.push_assign(block, source_info, &Place::from(result), box_);
114 // initialize the box contents:
117 &this.hir.tcx().mk_place_deref(Place::from(result)),
122 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
124 ExprKind::Cast { source } => {
125 let source = unpack!(block = this.as_operand(block, scope, source));
126 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
128 ExprKind::Pointer { cast, source } => {
129 let source = unpack!(block = this.as_operand(block, scope, source));
130 block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
132 ExprKind::Array { fields } => {
133 // (*) We would (maybe) be closer to codegen if we
134 // handled this and other aggregate cases via
135 // `into()`, not `as_rvalue` -- in that case, instead
140 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
142 // we could just generate
147 // The problem is that then we would need to:
149 // (a) have a more complex mechanism for handling
151 // (b) distinguish the case where the type `Foo` has a
152 // destructor, in which case creating an instance
153 // as a whole "arms" the destructor, and you can't
154 // write individual fields; and,
155 // (c) handle the case where the type Foo has no
156 // fields. We don't want `let x: ();` to compile
157 // to the same MIR as `let x = ();`.
159 // first process the set of fields
160 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
161 let fields: Vec<_> = fields
163 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
166 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
168 ExprKind::Tuple { fields } => {
170 // first process the set of fields
171 let fields: Vec<_> = fields
173 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
176 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
178 ExprKind::Closure { closure_id, substs, upvars, movability } => {
180 let operands: Vec<_> = upvars
183 let upvar = this.hir.mirror(upvar);
184 match Category::of(&upvar.kind) {
185 // Use as_place to avoid creating a temporary when
186 // moving a variable into a closure, so that
187 // borrowck knows which variables to mark as being
188 // used as mut. This is OK here because the upvar
189 // expressions have no side effects and act on
191 // This occurs when capturing by copy/move, while
192 // by reference captures use as_operand
193 Some(Category::Place) => {
194 let place = unpack!(block = this.as_place(block, upvar));
195 this.consume_by_copy_or_move(place)
198 // Turn mutable borrow captures into unique
199 // borrow captures when capturing an immutable
200 // variable. This is sound because the mutation
201 // that caused the capture will cause an error.
205 BorrowKind::Mut { allow_two_phase_borrow: false },
208 block = this.limit_capture_mutability(
209 upvar.span, upvar.ty, scope, block, arg,
212 _ => unpack!(block = this.as_operand(block, scope, upvar)),
218 let result = match substs {
219 UpvarSubsts::Generator(substs) => {
220 // We implicitly set the discriminant to 0. See
221 // librustc_mir/transform/deaggregator.rs for details.
222 let movability = movability.unwrap();
223 box AggregateKind::Generator(closure_id, substs, movability)
225 UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
227 block.and(Rvalue::Aggregate(result, operands))
229 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
230 block = unpack!(this.stmt_expr(block, expr, None));
231 block.and(this.unit_rvalue())
233 ExprKind::Yield { value } => {
234 let value = unpack!(block = this.as_operand(block, scope, value));
235 let resume = this.cfg.start_new_block();
236 let cleanup = this.generator_drop_cleanup();
240 TerminatorKind::Yield { value: value, resume: resume, drop: cleanup },
242 resume.and(this.unit_rvalue())
244 ExprKind::Literal { .. }
245 | ExprKind::StaticRef { .. }
246 | ExprKind::Block { .. }
247 | ExprKind::Match { .. }
248 | ExprKind::NeverToAny { .. }
249 | ExprKind::Use { .. }
250 | ExprKind::Borrow { .. }
251 | ExprKind::AddressOf { .. }
252 | ExprKind::Adt { .. }
253 | ExprKind::Loop { .. }
254 | ExprKind::LogicalOp { .. }
255 | ExprKind::Call { .. }
256 | ExprKind::Field { .. }
257 | ExprKind::Deref { .. }
258 | ExprKind::Index { .. }
259 | ExprKind::VarRef { .. }
261 | ExprKind::Break { .. }
262 | ExprKind::Continue { .. }
263 | ExprKind::Return { .. }
264 | ExprKind::InlineAsm { .. }
265 | ExprKind::PlaceTypeAscription { .. }
266 | ExprKind::ValueTypeAscription { .. } => {
267 // these do not have corresponding `Rvalue` variants,
268 // so make an operand and then return that
269 debug_assert!(match Category::of(&expr.kind) {
270 Some(Category::Rvalue(RvalueFunc::AsRvalue)) => false,
273 let operand = unpack!(block = this.as_operand(block, scope, expr));
274 block.and(Rvalue::Use(operand))
279 pub fn build_binary_op(
281 mut block: BasicBlock,
287 ) -> BlockAnd<Rvalue<'tcx>> {
288 let source_info = self.source_info(span);
289 let bool_ty = self.hir.bool_ty();
290 if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
291 let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
292 let result_value = self.temp(result_tup, span);
294 self.cfg.push_assign(
298 Rvalue::CheckedBinaryOp(op, lhs, rhs),
300 let val_fld = Field::new(0);
301 let of_fld = Field::new(1);
303 let tcx = self.hir.tcx();
304 let val = tcx.mk_place_field(result_value.clone(), val_fld, ty);
305 let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
307 let err = PanicInfo::Overflow(op);
309 block = self.assert(block, Operand::Move(of), false, err, span);
311 block.and(Rvalue::Use(Operand::Move(val)))
313 if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
314 // Checking division and remainder is more complex, since we 1. always check
315 // and 2. there are two possible failure cases, divide-by-zero and overflow.
317 let zero_err = if op == BinOp::Div {
318 PanicInfo::DivisionByZero
320 PanicInfo::RemainderByZero
322 let overflow_err = PanicInfo::Overflow(op);
325 let is_zero = self.temp(bool_ty, span);
326 let zero = self.zero_literal(span, ty);
327 self.cfg.push_assign(
331 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero),
334 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
336 // We only need to check for the overflow in one case:
337 // MIN / -1, and only for signed values.
339 let neg_1 = self.neg_1_literal(span, ty);
340 let min = self.minval_literal(span, ty);
342 let is_neg_1 = self.temp(bool_ty, span);
343 let is_min = self.temp(bool_ty, span);
344 let of = self.temp(bool_ty, span);
346 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
348 self.cfg.push_assign(
352 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1),
354 self.cfg.push_assign(
358 Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min),
361 let is_neg_1 = Operand::Move(is_neg_1);
362 let is_min = Operand::Move(is_min);
363 self.cfg.push_assign(
367 Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min),
370 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
374 block.and(Rvalue::BinaryOp(op, lhs, rhs))
378 fn limit_capture_mutability(
382 temp_lifetime: Option<region::Scope>,
383 mut block: BasicBlock,
385 ) -> BlockAnd<Operand<'tcx>> {
388 let source_info = this.source_info(upvar_span);
389 let temp = this.local_decls.push(LocalDecl::new_temp(upvar_ty, upvar_span));
391 this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
393 let arg_place = unpack!(block = this.as_place(block, arg));
395 let mutability = match arg_place.as_ref() {
396 PlaceRef { base: &PlaceBase::Local(local), projection: &[] } => {
397 this.local_decls[local].mutability
399 PlaceRef { base: &PlaceBase::Local(local), projection: &[ProjectionElem::Deref] } => {
401 this.local_decls[local].is_ref_for_guard(),
402 "Unexpected capture place",
404 this.local_decls[local].mutability
408 projection: &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _)],
413 &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
415 let place = PlaceRef { base, projection: proj_base };
417 // Not projected from the implicit `self` in a closure.
419 match place.local_or_deref_local() {
420 Some(local) => local == Local::new(1),
423 "Unexpected capture place"
427 this.upvar_mutbls.len() > upvar_index.index(),
428 "Unexpected capture place"
430 this.upvar_mutbls[upvar_index.index()]
432 _ => bug!("Unexpected capture place"),
435 let borrow_kind = match mutability {
436 Mutability::Not => BorrowKind::Unique,
437 Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
440 this.cfg.push_assign(
444 Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
447 // In constants, temp_lifetime is None. We should not need to drop
448 // anything because no values with a destructor can be created in
449 // a constant at this time, even if the type may need dropping.
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)