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