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