1use rustc_abi::FieldIdx;
4use rustc_hir::lang_items::LangItem;
5use rustc_index::{Idx, IndexVec};
6use rustc_middle::bug;
7use rustc_middle::middle::region::{self, TempLifetime};
8use rustc_middle::mir::interpret::Scalar;
9use rustc_middle::mir::*;
10use rustc_middle::thir::*;
11use rustc_middle::ty::adjustment::PointerCoercion;
12use rustc_middle::ty::cast::{CastTy, mir_cast_kind};
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 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 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; 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 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 = tcx.require_lang_item(LangItem::SizeOf, expr_span);
130 let size = Operand::unevaluated_constant(tcx, size, &[value_ty.into()], expr_span);
131
132 let align = tcx.require_lang_item(LangItem::AlignOf, expr_span);
133 let align =
134 Operand::unevaluated_constant(tcx, align, &[value_ty.into()], expr_span);
135
136 let exchange_malloc = Operand::function_handle(
138 tcx,
139 tcx.require_lang_item(LangItem::ExchangeMalloc, expr_span),
140 [],
141 expr_span,
142 );
143 let storage = this.temp(Ty::new_mut_ptr(tcx, tcx.types.u8), expr_span);
144 let success = this.cfg.start_new_block();
145 this.cfg.terminate(
146 block,
147 source_info,
148 TerminatorKind::Call {
149 func: exchange_malloc,
150 args: [
151 Spanned { node: size, span: DUMMY_SP },
152 Spanned { node: align, span: DUMMY_SP },
153 ]
154 .into(),
155 destination: storage,
156 target: Some(success),
157 unwind: UnwindAction::Continue,
158 call_source: CallSource::Misc,
159 fn_span: expr_span,
160 },
161 );
162 this.diverge_from(block);
163 block = success;
164
165 let result = this.local_decls.push(LocalDecl::new(expr.ty, expr_span));
166 this.cfg
167 .push(block, Statement::new(source_info, StatementKind::StorageLive(result)));
168 if let Some(scope) = scope.temp_lifetime {
169 this.schedule_drop_storage_and_value(expr_span, scope, result);
171 }
172
173 let box_ = Rvalue::ShallowInitBox(Operand::Move(storage), value_ty);
175 this.cfg.push_assign(block, source_info, Place::from(result), box_);
176
177 block = this
179 .expr_into_dest(this.tcx.mk_place_deref(Place::from(result)), block, value)
180 .into_block();
181 block.and(Rvalue::Use(Operand::Move(Place::from(result))))
182 }
183 ExprKind::Cast { source } => {
184 let source_expr = &this.thir[source];
185
186 let (source, ty) = if let ty::Adt(adt_def, ..) = source_expr.ty.kind()
190 && adt_def.is_enum()
191 {
192 let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx);
193 let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not));
194 let discr = this.temp(discr_ty, source_expr.span);
195 this.cfg.push_assign(
196 block,
197 source_info,
198 discr,
199 Rvalue::Discriminant(temp.into()),
200 );
201 (Operand::Move(discr), discr_ty)
202 } else {
203 let ty = source_expr.ty;
204 let source = unpack!(
205 block = this.as_operand(
206 block,
207 scope,
208 source,
209 LocalInfo::Boring,
210 NeedsTemporary::No
211 )
212 );
213 (source, ty)
214 };
215 let from_ty = CastTy::from_ty(ty);
216 let cast_ty = CastTy::from_ty(expr.ty);
217 debug!("ExprKind::Cast from_ty={from_ty:?}, cast_ty={:?}/{cast_ty:?}", expr.ty);
218 let cast_kind = mir_cast_kind(ty, expr.ty);
219 block.and(Rvalue::Cast(cast_kind, source, expr.ty))
220 }
221 ExprKind::PointerCoercion { cast, source, is_from_as_cast } => {
222 let source = unpack!(
223 block = this.as_operand(
224 block,
225 scope,
226 source,
227 LocalInfo::Boring,
228 NeedsTemporary::No
229 )
230 );
231 let origin =
232 if is_from_as_cast { CoercionSource::AsCast } else { CoercionSource::Implicit };
233 block.and(Rvalue::Cast(CastKind::PointerCoercion(cast, origin), source, expr.ty))
234 }
235 ExprKind::Array { ref fields } => {
236 let el_ty = expr.ty.sequence_element_type(this.tcx);
264 let fields: IndexVec<FieldIdx, _> = fields
265 .into_iter()
266 .copied()
267 .map(|f| {
268 unpack!(
269 block = this.as_operand(
270 block,
271 scope,
272 f,
273 LocalInfo::Boring,
274 NeedsTemporary::Maybe
275 )
276 )
277 })
278 .collect();
279
280 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(el_ty)), fields))
281 }
282 ExprKind::Tuple { ref fields } => {
283 let fields: IndexVec<FieldIdx, _> = fields
286 .into_iter()
287 .copied()
288 .map(|f| {
289 unpack!(
290 block = this.as_operand(
291 block,
292 scope,
293 f,
294 LocalInfo::Boring,
295 NeedsTemporary::Maybe
296 )
297 )
298 })
299 .collect();
300
301 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Tuple), fields))
302 }
303 ExprKind::Closure(box ClosureExpr {
304 closure_id,
305 args,
306 ref upvars,
307 ref fake_reads,
308 movability: _,
309 }) => {
310 for (thir_place, cause, hir_id) in fake_reads.into_iter() {
325 let place_builder = unpack!(block = this.as_place_builder(block, *thir_place));
326
327 if let Some(mir_place) = place_builder.try_to_place(this) {
328 this.cfg.push_fake_read(
329 block,
330 this.source_info(this.tcx.hir_span(*hir_id)),
331 *cause,
332 mir_place,
333 );
334 }
335 }
336
337 let operands: IndexVec<FieldIdx, _> = upvars
339 .into_iter()
340 .copied()
341 .map(|upvar| {
342 let upvar_expr = &this.thir[upvar];
343 match Category::of(&upvar_expr.kind) {
344 Some(Category::Place) => {
353 let place = unpack!(block = this.as_place(block, upvar));
354 this.consume_by_copy_or_move(place)
355 }
356 _ => {
357 match upvar_expr.kind {
362 ExprKind::Borrow {
363 borrow_kind:
364 BorrowKind::Mut { kind: MutBorrowKind::Default },
365 arg,
366 } => unpack!(
367 block = this.limit_capture_mutability(
368 upvar_expr.span,
369 upvar_expr.ty,
370 scope.temp_lifetime,
371 block,
372 arg,
373 )
374 ),
375 _ => {
376 unpack!(
377 block = this.as_operand(
378 block,
379 scope,
380 upvar,
381 LocalInfo::Boring,
382 NeedsTemporary::Maybe
383 )
384 )
385 }
386 }
387 }
388 }
389 })
390 .collect();
391
392 let result = match args {
393 UpvarArgs::Coroutine(args) => {
394 Box::new(AggregateKind::Coroutine(closure_id.to_def_id(), args))
395 }
396 UpvarArgs::Closure(args) => {
397 Box::new(AggregateKind::Closure(closure_id.to_def_id(), args))
398 }
399 UpvarArgs::CoroutineClosure(args) => {
400 Box::new(AggregateKind::CoroutineClosure(closure_id.to_def_id(), args))
401 }
402 };
403 block.and(Rvalue::Aggregate(result, operands))
404 }
405 ExprKind::Assign { .. } | ExprKind::AssignOp { .. } => {
406 block = this.stmt_expr(block, expr_id, None).into_block();
407 block.and(Rvalue::Use(Operand::Constant(Box::new(ConstOperand {
408 span: expr_span,
409 user_ty: None,
410 const_: Const::zero_sized(this.tcx.types.unit),
411 }))))
412 }
413
414 ExprKind::Literal { .. }
415 | ExprKind::NamedConst { .. }
416 | ExprKind::NonHirLiteral { .. }
417 | ExprKind::ZstLiteral { .. }
418 | ExprKind::ConstParam { .. }
419 | ExprKind::ConstBlock { .. }
420 | ExprKind::StaticRef { .. } => {
421 let constant = this.as_constant(expr);
422 block.and(Rvalue::Use(Operand::Constant(Box::new(constant))))
423 }
424
425 ExprKind::WrapUnsafeBinder { source } => {
426 let source = unpack!(
427 block = this.as_operand(
428 block,
429 scope,
430 source,
431 LocalInfo::Boring,
432 NeedsTemporary::Maybe
433 )
434 );
435 block.and(Rvalue::WrapUnsafeBinder(source, expr.ty))
436 }
437
438 ExprKind::Yield { .. }
439 | ExprKind::Block { .. }
440 | ExprKind::Match { .. }
441 | ExprKind::If { .. }
442 | ExprKind::NeverToAny { .. }
443 | ExprKind::Use { .. }
444 | ExprKind::Borrow { .. }
445 | ExprKind::RawBorrow { .. }
446 | ExprKind::Adt { .. }
447 | ExprKind::Loop { .. }
448 | ExprKind::LoopMatch { .. }
449 | ExprKind::LogicalOp { .. }
450 | ExprKind::Call { .. }
451 | ExprKind::Field { .. }
452 | ExprKind::Let { .. }
453 | ExprKind::Deref { .. }
454 | ExprKind::Index { .. }
455 | ExprKind::VarRef { .. }
456 | ExprKind::UpvarRef { .. }
457 | ExprKind::Break { .. }
458 | ExprKind::Continue { .. }
459 | ExprKind::ConstContinue { .. }
460 | ExprKind::Return { .. }
461 | ExprKind::Become { .. }
462 | ExprKind::InlineAsm { .. }
463 | ExprKind::PlaceTypeAscription { .. }
464 | ExprKind::ValueTypeAscription { .. }
465 | ExprKind::PlaceUnwrapUnsafeBinder { .. }
466 | ExprKind::ValueUnwrapUnsafeBinder { .. } => {
467 debug_assert!(!matches!(
470 Category::of(&expr.kind),
471 Some(Category::Rvalue(RvalueFunc::AsRvalue) | Category::Constant)
472 ));
473 let operand = unpack!(
474 block = this.as_operand(
475 block,
476 scope,
477 expr_id,
478 LocalInfo::Boring,
479 NeedsTemporary::No,
480 )
481 );
482 block.and(Rvalue::Use(operand))
483 }
484
485 ExprKind::ByUse { expr, span: _ } => {
486 let operand = unpack!(
487 block =
488 this.as_operand(block, scope, expr, LocalInfo::Boring, NeedsTemporary::No)
489 );
490 block.and(Rvalue::Use(operand))
491 }
492 }
493 }
494
495 pub(crate) fn build_binary_op(
496 &mut self,
497 mut block: BasicBlock,
498 op: BinOp,
499 span: Span,
500 ty: Ty<'tcx>,
501 lhs: Operand<'tcx>,
502 rhs: Operand<'tcx>,
503 ) -> BlockAnd<Rvalue<'tcx>> {
504 let source_info = self.source_info(span);
505 let bool_ty = self.tcx.types.bool;
506 let rvalue = match op {
507 BinOp::Add | BinOp::Sub | BinOp::Mul if self.check_overflow && ty.is_integral() => {
508 let result_tup = Ty::new_tup(self.tcx, &[ty, bool_ty]);
509 let result_value = self.temp(result_tup, span);
510
511 let op_with_overflow = op.wrapping_to_overflowing().unwrap();
512
513 self.cfg.push_assign(
514 block,
515 source_info,
516 result_value,
517 Rvalue::BinaryOp(op_with_overflow, Box::new((lhs.to_copy(), rhs.to_copy()))),
518 );
519 let val_fld = FieldIdx::ZERO;
520 let of_fld = FieldIdx::new(1);
521
522 let tcx = self.tcx;
523 let val = tcx.mk_place_field(result_value, val_fld, ty);
524 let of = tcx.mk_place_field(result_value, of_fld, bool_ty);
525
526 let err = AssertKind::Overflow(op, lhs, rhs);
527 block = self.assert(block, Operand::Move(of), false, err, span);
528
529 Rvalue::Use(Operand::Move(val))
530 }
531 BinOp::Shl | BinOp::Shr if self.check_overflow && ty.is_integral() => {
532 let (lhs_size, _) = ty.int_size_and_signed(self.tcx);
539 assert!(lhs_size.bits() <= 128);
540 let rhs_ty = rhs.ty(&self.local_decls, self.tcx);
541 let (rhs_size, _) = rhs_ty.int_size_and_signed(self.tcx);
542
543 let (unsigned_rhs, unsigned_ty) = match rhs_ty.kind() {
544 ty::Uint(_) => (rhs.to_copy(), rhs_ty),
545 ty::Int(int_width) => {
546 let uint_ty = Ty::new_uint(self.tcx, int_width.to_unsigned());
547 let rhs_temp = self.temp(uint_ty, span);
548 self.cfg.push_assign(
549 block,
550 source_info,
551 rhs_temp,
552 Rvalue::Cast(CastKind::IntToInt, rhs.to_copy(), uint_ty),
553 );
554 (Operand::Move(rhs_temp), uint_ty)
555 }
556 _ => unreachable!("only integers are shiftable"),
557 };
558
559 let lhs_bits = Operand::const_from_scalar(
562 self.tcx,
563 unsigned_ty,
564 Scalar::from_uint(lhs_size.bits(), rhs_size),
565 span,
566 );
567
568 let inbounds = self.temp(bool_ty, span);
569 self.cfg.push_assign(
570 block,
571 source_info,
572 inbounds,
573 Rvalue::BinaryOp(BinOp::Lt, Box::new((unsigned_rhs, lhs_bits))),
574 );
575
576 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
577 block = self.assert(block, Operand::Move(inbounds), true, overflow_err, span);
578 Rvalue::BinaryOp(op, Box::new((lhs, rhs)))
579 }
580 BinOp::Div | BinOp::Rem if ty.is_integral() => {
581 let zero_err = if op == BinOp::Div {
585 AssertKind::DivisionByZero(lhs.to_copy())
586 } else {
587 AssertKind::RemainderByZero(lhs.to_copy())
588 };
589 let overflow_err = AssertKind::Overflow(op, lhs.to_copy(), rhs.to_copy());
590
591 let is_zero = self.temp(bool_ty, span);
593 let zero = self.zero_literal(span, ty);
594 self.cfg.push_assign(
595 block,
596 source_info,
597 is_zero,
598 Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), zero))),
599 );
600
601 block = self.assert(block, Operand::Move(is_zero), false, zero_err, span);
602
603 if ty.is_signed() {
606 let neg_1 = self.neg_1_literal(span, ty);
607 let min = self.minval_literal(span, ty);
608
609 let is_neg_1 = self.temp(bool_ty, span);
610 let is_min = self.temp(bool_ty, span);
611 let of = self.temp(bool_ty, span);
612
613 self.cfg.push_assign(
616 block,
617 source_info,
618 is_neg_1,
619 Rvalue::BinaryOp(BinOp::Eq, Box::new((rhs.to_copy(), neg_1))),
620 );
621 self.cfg.push_assign(
622 block,
623 source_info,
624 is_min,
625 Rvalue::BinaryOp(BinOp::Eq, Box::new((lhs.to_copy(), min))),
626 );
627
628 let is_neg_1 = Operand::Move(is_neg_1);
629 let is_min = Operand::Move(is_min);
630 self.cfg.push_assign(
631 block,
632 source_info,
633 of,
634 Rvalue::BinaryOp(BinOp::BitAnd, Box::new((is_neg_1, is_min))),
635 );
636
637 block = self.assert(block, Operand::Move(of), false, overflow_err, span);
638 }
639
640 Rvalue::BinaryOp(op, Box::new((lhs, rhs)))
641 }
642 _ => Rvalue::BinaryOp(op, Box::new((lhs, rhs))),
643 };
644 block.and(rvalue)
645 }
646
647 fn check_constness(&self, mut kind: &'a ExprKind<'tcx>) -> bool {
650 loop {
651 debug!(?kind, "check_constness");
652 match kind {
653 &ExprKind::ValueTypeAscription { source: eid, user_ty: _, user_ty_span: _ }
654 | &ExprKind::Use { source: eid }
655 | &ExprKind::PointerCoercion {
656 cast: PointerCoercion::Unsize,
657 source: eid,
658 is_from_as_cast: _,
659 }
660 | &ExprKind::Scope { region_scope: _, lint_level: _, value: eid } => {
661 kind = &self.thir[eid].kind
662 }
663 _ => return matches!(Category::of(&kind), Some(Category::Constant)),
664 }
665 }
666 }
667
668 fn build_zero_repeat(
669 &mut self,
670 mut block: BasicBlock,
671 value: ExprId,
672 scope: TempLifetime,
673 outer_source_info: SourceInfo,
674 ) -> BlockAnd<Rvalue<'tcx>> {
675 let this = self; let value_expr = &this.thir[value];
677 let elem_ty = value_expr.ty;
678 if this.check_constness(&value_expr.kind) {
679 } else {
681 let value_operand = unpack!(
683 block = this.as_operand(block, scope, value, LocalInfo::Boring, NeedsTemporary::No)
684 );
685 if let Operand::Move(to_drop) = value_operand {
686 let success = this.cfg.start_new_block();
687 this.cfg.terminate(
688 block,
689 outer_source_info,
690 TerminatorKind::Drop {
691 place: to_drop,
692 target: success,
693 unwind: UnwindAction::Continue,
694 replace: false,
695 drop: None,
696 async_fut: None,
697 },
698 );
699 this.diverge_from(block);
700 block = success;
701 }
702 this.record_operands_moved(&[Spanned { node: value_operand, span: DUMMY_SP }]);
703 }
704 block.and(Rvalue::Aggregate(Box::new(AggregateKind::Array(elem_ty)), IndexVec::new()))
705 }
706
707 fn limit_capture_mutability(
708 &mut self,
709 upvar_span: Span,
710 upvar_ty: Ty<'tcx>,
711 temp_lifetime: Option<region::Scope>,
712 mut block: BasicBlock,
713 arg: ExprId,
714 ) -> BlockAnd<Operand<'tcx>> {
715 let this = self; let source_info = this.source_info(upvar_span);
718 let temp = this.local_decls.push(LocalDecl::new(upvar_ty, upvar_span));
719
720 this.cfg.push(block, Statement::new(source_info, StatementKind::StorageLive(temp)));
721
722 let arg_place_builder = unpack!(block = this.as_place_builder(block, arg));
723
724 let mutability = match arg_place_builder.base() {
725 PlaceBase::Local(local) => this.local_decls[local].mutability,
729 PlaceBase::Upvar { .. } => {
733 let enclosing_upvars_resolved = arg_place_builder.to_place(this);
734
735 match enclosing_upvars_resolved.as_ref() {
736 PlaceRef {
737 local,
738 projection: &[ProjectionElem::Field(upvar_index, _), ..],
739 }
740 | PlaceRef {
741 local,
742 projection:
743 &[ProjectionElem::Deref, ProjectionElem::Field(upvar_index, _), ..],
744 } => {
745 debug_assert!(
747 local == ty::CAPTURE_STRUCT_LOCAL,
748 "Expected local to be Local(1), found {local:?}"
749 );
750 debug_assert!(
752 this.upvars.len() > upvar_index.index(),
753 "Unexpected capture place, upvars={:#?}, upvar_index={:?}",
754 this.upvars,
755 upvar_index
756 );
757 this.upvars[upvar_index.index()].mutability
758 }
759 _ => bug!("Unexpected capture place"),
760 }
761 }
762 };
763
764 let borrow_kind = match mutability {
765 Mutability::Not => BorrowKind::Mut { kind: MutBorrowKind::ClosureCapture },
766 Mutability::Mut => BorrowKind::Mut { kind: MutBorrowKind::Default },
767 };
768
769 let arg_place = arg_place_builder.to_place(this);
770
771 this.cfg.push_assign(
772 block,
773 source_info,
774 Place::from(temp),
775 Rvalue::Ref(this.tcx.lifetimes.re_erased, borrow_kind, arg_place),
776 );
777
778 if let Some(temp_lifetime) = temp_lifetime {
781 this.schedule_drop_storage_and_value(upvar_span, temp_lifetime, temp);
782 }
783
784 block.and(Operand::Move(Place::from(temp)))
785 }
786
787 fn neg_1_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
789 let typing_env = ty::TypingEnv::fully_monomorphized();
790 let size = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size;
791 let literal = Const::from_bits(self.tcx, size.unsigned_int_max(), typing_env, ty);
792
793 self.literal_operand(span, literal)
794 }
795
796 fn minval_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
798 assert!(ty.is_signed());
799 let typing_env = ty::TypingEnv::fully_monomorphized();
800 let bits = self.tcx.layout_of(typing_env.as_query_input(ty)).unwrap().size.bits();
801 let n = 1 << (bits - 1);
802 let literal = Const::from_bits(self.tcx, n, typing_env, ty);
803
804 self.literal_operand(span, literal)
805 }
806}