1 //! See docs in `build/expr/mod.rs`.
3 use rustc_data_structures::fx::FxHashMap;
4 use rustc_data_structures::indexed_vec::Idx;
6 use crate::build::expr::category::{Category, RvalueFunc};
7 use crate::build::{BlockAnd, BlockAndExtension, Builder};
9 use rustc::middle::region;
10 use rustc::mir::interpret::InterpError;
12 use rustc::ty::{self, CanonicalUserTypeAnnotation, Ty, UpvarSubsts};
15 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
16 /// See comment on `as_local_operand`
17 pub fn as_local_rvalue<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Rvalue<'tcx>>
19 M: Mirror<'tcx, Output = Expr<'tcx>>,
21 let local_scope = self.local_scope();
22 self.as_rvalue(block, local_scope, expr)
25 /// Compile `expr`, yielding an rvalue.
29 scope: Option<region::Scope>,
31 ) -> BlockAnd<Rvalue<'tcx>>
33 M: Mirror<'tcx, Output = Expr<'tcx>>,
35 let expr = self.hir.mirror(expr);
36 self.expr_as_rvalue(block, scope, expr)
41 mut block: BasicBlock,
42 scope: Option<region::Scope>,
44 ) -> BlockAnd<Rvalue<'tcx>> {
46 "expr_as_rvalue(block={:?}, scope={:?}, expr={:?})",
51 let expr_span = expr.span;
52 let source_info = this.source_info(expr_span);
60 let region_scope = (region_scope, source_info);
61 this.in_scope(region_scope, lint_level, block, |this| {
62 this.as_rvalue(block, scope, value)
65 ExprKind::Repeat { value, count } => {
66 let value_operand = unpack!(block = this.as_operand(block, scope, value));
67 block.and(Rvalue::Repeat(value_operand, count))
73 let arg_place = match borrow_kind {
74 BorrowKind::Shared => unpack!(block = this.as_read_only_place(block, arg)),
75 _ => unpack!(block = this.as_place(block, arg)),
77 block.and(Rvalue::Ref(this.hir.tcx().types.re_erased, borrow_kind, arg_place))
79 ExprKind::Binary { op, lhs, rhs } => {
80 let lhs = unpack!(block = this.as_operand(block, scope, lhs));
81 let rhs = unpack!(block = this.as_operand(block, scope, rhs));
82 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
84 ExprKind::Unary { op, arg } => {
85 let arg = unpack!(block = this.as_operand(block, scope, arg));
86 // Check for -MIN on signed integers
87 if this.hir.check_overflow() && op == UnOp::Neg && expr.ty.is_signed() {
88 let bool_ty = this.hir.bool_ty();
90 let minval = this.minval_literal(expr_span, expr.ty);
91 let is_min = this.temp(bool_ty, expr_span);
97 Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval),
102 Operand::Move(is_min),
104 InterpError::OverflowNeg,
108 block.and(Rvalue::UnaryOp(op, arg))
110 ExprKind::Box { value } => {
111 let value = this.hir.mirror(value);
112 // The `Box<T>` temporary created here is not a part of the HIR,
113 // and therefore is not considered during generator OIBIT
114 // determination. See the comment about `box` at `yield_in_scope`.
117 .push(LocalDecl::new_internal(expr.ty, expr_span));
122 kind: StatementKind::StorageLive(result),
125 if let Some(scope) = scope {
126 // schedule a shallow free of that memory, lest we unwind:
127 this.schedule_drop_storage_and_value(
130 &Place::Base(PlaceBase::Local(result)),
135 // malloc some memory of suitable type (thus far, uninitialized):
136 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
138 .push_assign(block, source_info, &Place::Base(PlaceBase::Local(result)), box_);
140 // initialize the box contents:
143 &Place::Base(PlaceBase::Local(result)).deref(),
147 block.and(Rvalue::Use(Operand::Move(Place::Base(PlaceBase::Local(result)))))
149 ExprKind::Cast { source } => {
150 let source = unpack!(block = this.as_operand(block, scope, source));
151 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
153 ExprKind::Use { source } => {
154 let source = unpack!(block = this.as_operand(block, scope, source));
155 block.and(Rvalue::Use(source))
157 ExprKind::ReifyFnPointer { source } => {
158 let source = unpack!(block = this.as_operand(block, scope, source));
159 block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
161 ExprKind::UnsafeFnPointer { source } => {
162 let source = unpack!(block = this.as_operand(block, scope, source));
163 block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
165 ExprKind::ClosureFnPointer { source, unsafety } => {
166 let source = unpack!(block = this.as_operand(block, scope, source));
167 block.and(Rvalue::Cast(CastKind::ClosureFnPointer(unsafety), source, expr.ty))
169 ExprKind::MutToConstPointer { source } => {
170 let source = unpack!(block = this.as_operand(block, scope, source));
171 block.and(Rvalue::Cast(CastKind::MutToConstPointer, source, expr.ty))
173 ExprKind::Unsize { source } => {
174 let source = unpack!(block = this.as_operand(block, scope, source));
175 block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
177 ExprKind::Array { fields } => {
178 // (*) We would (maybe) be closer to codegen if we
179 // handled this and other aggregate cases via
180 // `into()`, not `as_rvalue` -- in that case, instead
185 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
187 // we could just generate
192 // The problem is that then we would need to:
194 // (a) have a more complex mechanism for handling
196 // (b) distinguish the case where the type `Foo` has a
197 // destructor, in which case creating an instance
198 // as a whole "arms" the destructor, and you can't
199 // write individual fields; and,
200 // (c) handle the case where the type Foo has no
201 // fields. We don't want `let x: ();` to compile
202 // to the same MIR as `let x = ();`.
204 // first process the set of fields
205 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
206 let fields: Vec<_> = fields
208 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
211 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
213 ExprKind::Tuple { fields } => {
215 // first process the set of fields
216 let fields: Vec<_> = fields
218 .map(|f| unpack!(block = this.as_operand(block, scope, f)))
221 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
230 let mut operands: Vec<_> = upvars
233 let upvar = this.hir.mirror(upvar);
234 match Category::of(&upvar.kind) {
235 // Use as_place to avoid creating a temporary when
236 // moving a variable into a closure, so that
237 // borrowck knows which variables to mark as being
238 // used as mut. This is OK here because the upvar
239 // expressions have no side effects and act on
241 // This occurs when capturing by copy/move, while
242 // by reference captures use as_operand
243 Some(Category::Place) => {
244 let place = unpack!(block = this.as_place(block, upvar));
245 this.consume_by_copy_or_move(place)
248 // Turn mutable borrow captures into unique
249 // borrow captures when capturing an immutable
250 // variable. This is sound because the mutation
251 // that caused the capture will cause an error.
256 allow_two_phase_borrow: false,
260 block = this.limit_capture_mutability(
261 upvar.span, upvar.ty, scope, block, arg,
264 _ => unpack!(block = this.as_operand(block, scope, upvar)),
269 let result = match substs {
270 UpvarSubsts::Generator(substs) => {
271 let movability = movability.unwrap();
272 // Add the state operand since it follows the upvars in the generator
273 // struct. See librustc_mir/transform/generator.rs for more details.
274 operands.push(Operand::Constant(box Constant {
276 ty: this.hir.tcx().types.u32,
278 literal: this.hir.tcx().mk_const(
279 ty::Const::from_bits(
282 ty::ParamEnv::empty().and(this.hir.tcx().types.u32),
286 box AggregateKind::Generator(closure_id, substs, movability)
288 UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
290 block.and(Rvalue::Aggregate(result, operands))
301 let is_union = adt_def.is_union();
302 let active_field_index = if is_union {
303 Some(fields[0].name.index())
308 // first process the set of fields that were provided
309 // (evaluating them in order given by user)
310 let fields_map: FxHashMap<_, _> = fields
315 unpack!(block = this.as_operand(block, scope, f.expr)),
319 let field_names = this.hir.all_fields(adt_def, variant_index);
321 let fields = if let Some(FruInfo { base, field_types }) = base {
322 let base = unpack!(block = this.as_place(block, base));
324 // MIR does not natively support FRU, so for each
325 // base-supplied field, generate an operand that
326 // reads it from the base.
329 .zip(field_types.into_iter())
330 .map(|(n, ty)| match fields_map.get(&n) {
331 Some(v) => v.clone(),
332 None => this.consume_by_copy_or_move(base.clone().field(n, ty)),
337 .filter_map(|n| fields_map.get(n).cloned())
341 let inferred_ty = expr.ty;
342 let user_ty = user_ty.map(|ty| {
343 this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
344 span: source_info.span,
349 let adt = box AggregateKind::Adt(
356 block.and(Rvalue::Aggregate(adt, fields))
358 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
359 block = unpack!(this.stmt_expr(block, expr, None));
360 block.and(this.unit_rvalue())
362 ExprKind::Yield { value } => {
363 let value = unpack!(block = this.as_operand(block, scope, value));
364 let resume = this.cfg.start_new_block();
365 let cleanup = this.generator_drop_cleanup();
369 TerminatorKind::Yield {
375 resume.and(this.unit_rvalue())
377 ExprKind::Literal { .. }
378 | ExprKind::Block { .. }
379 | ExprKind::Match { .. }
380 | ExprKind::If { .. }
381 | ExprKind::NeverToAny { .. }
382 | ExprKind::Loop { .. }
383 | ExprKind::LogicalOp { .. }
384 | ExprKind::Call { .. }
385 | ExprKind::Field { .. }
386 | ExprKind::Deref { .. }
387 | ExprKind::Index { .. }
388 | ExprKind::VarRef { .. }
390 | ExprKind::Break { .. }
391 | ExprKind::Continue { .. }
392 | ExprKind::Return { .. }
393 | ExprKind::InlineAsm { .. }
394 | ExprKind::StaticRef { .. }
395 | ExprKind::PlaceTypeAscription { .. }
396 | ExprKind::ValueTypeAscription { .. } => {
397 // these do not have corresponding `Rvalue` variants,
398 // so make an operand and then return that
399 debug_assert!(match Category::of(&expr.kind) {
400 Some(Category::Rvalue(RvalueFunc::AsRvalue)) => false,
403 let operand = unpack!(block = this.as_operand(block, scope, expr));
404 block.and(Rvalue::Use(operand))
409 pub fn build_binary_op(
411 mut block: BasicBlock,
417 ) -> BlockAnd<Rvalue<'tcx>> {
418 let source_info = self.source_info(span);
419 let bool_ty = self.hir.bool_ty();
420 if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
421 let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
422 let result_value = self.temp(result_tup, span);
424 self.cfg.push_assign(
428 Rvalue::CheckedBinaryOp(op, lhs, rhs),
430 let val_fld = Field::new(0);
431 let of_fld = Field::new(1);
433 let val = result_value.clone().field(val_fld, ty);
434 let of = result_value.field(of_fld, bool_ty);
436 let err = InterpError::Overflow(op);
438 block = self.assert(block, Operand::Move(of), false, err, span);
440 block.and(Rvalue::Use(Operand::Move(val)))
442 if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
443 // Checking division and remainder is more complex, since we 1. always check
444 // and 2. there are two possible failure cases, divide-by-zero and overflow.
446 let (zero_err, overflow_err) = if op == BinOp::Div {
447 (InterpError::DivisionByZero, InterpError::Overflow(op))
449 (InterpError::RemainderByZero, InterpError::Overflow(op))
453 let is_zero = self.temp(bool_ty, span);
454 let zero = self.zero_literal(span, ty);
455 self.cfg.push_assign(
459 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero),
462 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
464 // We only need to check for the overflow in one case:
465 // MIN / -1, and only for signed values.
467 let neg_1 = self.neg_1_literal(span, ty);
468 let min = self.minval_literal(span, ty);
470 let is_neg_1 = self.temp(bool_ty, span);
471 let is_min = self.temp(bool_ty, span);
472 let of = self.temp(bool_ty, span);
474 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
476 self.cfg.push_assign(
480 Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1),
482 self.cfg.push_assign(
486 Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min),
489 let is_neg_1 = Operand::Move(is_neg_1);
490 let is_min = Operand::Move(is_min);
491 self.cfg.push_assign(
495 Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min),
498 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
502 block.and(Rvalue::BinaryOp(op, lhs, rhs))
506 fn limit_capture_mutability(
510 temp_lifetime: Option<region::Scope>,
511 mut block: BasicBlock,
513 ) -> BlockAnd<Operand<'tcx>> {
516 let source_info = this.source_info(upvar_span);
519 .push(LocalDecl::new_temp(upvar_ty, upvar_span));
525 kind: StatementKind::StorageLive(temp),
529 let arg_place = unpack!(block = this.as_place(block, arg));
531 let mutability = match arg_place {
532 Place::Base(PlaceBase::Local(local)) => this.local_decls[local].mutability,
533 Place::Projection(box Projection {
534 base: Place::Base(PlaceBase::Local(local)),
535 elem: ProjectionElem::Deref,
538 if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
539 this.local_decls[local].is_user_variable
545 "Unexpected capture place",
547 this.local_decls[local].mutability
549 Place::Projection(box Projection {
551 elem: ProjectionElem::Field(upvar_index, _),
553 | Place::Projection(box Projection {
555 Place::Projection(box Projection {
557 elem: ProjectionElem::Field(upvar_index, _),
559 elem: ProjectionElem::Deref,
561 // Not projected from the implicit `self` in a closure.
564 Place::Base(PlaceBase::Local(local)) => local == Local::new(1),
565 Place::Projection(box Projection {
567 elem: ProjectionElem::Deref,
568 }) => *base == Place::Base(PlaceBase::Local(Local::new(1))),
571 "Unexpected capture place"
575 this.upvar_decls.len() > upvar_index.index(),
576 "Unexpected capture place"
578 this.upvar_decls[upvar_index.index()].mutability
580 _ => bug!("Unexpected capture place"),
583 let borrow_kind = match mutability {
584 Mutability::Not => BorrowKind::Unique,
585 Mutability::Mut => BorrowKind::Mut {
586 allow_two_phase_borrow: false,
590 this.cfg.push_assign(
593 &Place::Base(PlaceBase::Local(temp)),
594 Rvalue::Ref(this.hir.tcx().types.re_erased, borrow_kind, arg_place),
597 // In constants, temp_lifetime is None. We should not need to drop
598 // anything because no values with a destructor can be created in
599 // a constant at this time, even if the type may need dropping.
600 if let Some(temp_lifetime) = temp_lifetime {
601 this.schedule_drop_storage_and_value(
604 &Place::Base(PlaceBase::Local(temp)),
609 block.and(Operand::Move(Place::Base(PlaceBase::Local(temp))))
612 // Helper to get a `-1` value of the appropriate type
613 fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
614 let param_ty = ty::ParamEnv::empty().and(self.hir.tcx().lift_to_global(&ty).unwrap());
615 let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
616 let n = (!0u128) >> (128 - bits);
617 let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
619 self.literal_operand(span, ty, literal)
622 // Helper to get the minimum value of the appropriate type
623 fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
624 assert!(ty.is_signed());
625 let param_ty = ty::ParamEnv::empty().and(self.hir.tcx().lift_to_global(&ty).unwrap());
626 let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
627 let n = 1 << (bits - 1);
628 let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
630 self.literal_operand(span, ty, literal)