1 //! See docs in `build/expr/mod.rs`.
3 use rustc_index
::vec
::Idx
;
4 use rustc_middle
::ty
::util
::IntTypeExt
;
5 use rustc_target
::abi
::{Abi, Primitive}
;
7 use crate::build
::expr
::as_place
::PlaceBase
;
8 use crate::build
::expr
::category
::{Category, RvalueFunc}
;
9 use crate::build
::{BlockAnd, BlockAndExtension, Builder, NeedsTemporary}
;
10 use rustc_hir
::lang_items
::LangItem
;
11 use rustc_middle
::middle
::region
;
12 use rustc_middle
::mir
::AssertKind
;
13 use rustc_middle
::mir
::Place
;
14 use rustc_middle
::mir
::*;
15 use rustc_middle
::thir
::*;
16 use rustc_middle
::ty
::cast
::{mir_cast_kind, CastTy}
;
17 use rustc_middle
::ty
::{self, Ty, UpvarSubsts}
;
20 impl<'a
, 'tcx
> Builder
<'a
, 'tcx
> {
21 /// Returns an rvalue suitable for use until the end of the current
24 /// The operand returned from this function will *not be valid* after
25 /// an ExprKind::Scope is passed, so please do *not* return it from
26 /// functions to avoid bad miscompiles.
27 pub(crate) fn as_local_rvalue(
31 ) -> BlockAnd
<Rvalue
<'tcx
>> {
32 let local_scope
= self.local_scope();
33 self.as_rvalue(block
, Some(local_scope
), expr
)
36 /// Compile `expr`, yielding an rvalue.
37 pub(crate) fn as_rvalue(
39 mut block
: BasicBlock
,
40 scope
: Option
<region
::Scope
>,
42 ) -> BlockAnd
<Rvalue
<'tcx
>> {
43 debug
!("expr_as_rvalue(block={:?}, scope={:?}, expr={:?})", block
, scope
, expr
);
46 let expr_span
= expr
.span
;
47 let source_info
= this
.source_info(expr_span
);
50 ExprKind
::ThreadLocalRef(did
) => block
.and(Rvalue
::ThreadLocalRef(did
)),
51 ExprKind
::Scope { region_scope, lint_level, value }
=> {
52 let region_scope
= (region_scope
, source_info
);
53 this
.in_scope(region_scope
, lint_level
, |this
| {
54 this
.as_rvalue(block
, scope
, &this
.thir
[value
])
57 ExprKind
::Repeat { value, count }
=> {
58 if Some(0) == count
.try_eval_target_usize(this
.tcx
, this
.param_env
) {
59 this
.build_zero_repeat(block
, value
, scope
, source_info
)
61 let value_operand
= unpack
!(
62 block
= this
.as_operand(
70 block
.and(Rvalue
::Repeat(value_operand
, count
))
73 ExprKind
::Binary { op, lhs, rhs }
=> {
76 this
.as_operand(block
, scope
, &this
.thir
[lhs
], None
, NeedsTemporary
::Maybe
)
80 this
.as_operand(block
, scope
, &this
.thir
[rhs
], None
, NeedsTemporary
::No
)
82 this
.build_binary_op(block
, op
, expr_span
, expr
.ty
, lhs
, rhs
)
84 ExprKind
::Unary { op, arg }
=> {
87 this
.as_operand(block
, scope
, &this
.thir
[arg
], None
, NeedsTemporary
::No
)
89 // Check for -MIN on signed integers
90 if this
.check_overflow
&& op
== UnOp
::Neg
&& expr
.ty
.is_signed() {
91 let bool_ty
= this
.tcx
.types
.bool
;
93 let minval
= this
.minval_literal(expr_span
, expr
.ty
);
94 let is_min
= this
.temp(bool_ty
, expr_span
);
100 Rvalue
::BinaryOp(BinOp
::Eq
, Box
::new((arg
.to_copy(), minval
))),
105 Operand
::Move(is_min
),
107 AssertKind
::OverflowNeg(arg
.to_copy()),
111 block
.and(Rvalue
::UnaryOp(op
, arg
))
113 ExprKind
::Box { value }
=> {
114 let value
= &this
.thir
[value
];
117 // `exchange_malloc` is unsafe but box is safe, so need a new scope.
118 let synth_scope
= this
.new_source_scope(
120 LintLevel
::Inherited
,
121 Some(Safety
::BuiltinUnsafe
),
123 let synth_info
= SourceInfo { span: expr_span, scope: synth_scope }
;
125 let size
= this
.temp(tcx
.types
.usize, expr_span
);
126 this
.cfg
.push_assign(
130 Rvalue
::NullaryOp(NullOp
::SizeOf
, value
.ty
),
133 let align
= this
.temp(tcx
.types
.usize, expr_span
);
134 this
.cfg
.push_assign(
138 Rvalue
::NullaryOp(NullOp
::AlignOf
, value
.ty
),
141 // malloc some memory of suitable size and align:
142 let exchange_malloc
= Operand
::function_handle(
144 tcx
.require_lang_item(LangItem
::ExchangeMalloc
, Some(expr_span
)),
148 let storage
= this
.temp(tcx
.mk_mut_ptr(tcx
.types
.u8), expr_span
);
149 let success
= this
.cfg
.start_new_block();
153 TerminatorKind
::Call
{
154 func
: exchange_malloc
,
155 args
: vec
![Operand
::Move(size
), Operand
::Move(align
)],
156 destination
: storage
,
157 target
: Some(success
),
159 from_hir_call
: false,
163 this
.diverge_from(block
);
166 // The `Box<T>` temporary created here is not a part of the HIR,
167 // and therefore is not considered during generator auto-trait
168 // determination. See the comment about `box` at `yield_in_scope`.
169 let result
= this
.local_decls
.push(LocalDecl
::new(expr
.ty
, expr_span
).internal());
172 Statement { source_info, kind: StatementKind::StorageLive(result) }
,
174 if let Some(scope
) = scope
{
175 // schedule a shallow free of that memory, lest we unwind:
176 this
.schedule_drop_storage_and_value(expr_span
, scope
, result
);
179 // Transmute `*mut u8` to the box (thus far, uninitialized):
180 let box_
= Rvalue
::ShallowInitBox(Operand
::Move(storage
), value
.ty
);
181 this
.cfg
.push_assign(block
, source_info
, Place
::from(result
), box_
);
183 // initialize the box contents:
185 block
= this
.expr_into_dest(
186 this
.tcx
.mk_place_deref(Place
::from(result
)),
191 block
.and(Rvalue
::Use(Operand
::Move(Place
::from(result
))))
193 ExprKind
::Cast { source }
=> {
194 let source
= &this
.thir
[source
];
196 // Casting an enum to an integer is equivalent to computing the discriminant and casting the
197 // discriminant. Previously every backend had to repeat the logic for this operation. Now we
198 // create all the steps directly in MIR with operations all backends need to support anyway.
199 let (source
, ty
) = if let ty
::Adt(adt_def
, ..) = source
.ty
.kind() && adt_def
.is_enum() {
200 let discr_ty
= adt_def
.repr().discr_type().to_ty(this
.tcx
);
201 let temp
= unpack
!(block
= this
.as_temp(block
, scope
, source
, Mutability
::Not
));
202 let layout
= this
.tcx
.layout_of(this
.param_env
.and(source
.ty
));
203 let discr
= this
.temp(discr_ty
, source
.span
);
204 this
.cfg
.push_assign(
208 Rvalue
::Discriminant(temp
.into()),
210 let (op
,ty
) = (Operand
::Move(discr
), discr_ty
);
212 if let Abi
::Scalar(scalar
) = layout
.unwrap().abi
{
213 if let Primitive
::Int(_
, signed
) = scalar
.primitive() {
214 let range
= scalar
.valid_range(&this
.tcx
);
215 // FIXME: Handle wraparound cases too.
216 if range
.end
>= range
.start
{
217 let mut assumer
= |range
: u128
, bin_op
: BinOp
| {
218 // We will be overwriting this val if our scalar is signed value
219 // because sign extension on unsigned types might cause unintended things
221 ConstantKind
::from_bits(this
.tcx
, range
, ty
::ParamEnv
::empty().and(discr_ty
));
222 let bool_ty
= this
.tcx
.types
.bool
;
224 let scalar_size_extend
= scalar
.size(&this
.tcx
).sign_extend(range
);
225 let discr_layout
= this
.tcx
.layout_of(this
.param_env
.and(discr_ty
));
226 let truncated_val
= discr_layout
.unwrap().size
.truncate(scalar_size_extend
);
227 range_val
= ConstantKind
::from_bits(
230 ty
::ParamEnv
::empty().and(discr_ty
),
233 let lit_op
= this
.literal_operand(expr
.span
, range_val
);
234 let is_bin_op
= this
.temp(bool_ty
, expr_span
);
235 this
.cfg
.push_assign(
239 Rvalue
::BinaryOp(bin_op
, Box
::new(((lit_op
), (Operand
::Copy(discr
))))),
245 kind
: StatementKind
::Intrinsic(Box
::new(NonDivergingIntrinsic
::Assume(
246 Operand
::Copy(is_bin_op
),
251 assumer(range
.end
, BinOp
::Ge
);
252 assumer(range
.start
, BinOp
::Le
);
261 let source
= unpack
!(
262 block
= this
.as_operand(block
, scope
, source
, None
, NeedsTemporary
::No
)
266 let from_ty
= CastTy
::from_ty(ty
);
267 let cast_ty
= CastTy
::from_ty(expr
.ty
);
268 debug
!("ExprKind::Cast from_ty={from_ty:?}, cast_ty={:?}/{cast_ty:?}", expr
.ty
,);
269 let cast_kind
= mir_cast_kind(ty
, expr
.ty
);
270 block
.and(Rvalue
::Cast(cast_kind
, source
, expr
.ty
))
272 ExprKind
::Pointer { cast, source }
=> {
273 let source
= unpack
!(
275 this
.as_operand(block
, scope
, &this
.thir
[source
], None
, NeedsTemporary
::No
)
277 block
.and(Rvalue
::Cast(CastKind
::Pointer(cast
), source
, expr
.ty
))
279 ExprKind
::Array { ref fields }
=> {
280 // (*) We would (maybe) be closer to codegen if we
281 // handled this and other aggregate cases via
282 // `into()`, not `as_rvalue` -- in that case, instead
287 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
289 // we could just generate
294 // The problem is that then we would need to:
296 // (a) have a more complex mechanism for handling
298 // (b) distinguish the case where the type `Foo` has a
299 // destructor, in which case creating an instance
300 // as a whole "arms" the destructor, and you can't
301 // write individual fields; and,
302 // (c) handle the case where the type Foo has no
303 // fields. We don't want `let x: ();` to compile
304 // to the same MIR as `let x = ();`.
306 // first process the set of fields
307 let el_ty
= expr
.ty
.sequence_element_type(this
.tcx
);
308 let fields
: Vec
<_
> = fields
313 block
= this
.as_operand(
318 NeedsTemporary
::Maybe
324 block
.and(Rvalue
::Aggregate(Box
::new(AggregateKind
::Array(el_ty
)), fields
))
326 ExprKind
::Tuple { ref fields }
=> {
328 // first process the set of fields
329 let fields
: Vec
<_
> = fields
334 block
= this
.as_operand(
339 NeedsTemporary
::Maybe
345 block
.and(Rvalue
::Aggregate(Box
::new(AggregateKind
::Tuple
), fields
))
347 ExprKind
::Closure(box ClosureExpr
{
354 // Convert the closure fake reads, if any, from `ExprRef` to mir `Place`
355 // and push the fake reads.
356 // This must come before creating the operands. This is required in case
357 // there is a fake read and a borrow of the same path, since otherwise the
358 // fake read might interfere with the borrow. Consider an example like this
363 // &mut x; // mutable borrow of `x`
364 // match x { _ => () } // fake read of `x`
368 for (thir_place
, cause
, hir_id
) in fake_reads
.into_iter() {
370 unpack
!(block
= this
.as_place_builder(block
, &this
.thir
[*thir_place
]));
372 if let Some(mir_place
) = place_builder
.try_to_place(this
) {
373 this
.cfg
.push_fake_read(
375 this
.source_info(this
.tcx
.hir().span(*hir_id
)),
383 let operands
: Vec
<_
> = upvars
387 let upvar
= &this
.thir
[upvar
];
388 match Category
::of(&upvar
.kind
) {
389 // Use as_place to avoid creating a temporary when
390 // moving a variable into a closure, so that
391 // borrowck knows which variables to mark as being
392 // used as mut. This is OK here because the upvar
393 // expressions have no side effects and act on
395 // This occurs when capturing by copy/move, while
396 // by reference captures use as_operand
397 Some(Category
::Place
) => {
398 let place
= unpack
!(block
= this
.as_place(block
, upvar
));
399 this
.consume_by_copy_or_move(place
)
402 // Turn mutable borrow captures into unique
403 // borrow captures when capturing an immutable
404 // variable. This is sound because the mutation
405 // that caused the capture will cause an error.
409 BorrowKind
::Mut { allow_two_phase_borrow: false }
,
412 block
= this
.limit_capture_mutability(
422 block
= this
.as_operand(
427 NeedsTemporary
::Maybe
437 let result
= match substs
{
438 UpvarSubsts
::Generator(substs
) => {
439 // We implicitly set the discriminant to 0. See
440 // librustc_mir/transform/deaggregator.rs for details.
441 let movability
= movability
.unwrap();
442 Box
::new(AggregateKind
::Generator(
443 closure_id
.to_def_id(),
448 UpvarSubsts
::Closure(substs
) => {
449 Box
::new(AggregateKind
::Closure(closure_id
.to_def_id(), substs
))
452 block
.and(Rvalue
::Aggregate(result
, operands
))
454 ExprKind
::Assign { .. }
| ExprKind
::AssignOp { .. }
=> {
455 block
= unpack
!(this
.stmt_expr(block
, expr
, None
));
456 block
.and(Rvalue
::Use(Operand
::Constant(Box
::new(Constant
{
459 literal
: ConstantKind
::zero_sized(this
.tcx
.types
.unit
),
463 ExprKind
::Literal { .. }
464 | ExprKind
::NamedConst { .. }
465 | ExprKind
::NonHirLiteral { .. }
466 | ExprKind
::ZstLiteral { .. }
467 | ExprKind
::ConstParam { .. }
468 | ExprKind
::ConstBlock { .. }
469 | ExprKind
::StaticRef { .. }
=> {
470 let constant
= this
.as_constant(expr
);
471 block
.and(Rvalue
::Use(Operand
::Constant(Box
::new(constant
))))
474 ExprKind
::Yield { .. }
475 | ExprKind
::Block { .. }
476 | ExprKind
::Match { .. }
477 | ExprKind
::If { .. }
478 | ExprKind
::NeverToAny { .. }
479 | ExprKind
::Use { .. }
480 | ExprKind
::Borrow { .. }
481 | ExprKind
::AddressOf { .. }
482 | ExprKind
::Adt { .. }
483 | ExprKind
::Loop { .. }
484 | ExprKind
::LogicalOp { .. }
485 | ExprKind
::Call { .. }
486 | ExprKind
::Field { .. }
487 | ExprKind
::Let { .. }
488 | ExprKind
::Deref { .. }
489 | ExprKind
::Index { .. }
490 | ExprKind
::VarRef { .. }
491 | ExprKind
::UpvarRef { .. }
492 | ExprKind
::Break { .. }
493 | ExprKind
::Continue { .. }
494 | ExprKind
::Return { .. }
495 | ExprKind
::InlineAsm { .. }
496 | ExprKind
::PlaceTypeAscription { .. }
497 | ExprKind
::ValueTypeAscription { .. }
=> {
498 // these do not have corresponding `Rvalue` variants,
499 // so make an operand and then return that
500 debug_assert
!(!matches
!(
501 Category
::of(&expr
.kind
),
502 Some(Category
::Rvalue(RvalueFunc
::AsRvalue
) | Category
::Constant
)
505 unpack
!(block
= this
.as_operand(block
, scope
, expr
, None
, NeedsTemporary
::No
));
506 block
.and(Rvalue
::Use(operand
))
511 pub(crate) fn build_binary_op(
513 mut block
: BasicBlock
,
519 ) -> BlockAnd
<Rvalue
<'tcx
>> {
520 let source_info
= self.source_info(span
);
521 let bool_ty
= self.tcx
.types
.bool
;
522 if self.check_overflow
&& op
.is_checkable() && ty
.is_integral() {
523 let result_tup
= self.tcx
.mk_tup(&[ty
, bool_ty
]);
524 let result_value
= self.temp(result_tup
, span
);
526 self.cfg
.push_assign(
530 Rvalue
::CheckedBinaryOp(op
, Box
::new((lhs
.to_copy(), rhs
.to_copy()))),
532 let val_fld
= Field
::new(0);
533 let of_fld
= Field
::new(1);
536 let val
= tcx
.mk_place_field(result_value
, val_fld
, ty
);
537 let of
= tcx
.mk_place_field(result_value
, of_fld
, bool_ty
);
539 let err
= AssertKind
::Overflow(op
, lhs
, rhs
);
541 block
= self.assert(block
, Operand
::Move(of
), false, err
, span
);
543 block
.and(Rvalue
::Use(Operand
::Move(val
)))
545 if ty
.is_integral() && (op
== BinOp
::Div
|| op
== BinOp
::Rem
) {
546 // Checking division and remainder is more complex, since we 1. always check
547 // and 2. there are two possible failure cases, divide-by-zero and overflow.
549 let zero_err
= if op
== BinOp
::Div
{
550 AssertKind
::DivisionByZero(lhs
.to_copy())
552 AssertKind
::RemainderByZero(lhs
.to_copy())
554 let overflow_err
= AssertKind
::Overflow(op
, lhs
.to_copy(), rhs
.to_copy());
557 let is_zero
= self.temp(bool_ty
, span
);
558 let zero
= self.zero_literal(span
, ty
);
559 self.cfg
.push_assign(
563 Rvalue
::BinaryOp(BinOp
::Eq
, Box
::new((rhs
.to_copy(), zero
))),
566 block
= self.assert(block
, Operand
::Move(is_zero
), false, zero_err
, span
);
568 // We only need to check for the overflow in one case:
569 // MIN / -1, and only for signed values.
571 let neg_1
= self.neg_1_literal(span
, ty
);
572 let min
= self.minval_literal(span
, ty
);
574 let is_neg_1
= self.temp(bool_ty
, span
);
575 let is_min
= self.temp(bool_ty
, span
);
576 let of
= self.temp(bool_ty
, span
);
578 // this does (rhs == -1) & (lhs == MIN). It could short-circuit instead
580 self.cfg
.push_assign(
584 Rvalue
::BinaryOp(BinOp
::Eq
, Box
::new((rhs
.to_copy(), neg_1
))),
586 self.cfg
.push_assign(
590 Rvalue
::BinaryOp(BinOp
::Eq
, Box
::new((lhs
.to_copy(), min
))),
593 let is_neg_1
= Operand
::Move(is_neg_1
);
594 let is_min
= Operand
::Move(is_min
);
595 self.cfg
.push_assign(
599 Rvalue
::BinaryOp(BinOp
::BitAnd
, Box
::new((is_neg_1
, is_min
))),
602 block
= self.assert(block
, Operand
::Move(of
), false, overflow_err
, span
);
606 block
.and(Rvalue
::BinaryOp(op
, Box
::new((lhs
, rhs
))))
610 fn build_zero_repeat(
612 mut block
: BasicBlock
,
614 scope
: Option
<region
::Scope
>,
615 outer_source_info
: SourceInfo
,
616 ) -> BlockAnd
<Rvalue
<'tcx
>> {
618 let value
= &this
.thir
[value
];
619 let elem_ty
= value
.ty
;
620 if let Some(Category
::Constant
) = Category
::of(&value
.kind
) {
621 // Repeating a const does nothing
623 // For a non-const, we may need to generate an appropriate `Drop`
625 unpack
!(block
= this
.as_operand(block
, scope
, value
, None
, NeedsTemporary
::No
));
626 if let Operand
::Move(to_drop
) = value_operand
{
627 let success
= this
.cfg
.start_new_block();
631 TerminatorKind
::Drop { place: to_drop, target: success, unwind: None }
,
633 this
.diverge_from(block
);
636 this
.record_operands_moved(&[value_operand
]);
638 block
.and(Rvalue
::Aggregate(Box
::new(AggregateKind
::Array(elem_ty
)), Vec
::new()))
641 fn limit_capture_mutability(
645 temp_lifetime
: Option
<region
::Scope
>,
646 mut block
: BasicBlock
,
648 ) -> BlockAnd
<Operand
<'tcx
>> {
651 let source_info
= this
.source_info(upvar_span
);
652 let temp
= this
.local_decls
.push(LocalDecl
::new(upvar_ty
, upvar_span
));
654 this
.cfg
.push(block
, Statement { source_info, kind: StatementKind::StorageLive(temp) }
);
656 let arg_place_builder
= unpack
!(block
= this
.as_place_builder(block
, arg
));
658 let mutability
= match arg_place_builder
.base() {
659 // We are capturing a path that starts off a local variable in the parent.
660 // The mutability of the current capture is same as the mutability
661 // of the local declaration in the parent.
662 PlaceBase
::Local(local
) => this
.local_decls
[local
].mutability
,
663 // Parent is a closure and we are capturing a path that is captured
664 // by the parent itself. The mutability of the current capture
665 // is same as that of the capture in the parent closure.
666 PlaceBase
::Upvar { .. }
=> {
667 let enclosing_upvars_resolved
= arg_place_builder
.to_place(this
);
669 match enclosing_upvars_resolved
.as_ref() {
672 projection
: &[ProjectionElem
::Field(upvar_index
, _
), ..],
677 &[ProjectionElem
::Deref
, ProjectionElem
::Field(upvar_index
, _
), ..],
681 local
== ty
::CAPTURE_STRUCT_LOCAL
,
682 "Expected local to be Local(1), found {:?}",
687 this
.upvars
.len() > upvar_index
.index(),
688 "Unexpected capture place, upvars={:#?}, upvar_index={:?}",
692 this
.upvars
[upvar_index
.index()].mutability
694 _
=> bug
!("Unexpected capture place"),
699 let borrow_kind
= match mutability
{
700 Mutability
::Not
=> BorrowKind
::Unique
,
701 Mutability
::Mut
=> BorrowKind
::Mut { allow_two_phase_borrow: false }
,
704 let arg_place
= arg_place_builder
.to_place(this
);
706 this
.cfg
.push_assign(
710 Rvalue
::Ref(this
.tcx
.lifetimes
.re_erased
, borrow_kind
, arg_place
),
713 // See the comment in `expr_as_temp` and on the `rvalue_scopes` field for why
714 // this can be `None`.
715 if let Some(temp_lifetime
) = temp_lifetime
{
716 this
.schedule_drop_storage_and_value(upvar_span
, temp_lifetime
, temp
);
719 block
.and(Operand
::Move(Place
::from(temp
)))
722 // Helper to get a `-1` value of the appropriate type
723 fn neg_1_literal(&mut self, span
: Span
, ty
: Ty
<'tcx
>) -> Operand
<'tcx
> {
724 let param_ty
= ty
::ParamEnv
::empty().and(ty
);
725 let size
= self.tcx
.layout_of(param_ty
).unwrap().size
;
726 let literal
= ConstantKind
::from_bits(self.tcx
, size
.unsigned_int_max(), param_ty
);
728 self.literal_operand(span
, literal
)
731 // Helper to get the minimum value of the appropriate type
732 fn minval_literal(&mut self, span
: Span
, ty
: Ty
<'tcx
>) -> Operand
<'tcx
> {
733 assert
!(ty
.is_signed());
734 let param_ty
= ty
::ParamEnv
::empty().and(ty
);
735 let bits
= self.tcx
.layout_of(param_ty
).unwrap().size
.bits();
736 let n
= 1 << (bits
- 1);
737 let literal
= ConstantKind
::from_bits(self.tcx
, n
, param_ty
);
739 self.literal_operand(span
, literal
)