]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Auto merge of #79729 - matthiaskrgr:clones_, r=jyn514
[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::thir::*;
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};
12 use rustc_span::Span;
13
14 use std::slice;
15
16 impl<'a, 'tcx> Builder<'a, 'tcx> {
17     /// Returns an rvalue suitable for use until the end of the current
18     /// scope expression.
19     ///
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>>
24     where
25         M: Mirror<'tcx, Output = Expr<'tcx>>,
26     {
27         let local_scope = self.local_scope();
28         self.as_rvalue(block, local_scope, expr)
29     }
30
31     /// Compile `expr`, yielding an rvalue.
32     fn as_rvalue<M>(
33         &mut self,
34         block: BasicBlock,
35         scope: Option<region::Scope>,
36         expr: M,
37     ) -> BlockAnd<Rvalue<'tcx>>
38     where
39         M: Mirror<'tcx, Output = Expr<'tcx>>,
40     {
41         let expr = self.hir.mirror(expr);
42         self.expr_as_rvalue(block, scope, expr)
43     }
44
45     fn expr_as_rvalue(
46         &mut self,
47         mut block: BasicBlock,
48         scope: Option<region::Scope>,
49         expr: Expr<'tcx>,
50     ) -> BlockAnd<Rvalue<'tcx>> {
51         debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
52
53         let this = self;
54         let expr_span = expr.span;
55         let source_info = this.source_info(expr_span);
56
57         match expr.kind {
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))
62             }
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))
66             }
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)
71             }
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();
77
78                     let minval = this.minval_literal(expr_span, expr.ty);
79                     let is_min = this.temp(bool_ty, expr_span);
80
81                     this.cfg.push_assign(
82                         block,
83                         source_info,
84                         is_min,
85                         Rvalue::BinaryOp(BinOp::Eq, arg.to_copy(), minval),
86                     );
87
88                     block = this.assert(
89                         block,
90                         Operand::Move(is_min),
91                         false,
92                         AssertKind::OverflowNeg(arg.to_copy()),
93                         expr_span,
94                     );
95                 }
96                 block.and(Rvalue::UnaryOp(op, arg))
97             }
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());
104                 this.cfg.push(
105                     block,
106                     Statement { source_info, kind: StatementKind::StorageLive(result) },
107                 );
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);
111                 }
112
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_);
116
117                 // Initialize the box contents. No scope is needed since the
118                 // `Box` is already scheduled to be dropped.
119                 unpack!(
120                     block = this.into(
121                         this.hir.tcx().mk_place_deref(Place::from(result)),
122                         None,
123                         block,
124                         value,
125                     )
126                 );
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))
130             }
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))
134             }
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))
138             }
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
143                 // of generating
144                 //
145                 //     let tmp1 = ...1;
146                 //     let tmp2 = ...2;
147                 //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
148                 //
149                 // we could just generate
150                 //
151                 //     dest.f = ...1;
152                 //     dest.g = ...2;
153                 //
154                 // The problem is that then we would need to:
155                 //
156                 // (a) have a more complex mechanism for handling
157                 //     partial cleanup;
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 = ();`.
165
166                 // first process the set of fields
167                 let el_ty = expr.ty.sequence_element_type(this.hir.tcx());
168                 let fields: Vec<_> = fields
169                     .into_iter()
170                     .map(|f| unpack!(block = this.as_operand(block, scope, f)))
171                     .collect();
172
173                 this.record_operands_moved(&fields);
174                 block.and(Rvalue::Aggregate(box AggregateKind::Array(el_ty), fields))
175             }
176             ExprKind::Tuple { fields } => {
177                 // see (*) above
178                 // first process the set of fields
179                 let fields: Vec<_> = fields
180                     .into_iter()
181                     .map(|f| unpack!(block = this.as_operand(block, scope, f)))
182                     .collect();
183
184                 this.record_operands_moved(&fields);
185                 block.and(Rvalue::Aggregate(box AggregateKind::Tuple, fields))
186             }
187             ExprKind::Closure { closure_id, substs, upvars, movability } => {
188                 // see (*) above
189                 let operands: Vec<_> = upvars
190                     .into_iter()
191                     .map(|upvar| {
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
199                             // disjoint places.
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)
205                             }
206                             _ => {
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.
211                                 match upvar.kind {
212                                     ExprKind::Borrow {
213                                         borrow_kind:
214                                             BorrowKind::Mut { allow_two_phase_borrow: false },
215                                         arg,
216                                     } => unpack!(
217                                         block = this.limit_capture_mutability(
218                                             upvar.span, upvar.ty, scope, block, arg,
219                                         )
220                                     ),
221                                     _ => unpack!(block = this.as_operand(block, scope, upvar)),
222                                 }
223                             }
224                         }
225                     })
226                     .collect();
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)
233                     }
234                     UpvarSubsts::Closure(substs) => box AggregateKind::Closure(closure_id, substs),
235                 };
236                 this.record_operands_moved(&operands);
237                 block.and(Rvalue::Aggregate(result, operands))
238             }
239             ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
240                 block = unpack!(this.stmt_expr(block, expr, None));
241                 block.and(Rvalue::Use(Operand::Constant(box Constant {
242                     span: expr_span,
243                     user_ty: None,
244                     literal: ty::Const::zero_sized(this.hir.tcx(), this.hir.tcx().types.unit),
245                 })))
246             }
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))
278             }
279         }
280     }
281
282     crate fn build_binary_op(
283         &mut self,
284         mut block: BasicBlock,
285         op: BinOp,
286         span: Span,
287         ty: Ty<'tcx>,
288         lhs: Operand<'tcx>,
289         rhs: Operand<'tcx>,
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);
296
297             self.cfg.push_assign(
298                 block,
299                 source_info,
300                 result_value,
301                 Rvalue::CheckedBinaryOp(op, lhs.to_copy(), rhs.to_copy()),
302             );
303             let val_fld = Field::new(0);
304             let of_fld = Field::new(1);
305
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);
309
310             let err = AssertKind::Overflow(op, lhs, rhs);
311
312             block = self.assert(block, Operand::Move(of), false, err, span);
313
314             block.and(Rvalue::Use(Operand::Move(val)))
315         } else {
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.
319
320                 let zero_err = if op == BinOp::Div {
321                     AssertKind::DivisionByZero(lhs.to_copy())
322                 } else {
323                     AssertKind::RemainderByZero(lhs.to_copy())
324                 };
325                 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
326
327                 // Check for / 0
328                 let is_zero = self.temp(bool_ty, span);
329                 let zero = self.zero_literal(span, ty);
330                 self.cfg.push_assign(
331                     block,
332                     source_info,
333                     is_zero,
334                     Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), zero),
335                 );
336
337                 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
338
339                 // We only need to check for the overflow in one case:
340                 // MIN / -1, and only for signed values.
341                 if ty.is_signed() {
342                     let neg_1 = self.neg_1_literal(span, ty);
343                     let min = self.minval_literal(span, ty);
344
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);
348
349                     // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
350
351                     self.cfg.push_assign(
352                         block,
353                         source_info,
354                         is_neg_1,
355                         Rvalue::BinaryOp(BinOp::Eq, rhs.to_copy(), neg_1),
356                     );
357                     self.cfg.push_assign(
358                         block,
359                         source_info,
360                         is_min,
361                         Rvalue::BinaryOp(BinOp::Eq, lhs.to_copy(), min),
362                     );
363
364                     let is_neg_1 = Operand::Move(is_neg_1);
365                     let is_min = Operand::Move(is_min);
366                     self.cfg.push_assign(
367                         block,
368                         source_info,
369                         of,
370                         Rvalue::BinaryOp(BinOp::BitAnd, is_neg_1, is_min),
371                     );
372
373                     block = self.assert(block, Operand::Move(of), false, overflow_err, span);
374                 }
375             }
376
377             block.and(Rvalue::BinaryOp(op, lhs, rhs))
378         }
379     }
380
381     fn limit_capture_mutability(
382         &mut self,
383         upvar_span: Span,
384         upvar_ty: Ty<'tcx>,
385         temp_lifetime: Option<region::Scope>,
386         mut block: BasicBlock,
387         arg: ExprRef<'tcx>,
388     ) -> BlockAnd<Operand<'tcx>> {
389         let this = self;
390
391         let source_info = this.source_info(upvar_span);
392         let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
393
394         this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
395
396         let arg_place = unpack!(block = this.as_place(block, arg));
397
398         let mutability = match arg_place.as_ref() {
399             PlaceRef { local, projection: &[] } => this.local_decls[local].mutability,
400             PlaceRef { local, projection: &[ProjectionElem::Deref] } => {
401                 debug_assert!(
402                     this.local_decls[local].is_ref_for_guard(),
403                     "Unexpected capture place",
404                 );
405                 this.local_decls[local].mutability
406             }
407             PlaceRef {
408                 local,
409                 projection: &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _)],
410             }
411             | PlaceRef {
412                 local,
413                 projection:
414                     &[ref proj_base @ .., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
415             } => {
416                 let place = PlaceRef { local, projection: proj_base };
417
418                 // Not projected from the implicit `self` in a closure.
419                 debug_assert!(
420                     match place.local_or_deref_local() {
421                         Some(local) => local == Local::new(1),
422                         None => false,
423                     },
424                     "Unexpected capture place"
425                 );
426                 // Not in a closure
427                 debug_assert!(
428                     this.upvar_mutbls.len() > upvar_index.index(),
429                     "Unexpected capture place"
430                 );
431                 this.upvar_mutbls[upvar_index.index()]
432             }
433             _ => bug!("Unexpected capture place"),
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         this.cfg.push_assign(
442             block,
443             source_info,
444             Place::from(temp),
445             Rvalue::Ref(this.hir.tcx().lifetimes.re_erased, borrow_kind, arg_place),
446         );
447
448         // In constants, temp_lifetime is None. We should not need to drop
449         // anything because no values with a destructor can be created in
450         // a constant at this time, even if the type may need dropping.
451         if let Some(temp_lifetime) = temp_lifetime {
452             this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
453         }
454
455         block.and(Operand::Move(Place::from(temp)))
456     }
457
458     // Helper to get a `-1` value of the appropriate type
459     fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
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 = (!0u128) >> (128 - bits);
463         let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
464
465         self.literal_operand(span, literal)
466     }
467
468     // Helper to get the minimum value of the appropriate type
469     fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
470         assert!(ty.is_signed());
471         let param_ty = ty::ParamEnv::empty().and(ty);
472         let bits = self.hir.tcx().layout_of(param_ty).unwrap().size.bits();
473         let n = 1 << (bits - 1);
474         let literal = ty::Const::from_bits(self.hir.tcx(), n, param_ty);
475
476         self.literal_operand(span, literal)
477     }
478 }