rustc_mir_build/builder/expr/
as_rvalue.rs

1//! See docs in `build/expr/mod.rs`.
2
3use rustc_abi::{BackendRepr, FieldIdx, Primitive};
4use rustc_hir::lang_items::LangItem;
5use rustc_index::{Idx, IndexVec};
6use rustc_middle::bug;
7use rustc_middle::middle::region;
8use rustc_middle::mir::interpret::Scalar;
9use rustc_middle::mir::*;
10use rustc_middle::thir::*;
11use rustc_middle::ty::cast::{CastTy, mir_cast_kind};
12use rustc_middle::ty::layout::IntegerExt;
13use rustc_middle::ty::util::IntTypeExt;
14use rustc_middle::ty::{self, Ty, UpvarArgs};
15use rustc_span::source_map::Spanned;
16use rustc_span::{DUMMY_SP, Span};
17use tracing::debug;
18
19use crate::builder::expr::as_place::PlaceBase;
20use crate::builder::expr::category::{Category, RvalueFunc};
21use crate::builder::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary};
22
23impl<'a, 'tcx> Builder<'a, 'tcx> {
24    /// Returns an rvalue suitable for use until the end of the current
25    /// scope expression.
26    ///
27    /// The operand returned from this function will *not be valid* after
28    /// an ExprKind::Scope is passed, so please do *not* return it from
29    /// functions to avoid bad miscompiles.
30    pub(crate) fn as_local_rvalue(
31        &mut self,
32        block: BasicBlock,
33        expr_id: ExprId,
34    ) -> BlockAnd<Rvalue<'tcx>> {
35        let local_scope = self.local_scope();
36        self.as_rvalue(
37            block,
38            TempLifetime { temp_lifetime: Some(local_scope), backwards_incompatible: None },
39            expr_id,
40        )
41    }
42
43    /// Compile `expr`, yielding an rvalue.
44    pub(crate) fn as_rvalue(
45        &mut self,
46        mut block: BasicBlock,
47        scope: TempLifetime,
48        expr_id: ExprId,
49    ) -> BlockAnd<Rvalue<'tcx>> {
50        let this = self;
51        let expr = &this.thir[expr_id];
52        debug!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block, scope, expr);
53
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                if Some(0) == count.try_to_target_usize(this.tcx) {
65                    this.build_zero_repeat(block, value, scope, source_info)
66                } else {
67                    let value_operand = unpack!(
68                        block = this.as_operand(
69                            block,
70                            scope,
71                            value,
72                            LocalInfo::Boring,
73                            NeedsTemporary::No
74                        )
75                    );
76                    block.and(Rvalue::Repeat(value_operand, count))
77                }
78            }
79            ExprKind::Binary { op, lhs, rhs } => {
80                let lhs = unpack!(
81                    block = this.as_operand(
82                        block,
83                        scope,
84                        lhs,
85                        LocalInfo::Boring,
86                        NeedsTemporary::Maybe
87                    )
88                );
89                let rhs = unpack!(
90                    block =
91                        this.as_operand(block, scope, rhs, LocalInfo::Boring, NeedsTemporary::No)
92                );
93                this.build_binary_op(block, op, expr_span, expr.ty, lhs, rhs)
94            }
95            ExprKind::Unary { op, arg } => {
96                let arg = unpack!(
97                    block =
98                        this.as_operand(block, scope, arg, LocalInfo::Boring, NeedsTemporary::No)
99                );
100                // Check for -MIN on signed integers
101                if this.check_overflow && op == UnOp::Neg && expr.ty.is_signed() {
102                    let bool_ty = this.tcx.types.bool;
103
104                    let minval = this.minval_literal(expr_span, expr.ty);
105                    let is_min = this.temp(bool_ty, expr_span);
106
107                    this.cfg.push_assign(
108                        block,
109                        source_info,
110                        is_min,
111                        Rvalue::BinaryOp(BinOp::Eq, Box::new((arg.to_copy(), minval))),
112                    );
113
114                    block = this.assert(
115                        block,
116                        Operand::Move(is_min),
117                        false,
118                        AssertKind::OverflowNeg(arg.to_copy()),
119                        expr_span,
120                    );
121                }
122                block.and(Rvalue::UnaryOp(op, arg))
123            }
124            ExprKind::Box { value } => {
125                let value_ty = this.thir[value].ty;
126                let tcx = this.tcx;
127                let source_info = this.source_info(expr_span);
128
129                let size = this.temp(tcx.types.usize, expr_span);
130                this.cfg.push_assign(
131                    block,
132                    source_info,
133                    size,
134                    Rvalue::NullaryOp(NullOp::SizeOf, value_ty),
135                );
136
137                let align = this.temp(tcx.types.usize, expr_span);
138                this.cfg.push_assign(
139                    block,
140                    source_info,
141                    align,
142                    Rvalue::NullaryOp(NullOp::AlignOf, value_ty),
143                );
144
145                // malloc some memory of suitable size and align:
146                let exchange_malloc = Operand::function_handle(
147                    tcx,
148                    tcx.require_lang_item(LangItem::ExchangeMalloc, expr_span),
149                    [],
150                    expr_span,
151                );
152                let storage = this.temp(Ty::new_mut_ptr(tcx, tcx.types.u8), expr_span);
153                let success = this.cfg.start_new_block();
154                this.cfg.terminate(
155                    block,
156                    source_info,
157                    TerminatorKind::Call {
158                        func: exchange_malloc,
159                        args: [
160                            Spanned { node: Operand::Move(size), span: DUMMY_SP },
161                            Spanned { node: Operand::Move(align), span: DUMMY_SP },
162                        ]
163                        .into(),
164                        destination: storage,
165                        target: Some(success),
166                        unwind: UnwindAction::Continue,
167                        call_source: CallSource::Misc,
168                        fn_span: expr_span,
169                    },
170                );
171                this.diverge_from(block);
172                block = success;
173
174                let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span));
175                this.cfg
176                    .push(block, Statement::new(source_info, StatementKind::StorageLive(result)));
177                if let Some(scope) = scope.temp_lifetime {
178                    // schedule a shallow free of that memory, lest we unwind:
179                    this.schedule_drop_storage_and_value(expr_span, scope, result);
180                }
181
182                // Transmute `*mut u8` to the box (thus far, uninitialized):
183                let box_ = Rvalue::ShallowInitBox(Operand::Move(storage), value_ty);
184                this.cfg.push_assign(block, source_info, Place::from(result), box_);
185
186                // initialize the box contents:
187                block = this
188                    .expr_into_dest(this.tcx.mk_place_deref(Place::from(result)), block, value)
189                    .into_block();
190                block.and(Rvalue::Use(Operand::Move(Place::from(result))))
191            }
192            ExprKind::Cast { source } => {
193                let source_expr = &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_expr.ty.kind()
199                    && adt_def.is_enum()
200                {
201                    let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx);
202                    let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not));
203                    let layout =
204                        this.tcx.layout_of(this.typing_env().as_query_input(source_expr.ty));
205                    let discr = this.temp(discr_ty, source_expr.span);
206                    this.cfg.push_assign(
207                        block,
208                        source_info,
209                        discr,
210                        Rvalue::Discriminant(temp.into()),
211                    );
212                    let (op, ty) = (Operand::Move(discr), discr_ty);
213
214                    if let BackendRepr::Scalar(scalar) = layout.unwrap().backend_repr
215                        && !scalar.is_always_valid(&this.tcx)
216                        && let Primitive::Int(int_width, _signed) = scalar.primitive()
217                    {
218                        let unsigned_ty = int_width.to_ty(this.tcx, false);
219                        let unsigned_place = this.temp(unsigned_ty, expr_span);
220                        this.cfg.push_assign(
221                            block,
222                            source_info,
223                            unsigned_place,
224                            Rvalue::Cast(CastKind::IntToInt, Operand::Copy(discr), unsigned_ty),
225                        );
226
227                        let bool_ty = this.tcx.types.bool;
228                        let range = scalar.valid_range(&this.tcx);
229                        let merge_op =
230                            if range.start <= range.end { BinOp::BitAnd } else { BinOp::BitOr };
231
232                        let mut comparer = |range: u128, bin_op: BinOp| -> Place<'tcx> {
233                            // We can use `ty::TypingEnv::fully_monomorphized()` here
234                            // as we only need it to compute the layout of a primitive.
235                            let range_val = Const::from_bits(
236                                this.tcx,
237                                range,
238                                ty::TypingEnv::fully_monomorphized(),
239                                unsigned_ty,
240                            );
241                            let lit_op = this.literal_operand(expr.span, range_val);
242                            let is_bin_op = this.temp(bool_ty, expr_span);
243                            this.cfg.push_assign(
244                                block,
245                                source_info,
246                                is_bin_op,
247                                Rvalue::BinaryOp(
248                                    bin_op,
249                                    Box::new((Operand::Copy(unsigned_place), lit_op)),
250                                ),
251                            );
252                            is_bin_op
253                        };
254                        let assert_place = if range.start == 0 {
255                            comparer(range.end, BinOp::Le)
256                        } else {
257                            let start_place = comparer(range.start, BinOp::Ge);
258                            let end_place = comparer(range.end, BinOp::Le);
259                            let merge_place = this.temp(bool_ty, expr_span);
260                            this.cfg.push_assign(
261                                block,
262                                source_info,
263                                merge_place,
264                                Rvalue::BinaryOp(
265                                    merge_op,
266                                    Box::new((
267                                        Operand::Move(start_place),
268                                        Operand::Move(end_place),
269                                    )),
270                                ),
271                            );
272                            merge_place
273                        };
274                        this.cfg.push(
275                            block,
276                            Statement::new(
277                                source_info,
278                                StatementKind::Intrinsic(Box::new(NonDivergingIntrinsic::Assume(
279                                    Operand::Move(assert_place),
280                                ))),
281                            ),
282                        );
283                    }
284
285                    (op, ty)
286                } else {
287                    let ty = source_expr.ty;
288                    let source = unpack!(
289                        block = this.as_operand(
290                            block,
291                            scope,
292                            source,
293                            LocalInfo::Boring,
294                            NeedsTemporary::No
295                        )
296                    );
297                    (source, ty)
298                };
299                let from_ty = CastTy::from_ty(ty);
300                let cast_ty = CastTy::from_ty(expr.ty);
301                debug!("ExprKind::Cast from_ty={from_ty:?}, cast_ty={:?}/{cast_ty:?}", expr.ty);
302                let cast_kind = mir_cast_kind(ty, expr.ty);
303                block.and(Rvalue::Cast(cast_kind, source, expr.ty))
304            }
305            ExprKind::PointerCoercion { cast, source, is_from_as_cast } => {
306                let source = unpack!(
307                    block = this.as_operand(
308                        block,
309                        scope,
310                        source,
311                        LocalInfo::Boring,
312                        NeedsTemporary::No
313                    )
314                );
315                let origin =
316                    if is_from_as_cast { CoercionSource::AsCast } else { CoercionSource::Implicit };
317                block.and(Rvalue::Cast(CastKind::PointerCoercion(cast, origin), source, expr.ty))
318            }
319            ExprKind::Array { ref fields } => {
320                // (*) We would (maybe) be closer to codegen if we
321                // handled this and other aggregate cases via
322                // `into()`, not `as_rvalue` -- in that case, instead
323                // of generating
324                //
325                //     let tmp1 = ...1;
326                //     let tmp2 = ...2;
327                //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
328                //
329                // we could just generate
330                //
331                //     dest.f = ...1;
332                //     dest.g = ...2;
333                //
334                // The problem is that then we would need to:
335                //
336                // (a) have a more complex mechanism for handling
337                //     partial cleanup;
338                // (b) distinguish the case where the type `Foo` has a
339                //     destructor, in which case creating an instance
340                //     as a whole "arms" the destructor, and you can't
341                //     write individual fields; and,
342                // (c) handle the case where the type Foo has no
343                //     fields. We don't want `let x: ();` to compile
344                //     to the same MIR as `let x = ();`.
345
346                // first process the set of fields
347                let el_ty = expr.ty.sequence_element_type(this.tcx);
348                let fields: IndexVec<FieldIdx, _> = fields
349                    .into_iter()
350                    .copied()
351                    .map(|f| {
352                        unpack!(
353                            block = this.as_operand(
354                                block,
355                                scope,
356                                f,
357                                LocalInfo::Boring,
358                                NeedsTemporary::Maybe
359                            )
360                        )
361                    })
362                    .collect();
363
364                block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(el_ty)), fields))
365            }
366            ExprKind::Tuple { ref fields } => {
367                // see (*) above
368                // first process the set of fields
369                let fields: IndexVec<FieldIdx, _> = fields
370                    .into_iter()
371                    .copied()
372                    .map(|f| {
373                        unpack!(
374                            block = this.as_operand(
375                                block,
376                                scope,
377                                f,
378                                LocalInfo::Boring,
379                                NeedsTemporary::Maybe
380                            )
381                        )
382                    })
383                    .collect();
384
385                block.and(Rvalue::Aggregate(Box::new(AggregateKind::Tuple), fields))
386            }
387            ExprKind::Closure(box ClosureExpr {
388                closure_id,
389                args,
390                ref upvars,
391                ref fake_reads,
392                movability: _,
393            }) => {
394                // Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
395                // and push the fake reads.
396                // This must come before creating the operands. This is required in case
397                // there is a fake read and a borrow of the same path, since otherwise the
398                // fake read might interfere with the borrow. Consider an example like this
399                // one:
400                // ```
401                // let mut x = 0;
402                // let c = || {
403                //     &mut x; // mutable borrow of `x`
404                //     match x { _ => () } // fake read of `x`
405                // };
406                // ```
407                //
408                for (thir_place, cause, hir_id) in fake_reads.into_iter() {
409                    let place_builder = unpack!(block = this.as_place_builder(block, *thir_place));
410
411                    if let Some(mir_place) = place_builder.try_to_place(this) {
412                        this.cfg.push_fake_read(
413                            block,
414                            this.source_info(this.tcx.hir_span(*hir_id)),
415                            *cause,
416                            mir_place,
417                        );
418                    }
419                }
420
421                // see (*) above
422                let operands: IndexVec<FieldIdx, _> = upvars
423                    .into_iter()
424                    .copied()
425                    .map(|upvar| {
426                        let upvar_expr = &this.thir[upvar];
427                        match Category::of(&upvar_expr.kind) {
428                            // Use as_place to avoid creating a temporary when
429                            // moving a variable into a closure, so that
430                            // borrowck knows which variables to mark as being
431                            // used as mut. This is OK here because the upvar
432                            // expressions have no side effects and act on
433                            // disjoint places.
434                            // This occurs when capturing by copy/move, while
435                            // by reference captures use as_operand
436                            Some(Category::Place) => {
437                                let place = unpack!(block = this.as_place(block, upvar));
438                                this.consume_by_copy_or_move(place)
439                            }
440                            _ => {
441                                // Turn mutable borrow captures into unique
442                                // borrow captures when capturing an immutable
443                                // variable. This is sound because the mutation
444                                // that caused the capture will cause an error.
445                                match upvar_expr.kind {
446                                    ExprKind::Borrow {
447                                        borrow_kind:
448                                            BorrowKind::Mut { kind: MutBorrowKind::Default },
449                                        arg,
450                                    } => unpack!(
451                                        block = this.limit_capture_mutability(
452                                            upvar_expr.span,
453                                            upvar_expr.ty,
454                                            scope.temp_lifetime,
455                                            block,
456                                            arg,
457                                        )
458                                    ),
459                                    _ => {
460                                        unpack!(
461                                            block = this.as_operand(
462                                                block,
463                                                scope,
464                                                upvar,
465                                                LocalInfo::Boring,
466                                                NeedsTemporary::Maybe
467                                            )
468                                        )
469                                    }
470                                }
471                            }
472                        }
473                    })
474                    .collect();
475
476                let result = match args {
477                    UpvarArgs::Coroutine(args) => {
478                        Box::new(AggregateKind::Coroutine(closure_id.to_def_id(), args))
479                    }
480                    UpvarArgs::Closure(args) => {
481                        Box::new(AggregateKind::Closure(closure_id.to_def_id(), args))
482                    }
483                    UpvarArgs::CoroutineClosure(args) => {
484                        Box::new(AggregateKind::CoroutineClosure(closure_id.to_def_id(), args))
485                    }
486                };
487                block.and(Rvalue::Aggregate(result, operands))
488            }
489            ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
490                block = this.stmt_expr(block, expr_id, None).into_block();
491                block.and(Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
492                    span: expr_span,
493                    user_ty: None,
494                    const_: Const::zero_sized(this.tcx.types.unit),
495                }))))
496            }
497
498            ExprKind::OffsetOf { container, fields } => {
499                block.and(Rvalue::NullaryOp(NullOp::OffsetOf(fields), container))
500            }
501
502            ExprKind::Literal { .. }
503            | ExprKind::NamedConst { .. }
504            | ExprKind::NonHirLiteral { .. }
505            | ExprKind::ZstLiteral { .. }
506            | ExprKind::ConstParam { .. }
507            | ExprKind::ConstBlock { .. }
508            | ExprKind::StaticRef { .. } => {
509                let constant = this.as_constant(expr);
510                block.and(Rvalue::Use(Operand::Constant(Box::new(constant))))
511            }
512
513            ExprKind::WrapUnsafeBinder { source } => {
514                let source = unpack!(
515                    block = this.as_operand(
516                        block,
517                        scope,
518                        source,
519                        LocalInfo::Boring,
520                        NeedsTemporary::Maybe
521                    )
522                );
523                block.and(Rvalue::WrapUnsafeBinder(source, expr.ty))
524            }
525
526            ExprKind::Yield { .. }
527            | ExprKind::Block { .. }
528            | ExprKind::Match { .. }
529            | ExprKind::If { .. }
530            | ExprKind::NeverToAny { .. }
531            | ExprKind::Use { .. }
532            | ExprKind::Borrow { .. }
533            | ExprKind::RawBorrow { .. }
534            | ExprKind::Adt { .. }
535            | ExprKind::Loop { .. }
536            | ExprKind::LoopMatch { .. }
537            | ExprKind::LogicalOp { .. }
538            | ExprKind::Call { .. }
539            | ExprKind::Field { .. }
540            | ExprKind::Let { .. }
541            | ExprKind::Deref { .. }
542            | ExprKind::Index { .. }
543            | ExprKind::VarRef { .. }
544            | ExprKind::UpvarRef { .. }
545            | ExprKind::Break { .. }
546            | ExprKind::Continue { .. }
547            | ExprKind::ConstContinue { .. }
548            | ExprKind::Return { .. }
549            | ExprKind::Become { .. }
550            | ExprKind::InlineAsm { .. }
551            | ExprKind::PlaceTypeAscription { .. }
552            | ExprKind::ValueTypeAscription { .. }
553            | ExprKind::PlaceUnwrapUnsafeBinder { .. }
554            | ExprKind::ValueUnwrapUnsafeBinder { .. } => {
555                // these do not have corresponding `Rvalue` variants,
556                // so make an operand and then return that
557                debug_assert!(!matches!(
558                    Category::of(&expr.kind),
559                    Some(Category::Rvalue(RvalueFunc::AsRvalue) | Category::Constant)
560                ));
561                let operand = unpack!(
562                    block = this.as_operand(
563                        block,
564                        scope,
565                        expr_id,
566                        LocalInfo::Boring,
567                        NeedsTemporary::No,
568                    )
569                );
570                block.and(Rvalue::Use(operand))
571            }
572
573            ExprKind::ByUse { expr, span: _ } => {
574                let operand = unpack!(
575                    block =
576                        this.as_operand(block, scope, expr, LocalInfo::Boring, NeedsTemporary::No)
577                );
578                block.and(Rvalue::Use(operand))
579            }
580        }
581    }
582
583    pub(crate) fn build_binary_op(
584        &mut self,
585        mut block: BasicBlock,
586        op: BinOp,
587        span: Span,
588        ty: Ty<'tcx>,
589        lhs: Operand<'tcx>,
590        rhs: Operand<'tcx>,
591    ) -> BlockAnd<Rvalue<'tcx>> {
592        let source_info = self.source_info(span);
593        let bool_ty = self.tcx.types.bool;
594        let rvalue = match op {
595            BinOp::Add | BinOp::Sub | BinOp::Mul if self.check_overflow && ty.is_integral() => {
596                let result_tup = Ty::new_tup(self.tcx, &[ty, bool_ty]);
597                let result_value = self.temp(result_tup, span);
598
599                let op_with_overflow = op.wrapping_to_overflowing().unwrap();
600
601                self.cfg.push_assign(
602                    block,
603                    source_info,
604                    result_value,
605                    Rvalue::BinaryOp(op_with_overflow, Box::new((lhs.to_copy(), rhs.to_copy()))),
606                );
607                let val_fld = FieldIdx::ZERO;
608                let of_fld = FieldIdx::new(1);
609
610                let tcx = self.tcx;
611                let val = tcx.mk_place_field(result_value, val_fld, ty);
612                let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
613
614                let err = AssertKind::Overflow(op, lhs, rhs);
615                block = self.assert(block, Operand::Move(of), false, err, span);
616
617                Rvalue::Use(Operand::Move(val))
618            }
619            BinOp::Shl | BinOp::Shr if self.check_overflow && ty.is_integral() => {
620                // For an unsigned RHS, the shift is in-range for `rhs < bits`.
621                // For a signed RHS, `IntToInt` cast to the equivalent unsigned
622                // type and do that same comparison.
623                // A negative value will be *at least* 128 after the cast (that's i8::MIN),
624                // and 128 is an overflowing shift amount for all our currently existing types,
625                // so this cast can never make us miss an overflow.
626                let (lhs_size, _) = ty.int_size_and_signed(self.tcx);
627                assert!(lhs_size.bits() <= 128);
628                let rhs_ty = rhs.ty(&self.local_decls, self.tcx);
629                let (rhs_size, _) = rhs_ty.int_size_and_signed(self.tcx);
630
631                let (unsigned_rhs, unsigned_ty) = match rhs_ty.kind() {
632                    ty::Uint(_) => (rhs.to_copy(), rhs_ty),
633                    ty::Int(int_width) => {
634                        let uint_ty = Ty::new_uint(self.tcx, int_width.to_unsigned());
635                        let rhs_temp = self.temp(uint_ty, span);
636                        self.cfg.push_assign(
637                            block,
638                            source_info,
639                            rhs_temp,
640                            Rvalue::Cast(CastKind::IntToInt, rhs.to_copy(), uint_ty),
641                        );
642                        (Operand::Move(rhs_temp), uint_ty)
643                    }
644                    _ => unreachable!("only integers are shiftable"),
645                };
646
647                // This can't overflow because the largest shiftable types are 128-bit,
648                // which fits in `u8`, the smallest possible `unsigned_ty`.
649                let lhs_bits = Operand::const_from_scalar(
650                    self.tcx,
651                    unsigned_ty,
652                    Scalar::from_uint(lhs_size.bits(), rhs_size),
653                    span,
654                );
655
656                let inbounds = self.temp(bool_ty, span);
657                self.cfg.push_assign(
658                    block,
659                    source_info,
660                    inbounds,
661                    Rvalue::BinaryOp(BinOp::Lt, Box::new((unsigned_rhs, lhs_bits))),
662                );
663
664                let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
665                block = self.assert(block, Operand::Move(inbounds), true, overflow_err, span);
666                Rvalue::BinaryOp(op, Box::new((lhs, rhs)))
667            }
668            BinOp::Div | BinOp::Rem if ty.is_integral() => {
669                // Checking division and remainder is more complex, since we 1. always check
670                // and 2. there are two possible failure cases, divide-by-zero and overflow.
671
672                let zero_err = if op == BinOp::Div {
673                    AssertKind::DivisionByZero(lhs.to_copy())
674                } else {
675                    AssertKind::RemainderByZero(lhs.to_copy())
676                };
677                let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
678
679                // Check for / 0
680                let is_zero = self.temp(bool_ty, span);
681                let zero = self.zero_literal(span, ty);
682                self.cfg.push_assign(
683                    block,
684                    source_info,
685                    is_zero,
686                    Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), zero))),
687                );
688
689                block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
690
691                // We only need to check for the overflow in one case:
692                // MIN / -1, and only for signed values.
693                if ty.is_signed() {
694                    let neg_1 = self.neg_1_literal(span, ty);
695                    let min = self.minval_literal(span, ty);
696
697                    let is_neg_1 = self.temp(bool_ty, span);
698                    let is_min = self.temp(bool_ty, span);
699                    let of = self.temp(bool_ty, span);
700
701                    // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
702
703                    self.cfg.push_assign(
704                        block,
705                        source_info,
706                        is_neg_1,
707                        Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), neg_1))),
708                    );
709                    self.cfg.push_assign(
710                        block,
711                        source_info,
712                        is_min,
713                        Rvalue::BinaryOp(BinOp::Eq, Box::new((lhs.to_copy(), min))),
714                    );
715
716                    let is_neg_1 = Operand::Move(is_neg_1);
717                    let is_min = Operand::Move(is_min);
718                    self.cfg.push_assign(
719                        block,
720                        source_info,
721                        of,
722                        Rvalue::BinaryOp(BinOp::BitAnd, Box::new((is_neg_1, is_min))),
723                    );
724
725                    block = self.assert(block, Operand::Move(of), false, overflow_err, span);
726                }
727
728                Rvalue::BinaryOp(op, Box::new((lhs, rhs)))
729            }
730            _ => Rvalue::BinaryOp(op, Box::new((lhs, rhs))),
731        };
732        block.and(rvalue)
733    }
734
735    fn build_zero_repeat(
736        &mut self,
737        mut block: BasicBlock,
738        value: ExprId,
739        scope: TempLifetime,
740        outer_source_info: SourceInfo,
741    ) -> BlockAnd<Rvalue<'tcx>> {
742        let this = self;
743        let value_expr = &this.thir[value];
744        let elem_ty = value_expr.ty;
745        if let Some(Category::Constant) = Category::of(&value_expr.kind) {
746            // Repeating a const does nothing
747        } else {
748            // For a non-const, we may need to generate an appropriate `Drop`
749            let value_operand = unpack!(
750                block = this.as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No)
751            );
752            if let Operand::Move(to_drop) = value_operand {
753                let success = this.cfg.start_new_block();
754                this.cfg.terminate(
755                    block,
756                    outer_source_info,
757                    TerminatorKind::Drop {
758                        place: to_drop,
759                        target: success,
760                        unwind: UnwindAction::Continue,
761                        replace: false,
762                        drop: None,
763                        async_fut: None,
764                    },
765                );
766                this.diverge_from(block);
767                block = success;
768            }
769            this.record_operands_moved(&[Spanned { node: value_operand, span: DUMMY_SP }]);
770        }
771        block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(elem_ty)), IndexVec::new()))
772    }
773
774    fn limit_capture_mutability(
775        &mut self,
776        upvar_span: Span,
777        upvar_ty: Ty<'tcx>,
778        temp_lifetime: Option<region::Scope>,
779        mut block: BasicBlock,
780        arg: ExprId,
781    ) -> BlockAnd<Operand<'tcx>> {
782        let this = self;
783
784        let source_info = this.source_info(upvar_span);
785        let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
786
787        this.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(temp)));
788
789        let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
790
791        let mutability = match arg_place_builder.base() {
792            // We are capturing a path that starts off a local variable in the parent.
793            // The mutability of the current capture is same as the mutability
794            // of the local declaration in the parent.
795            PlaceBase::Local(local) => this.local_decls[local].mutability,
796            // Parent is a closure and we are capturing a path that is captured
797            // by the parent itself. The mutability of the current capture
798            // is same as that of the capture in the parent closure.
799            PlaceBase::Upvar { .. } => {
800                let enclosing_upvars_resolved = arg_place_builder.to_place(this);
801
802                match enclosing_upvars_resolved.as_ref() {
803                    PlaceRef {
804                        local,
805                        projection: &[ProjectionElem::Field(upvar_index, _), ..],
806                    }
807                    | PlaceRef {
808                        local,
809                        projection:
810                            &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
811                    } => {
812                        // Not in a closure
813                        debug_assert!(
814                            local == ty::CAPTURE_STRUCT_LOCAL,
815                            "Expected local to be Local(1), found {local:?}"
816                        );
817                        // Not in a closure
818                        debug_assert!(
819                            this.upvars.len() > upvar_index.index(),
820                            "Unexpected capture place, upvars={:#?}, upvar_index={:?}",
821                            this.upvars,
822                            upvar_index
823                        );
824                        this.upvars[upvar_index.index()].mutability
825                    }
826                    _ => bug!("Unexpected capture place"),
827                }
828            }
829        };
830
831        let borrow_kind = match mutability {
832            Mutability::Not => BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture },
833            Mutability::Mut => BorrowKind::Mut { kind: MutBorrowKind::Default },
834        };
835
836        let arg_place = arg_place_builder.to_place(this);
837
838        this.cfg.push_assign(
839            block,
840            source_info,
841            Place::from(temp),
842            Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
843        );
844
845        // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
846        // this can be `None`.
847        if let Some(temp_lifetime) = temp_lifetime {
848            this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
849        }
850
851        block.and(Operand::Move(Place::from(temp)))
852    }
853
854    // Helper to get a `-1` value of the appropriate type
855    fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
856        let typing_env = ty::TypingEnv::fully_monomorphized();
857        let size = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size;
858        let literal = Const::from_bits(self.tcx, size.unsigned_int_max(), typing_env, ty);
859
860        self.literal_operand(span, literal)
861    }
862
863    // Helper to get the minimum value of the appropriate type
864    fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
865        assert!(ty.is_signed());
866        let typing_env = ty::TypingEnv::fully_monomorphized();
867        let bits = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size.bits();
868        let n = 1 << (bits - 1);
869        let literal = Const::from_bits(self.tcx, n, typing_env, ty);
870
871        self.literal_operand(span, literal)
872    }
873}