1use tracing::{debug, instrument};
4
5use super::interpret::GlobalAlloc;
6use super::*;
7use crate::ty::CoroutineArgsExt;
8
9#[derive(Clone, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
14#[non_exhaustive]
15pub struct Statement<'tcx> {
16 pub source_info: SourceInfo,
17 pub kind: StatementKind<'tcx>,
18}
19
20impl<'tcx> Statement<'tcx> {
21 pub fn make_nop(&mut self) {
24 self.kind = StatementKind::Nop
25 }
26
27 pub fn new(source_info: SourceInfo, kind: StatementKind<'tcx>) -> Self {
28 Statement { source_info, kind }
29 }
30}
31
32impl<'tcx> StatementKind<'tcx> {
33 pub const fn name(&self) -> &'static str {
36 match self {
37 StatementKind::Assign(..) => "Assign",
38 StatementKind::FakeRead(..) => "FakeRead",
39 StatementKind::SetDiscriminant { .. } => "SetDiscriminant",
40 StatementKind::Deinit(..) => "Deinit",
41 StatementKind::StorageLive(..) => "StorageLive",
42 StatementKind::StorageDead(..) => "StorageDead",
43 StatementKind::Retag(..) => "Retag",
44 StatementKind::PlaceMention(..) => "PlaceMention",
45 StatementKind::AscribeUserType(..) => "AscribeUserType",
46 StatementKind::Coverage(..) => "Coverage",
47 StatementKind::Intrinsic(..) => "Intrinsic",
48 StatementKind::ConstEvalCounter => "ConstEvalCounter",
49 StatementKind::Nop => "Nop",
50 StatementKind::BackwardIncompatibleDropHint { .. } => "BackwardIncompatibleDropHint",
51 }
52 }
53 pub fn as_assign_mut(&mut self) -> Option<&mut (Place<'tcx>, Rvalue<'tcx>)> {
54 match self {
55 StatementKind::Assign(x) => Some(x),
56 _ => None,
57 }
58 }
59
60 pub fn as_assign(&self) -> Option<&(Place<'tcx>, Rvalue<'tcx>)> {
61 match self {
62 StatementKind::Assign(x) => Some(x),
63 _ => None,
64 }
65 }
66}
67
68#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable)]
72pub struct PlaceTy<'tcx> {
73 pub ty: Ty<'tcx>,
74 pub variant_index: Option<VariantIdx>,
76}
77
78#[cfg(target_pointer_width = "64")]
80rustc_data_structures::static_assert_size!(PlaceTy<'_>, 16);
81
82impl<'tcx> PlaceTy<'tcx> {
83 #[inline]
84 pub fn from_ty(ty: Ty<'tcx>) -> PlaceTy<'tcx> {
85 PlaceTy { ty, variant_index: None }
86 }
87
88 #[instrument(level = "debug", skip(tcx), ret)]
96 pub fn field_ty(
97 tcx: TyCtxt<'tcx>,
98 self_ty: Ty<'tcx>,
99 variant_idx: Option<VariantIdx>,
100 f: FieldIdx,
101 ) -> Ty<'tcx> {
102 if let Some(variant_index) = variant_idx {
103 match *self_ty.kind() {
104 ty::Adt(adt_def, args) if adt_def.is_enum() => {
105 adt_def.variant(variant_index).fields[f].ty(tcx, args)
106 }
107 ty::Coroutine(def_id, args) => {
108 let mut variants = args.as_coroutine().state_tys(def_id, tcx);
109 let Some(mut variant) = variants.nth(variant_index.into()) else {
110 bug!("variant {variant_index:?} of coroutine out of range: {self_ty:?}");
111 };
112
113 variant.nth(f.index()).unwrap_or_else(|| {
114 bug!("field {f:?} out of range of variant: {self_ty:?} {variant_idx:?}")
115 })
116 }
117 _ => bug!("can't downcast non-adt non-coroutine type: {self_ty:?}"),
118 }
119 } else {
120 match self_ty.kind() {
121 ty::Adt(adt_def, args) if !adt_def.is_enum() => {
122 adt_def.non_enum_variant().fields[f].ty(tcx, args)
123 }
124 ty::Closure(_, args) => args
125 .as_closure()
126 .upvar_tys()
127 .get(f.index())
128 .copied()
129 .unwrap_or_else(|| bug!("field {f:?} out of range: {self_ty:?}")),
130 ty::CoroutineClosure(_, args) => args
131 .as_coroutine_closure()
132 .upvar_tys()
133 .get(f.index())
134 .copied()
135 .unwrap_or_else(|| bug!("field {f:?} out of range: {self_ty:?}")),
136 ty::Coroutine(_, args) => {
139 args.as_coroutine().prefix_tys().get(f.index()).copied().unwrap_or_else(|| {
140 bug!("field {f:?} out of range of prefixes for {self_ty}")
141 })
142 }
143 ty::Tuple(tys) => tys
144 .get(f.index())
145 .copied()
146 .unwrap_or_else(|| bug!("field {f:?} out of range: {self_ty:?}")),
147 _ => bug!("can't project out of {self_ty:?}"),
148 }
149 }
150 }
151
152 pub fn multi_projection_ty(
153 self,
154 tcx: TyCtxt<'tcx>,
155 elems: &[PlaceElem<'tcx>],
156 ) -> PlaceTy<'tcx> {
157 elems.iter().fold(self, |place_ty, &elem| place_ty.projection_ty(tcx, elem))
158 }
159
160 pub fn projection_ty(self, tcx: TyCtxt<'tcx>, elem: PlaceElem<'tcx>) -> PlaceTy<'tcx> {
164 self.projection_ty_core(tcx, &elem, |ty| ty, |_, _, _, ty| ty, |ty| ty)
165 }
166
167 pub fn projection_ty_core<V, T>(
173 self,
174 tcx: TyCtxt<'tcx>,
175 elem: &ProjectionElem<V, T>,
176 mut structurally_normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
177 mut handle_field: impl FnMut(Ty<'tcx>, Option<VariantIdx>, FieldIdx, T) -> Ty<'tcx>,
178 mut handle_opaque_cast_and_subtype: impl FnMut(T) -> Ty<'tcx>,
179 ) -> PlaceTy<'tcx>
180 where
181 V: ::std::fmt::Debug,
182 T: ::std::fmt::Debug + Copy,
183 {
184 if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) {
185 bug!("cannot use non field projection on downcasted place")
186 }
187 let answer = match *elem {
188 ProjectionElem::Deref => {
189 let ty = structurally_normalize(self.ty).builtin_deref(true).unwrap_or_else(|| {
190 bug!("deref projection of non-dereferenceable ty {:?}", self)
191 });
192 PlaceTy::from_ty(ty)
193 }
194 ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
195 PlaceTy::from_ty(structurally_normalize(self.ty).builtin_index().unwrap())
196 }
197 ProjectionElem::Subslice { from, to, from_end } => {
198 PlaceTy::from_ty(match structurally_normalize(self.ty).kind() {
199 ty::Slice(..) => self.ty,
200 ty::Array(inner, _) if !from_end => Ty::new_array(tcx, *inner, to - from),
201 ty::Array(inner, size) if from_end => {
202 let size = size
203 .try_to_target_usize(tcx)
204 .expect("expected subslice projection on fixed-size array");
205 let len = size - from - to;
206 Ty::new_array(tcx, *inner, len)
207 }
208 _ => bug!("cannot subslice non-array type: `{:?}`", self),
209 })
210 }
211 ProjectionElem::Downcast(_name, index) => {
212 PlaceTy { ty: self.ty, variant_index: Some(index) }
213 }
214 ProjectionElem::Field(f, fty) => PlaceTy::from_ty(handle_field(
215 structurally_normalize(self.ty),
216 self.variant_index,
217 f,
218 fty,
219 )),
220 ProjectionElem::OpaqueCast(ty) => PlaceTy::from_ty(handle_opaque_cast_and_subtype(ty)),
221 ProjectionElem::Subtype(ty) => PlaceTy::from_ty(handle_opaque_cast_and_subtype(ty)),
222
223 ProjectionElem::UnwrapUnsafeBinder(ty) => {
225 PlaceTy::from_ty(handle_opaque_cast_and_subtype(ty))
226 }
227 };
228 debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
229 answer
230 }
231}
232
233impl<V, T> ProjectionElem<V, T> {
234 fn is_indirect(&self) -> bool {
237 match self {
238 Self::Deref => true,
239
240 Self::Field(_, _)
241 | Self::Index(_)
242 | Self::OpaqueCast(_)
243 | Self::Subtype(_)
244 | Self::ConstantIndex { .. }
245 | Self::Subslice { .. }
246 | Self::Downcast(_, _)
247 | Self::UnwrapUnsafeBinder(..) => false,
248 }
249 }
250
251 pub fn is_stable_offset(&self) -> bool {
254 match self {
255 Self::Deref | Self::Index(_) => false,
256 Self::Field(_, _)
257 | Self::OpaqueCast(_)
258 | Self::Subtype(_)
259 | Self::ConstantIndex { .. }
260 | Self::Subslice { .. }
261 | Self::Downcast(_, _)
262 | Self::UnwrapUnsafeBinder(..) => true,
263 }
264 }
265
266 pub fn is_downcast_to(&self, v: VariantIdx) -> bool {
268 matches!(*self, Self::Downcast(_, x) if x == v)
269 }
270
271 pub fn is_field_to(&self, f: FieldIdx) -> bool {
273 matches!(*self, Self::Field(x, _) if x == f)
274 }
275
276 pub fn can_use_in_debuginfo(&self) -> bool {
278 match self {
279 Self::ConstantIndex { from_end: false, .. }
280 | Self::Deref
281 | Self::Downcast(_, _)
282 | Self::Field(_, _) => true,
283 Self::ConstantIndex { from_end: true, .. }
284 | Self::Index(_)
285 | Self::Subtype(_)
286 | Self::OpaqueCast(_)
287 | Self::Subslice { .. } => false,
288
289 Self::UnwrapUnsafeBinder(..) => false,
291 }
292 }
293}
294
295pub type ProjectionKind = ProjectionElem<(), ()>;
298
299#[derive(Clone, Copy, PartialEq, Eq, Hash)]
300pub struct PlaceRef<'tcx> {
301 pub local: Local,
302 pub projection: &'tcx [PlaceElem<'tcx>],
303}
304
305impl<'tcx> !PartialOrd for PlaceRef<'tcx> {}
310
311impl<'tcx> Place<'tcx> {
312 pub fn return_place() -> Place<'tcx> {
314 Place { local: RETURN_PLACE, projection: List::empty() }
315 }
316
317 pub fn is_indirect(&self) -> bool {
322 self.projection.iter().any(|elem| elem.is_indirect())
323 }
324
325 pub fn is_indirect_first_projection(&self) -> bool {
331 self.as_ref().is_indirect_first_projection()
332 }
333
334 #[inline(always)]
337 pub fn local_or_deref_local(&self) -> Option<Local> {
338 self.as_ref().local_or_deref_local()
339 }
340
341 #[inline(always)]
344 pub fn as_local(&self) -> Option<Local> {
345 self.as_ref().as_local()
346 }
347
348 #[inline]
349 pub fn as_ref(&self) -> PlaceRef<'tcx> {
350 PlaceRef { local: self.local, projection: self.projection }
351 }
352
353 #[inline]
361 pub fn iter_projections(
362 self,
363 ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
364 self.as_ref().iter_projections()
365 }
366
367 pub fn project_deeper(self, more_projections: &[PlaceElem<'tcx>], tcx: TyCtxt<'tcx>) -> Self {
370 if more_projections.is_empty() {
371 return self;
372 }
373
374 self.as_ref().project_deeper(more_projections, tcx)
375 }
376
377 pub fn ty_from<D: ?Sized>(
378 local: Local,
379 projection: &[PlaceElem<'tcx>],
380 local_decls: &D,
381 tcx: TyCtxt<'tcx>,
382 ) -> PlaceTy<'tcx>
383 where
384 D: HasLocalDecls<'tcx>,
385 {
386 PlaceTy::from_ty(local_decls.local_decls()[local].ty).multi_projection_ty(tcx, projection)
387 }
388
389 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
390 where
391 D: HasLocalDecls<'tcx>,
392 {
393 Place::ty_from(self.local, self.projection, local_decls, tcx)
394 }
395}
396
397impl From<Local> for Place<'_> {
398 #[inline]
399 fn from(local: Local) -> Self {
400 Place { local, projection: List::empty() }
401 }
402}
403
404impl<'tcx> PlaceRef<'tcx> {
405 pub fn local_or_deref_local(&self) -> Option<Local> {
408 match *self {
409 PlaceRef { local, projection: [] }
410 | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
411 _ => None,
412 }
413 }
414
415 pub fn is_indirect(&self) -> bool {
420 self.projection.iter().any(|elem| elem.is_indirect())
421 }
422
423 pub fn is_indirect_first_projection(&self) -> bool {
429 debug_assert!(
431 self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref)
432 );
433 self.projection.first() == Some(&PlaceElem::Deref)
434 }
435
436 #[inline]
439 pub fn as_local(&self) -> Option<Local> {
440 match *self {
441 PlaceRef { local, projection: [] } => Some(local),
442 _ => None,
443 }
444 }
445
446 #[inline]
447 pub fn to_place(&self, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
448 Place { local: self.local, projection: tcx.mk_place_elems(self.projection) }
449 }
450
451 #[inline]
452 pub fn last_projection(&self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
453 if let &[ref proj_base @ .., elem] = self.projection {
454 Some((PlaceRef { local: self.local, projection: proj_base }, elem))
455 } else {
456 None
457 }
458 }
459
460 #[inline]
468 pub fn iter_projections(
469 self,
470 ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
471 self.projection.iter().enumerate().map(move |(i, proj)| {
472 let base = PlaceRef { local: self.local, projection: &self.projection[..i] };
473 (base, *proj)
474 })
475 }
476
477 pub fn project_deeper(
480 self,
481 more_projections: &[PlaceElem<'tcx>],
482 tcx: TyCtxt<'tcx>,
483 ) -> Place<'tcx> {
484 let mut v: Vec<PlaceElem<'tcx>>;
485
486 let new_projections = if self.projection.is_empty() {
487 more_projections
488 } else {
489 v = Vec::with_capacity(self.projection.len() + more_projections.len());
490 v.extend(self.projection);
491 v.extend(more_projections);
492 &v
493 };
494
495 Place { local: self.local, projection: tcx.mk_place_elems(new_projections) }
496 }
497
498 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
499 where
500 D: HasLocalDecls<'tcx>,
501 {
502 Place::ty_from(self.local, self.projection, local_decls, tcx)
503 }
504}
505
506impl From<Local> for PlaceRef<'_> {
507 #[inline]
508 fn from(local: Local) -> Self {
509 PlaceRef { local, projection: &[] }
510 }
511}
512
513impl<'tcx> Operand<'tcx> {
517 pub fn function_handle(
521 tcx: TyCtxt<'tcx>,
522 def_id: DefId,
523 args: impl IntoIterator<Item = GenericArg<'tcx>>,
524 span: Span,
525 ) -> Self {
526 let ty = Ty::new_fn_def(tcx, def_id, args);
527 Operand::Constant(Box::new(ConstOperand {
528 span,
529 user_ty: None,
530 const_: Const::Val(ConstValue::ZeroSized, ty),
531 }))
532 }
533
534 pub fn is_move(&self) -> bool {
535 matches!(self, Operand::Move(..))
536 }
537
538 pub fn const_from_scalar(
541 tcx: TyCtxt<'tcx>,
542 ty: Ty<'tcx>,
543 val: Scalar,
544 span: Span,
545 ) -> Operand<'tcx> {
546 debug_assert!({
547 let typing_env = ty::TypingEnv::fully_monomorphized();
548 let type_size = tcx
549 .layout_of(typing_env.as_query_input(ty))
550 .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}"))
551 .size;
552 let scalar_size = match val {
553 Scalar::Int(int) => int.size(),
554 _ => panic!("Invalid scalar type {val:?}"),
555 };
556 scalar_size == type_size
557 });
558 Operand::Constant(Box::new(ConstOperand {
559 span,
560 user_ty: None,
561 const_: Const::Val(ConstValue::Scalar(val), ty),
562 }))
563 }
564
565 pub fn to_copy(&self) -> Self {
566 match *self {
567 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
568 Operand::Move(place) => Operand::Copy(place),
569 }
570 }
571
572 pub fn place(&self) -> Option<Place<'tcx>> {
575 match self {
576 Operand::Copy(place) | Operand::Move(place) => Some(*place),
577 Operand::Constant(_) => None,
578 }
579 }
580
581 pub fn constant(&self) -> Option<&ConstOperand<'tcx>> {
584 match self {
585 Operand::Constant(x) => Some(&**x),
586 Operand::Copy(_) | Operand::Move(_) => None,
587 }
588 }
589
590 pub fn const_fn_def(&self) -> Option<(DefId, GenericArgsRef<'tcx>)> {
595 let const_ty = self.constant()?.const_.ty();
596 if let ty::FnDef(def_id, args) = *const_ty.kind() { Some((def_id, args)) } else { None }
597 }
598
599 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
600 where
601 D: HasLocalDecls<'tcx>,
602 {
603 match self {
604 &Operand::Copy(ref l) | &Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
605 Operand::Constant(c) => c.const_.ty(),
606 }
607 }
608
609 pub fn span<D: ?Sized>(&self, local_decls: &D) -> Span
610 where
611 D: HasLocalDecls<'tcx>,
612 {
613 match self {
614 &Operand::Copy(ref l) | &Operand::Move(ref l) => {
615 local_decls.local_decls()[l.local].source_info.span
616 }
617 Operand::Constant(c) => c.span,
618 }
619 }
620}
621
622impl<'tcx> ConstOperand<'tcx> {
623 pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
624 match self.const_.try_to_scalar() {
625 Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance.alloc_id()) {
626 GlobalAlloc::Static(def_id) => {
627 assert!(!tcx.is_thread_local_static(def_id));
628 Some(def_id)
629 }
630 _ => None,
631 },
632 _ => None,
633 }
634 }
635
636 #[inline]
637 pub fn ty(&self) -> Ty<'tcx> {
638 self.const_.ty()
639 }
640}
641
642pub enum RvalueInitializationState {
646 Shallow,
647 Deep,
648}
649
650impl<'tcx> Rvalue<'tcx> {
651 #[inline]
653 pub fn is_safe_to_remove(&self) -> bool {
654 match self {
655 Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => false,
659
660 Rvalue::Use(_)
661 | Rvalue::CopyForDeref(_)
662 | Rvalue::Repeat(_, _)
663 | Rvalue::Ref(_, _, _)
664 | Rvalue::ThreadLocalRef(_)
665 | Rvalue::RawPtr(_, _)
666 | Rvalue::Len(_)
667 | Rvalue::Cast(
668 CastKind::IntToInt
669 | CastKind::FloatToInt
670 | CastKind::FloatToFloat
671 | CastKind::IntToFloat
672 | CastKind::FnPtrToPtr
673 | CastKind::PtrToPtr
674 | CastKind::PointerCoercion(_, _)
675 | CastKind::PointerWithExposedProvenance
676 | CastKind::Transmute,
677 _,
678 _,
679 )
680 | Rvalue::BinaryOp(_, _)
681 | Rvalue::NullaryOp(_, _)
682 | Rvalue::UnaryOp(_, _)
683 | Rvalue::Discriminant(_)
684 | Rvalue::Aggregate(_, _)
685 | Rvalue::ShallowInitBox(_, _)
686 | Rvalue::WrapUnsafeBinder(_, _) => true,
687 }
688 }
689
690 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
691 where
692 D: HasLocalDecls<'tcx>,
693 {
694 match *self {
695 Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
696 Rvalue::Repeat(ref operand, count) => {
697 Ty::new_array_with_const_len(tcx, operand.ty(local_decls, tcx), count)
698 }
699 Rvalue::ThreadLocalRef(did) => tcx.thread_local_ptr_ty(did),
700 Rvalue::Ref(reg, bk, ref place) => {
701 let place_ty = place.ty(local_decls, tcx).ty;
702 Ty::new_ref(tcx, reg, place_ty, bk.to_mutbl_lossy())
703 }
704 Rvalue::RawPtr(kind, ref place) => {
705 let place_ty = place.ty(local_decls, tcx).ty;
706 Ty::new_ptr(tcx, place_ty, kind.to_mutbl_lossy())
707 }
708 Rvalue::Len(..) => tcx.types.usize,
709 Rvalue::Cast(.., ty) => ty,
710 Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) => {
711 let lhs_ty = lhs.ty(local_decls, tcx);
712 let rhs_ty = rhs.ty(local_decls, tcx);
713 op.ty(tcx, lhs_ty, rhs_ty)
714 }
715 Rvalue::UnaryOp(op, ref operand) => {
716 let arg_ty = operand.ty(local_decls, tcx);
717 op.ty(tcx, arg_ty)
718 }
719 Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
720 Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
721 tcx.types.usize
722 }
723 Rvalue::NullaryOp(NullOp::ContractChecks, _)
724 | Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
725 Rvalue::Aggregate(ref ak, ref ops) => match **ak {
726 AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
727 AggregateKind::Tuple => {
728 Ty::new_tup_from_iter(tcx, ops.iter().map(|op| op.ty(local_decls, tcx)))
729 }
730 AggregateKind::Adt(did, _, args, _, _) => tcx.type_of(did).instantiate(tcx, args),
731 AggregateKind::Closure(did, args) => Ty::new_closure(tcx, did, args),
732 AggregateKind::Coroutine(did, args) => Ty::new_coroutine(tcx, did, args),
733 AggregateKind::CoroutineClosure(did, args) => {
734 Ty::new_coroutine_closure(tcx, did, args)
735 }
736 AggregateKind::RawPtr(ty, mutability) => Ty::new_ptr(tcx, ty, mutability),
737 },
738 Rvalue::ShallowInitBox(_, ty) => Ty::new_box(tcx, ty),
739 Rvalue::CopyForDeref(ref place) => place.ty(local_decls, tcx).ty,
740 Rvalue::WrapUnsafeBinder(_, ty) => ty,
741 }
742 }
743
744 #[inline]
745 pub fn initialization_state(&self) -> RvalueInitializationState {
748 match *self {
749 Rvalue::ShallowInitBox(_, _) => RvalueInitializationState::Shallow,
750 _ => RvalueInitializationState::Deep,
751 }
752 }
753}
754
755impl BorrowKind {
756 pub fn mutability(&self) -> Mutability {
757 match *self {
758 BorrowKind::Shared | BorrowKind::Fake(_) => Mutability::Not,
759 BorrowKind::Mut { .. } => Mutability::Mut,
760 }
761 }
762
763 pub fn allows_two_phase_borrow(&self) -> bool {
766 match *self {
767 BorrowKind::Shared
768 | BorrowKind::Fake(_)
769 | BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::ClosureCapture } => {
770 false
771 }
772 BorrowKind::Mut { kind: MutBorrowKind::TwoPhaseBorrow } => true,
773 }
774 }
775
776 pub fn to_mutbl_lossy(self) -> hir::Mutability {
777 match self {
778 BorrowKind::Mut { .. } => hir::Mutability::Mut,
779 BorrowKind::Shared => hir::Mutability::Not,
780
781 BorrowKind::Fake(_) => hir::Mutability::Not,
784 }
785 }
786}
787
788impl<'tcx> NullOp<'tcx> {
789 pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
790 match self {
791 NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
792 NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
793 }
794 }
795}
796
797impl<'tcx> UnOp {
798 pub fn ty(&self, tcx: TyCtxt<'tcx>, arg_ty: Ty<'tcx>) -> Ty<'tcx> {
799 match self {
800 UnOp::Not | UnOp::Neg => arg_ty,
801 UnOp::PtrMetadata => arg_ty.pointee_metadata_ty_or_projection(tcx),
802 }
803 }
804}
805
806impl<'tcx> BinOp {
807 pub fn ty(&self, tcx: TyCtxt<'tcx>, lhs_ty: Ty<'tcx>, rhs_ty: Ty<'tcx>) -> Ty<'tcx> {
808 match self {
810 &BinOp::Add
811 | &BinOp::AddUnchecked
812 | &BinOp::Sub
813 | &BinOp::SubUnchecked
814 | &BinOp::Mul
815 | &BinOp::MulUnchecked
816 | &BinOp::Div
817 | &BinOp::Rem
818 | &BinOp::BitXor
819 | &BinOp::BitAnd
820 | &BinOp::BitOr => {
821 assert_eq!(lhs_ty, rhs_ty);
823 lhs_ty
824 }
825 &BinOp::AddWithOverflow | &BinOp::SubWithOverflow | &BinOp::MulWithOverflow => {
826 assert_eq!(lhs_ty, rhs_ty);
828 Ty::new_tup(tcx, &[lhs_ty, tcx.types.bool])
829 }
830 &BinOp::Shl
831 | &BinOp::ShlUnchecked
832 | &BinOp::Shr
833 | &BinOp::ShrUnchecked
834 | &BinOp::Offset => {
835 lhs_ty }
837 &BinOp::Eq | &BinOp::Lt | &BinOp::Le | &BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
838 tcx.types.bool
839 }
840 &BinOp::Cmp => {
841 assert_eq!(lhs_ty, rhs_ty);
843 tcx.ty_ordering_enum(DUMMY_SP)
844 }
845 }
846 }
847 pub(crate) fn to_hir_binop(self) -> hir::BinOpKind {
848 match self {
849 BinOp::Add | BinOp::AddWithOverflow => hir::BinOpKind::Add,
852 BinOp::Sub | BinOp::SubWithOverflow => hir::BinOpKind::Sub,
853 BinOp::Mul | BinOp::MulWithOverflow => hir::BinOpKind::Mul,
854 BinOp::Div => hir::BinOpKind::Div,
855 BinOp::Rem => hir::BinOpKind::Rem,
856 BinOp::BitXor => hir::BinOpKind::BitXor,
857 BinOp::BitAnd => hir::BinOpKind::BitAnd,
858 BinOp::BitOr => hir::BinOpKind::BitOr,
859 BinOp::Shl => hir::BinOpKind::Shl,
860 BinOp::Shr => hir::BinOpKind::Shr,
861 BinOp::Eq => hir::BinOpKind::Eq,
862 BinOp::Ne => hir::BinOpKind::Ne,
863 BinOp::Lt => hir::BinOpKind::Lt,
864 BinOp::Gt => hir::BinOpKind::Gt,
865 BinOp::Le => hir::BinOpKind::Le,
866 BinOp::Ge => hir::BinOpKind::Ge,
867 BinOp::Cmp
869 | BinOp::AddUnchecked
870 | BinOp::SubUnchecked
871 | BinOp::MulUnchecked
872 | BinOp::ShlUnchecked
873 | BinOp::ShrUnchecked
874 | BinOp::Offset => {
875 unreachable!()
876 }
877 }
878 }
879
880 pub fn overflowing_to_wrapping(self) -> Option<BinOp> {
882 Some(match self {
883 BinOp::AddWithOverflow => BinOp::Add,
884 BinOp::SubWithOverflow => BinOp::Sub,
885 BinOp::MulWithOverflow => BinOp::Mul,
886 _ => return None,
887 })
888 }
889
890 pub fn is_overflowing(self) -> bool {
892 self.overflowing_to_wrapping().is_some()
893 }
894
895 pub fn wrapping_to_overflowing(self) -> Option<BinOp> {
897 Some(match self {
898 BinOp::Add => BinOp::AddWithOverflow,
899 BinOp::Sub => BinOp::SubWithOverflow,
900 BinOp::Mul => BinOp::MulWithOverflow,
901 _ => return None,
902 })
903 }
904}
905
906impl From<Mutability> for RawPtrKind {
907 fn from(other: Mutability) -> Self {
908 match other {
909 Mutability::Mut => RawPtrKind::Mut,
910 Mutability::Not => RawPtrKind::Const,
911 }
912 }
913}
914
915impl RawPtrKind {
916 pub fn is_fake(self) -> bool {
917 match self {
918 RawPtrKind::Mut | RawPtrKind::Const => false,
919 RawPtrKind::FakeForPtrMetadata => true,
920 }
921 }
922
923 pub fn to_mutbl_lossy(self) -> Mutability {
924 match self {
925 RawPtrKind::Mut => Mutability::Mut,
926 RawPtrKind::Const => Mutability::Not,
927
928 RawPtrKind::FakeForPtrMetadata => Mutability::Not,
931 }
932 }
933
934 pub fn ptr_str(self) -> &'static str {
935 match self {
936 RawPtrKind::Mut => "mut",
937 RawPtrKind::Const => "const",
938 RawPtrKind::FakeForPtrMetadata => "const (fake)",
939 }
940 }
941}