]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Auto merge of #100968 - cjgillot:mir-upvar-vec, r=wesleywiser
[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 use rustc_middle::ty::util::IntTypeExt;
5
6 use crate::build::expr::as_place::PlaceBase;
7 use crate::build::expr::category::{Category, RvalueFunc};
8 use crate::build::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
9 use rustc_hir::lang_items::LangItem;
10 use rustc_middle::middle::region;
11 use rustc_middle::mir::AssertKind;
12 use rustc_middle::mir::Place;
13 use rustc_middle::mir::*;
14 use rustc_middle::thir::*;
15 use rustc_middle::ty::cast::CastTy;
16 use rustc_middle::ty::{self, Ty, UpvarSubsts};
17 use rustc_span::Span;
18
19 impl<'a, 'tcx> Builder<'a, 'tcx> {
20     /// Returns an rvalue suitable for use until the end of the current
21     /// scope expression.
22     ///
23     /// The operand returned from this function will *not be valid* after
24     /// an ExprKind::Scope is passed, so please do *not* return it from
25     /// functions to avoid bad miscompiles.
26     pub(crate) fn as_local_rvalue(
27         &mut self,
28         block: BasicBlock,
29         expr: &Expr<'tcx>,
30     ) -> BlockAnd<Rvalue<'tcx>> {
31         let local_scope = self.local_scope();
32         self.as_rvalue(block, Some(local_scope), expr)
33     }
34
35     /// Compile `expr`, yielding an rvalue.
36     pub(crate) fn as_rvalue(
37         &mut self,
38         mut block: BasicBlock,
39         scope: Option<region::Scope>,
40         expr: &Expr<'tcx>,
41     ) -> BlockAnd<Rvalue<'tcx>> {
42         debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
43
44         let this = self;
45         let expr_span = expr.span;
46         let source_info = this.source_info(expr_span);
47
48         match expr.kind {
49             ExprKind::ThreadLocalRef(did) => block.and(Rvalue::ThreadLocalRef(did)),
50             ExprKind::Scope { region_scope, lint_level, value } => {
51                 let region_scope = (region_scope, source_info);
52                 this.in_scope(region_scope, lint_level, |this| {
53                     this.as_rvalue(block, scope, &this.thir[value])
54                 })
55             }
56             ExprKind::Repeat { value, count } => {
57                 if Some(0) == count.try_eval_usize(this.tcx, this.param_env) {
58                     this.build_zero_repeat(block, value, scope, source_info)
59                 } else {
60                     let value_operand = unpack!(
61                         block = this.as_operand(
62                             block,
63                             scope,
64                             &this.thir[value],
65                             None,
66                             NeedsTemporary::No
67                         )
68                     );
69                     block.and(Rvalue::Repeat(value_operand, count))
70                 }
71             }
72             ExprKind::Binary { op, lhs, rhs } => {
73                 let lhs = unpack!(
74                     block =
75                         this.as_operand(block, scope, &this.thir[lhs], None, NeedsTemporary::Maybe)
76                 );
77                 let rhs = unpack!(
78                     block =
79                         this.as_operand(block, scope, &this.thir[rhs], None, NeedsTemporary::No)
80                 );
81                 this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
82             }
83             ExprKind::Unary { op, arg } => {
84                 let arg = unpack!(
85                     block =
86                         this.as_operand(block, scope, &this.thir[arg], None, NeedsTemporary::No)
87                 );
88                 // Check for -MIN on signed integers
89                 if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() {
90                     let bool_ty = this.tcx.types.bool;
91
92                     let minval = this.minval_literal(expr_span, expr.ty);
93                     let is_min = this.temp(bool_ty, expr_span);
94
95                     this.cfg.push_assign(
96                         block,
97                         source_info,
98                         is_min,
99                         Rvalue::BinaryOp(BinOp::Eq, Box::new((arg.to_copy(), minval))),
100                     );
101
102                     block = this.assert(
103                         block,
104                         Operand::Move(is_min),
105                         false,
106                         AssertKind::OverflowNeg(arg.to_copy()),
107                         expr_span,
108                     );
109                 }
110                 block.and(Rvalue::UnaryOp(op, arg))
111             }
112             ExprKind::Box { value } => {
113                 let value = &this.thir[value];
114                 let tcx = this.tcx;
115
116                 // `exchange_malloc` is unsafe but box is safe, so need a new scope.
117                 let synth_scope = this.new_source_scope(
118                     expr_span,
119                     LintLevel::Inherited,
120                     Some(Safety::BuiltinUnsafe),
121                 );
122                 let synth_info = SourceInfo { span: expr_span, scope: synth_scope };
123
124                 let size = this.temp(tcx.types.usize, expr_span);
125                 this.cfg.push_assign(
126                     block,
127                     synth_info,
128                     size,
129                     Rvalue::NullaryOp(NullOp::SizeOf, value.ty),
130                 );
131
132                 let align = this.temp(tcx.types.usize, expr_span);
133                 this.cfg.push_assign(
134                     block,
135                     synth_info,
136                     align,
137                     Rvalue::NullaryOp(NullOp::AlignOf, value.ty),
138                 );
139
140                 // malloc some memory of suitable size and align:
141                 let exchange_malloc = Operand::function_handle(
142                     tcx,
143                     tcx.require_lang_item(LangItem::ExchangeMalloc, Some(expr_span)),
144                     ty::List::empty(),
145                     expr_span,
146                 );
147                 let storage = this.temp(tcx.mk_mut_ptr(tcx.types.u8), expr_span);
148                 let success = this.cfg.start_new_block();
149                 this.cfg.terminate(
150                     block,
151                     synth_info,
152                     TerminatorKind::Call {
153                         func: exchange_malloc,
154                         args: vec![Operand::Move(size), Operand::Move(align)],
155                         destination: storage,
156                         target: Some(success),
157                         cleanup: None,
158                         from_hir_call: false,
159                         fn_span: expr_span,
160                     },
161                 );
162                 this.diverge_from(block);
163                 block = success;
164
165                 // The `Box<T>` temporary created here is not a part of the HIR,
166                 // and therefore is not considered during generator auto-trait
167                 // determination. See the comment about `box` at `yield_in_scope`.
168                 let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span).internal());
169                 this.cfg.push(
170                     block,
171                     Statement { source_info, kind: StatementKind::StorageLive(result) },
172                 );
173                 if let Some(scope) = scope {
174                     // schedule a shallow free of that memory, lest we unwind:
175                     this.schedule_drop_storage_and_value(expr_span, scope, result);
176                 }
177
178                 // Transmute `*mut u8` to the box (thus far, uninitialized):
179                 let box_ = Rvalue::ShallowInitBox(Operand::Move(storage), value.ty);
180                 this.cfg.push_assign(block, source_info, Place::from(result), box_);
181
182                 // initialize the box contents:
183                 unpack!(
184                     block = this.expr_into_dest(
185                         this.tcx.mk_place_deref(Place::from(result)),
186                         block,
187                         value
188                     )
189                 );
190                 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
191             }
192             ExprKind::Cast { source } => {
193                 let source = &this.thir[source];
194
195                 // Casting an enum to an integer is equivalent to computing the discriminant and casting the
196                 // discriminant. Previously every backend had to repeat the logic for this operation. Now we
197                 // create all the steps directly in MIR with operations all backends need to support anyway.
198                 let (source, ty) = if let ty::Adt(adt_def, ..) = source.ty.kind() && adt_def.is_enum() {
199                     let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx);
200                     let place = unpack!(block = this.as_place(block, source));
201                     let discr = this.temp(discr_ty, source.span);
202                     this.cfg.push_assign(
203                         block,
204                         source_info,
205                         discr,
206                         Rvalue::Discriminant(place),
207                     );
208
209                     (Operand::Move(discr), discr_ty)
210                 } else {
211                     let ty = source.ty;
212                     let source = unpack!(
213                         block = this.as_operand(block, scope, source, None, NeedsTemporary::No)
214                     );
215                     (source, ty)
216                 };
217                 let from_ty = CastTy::from_ty(ty);
218                 let cast_ty = CastTy::from_ty(expr.ty);
219                 let cast_kind = match (from_ty, cast_ty) {
220                     (Some(CastTy::Ptr(_) | CastTy::FnPtr), Some(CastTy::Int(_))) => {
221                         CastKind::PointerExposeAddress
222                     }
223                     (Some(CastTy::Int(_)), Some(CastTy::Ptr(_))) => {
224                         CastKind::PointerFromExposedAddress
225                     }
226                     (_, _) => CastKind::Misc,
227                 };
228                 block.and(Rvalue::Cast(cast_kind, source, expr.ty))
229             }
230             ExprKind::Pointer { cast, source } => {
231                 let source = unpack!(
232                     block =
233                         this.as_operand(block, scope, &this.thir[source], None, NeedsTemporary::No)
234                 );
235                 block.and(Rvalue::Cast(CastKind::Pointer(cast), source, expr.ty))
236             }
237             ExprKind::Array { ref fields } => {
238                 // (*) We would (maybe) be closer to codegen if we
239                 // handled this and other aggregate cases via
240                 // `into()`, not `as_rvalue` -- in that case, instead
241                 // of generating
242                 //
243                 //     let tmp1 = ...1;
244                 //     let tmp2 = ...2;
245                 //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
246                 //
247                 // we could just generate
248                 //
249                 //     dest.f = ...1;
250                 //     dest.g = ...2;
251                 //
252                 // The problem is that then we would need to:
253                 //
254                 // (a) have a more complex mechanism for handling
255                 //     partial cleanup;
256                 // (b) distinguish the case where the type `Foo` has a
257                 //     destructor, in which case creating an instance
258                 //     as a whole "arms" the destructor, and you can't
259                 //     write individual fields; and,
260                 // (c) handle the case where the type Foo has no
261                 //     fields. We don't want `let x: ();` to compile
262                 //     to the same MIR as `let x = ();`.
263
264                 // first process the set of fields
265                 let el_ty = expr.ty.sequence_element_type(this.tcx);
266                 let fields: Vec<_> = fields
267                     .into_iter()
268                     .copied()
269                     .map(|f| {
270                         unpack!(
271                             block = this.as_operand(
272                                 block,
273                                 scope,
274                                 &this.thir[f],
275                                 None,
276                                 NeedsTemporary::Maybe
277                             )
278                         )
279                     })
280                     .collect();
281
282                 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(el_ty)), fields))
283             }
284             ExprKind::Tuple { ref fields } => {
285                 // see (*) above
286                 // first process the set of fields
287                 let fields: Vec<_> = fields
288                     .into_iter()
289                     .copied()
290                     .map(|f| {
291                         unpack!(
292                             block = this.as_operand(
293                                 block,
294                                 scope,
295                                 &this.thir[f],
296                                 None,
297                                 NeedsTemporary::Maybe
298                             )
299                         )
300                     })
301                     .collect();
302
303                 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Tuple), fields))
304             }
305             ExprKind::Closure(box ClosureExpr {
306                 closure_id,
307                 substs,
308                 ref upvars,
309                 movability,
310                 ref fake_reads,
311             }) => {
312                 // Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
313                 // and push the fake reads.
314                 // This must come before creating the operands. This is required in case
315                 // there is a fake read and a borrow of the same path, since otherwise the
316                 // fake read might interfere with the borrow. Consider an example like this
317                 // one:
318                 // ```
319                 // let mut x = 0;
320                 // let c = || {
321                 //     &mut x; // mutable borrow of `x`
322                 //     match x { _ => () } // fake read of `x`
323                 // };
324                 // ```
325                 //
326                 for (thir_place, cause, hir_id) in fake_reads.into_iter() {
327                     let place_builder =
328                         unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
329
330                     if let Ok(place_builder_resolved) =
331                         place_builder.try_upvars_resolved(this.tcx, &this.upvars)
332                     {
333                         let mir_place = place_builder_resolved.into_place(this.tcx, &this.upvars);
334                         this.cfg.push_fake_read(
335                             block,
336                             this.source_info(this.tcx.hir().span(*hir_id)),
337                             *cause,
338                             mir_place,
339                         );
340                     }
341                 }
342
343                 // see (*) above
344                 let operands: Vec<_> = upvars
345                     .into_iter()
346                     .copied()
347                     .map(|upvar| {
348                         let upvar = &this.thir[upvar];
349                         match Category::of(&upvar.kind) {
350                             // Use as_place to avoid creating a temporary when
351                             // moving a variable into a closure, so that
352                             // borrowck knows which variables to mark as being
353                             // used as mut. This is OK here because the upvar
354                             // expressions have no side effects and act on
355                             // disjoint places.
356                             // This occurs when capturing by copy/move, while
357                             // by reference captures use as_operand
358                             Some(Category::Place) => {
359                                 let place = unpack!(block = this.as_place(block, upvar));
360                                 this.consume_by_copy_or_move(place)
361                             }
362                             _ => {
363                                 // Turn mutable borrow captures into unique
364                                 // borrow captures when capturing an immutable
365                                 // variable. This is sound because the mutation
366                                 // that caused the capture will cause an error.
367                                 match upvar.kind {
368                                     ExprKind::Borrow {
369                                         borrow_kind:
370                                             BorrowKind::Mut { allow_two_phase_borrow: false },
371                                         arg,
372                                     } => unpack!(
373                                         block = this.limit_capture_mutability(
374                                             upvar.span,
375                                             upvar.ty,
376                                             scope,
377                                             block,
378                                             &this.thir[arg],
379                                         )
380                                     ),
381                                     _ => {
382                                         unpack!(
383                                             block = this.as_operand(
384                                                 block,
385                                                 scope,
386                                                 upvar,
387                                                 None,
388                                                 NeedsTemporary::Maybe
389                                             )
390                                         )
391                                     }
392                                 }
393                             }
394                         }
395                     })
396                     .collect();
397
398                 let result = match substs {
399                     UpvarSubsts::Generator(substs) => {
400                         // We implicitly set the discriminant to 0. See
401                         // librustc_mir/transform/deaggregator.rs for details.
402                         let movability = movability.unwrap();
403                         Box::new(AggregateKind::Generator(closure_id, substs, movability))
404                     }
405                     UpvarSubsts::Closure(substs) => {
406                         Box::new(AggregateKind::Closure(closure_id, substs))
407                     }
408                 };
409                 block.and(Rvalue::Aggregate(result, operands))
410             }
411             ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
412                 block = unpack!(this.stmt_expr(block, expr, None));
413                 block.and(Rvalue::Use(Operand::Constant(Box::new(Constant {
414                     span: expr_span,
415                     user_ty: None,
416                     literal: ConstantKind::zero_sized(this.tcx.types.unit),
417                 }))))
418             }
419
420             ExprKind::Literal { .. }
421             | ExprKind::NamedConst { .. }
422             | ExprKind::NonHirLiteral { .. }
423             | ExprKind::ZstLiteral { .. }
424             | ExprKind::ConstParam { .. }
425             | ExprKind::ConstBlock { .. }
426             | ExprKind::StaticRef { .. } => {
427                 let constant = this.as_constant(expr);
428                 block.and(Rvalue::Use(Operand::Constant(Box::new(constant))))
429             }
430
431             ExprKind::Yield { .. }
432             | ExprKind::Block { .. }
433             | ExprKind::Match { .. }
434             | ExprKind::If { .. }
435             | ExprKind::NeverToAny { .. }
436             | ExprKind::Use { .. }
437             | ExprKind::Borrow { .. }
438             | ExprKind::AddressOf { .. }
439             | ExprKind::Adt { .. }
440             | ExprKind::Loop { .. }
441             | ExprKind::LogicalOp { .. }
442             | ExprKind::Call { .. }
443             | ExprKind::Field { .. }
444             | ExprKind::Let { .. }
445             | ExprKind::Deref { .. }
446             | ExprKind::Index { .. }
447             | ExprKind::VarRef { .. }
448             | ExprKind::UpvarRef { .. }
449             | ExprKind::Break { .. }
450             | ExprKind::Continue { .. }
451             | ExprKind::Return { .. }
452             | ExprKind::InlineAsm { .. }
453             | ExprKind::PlaceTypeAscription { .. }
454             | ExprKind::ValueTypeAscription { .. } => {
455                 // these do not have corresponding `Rvalue` variants,
456                 // so make an operand and then return that
457                 debug_assert!(!matches!(
458                     Category::of(&expr.kind),
459                     Some(Category::Rvalue(RvalueFunc::AsRvalue) | Category::Constant)
460                 ));
461                 let operand =
462                     unpack!(block = this.as_operand(block, scope, expr, None, NeedsTemporary::No));
463                 block.and(Rvalue::Use(operand))
464             }
465         }
466     }
467
468     pub(crate) fn build_binary_op(
469         &mut self,
470         mut block: BasicBlock,
471         op: BinOp,
472         span: Span,
473         ty: Ty<'tcx>,
474         lhs: Operand<'tcx>,
475         rhs: Operand<'tcx>,
476     ) -> BlockAnd<Rvalue<'tcx>> {
477         let source_info = self.source_info(span);
478         let bool_ty = self.tcx.types.bool;
479         if self.check_overflow && op.is_checkable() && ty.is_integral() {
480             let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
481             let result_value = self.temp(result_tup, span);
482
483             self.cfg.push_assign(
484                 block,
485                 source_info,
486                 result_value,
487                 Rvalue::CheckedBinaryOp(op, Box::new((lhs.to_copy(), rhs.to_copy()))),
488             );
489             let val_fld = Field::new(0);
490             let of_fld = Field::new(1);
491
492             let tcx = self.tcx;
493             let val = tcx.mk_place_field(result_value, val_fld, ty);
494             let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
495
496             let err = AssertKind::Overflow(op, lhs, rhs);
497
498             block = self.assert(block, Operand::Move(of), false, err, span);
499
500             block.and(Rvalue::Use(Operand::Move(val)))
501         } else {
502             if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
503                 // Checking division and remainder is more complex, since we 1. always check
504                 // and 2. there are two possible failure cases, divide-by-zero and overflow.
505
506                 let zero_err = if op == BinOp::Div {
507                     AssertKind::DivisionByZero(lhs.to_copy())
508                 } else {
509                     AssertKind::RemainderByZero(lhs.to_copy())
510                 };
511                 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
512
513                 // Check for / 0
514                 let is_zero = self.temp(bool_ty, span);
515                 let zero = self.zero_literal(span, ty);
516                 self.cfg.push_assign(
517                     block,
518                     source_info,
519                     is_zero,
520                     Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), zero))),
521                 );
522
523                 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
524
525                 // We only need to check for the overflow in one case:
526                 // MIN / -1, and only for signed values.
527                 if ty.is_signed() {
528                     let neg_1 = self.neg_1_literal(span, ty);
529                     let min = self.minval_literal(span, ty);
530
531                     let is_neg_1 = self.temp(bool_ty, span);
532                     let is_min = self.temp(bool_ty, span);
533                     let of = self.temp(bool_ty, span);
534
535                     // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
536
537                     self.cfg.push_assign(
538                         block,
539                         source_info,
540                         is_neg_1,
541                         Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), neg_1))),
542                     );
543                     self.cfg.push_assign(
544                         block,
545                         source_info,
546                         is_min,
547                         Rvalue::BinaryOp(BinOp::Eq, Box::new((lhs.to_copy(), min))),
548                     );
549
550                     let is_neg_1 = Operand::Move(is_neg_1);
551                     let is_min = Operand::Move(is_min);
552                     self.cfg.push_assign(
553                         block,
554                         source_info,
555                         of,
556                         Rvalue::BinaryOp(BinOp::BitAnd, Box::new((is_neg_1, is_min))),
557                     );
558
559                     block = self.assert(block, Operand::Move(of), false, overflow_err, span);
560                 }
561             }
562
563             block.and(Rvalue::BinaryOp(op, Box::new((lhs, rhs))))
564         }
565     }
566
567     fn build_zero_repeat(
568         &mut self,
569         mut block: BasicBlock,
570         value: ExprId,
571         scope: Option<region::Scope>,
572         outer_source_info: SourceInfo,
573     ) -> BlockAnd<Rvalue<'tcx>> {
574         let this = self;
575         let value = &this.thir[value];
576         let elem_ty = value.ty;
577         if let Some(Category::Constant) = Category::of(&value.kind) {
578             // Repeating a const does nothing
579         } else {
580             // For a non-const, we may need to generate an appropriate `Drop`
581             let value_operand =
582                 unpack!(block = this.as_operand(block, scope, value, None, NeedsTemporary::No));
583             if let Operand::Move(to_drop) = value_operand {
584                 let success = this.cfg.start_new_block();
585                 this.cfg.terminate(
586                     block,
587                     outer_source_info,
588                     TerminatorKind::Drop { place: to_drop, target: success, unwind: None },
589                 );
590                 this.diverge_from(block);
591                 block = success;
592             }
593             this.record_operands_moved(&[value_operand]);
594         }
595         block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(elem_ty)), Vec::new()))
596     }
597
598     fn limit_capture_mutability(
599         &mut self,
600         upvar_span: Span,
601         upvar_ty: Ty<'tcx>,
602         temp_lifetime: Option<region::Scope>,
603         mut block: BasicBlock,
604         arg: &Expr<'tcx>,
605     ) -> BlockAnd<Operand<'tcx>> {
606         let this = self;
607
608         let source_info = this.source_info(upvar_span);
609         let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
610
611         this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
612
613         let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
614
615         let mutability = match arg_place_builder.base() {
616             // We are capturing a path that starts off a local variable in the parent.
617             // The mutability of the current capture is same as the mutability
618             // of the local declaration in the parent.
619             PlaceBase::Local(local) => this.local_decls[local].mutability,
620             // Parent is a closure and we are capturing a path that is captured
621             // by the parent itself. The mutability of the current capture
622             // is same as that of the capture in the parent closure.
623             PlaceBase::Upvar { .. } => {
624                 let enclosing_upvars_resolved =
625                     arg_place_builder.clone().into_place(this.tcx, &this.upvars);
626
627                 match enclosing_upvars_resolved.as_ref() {
628                     PlaceRef {
629                         local,
630                         projection: &[ProjectionElem::Field(upvar_index, _), ..],
631                     }
632                     | PlaceRef {
633                         local,
634                         projection:
635                             &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
636                     } => {
637                         // Not in a closure
638                         debug_assert!(
639                             local == ty::CAPTURE_STRUCT_LOCAL,
640                             "Expected local to be Local(1), found {:?}",
641                             local
642                         );
643                         // Not in a closure
644                         debug_assert!(
645                             this.upvars.len() > upvar_index.index(),
646                             "Unexpected capture place, upvars={:#?}, upvar_index={:?}",
647                             this.upvars,
648                             upvar_index
649                         );
650                         this.upvars[upvar_index.index()].mutability
651                     }
652                     _ => bug!("Unexpected capture place"),
653                 }
654             }
655         };
656
657         let borrow_kind = match mutability {
658             Mutability::Not => BorrowKind::Unique,
659             Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
660         };
661
662         let arg_place = arg_place_builder.into_place(this.tcx, &this.upvars);
663
664         this.cfg.push_assign(
665             block,
666             source_info,
667             Place::from(temp),
668             Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
669         );
670
671         // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
672         // this can be `None`.
673         if let Some(temp_lifetime) = temp_lifetime {
674             this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
675         }
676
677         block.and(Operand::Move(Place::from(temp)))
678     }
679
680     // Helper to get a `-1` value of the appropriate type
681     fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
682         let param_ty = ty::ParamEnv::empty().and(ty);
683         let size = self.tcx.layout_of(param_ty).unwrap().size;
684         let literal = ConstantKind::from_bits(self.tcx, size.unsigned_int_max(), param_ty);
685
686         self.literal_operand(span, literal)
687     }
688
689     // Helper to get the minimum value of the appropriate type
690     fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
691         assert!(ty.is_signed());
692         let param_ty = ty::ParamEnv::empty().and(ty);
693         let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
694         let n = 1 << (bits - 1);
695         let literal = ConstantKind::from_bits(self.tcx, n, param_ty);
696
697         self.literal_operand(span, literal)
698     }
699 }