]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
8082315408c6fc7c8825f970b9e7846bf6c16f27
[rust.git] / compiler / rustc_mir_build / src / build / expr / as_rvalue.rs
1 //! See docs in `build/expr/mod.rs`.
2
3 use rustc_index::vec::Idx;
4
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 crate::thir::*;
9 use rustc_middle::middle::region;
10 use rustc_middle::mir::AssertKind;
11 use rustc_middle::mir::*;
12 use rustc_middle::ty::{self, Ty, UpvarSubsts};
13 use rustc_span::Span;
14
15 impl<'a, 'tcx> Builder<'a, 'tcx> {
16     /// Returns an rvalue suitable for use until the end of the current
17     /// scope expression.
18     ///
19     /// The operand returned from this function will *not be valid* after
20     /// an ExprKind::Scope is passed, so please do *not* return it from
21     /// functions to avoid bad miscompiles.
22     crate fn as_local_rvalue(
23         &mut self,
24         block: BasicBlock,
25         expr: &Expr<'tcx>,
26     ) -> BlockAnd<Rvalue<'tcx>> {
27         let local_scope = self.local_scope();
28         self.as_rvalue(block, Some(local_scope), expr)
29     }
30
31     /// Compile `expr`, yielding an rvalue.
32     crate fn as_rvalue(
33         &mut self,
34         mut block: BasicBlock,
35         scope: Option<region::Scope>,
36         expr: &Expr<'tcx>,
37     ) -> BlockAnd<Rvalue<'tcx>> {
38         debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
39
40         let this = self;
41         let expr_span = expr.span;
42         let source_info = this.source_info(expr_span);
43
44         match &expr.kind {
45             ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(*did)),
46             ExprKind::Scope { region_scope, lint_level, value } => {
47                 let region_scope = (*region_scope, source_info);
48                 this.in_scope(region_scope, *lint_level, |this| {
49                     this.as_rvalue(block, scope, &value)
50                 })
51             }
52             ExprKind::Repeat { value, count } => {
53                 let value_operand = unpack!(block = this.as_operand(block, scope, &value));
54                 block.and(Rvalue::Repeat(value_operand, count))
55             }
56             ExprKind::Binary { op, lhs, rhs } => {
57                 let lhs = unpack!(block = this.as_operand(block, scope, &lhs));
58                 let rhs = unpack!(block = this.as_operand(block, scope, &rhs));
59                 this.build_binary_op(block, *op, expr_span, expr.ty, lhs, rhs)
60             }
61             ExprKind::Unary { op, arg } => {
62                 let arg = unpack!(block = this.as_operand(block, scope, &arg));
63                 // Check for -MIN on signed integers
64                 if this.hir.check_overflow() && *op == UnOp::Neg && expr.ty.is_signed() {
65                     let bool_ty = this.hir.bool_ty();
66
67                     let minval = this.minval_literal(expr_span, expr.ty);
68                     let is_min = this.temp(bool_ty, expr_span);
69
70                     this.cfg.push_assign(
71                         block,
72                         source_info,
73                         is_min,
74                         Rvalue::BinaryOp(BinOp::Eq, box (arg.to_copy(), minval)),
75                     );
76
77                     block = this.assert(
78                         block,
79                         Operand::Move(is_min),
80                         false,
81                         AssertKind::OverflowNeg(arg.to_copy()),
82                         expr_span,
83                     );
84                 }
85                 block.and(Rvalue::UnaryOp(*op, arg))
86             }
87             ExprKind::Box { value } => {
88                 // The `Box<T>` temporary created here is not a part of the HIR,
89                 // and therefore is not considered during generator auto-trait
90                 // determination. See the comment about `box` at `yield_in_scope`.
91                 let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
92                 this.cfg.push(
93                     block,
94                     Statement { source_info, kind: StatementKind::StorageLive(result) },
95                 );
96                 if let Some(scope) = scope {
97                     // schedule a shallow free of that memory, lest we unwind:
98                     this.schedule_drop_storage_and_value(expr_span, scope, result);
99                 }
100
101                 // malloc some memory of suitable type (thus far, uninitialized):
102                 let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
103                 this.cfg.push_assign(block, source_info, Place::from(result), box_);
104
105                 // initialize the box contents:
106                 unpack!(
107                     block = this.expr_into_dest(
108                         this.hir.tcx().mk_place_deref(Place::from(result)),
109                         block,
110                         &value
111                     )
112                 );
113                 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
114             }
115             ExprKind::Cast { source } => {
116                 let source = unpack!(block = this.as_operand(block, scope, &source));
117                 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
118             }
119             ExprKind::Pointer { cast, source } => {
120                 let source = unpack!(block = this.as_operand(block, scope, &source));
121                 block.and(Rvalue::Cast(CastKind::Pointer(*cast), source, expr.ty))
122             }
123             ExprKind::Array { fields } => {
124                 // (*) We would (maybe) be closer to codegen if we
125                 // handled this and other aggregate cases via
126                 // `into()`, not `as_rvalue` -- in that case, instead
127                 // of generating
128                 //
129                 //     let tmp1 = ...1;
130                 //     let tmp2 = ...2;
131                 //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
132                 //
133                 // we could just generate
134                 //
135                 //     dest.f = ...1;
136                 //     dest.g = ...2;
137                 //
138                 // The problem is that then we would need to:
139                 //
140                 // (a) have a more complex mechanism for handling
141                 //     partial cleanup;
142                 // (b) distinguish the case where the type `Foo` has a
143                 //     destructor, in which case creating an instance
144                 //     as a whole "arms" the destructor, and you can't
145                 //     write individual fields; and,
146                 // (c) handle the case where the type Foo has no
147                 //     fields. We don't want `let x: ();` to compile
148                 //     to the same MIR as `let x = ();`.
149
150                 // first process the set of fields
151                 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
152                 let fields: Vec<_> = fields
153                     .into_iter()
154                     .map(|f| unpack!(block = this.as_operand(block, scope, &f)))
155                     .collect();
156
157                 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
158             }
159             ExprKind::Tuple { fields } => {
160                 // see (*) above
161                 // first process the set of fields
162                 let fields: Vec<_> = fields
163                     .into_iter()
164                     .map(|f| unpack!(block = this.as_operand(block, scope, &f)))
165                     .collect();
166
167                 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
168             }
169             ExprKind::Closure { closure_id, substs, upvars, movability } => {
170                 // see (*) above
171                 let operands: Vec<_> = upvars
172                     .into_iter()
173                     .map(|upvar| {
174                         match Category::of(&upvar.kind) {
175                             // Use as_place to avoid creating a temporary when
176                             // moving a variable into a closure, so that
177                             // borrowck knows which variables to mark as being
178                             // used as mut. This is OK here because the upvar
179                             // expressions have no side effects and act on
180                             // disjoint places.
181                             // This occurs when capturing by copy/move, while
182                             // by reference captures use as_operand
183                             Some(Category::Place) => {
184                                 let place = unpack!(block = this.as_place(block, &upvar));
185                                 this.consume_by_copy_or_move(place)
186                             }
187                             _ => {
188                                 // Turn mutable borrow captures into unique
189                                 // borrow captures when capturing an immutable
190                                 // variable. This is sound because the mutation
191                                 // that caused the capture will cause an error.
192                                 match &upvar.kind {
193                                     ExprKind::Borrow {
194                                         borrow_kind:
195                                             BorrowKind::Mut { allow_two_phase_borrow: false },
196                                         arg,
197                                     } => unpack!(
198                                         block = this.limit_capture_mutability(
199                                             upvar.span, upvar.ty, scope, block, &arg,
200                                         )
201                                     ),
202                                     _ => unpack!(block = this.as_operand(block, scope, &upvar)),
203                                 }
204                             }
205                         }
206                     })
207                     .collect();
208                 let result = match substs {
209                     UpvarSubsts::Generator(substs) => {
210                         // We implicitly set the discriminant to 0. See
211                         // librustc_mir/transform/deaggregator.rs for details.
212                         let movability = movability.unwrap();
213                         box AggregateKind::Generator(*closure_id, substs, movability)
214                     }
215                     UpvarSubsts::Closure(substs) => box AggregateKind::Closure(*closure_id, substs),
216                 };
217                 block.and(Rvalue::Aggregate(result, operands))
218             }
219             ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
220                 block = unpack!(this.stmt_expr(block, expr, None));
221                 block.and(Rvalue::Use(Operand::Constant(box Constant {
222                     span: expr_span,
223                     user_ty: None,
224                     literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
225                 })))
226             }
227             ExprKind::Yield { .. }
228             | ExprKind::Literal { .. }
229             | ExprKind::ConstBlock { .. }
230             | ExprKind::StaticRef { .. }
231             | ExprKind::Block { .. }
232             | ExprKind::Match { .. }
233             | ExprKind::If { .. }
234             | ExprKind::NeverToAny { .. }
235             | ExprKind::Use { .. }
236             | ExprKind::Borrow { .. }
237             | ExprKind::AddressOf { .. }
238             | ExprKind::Adt { .. }
239             | ExprKind::Loop { .. }
240             | ExprKind::LogicalOp { .. }
241             | ExprKind::Call { .. }
242             | ExprKind::Field { .. }
243             | ExprKind::Deref { .. }
244             | ExprKind::Index { .. }
245             | ExprKind::VarRef { .. }
246             | ExprKind::UpvarRef { .. }
247             | ExprKind::Break { .. }
248             | ExprKind::Continue { .. }
249             | ExprKind::Return { .. }
250             | ExprKind::InlineAsm { .. }
251             | ExprKind::LlvmInlineAsm { .. }
252             | ExprKind::PlaceTypeAscription { .. }
253             | ExprKind::ValueTypeAscription { .. } => {
254                 // these do not have corresponding `Rvalue` variants,
255                 // so make an operand and then return that
256                 debug_assert!(!matches!(
257                     Category::of(&expr.kind),
258                     Some(Category::Rvalue(RvalueFunc::AsRvalue))
259                 ));
260                 let operand = unpack!(block = this.as_operand(block, scope, expr));
261                 block.and(Rvalue::Use(operand))
262             }
263         }
264     }
265
266     crate fn build_binary_op(
267         &mut self,
268         mut block: BasicBlock,
269         op: BinOp,
270         span: Span,
271         ty: Ty<'tcx>,
272         lhs: Operand<'tcx>,
273         rhs: Operand<'tcx>,
274     ) -> BlockAnd<Rvalue<'tcx>> {
275         let source_info = self.source_info(span);
276         let bool_ty = self.hir.bool_ty();
277         if self.hir.check_overflow() && op.is_checkable() && ty.is_integral() {
278             let result_tup = self.hir.tcx().intern_tup(&[ty, bool_ty]);
279             let result_value = self.temp(result_tup, span);
280
281             self.cfg.push_assign(
282                 block,
283                 source_info,
284                 result_value,
285                 Rvalue::CheckedBinaryOp(op, box (lhs.to_copy(), rhs.to_copy())),
286             );
287             let val_fld = Field::new(0);
288             let of_fld = Field::new(1);
289
290             let tcx = self.hir.tcx();
291             let val = tcx.mk_place_field(result_value, val_fld, ty);
292             let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
293
294             let err = AssertKind::Overflow(op, lhs, rhs);
295
296             block = self.assert(block, Operand::Move(of), false, err, span);
297
298             block.and(Rvalue::Use(Operand::Move(val)))
299         } else {
300             if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
301                 // Checking division and remainder is more complex, since we 1. always check
302                 // and 2. there are two possible failure cases, divide-by-zero and overflow.
303
304                 let zero_err = if op == BinOp::Div {
305                     AssertKind::DivisionByZero(lhs.to_copy())
306                 } else {
307                     AssertKind::RemainderByZero(lhs.to_copy())
308                 };
309                 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
310
311                 // Check for / 0
312                 let is_zero = self.temp(bool_ty, span);
313                 let zero = self.zero_literal(span, ty);
314                 self.cfg.push_assign(
315                     block,
316                     source_info,
317                     is_zero,
318                     Rvalue::BinaryOp(BinOp::Eq, box (rhs.to_copy(), zero)),
319                 );
320
321                 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
322
323                 // We only need to check for the overflow in one case:
324                 // MIN / -1, and only for signed values.
325                 if ty.is_signed() {
326                     let neg_1 = self.neg_1_literal(span, ty);
327                     let min = self.minval_literal(span, ty);
328
329                     let is_neg_1 = self.temp(bool_ty, span);
330                     let is_min = self.temp(bool_ty, span);
331                     let of = self.temp(bool_ty, span);
332
333                     // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
334
335                     self.cfg.push_assign(
336                         block,
337                         source_info,
338                         is_neg_1,
339                         Rvalue::BinaryOp(BinOp::Eq, box (rhs.to_copy(), neg_1)),
340                     );
341                     self.cfg.push_assign(
342                         block,
343                         source_info,
344                         is_min,
345                         Rvalue::BinaryOp(BinOp::Eq, box (lhs.to_copy(), min)),
346                     );
347
348                     let is_neg_1 = Operand::Move(is_neg_1);
349                     let is_min = Operand::Move(is_min);
350                     self.cfg.push_assign(
351                         block,
352                         source_info,
353                         of,
354                         Rvalue::BinaryOp(BinOp::BitAnd, box (is_neg_1, is_min)),
355                     );
356
357                     block = self.assert(block, Operand::Move(of), false, overflow_err, span);
358                 }
359             }
360
361             block.and(Rvalue::BinaryOp(op, box (lhs, rhs)))
362         }
363     }
364
365     fn limit_capture_mutability(
366         &mut self,
367         upvar_span: Span,
368         upvar_ty: Ty<'tcx>,
369         temp_lifetime: Option<region::Scope>,
370         mut block: BasicBlock,
371         arg: &Expr<'tcx>,
372     ) -> BlockAnd<Operand<'tcx>> {
373         let this = self;
374
375         let source_info = this.source_info(upvar_span);
376         let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
377
378         this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
379
380         let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
381
382         let mutability = match arg_place_builder.base() {
383             // We are capturing a path that starts off a local variable in the parent.
384             // The mutability of the current capture is same as the mutability
385             // of the local declaration in the parent.
386             PlaceBase::Local(local) => this.local_decls[local].mutability,
387             // Parent is a closure and we are capturing a path that is captured
388             // by the parent itself. The mutability of the current capture
389             // is same as that of the capture in the parent closure.
390             PlaceBase::Upvar { .. } => {
391                 let enclosing_upvars_resolved =
392                     arg_place_builder.clone().into_place(this.hir.tcx(), this.hir.typeck_results());
393
394                 match enclosing_upvars_resolved.as_ref() {
395                     PlaceRef {
396                         local,
397                         projection: &[ProjectionElem::Field(upvar_index, _), ..],
398                     }
399                     | PlaceRef {
400                         local,
401                         projection:
402                             &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
403                     } => {
404                         // Not in a closure
405                         debug_assert!(
406                             local == Local::new(1),
407                             "Expected local to be Local(1), found {:?}",
408                             local
409                         );
410                         // Not in a closure
411                         debug_assert!(
412                             this.upvar_mutbls.len() > upvar_index.index(),
413                             "Unexpected capture place, upvar_mutbls={:#?}, upvar_index={:?}",
414                             this.upvar_mutbls,
415                             upvar_index
416                         );
417                         this.upvar_mutbls[upvar_index.index()]
418                     }
419                     _ => bug!("Unexpected capture place"),
420                 }
421             }
422         };
423
424         let borrow_kind = match mutability {
425             Mutability::Not => BorrowKind::Unique,
426             Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
427         };
428
429         let arg_place = arg_place_builder.into_place(this.hir.tcx(), this.hir.typeck_results());
430
431         this.cfg.push_assign(
432             block,
433             source_info,
434             Place::from(temp),
435             Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
436         );
437
438         // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
439         // this can be `None`.
440         if let Some(temp_lifetime) = temp_lifetime {
441             this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
442         }
443
444         block.and(Operand::Move(Place::from(temp)))
445     }
446
447     // Helper to get a `-1` value of the appropriate type
448     fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
449         let param_ty = ty::ParamEnv::empty().and(ty);
450         let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
451         let n = (!0u128) >> (128 - bits);
452         let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
453
454         self.literal_operand(span, literal)
455     }
456
457     // Helper to get the minimum value of the appropriate type
458     fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
459         assert!(ty.is_signed());
460         let param_ty = ty::ParamEnv::empty().and(ty);
461         let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
462         let n = 1 << (bits - 1);
463         let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
464
465         self.literal_operand(span, literal)
466     }
467 }