]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_build/src/build/expr/as_rvalue.rs
Fix off by one error and add ui test.
[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 { closure_id, substs, ref upvars, movability, ref fake_reads } => {
306                 // Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
307                 // and push the fake reads.
308                 // This must come before creating the operands. This is required in case
309                 // there is a fake read and a borrow of the same path, since otherwise the
310                 // fake read might interfere with the borrow. Consider an example like this
311                 // one:
312                 // ```
313                 // let mut x = 0;
314                 // let c = || {
315                 //     &mut x; // mutable borrow of `x`
316                 //     match x { _ => () } // fake read of `x`
317                 // };
318                 // ```
319                 //
320                 for (thir_place, cause, hir_id) in fake_reads.into_iter() {
321                     let place_builder =
322                         unpack!(block = this.as_place_builder(block, &this.thir[*thir_place]));
323
324                     if let Ok(place_builder_resolved) = place_builder.try_upvars_resolved(this) {
325                         let mir_place = place_builder_resolved.into_place(this);
326                         this.cfg.push_fake_read(
327                             block,
328                             this.source_info(this.tcx.hir().span(*hir_id)),
329                             *cause,
330                             mir_place,
331                         );
332                     }
333                 }
334
335                 // see (*) above
336                 let operands: Vec<_> = upvars
337                     .into_iter()
338                     .copied()
339                     .map(|upvar| {
340                         let upvar = &this.thir[upvar];
341                         match Category::of(&upvar.kind) {
342                             // Use as_place to avoid creating a temporary when
343                             // moving a variable into a closure, so that
344                             // borrowck knows which variables to mark as being
345                             // used as mut. This is OK here because the upvar
346                             // expressions have no side effects and act on
347                             // disjoint places.
348                             // This occurs when capturing by copy/move, while
349                             // by reference captures use as_operand
350                             Some(Category::Place) => {
351                                 let place = unpack!(block = this.as_place(block, upvar));
352                                 this.consume_by_copy_or_move(place)
353                             }
354                             _ => {
355                                 // Turn mutable borrow captures into unique
356                                 // borrow captures when capturing an immutable
357                                 // variable. This is sound because the mutation
358                                 // that caused the capture will cause an error.
359                                 match upvar.kind {
360                                     ExprKind::Borrow {
361                                         borrow_kind:
362                                             BorrowKind::Mut { allow_two_phase_borrow: false },
363                                         arg,
364                                     } => unpack!(
365                                         block = this.limit_capture_mutability(
366                                             upvar.span,
367                                             upvar.ty,
368                                             scope,
369                                             block,
370                                             &this.thir[arg],
371                                         )
372                                     ),
373                                     _ => {
374                                         unpack!(
375                                             block = this.as_operand(
376                                                 block,
377                                                 scope,
378                                                 upvar,
379                                                 None,
380                                                 NeedsTemporary::Maybe
381                                             )
382                                         )
383                                     }
384                                 }
385                             }
386                         }
387                     })
388                     .collect();
389
390                 let result = match substs {
391                     UpvarSubsts::Generator(substs) => {
392                         // We implicitly set the discriminant to 0. See
393                         // librustc_mir/transform/deaggregator.rs for details.
394                         let movability = movability.unwrap();
395                         Box::new(AggregateKind::Generator(closure_id, substs, movability))
396                     }
397                     UpvarSubsts::Closure(substs) => {
398                         Box::new(AggregateKind::Closure(closure_id, substs))
399                     }
400                 };
401                 block.and(Rvalue::Aggregate(result, operands))
402             }
403             ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
404                 block = unpack!(this.stmt_expr(block, expr, None));
405                 block.and(Rvalue::Use(Operand::Constant(Box::new(Constant {
406                     span: expr_span,
407                     user_ty: None,
408                     literal: ConstantKind::zero_sized(this.tcx.types.unit),
409                 }))))
410             }
411
412             ExprKind::Literal { .. }
413             | ExprKind::NamedConst { .. }
414             | ExprKind::NonHirLiteral { .. }
415             | ExprKind::ZstLiteral { .. }
416             | ExprKind::ConstParam { .. }
417             | ExprKind::ConstBlock { .. }
418             | ExprKind::StaticRef { .. } => {
419                 let constant = this.as_constant(expr);
420                 block.and(Rvalue::Use(Operand::Constant(Box::new(constant))))
421             }
422
423             ExprKind::Yield { .. }
424             | ExprKind::Block { .. }
425             | ExprKind::Match { .. }
426             | ExprKind::If { .. }
427             | ExprKind::NeverToAny { .. }
428             | ExprKind::Use { .. }
429             | ExprKind::Borrow { .. }
430             | ExprKind::AddressOf { .. }
431             | ExprKind::Adt { .. }
432             | ExprKind::Loop { .. }
433             | ExprKind::LogicalOp { .. }
434             | ExprKind::Call { .. }
435             | ExprKind::Field { .. }
436             | ExprKind::Let { .. }
437             | ExprKind::Deref { .. }
438             | ExprKind::Index { .. }
439             | ExprKind::VarRef { .. }
440             | ExprKind::UpvarRef { .. }
441             | ExprKind::Break { .. }
442             | ExprKind::Continue { .. }
443             | ExprKind::Return { .. }
444             | ExprKind::InlineAsm { .. }
445             | ExprKind::PlaceTypeAscription { .. }
446             | ExprKind::ValueTypeAscription { .. } => {
447                 // these do not have corresponding `Rvalue` variants,
448                 // so make an operand and then return that
449                 debug_assert!(!matches!(
450                     Category::of(&expr.kind),
451                     Some(Category::Rvalue(RvalueFunc::AsRvalue) | Category::Constant)
452                 ));
453                 let operand =
454                     unpack!(block = this.as_operand(block, scope, expr, None, NeedsTemporary::No));
455                 block.and(Rvalue::Use(operand))
456             }
457         }
458     }
459
460     pub(crate) fn build_binary_op(
461         &mut self,
462         mut block: BasicBlock,
463         op: BinOp,
464         span: Span,
465         ty: Ty<'tcx>,
466         lhs: Operand<'tcx>,
467         rhs: Operand<'tcx>,
468     ) -> BlockAnd<Rvalue<'tcx>> {
469         let source_info = self.source_info(span);
470         let bool_ty = self.tcx.types.bool;
471         if self.check_overflow && op.is_checkable() && ty.is_integral() {
472             let result_tup = self.tcx.intern_tup(&[ty, bool_ty]);
473             let result_value = self.temp(result_tup, span);
474
475             self.cfg.push_assign(
476                 block,
477                 source_info,
478                 result_value,
479                 Rvalue::CheckedBinaryOp(op, Box::new((lhs.to_copy(), rhs.to_copy()))),
480             );
481             let val_fld = Field::new(0);
482             let of_fld = Field::new(1);
483
484             let tcx = self.tcx;
485             let val = tcx.mk_place_field(result_value, val_fld, ty);
486             let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
487
488             let err = AssertKind::Overflow(op, lhs, rhs);
489
490             block = self.assert(block, Operand::Move(of), false, err, span);
491
492             block.and(Rvalue::Use(Operand::Move(val)))
493         } else {
494             if ty.is_integral() && (op == BinOp::Div || op == BinOp::Rem) {
495                 // Checking division and remainder is more complex, since we 1. always check
496                 // and 2. there are two possible failure cases, divide-by-zero and overflow.
497
498                 let zero_err = if op == BinOp::Div {
499                     AssertKind::DivisionByZero(lhs.to_copy())
500                 } else {
501                     AssertKind::RemainderByZero(lhs.to_copy())
502                 };
503                 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
504
505                 // Check for / 0
506                 let is_zero = self.temp(bool_ty, span);
507                 let zero = self.zero_literal(span, ty);
508                 self.cfg.push_assign(
509                     block,
510                     source_info,
511                     is_zero,
512                     Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), zero))),
513                 );
514
515                 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
516
517                 // We only need to check for the overflow in one case:
518                 // MIN / -1, and only for signed values.
519                 if ty.is_signed() {
520                     let neg_1 = self.neg_1_literal(span, ty);
521                     let min = self.minval_literal(span, ty);
522
523                     let is_neg_1 = self.temp(bool_ty, span);
524                     let is_min = self.temp(bool_ty, span);
525                     let of = self.temp(bool_ty, span);
526
527                     // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
528
529                     self.cfg.push_assign(
530                         block,
531                         source_info,
532                         is_neg_1,
533                         Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), neg_1))),
534                     );
535                     self.cfg.push_assign(
536                         block,
537                         source_info,
538                         is_min,
539                         Rvalue::BinaryOp(BinOp::Eq, Box::new((lhs.to_copy(), min))),
540                     );
541
542                     let is_neg_1 = Operand::Move(is_neg_1);
543                     let is_min = Operand::Move(is_min);
544                     self.cfg.push_assign(
545                         block,
546                         source_info,
547                         of,
548                         Rvalue::BinaryOp(BinOp::BitAnd, Box::new((is_neg_1, is_min))),
549                     );
550
551                     block = self.assert(block, Operand::Move(of), false, overflow_err, span);
552                 }
553             }
554
555             block.and(Rvalue::BinaryOp(op, Box::new((lhs, rhs))))
556         }
557     }
558
559     fn build_zero_repeat(
560         &mut self,
561         mut block: BasicBlock,
562         value: ExprId,
563         scope: Option<region::Scope>,
564         outer_source_info: SourceInfo,
565     ) -> BlockAnd<Rvalue<'tcx>> {
566         let this = self;
567         let value = &this.thir[value];
568         let elem_ty = value.ty;
569         if let Some(Category::Constant) = Category::of(&value.kind) {
570             // Repeating a const does nothing
571         } else {
572             // For a non-const, we may need to generate an appropriate `Drop`
573             let value_operand =
574                 unpack!(block = this.as_operand(block, scope, value, None, NeedsTemporary::No));
575             if let Operand::Move(to_drop) = value_operand {
576                 let success = this.cfg.start_new_block();
577                 this.cfg.terminate(
578                     block,
579                     outer_source_info,
580                     TerminatorKind::Drop { place: to_drop, target: success, unwind: None },
581                 );
582                 this.diverge_from(block);
583                 block = success;
584             }
585             this.record_operands_moved(&[value_operand]);
586         }
587         block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(elem_ty)), Vec::new()))
588     }
589
590     fn limit_capture_mutability(
591         &mut self,
592         upvar_span: Span,
593         upvar_ty: Ty<'tcx>,
594         temp_lifetime: Option<region::Scope>,
595         mut block: BasicBlock,
596         arg: &Expr<'tcx>,
597     ) -> BlockAnd<Operand<'tcx>> {
598         let this = self;
599
600         let source_info = this.source_info(upvar_span);
601         let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
602
603         this.cfg.push(block, Statement { source_info, kind: StatementKind::StorageLive(temp) });
604
605         let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
606
607         let mutability = match arg_place_builder.base() {
608             // We are capturing a path that starts off a local variable in the parent.
609             // The mutability of the current capture is same as the mutability
610             // of the local declaration in the parent.
611             PlaceBase::Local(local) => this.local_decls[local].mutability,
612             // Parent is a closure and we are capturing a path that is captured
613             // by the parent itself. The mutability of the current capture
614             // is same as that of the capture in the parent closure.
615             PlaceBase::Upvar { .. } => {
616                 let enclosing_upvars_resolved = arg_place_builder.clone().into_place(this);
617
618                 match enclosing_upvars_resolved.as_ref() {
619                     PlaceRef {
620                         local,
621                         projection: &[ProjectionElem::Field(upvar_index, _), ..],
622                     }
623                     | PlaceRef {
624                         local,
625                         projection:
626                             &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
627                     } => {
628                         // Not in a closure
629                         debug_assert!(
630                             local == ty::CAPTURE_STRUCT_LOCAL,
631                             "Expected local to be Local(1), found {:?}",
632                             local
633                         );
634                         // Not in a closure
635                         debug_assert!(
636                             this.upvar_mutbls.len() > upvar_index.index(),
637                             "Unexpected capture place, upvar_mutbls={:#?}, upvar_index={:?}",
638                             this.upvar_mutbls,
639                             upvar_index
640                         );
641                         this.upvar_mutbls[upvar_index.index()]
642                     }
643                     _ => bug!("Unexpected capture place"),
644                 }
645             }
646         };
647
648         let borrow_kind = match mutability {
649             Mutability::Not => BorrowKind::Unique,
650             Mutability::Mut => BorrowKind::Mut { allow_two_phase_borrow: false },
651         };
652
653         let arg_place = arg_place_builder.into_place(this);
654
655         this.cfg.push_assign(
656             block,
657             source_info,
658             Place::from(temp),
659             Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
660         );
661
662         // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
663         // this can be `None`.
664         if let Some(temp_lifetime) = temp_lifetime {
665             this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
666         }
667
668         block.and(Operand::Move(Place::from(temp)))
669     }
670
671     // Helper to get a `-1` value of the appropriate type
672     fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
673         let param_ty = ty::ParamEnv::empty().and(ty);
674         let size = self.tcx.layout_of(param_ty).unwrap().size;
675         let literal = ConstantKind::from_bits(self.tcx, size.unsigned_int_max(), param_ty);
676
677         self.literal_operand(span, literal)
678     }
679
680     // Helper to get the minimum value of the appropriate type
681     fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
682         assert!(ty.is_signed());
683         let param_ty = ty::ParamEnv::empty().and(ty);
684         let bits = self.tcx.layout_of(param_ty).unwrap().size.bits();
685         let n = 1 << (bits - 1);
686         let literal = ConstantKind::from_bits(self.tcx, n, param_ty);
687
688         self.literal_operand(span, literal)
689     }
690 }