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<V: ::std::fmt::Debug>(
164 self,
165 tcx: TyCtxt<'tcx>,
166 elem: ProjectionElem<V, Ty<'tcx>>,
167 ) -> PlaceTy<'tcx> {
168 self.projection_ty_core(tcx, &elem, |ty| ty, |_, _, _, ty| ty, |ty| ty)
169 }
170
171 pub fn projection_ty_core<V, T>(
177 self,
178 tcx: TyCtxt<'tcx>,
179 elem: &ProjectionElem<V, T>,
180 mut structurally_normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
181 mut handle_field: impl FnMut(Ty<'tcx>, Option<VariantIdx>, FieldIdx, T) -> Ty<'tcx>,
182 mut handle_opaque_cast_and_subtype: impl FnMut(T) -> Ty<'tcx>,
183 ) -> PlaceTy<'tcx>
184 where
185 V: ::std::fmt::Debug,
186 T: ::std::fmt::Debug + Copy,
187 {
188 if self.variant_index.is_some() && !matches!(elem, ProjectionElem::Field(..)) {
189 bug!("cannot use non field projection on downcasted place")
190 }
191 let answer = match *elem {
192 ProjectionElem::Deref => {
193 let ty = structurally_normalize(self.ty).builtin_deref(true).unwrap_or_else(|| {
194 bug!("deref projection of non-dereferenceable ty {:?}", self)
195 });
196 PlaceTy::from_ty(ty)
197 }
198 ProjectionElem::Index(_) | ProjectionElem::ConstantIndex { .. } => {
199 PlaceTy::from_ty(structurally_normalize(self.ty).builtin_index().unwrap())
200 }
201 ProjectionElem::Subslice { from, to, from_end } => {
202 PlaceTy::from_ty(match structurally_normalize(self.ty).kind() {
203 ty::Slice(..) => self.ty,
204 ty::Array(inner, _) if !from_end => Ty::new_array(tcx, *inner, to - from),
205 ty::Array(inner, size) if from_end => {
206 let size = size
207 .try_to_target_usize(tcx)
208 .expect("expected subslice projection on fixed-size array");
209 let len = size - from - to;
210 Ty::new_array(tcx, *inner, len)
211 }
212 _ => bug!("cannot subslice non-array type: `{:?}`", self),
213 })
214 }
215 ProjectionElem::Downcast(_name, index) => {
216 PlaceTy { ty: self.ty, variant_index: Some(index) }
217 }
218 ProjectionElem::Field(f, fty) => PlaceTy::from_ty(handle_field(
219 structurally_normalize(self.ty),
220 self.variant_index,
221 f,
222 fty,
223 )),
224 ProjectionElem::OpaqueCast(ty) => PlaceTy::from_ty(handle_opaque_cast_and_subtype(ty)),
225 ProjectionElem::Subtype(ty) => PlaceTy::from_ty(handle_opaque_cast_and_subtype(ty)),
226
227 ProjectionElem::UnwrapUnsafeBinder(ty) => {
229 PlaceTy::from_ty(handle_opaque_cast_and_subtype(ty))
230 }
231 };
232 debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
233 answer
234 }
235}
236
237impl<V, T> ProjectionElem<V, T> {
238 fn is_indirect(&self) -> bool {
241 match self {
242 Self::Deref => true,
243
244 Self::Field(_, _)
245 | Self::Index(_)
246 | Self::OpaqueCast(_)
247 | Self::Subtype(_)
248 | Self::ConstantIndex { .. }
249 | Self::Subslice { .. }
250 | Self::Downcast(_, _)
251 | Self::UnwrapUnsafeBinder(..) => false,
252 }
253 }
254
255 pub fn is_stable_offset(&self) -> bool {
258 match self {
259 Self::Deref | Self::Index(_) => false,
260 Self::Field(_, _)
261 | Self::OpaqueCast(_)
262 | Self::Subtype(_)
263 | Self::ConstantIndex { .. }
264 | Self::Subslice { .. }
265 | Self::Downcast(_, _)
266 | Self::UnwrapUnsafeBinder(..) => true,
267 }
268 }
269
270 pub fn is_downcast_to(&self, v: VariantIdx) -> bool {
272 matches!(*self, Self::Downcast(_, x) if x == v)
273 }
274
275 pub fn is_field_to(&self, f: FieldIdx) -> bool {
277 matches!(*self, Self::Field(x, _) if x == f)
278 }
279
280 pub fn can_use_in_debuginfo(&self) -> bool {
282 match self {
283 Self::ConstantIndex { from_end: false, .. }
284 | Self::Deref
285 | Self::Downcast(_, _)
286 | Self::Field(_, _) => true,
287 Self::ConstantIndex { from_end: true, .. }
288 | Self::Index(_)
289 | Self::Subtype(_)
290 | Self::OpaqueCast(_)
291 | Self::Subslice { .. } => false,
292
293 Self::UnwrapUnsafeBinder(..) => false,
295 }
296 }
297
298 pub fn kind(self) -> ProjectionKind {
300 self.try_map(|_| Some(()), |_| ()).unwrap()
301 }
302
303 pub fn try_map<V2, T2>(
305 self,
306 v: impl FnOnce(V) -> Option<V2>,
307 t: impl FnOnce(T) -> T2,
308 ) -> Option<ProjectionElem<V2, T2>> {
309 Some(match self {
310 ProjectionElem::Deref => ProjectionElem::Deref,
311 ProjectionElem::Downcast(name, read_variant) => {
312 ProjectionElem::Downcast(name, read_variant)
313 }
314 ProjectionElem::Field(f, ty) => ProjectionElem::Field(f, t(ty)),
315 ProjectionElem::ConstantIndex { offset, min_length, from_end } => {
316 ProjectionElem::ConstantIndex { offset, min_length, from_end }
317 }
318 ProjectionElem::Subslice { from, to, from_end } => {
319 ProjectionElem::Subslice { from, to, from_end }
320 }
321 ProjectionElem::OpaqueCast(ty) => ProjectionElem::OpaqueCast(t(ty)),
322 ProjectionElem::Subtype(ty) => ProjectionElem::Subtype(t(ty)),
323 ProjectionElem::UnwrapUnsafeBinder(ty) => ProjectionElem::UnwrapUnsafeBinder(t(ty)),
324 ProjectionElem::Index(val) => ProjectionElem::Index(v(val)?),
325 })
326 }
327}
328
329pub type ProjectionKind = ProjectionElem<(), ()>;
332
333#[derive(Clone, Copy, PartialEq, Eq, Hash)]
334pub struct PlaceRef<'tcx> {
335 pub local: Local,
336 pub projection: &'tcx [PlaceElem<'tcx>],
337}
338
339impl<'tcx> !PartialOrd for PlaceRef<'tcx> {}
344
345impl<'tcx> Place<'tcx> {
346 pub fn return_place() -> Place<'tcx> {
348 Place { local: RETURN_PLACE, projection: List::empty() }
349 }
350
351 pub fn is_indirect(&self) -> bool {
356 self.projection.iter().any(|elem| elem.is_indirect())
357 }
358
359 pub fn is_indirect_first_projection(&self) -> bool {
365 self.as_ref().is_indirect_first_projection()
366 }
367
368 #[inline(always)]
371 pub fn local_or_deref_local(&self) -> Option<Local> {
372 self.as_ref().local_or_deref_local()
373 }
374
375 #[inline(always)]
378 pub fn as_local(&self) -> Option<Local> {
379 self.as_ref().as_local()
380 }
381
382 #[inline]
383 pub fn as_ref(&self) -> PlaceRef<'tcx> {
384 PlaceRef { local: self.local, projection: self.projection }
385 }
386
387 #[inline]
395 pub fn iter_projections(
396 self,
397 ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
398 self.as_ref().iter_projections()
399 }
400
401 pub fn project_deeper(self, more_projections: &[PlaceElem<'tcx>], tcx: TyCtxt<'tcx>) -> Self {
404 if more_projections.is_empty() {
405 return self;
406 }
407
408 self.as_ref().project_deeper(more_projections, tcx)
409 }
410
411 pub fn ty_from<D: ?Sized>(
412 local: Local,
413 projection: &[PlaceElem<'tcx>],
414 local_decls: &D,
415 tcx: TyCtxt<'tcx>,
416 ) -> PlaceTy<'tcx>
417 where
418 D: HasLocalDecls<'tcx>,
419 {
420 PlaceTy::from_ty(local_decls.local_decls()[local].ty).multi_projection_ty(tcx, projection)
421 }
422
423 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
424 where
425 D: HasLocalDecls<'tcx>,
426 {
427 Place::ty_from(self.local, self.projection, local_decls, tcx)
428 }
429}
430
431impl From<Local> for Place<'_> {
432 #[inline]
433 fn from(local: Local) -> Self {
434 Place { local, projection: List::empty() }
435 }
436}
437
438impl<'tcx> PlaceRef<'tcx> {
439 pub fn local_or_deref_local(&self) -> Option<Local> {
442 match *self {
443 PlaceRef { local, projection: [] }
444 | PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(local),
445 _ => None,
446 }
447 }
448
449 pub fn is_indirect(&self) -> bool {
454 self.projection.iter().any(|elem| elem.is_indirect())
455 }
456
457 pub fn is_indirect_first_projection(&self) -> bool {
463 debug_assert!(
465 self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref)
466 );
467 self.projection.first() == Some(&PlaceElem::Deref)
468 }
469
470 #[inline]
473 pub fn as_local(&self) -> Option<Local> {
474 match *self {
475 PlaceRef { local, projection: [] } => Some(local),
476 _ => None,
477 }
478 }
479
480 #[inline]
481 pub fn to_place(&self, tcx: TyCtxt<'tcx>) -> Place<'tcx> {
482 Place { local: self.local, projection: tcx.mk_place_elems(self.projection) }
483 }
484
485 #[inline]
486 pub fn last_projection(&self) -> Option<(PlaceRef<'tcx>, PlaceElem<'tcx>)> {
487 if let &[ref proj_base @ .., elem] = self.projection {
488 Some((PlaceRef { local: self.local, projection: proj_base }, elem))
489 } else {
490 None
491 }
492 }
493
494 #[inline]
502 pub fn iter_projections(
503 self,
504 ) -> impl Iterator<Item = (PlaceRef<'tcx>, PlaceElem<'tcx>)> + DoubleEndedIterator {
505 self.projection.iter().enumerate().map(move |(i, proj)| {
506 let base = PlaceRef { local: self.local, projection: &self.projection[..i] };
507 (base, *proj)
508 })
509 }
510
511 pub fn project_deeper(
514 self,
515 more_projections: &[PlaceElem<'tcx>],
516 tcx: TyCtxt<'tcx>,
517 ) -> Place<'tcx> {
518 let mut v: Vec<PlaceElem<'tcx>>;
519
520 let new_projections = if self.projection.is_empty() {
521 more_projections
522 } else {
523 v = Vec::with_capacity(self.projection.len() + more_projections.len());
524 v.extend(self.projection);
525 v.extend(more_projections);
526 &v
527 };
528
529 Place { local: self.local, projection: tcx.mk_place_elems(new_projections) }
530 }
531
532 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
533 where
534 D: HasLocalDecls<'tcx>,
535 {
536 Place::ty_from(self.local, self.projection, local_decls, tcx)
537 }
538}
539
540impl From<Local> for PlaceRef<'_> {
541 #[inline]
542 fn from(local: Local) -> Self {
543 PlaceRef { local, projection: &[] }
544 }
545}
546
547impl<'tcx> Operand<'tcx> {
551 pub fn function_handle(
555 tcx: TyCtxt<'tcx>,
556 def_id: DefId,
557 args: impl IntoIterator<Item = GenericArg<'tcx>>,
558 span: Span,
559 ) -> Self {
560 let ty = Ty::new_fn_def(tcx, def_id, args);
561 Operand::Constant(Box::new(ConstOperand {
562 span,
563 user_ty: None,
564 const_: Const::Val(ConstValue::ZeroSized, ty),
565 }))
566 }
567
568 pub fn is_move(&self) -> bool {
569 matches!(self, Operand::Move(..))
570 }
571
572 pub fn const_from_scalar(
575 tcx: TyCtxt<'tcx>,
576 ty: Ty<'tcx>,
577 val: Scalar,
578 span: Span,
579 ) -> Operand<'tcx> {
580 debug_assert!({
581 let typing_env = ty::TypingEnv::fully_monomorphized();
582 let type_size = tcx
583 .layout_of(typing_env.as_query_input(ty))
584 .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}"))
585 .size;
586 let scalar_size = match val {
587 Scalar::Int(int) => int.size(),
588 _ => panic!("Invalid scalar type {val:?}"),
589 };
590 scalar_size == type_size
591 });
592 Operand::Constant(Box::new(ConstOperand {
593 span,
594 user_ty: None,
595 const_: Const::Val(ConstValue::Scalar(val), ty),
596 }))
597 }
598
599 pub fn to_copy(&self) -> Self {
600 match *self {
601 Operand::Copy(_) | Operand::Constant(_) => self.clone(),
602 Operand::Move(place) => Operand::Copy(place),
603 }
604 }
605
606 pub fn place(&self) -> Option<Place<'tcx>> {
609 match self {
610 Operand::Copy(place) | Operand::Move(place) => Some(*place),
611 Operand::Constant(_) => None,
612 }
613 }
614
615 pub fn constant(&self) -> Option<&ConstOperand<'tcx>> {
618 match self {
619 Operand::Constant(x) => Some(&**x),
620 Operand::Copy(_) | Operand::Move(_) => None,
621 }
622 }
623
624 pub fn const_fn_def(&self) -> Option<(DefId, GenericArgsRef<'tcx>)> {
629 let const_ty = self.constant()?.const_.ty();
630 if let ty::FnDef(def_id, args) = *const_ty.kind() { Some((def_id, args)) } else { None }
631 }
632
633 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
634 where
635 D: HasLocalDecls<'tcx>,
636 {
637 match self {
638 &Operand::Copy(ref l) | &Operand::Move(ref l) => l.ty(local_decls, tcx).ty,
639 Operand::Constant(c) => c.const_.ty(),
640 }
641 }
642
643 pub fn span<D: ?Sized>(&self, local_decls: &D) -> Span
644 where
645 D: HasLocalDecls<'tcx>,
646 {
647 match self {
648 &Operand::Copy(ref l) | &Operand::Move(ref l) => {
649 local_decls.local_decls()[l.local].source_info.span
650 }
651 Operand::Constant(c) => c.span,
652 }
653 }
654}
655
656impl<'tcx> ConstOperand<'tcx> {
657 pub fn check_static_ptr(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
658 match self.const_.try_to_scalar() {
659 Some(Scalar::Ptr(ptr, _size)) => match tcx.global_alloc(ptr.provenance.alloc_id()) {
660 GlobalAlloc::Static(def_id) => {
661 assert!(!tcx.is_thread_local_static(def_id));
662 Some(def_id)
663 }
664 _ => None,
665 },
666 _ => None,
667 }
668 }
669
670 #[inline]
671 pub fn ty(&self) -> Ty<'tcx> {
672 self.const_.ty()
673 }
674}
675
676pub enum RvalueInitializationState {
680 Shallow,
681 Deep,
682}
683
684impl<'tcx> Rvalue<'tcx> {
685 #[inline]
687 pub fn is_safe_to_remove(&self) -> bool {
688 match self {
689 Rvalue::Cast(CastKind::PointerExposeProvenance, _, _) => false,
693
694 Rvalue::Use(_)
695 | Rvalue::CopyForDeref(_)
696 | Rvalue::Repeat(_, _)
697 | Rvalue::Ref(_, _, _)
698 | Rvalue::ThreadLocalRef(_)
699 | Rvalue::RawPtr(_, _)
700 | Rvalue::Len(_)
701 | Rvalue::Cast(
702 CastKind::IntToInt
703 | CastKind::FloatToInt
704 | CastKind::FloatToFloat
705 | CastKind::IntToFloat
706 | CastKind::FnPtrToPtr
707 | CastKind::PtrToPtr
708 | CastKind::PointerCoercion(_, _)
709 | CastKind::PointerWithExposedProvenance
710 | CastKind::Transmute,
711 _,
712 _,
713 )
714 | Rvalue::BinaryOp(_, _)
715 | Rvalue::NullaryOp(_, _)
716 | Rvalue::UnaryOp(_, _)
717 | Rvalue::Discriminant(_)
718 | Rvalue::Aggregate(_, _)
719 | Rvalue::ShallowInitBox(_, _)
720 | Rvalue::WrapUnsafeBinder(_, _) => true,
721 }
722 }
723
724 pub fn ty<D: ?Sized>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> Ty<'tcx>
725 where
726 D: HasLocalDecls<'tcx>,
727 {
728 match *self {
729 Rvalue::Use(ref operand) => operand.ty(local_decls, tcx),
730 Rvalue::Repeat(ref operand, count) => {
731 Ty::new_array_with_const_len(tcx, operand.ty(local_decls, tcx), count)
732 }
733 Rvalue::ThreadLocalRef(did) => tcx.thread_local_ptr_ty(did),
734 Rvalue::Ref(reg, bk, ref place) => {
735 let place_ty = place.ty(local_decls, tcx).ty;
736 Ty::new_ref(tcx, reg, place_ty, bk.to_mutbl_lossy())
737 }
738 Rvalue::RawPtr(kind, ref place) => {
739 let place_ty = place.ty(local_decls, tcx).ty;
740 Ty::new_ptr(tcx, place_ty, kind.to_mutbl_lossy())
741 }
742 Rvalue::Len(..) => tcx.types.usize,
743 Rvalue::Cast(.., ty) => ty,
744 Rvalue::BinaryOp(op, box (ref lhs, ref rhs)) => {
745 let lhs_ty = lhs.ty(local_decls, tcx);
746 let rhs_ty = rhs.ty(local_decls, tcx);
747 op.ty(tcx, lhs_ty, rhs_ty)
748 }
749 Rvalue::UnaryOp(op, ref operand) => {
750 let arg_ty = operand.ty(local_decls, tcx);
751 op.ty(tcx, arg_ty)
752 }
753 Rvalue::Discriminant(ref place) => place.ty(local_decls, tcx).ty.discriminant_ty(tcx),
754 Rvalue::NullaryOp(NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(..), _) => {
755 tcx.types.usize
756 }
757 Rvalue::NullaryOp(NullOp::ContractChecks, _)
758 | Rvalue::NullaryOp(NullOp::UbChecks, _) => tcx.types.bool,
759 Rvalue::Aggregate(ref ak, ref ops) => match **ak {
760 AggregateKind::Array(ty) => Ty::new_array(tcx, ty, ops.len() as u64),
761 AggregateKind::Tuple => {
762 Ty::new_tup_from_iter(tcx, ops.iter().map(|op| op.ty(local_decls, tcx)))
763 }
764 AggregateKind::Adt(did, _, args, _, _) => tcx.type_of(did).instantiate(tcx, args),
765 AggregateKind::Closure(did, args) => Ty::new_closure(tcx, did, args),
766 AggregateKind::Coroutine(did, args) => Ty::new_coroutine(tcx, did, args),
767 AggregateKind::CoroutineClosure(did, args) => {
768 Ty::new_coroutine_closure(tcx, did, args)
769 }
770 AggregateKind::RawPtr(ty, mutability) => Ty::new_ptr(tcx, ty, mutability),
771 },
772 Rvalue::ShallowInitBox(_, ty) => Ty::new_box(tcx, ty),
773 Rvalue::CopyForDeref(ref place) => place.ty(local_decls, tcx).ty,
774 Rvalue::WrapUnsafeBinder(_, ty) => ty,
775 }
776 }
777
778 #[inline]
779 pub fn initialization_state(&self) -> RvalueInitializationState {
782 match *self {
783 Rvalue::ShallowInitBox(_, _) => RvalueInitializationState::Shallow,
784 _ => RvalueInitializationState::Deep,
785 }
786 }
787}
788
789impl BorrowKind {
790 pub fn mutability(&self) -> Mutability {
791 match *self {
792 BorrowKind::Shared | BorrowKind::Fake(_) => Mutability::Not,
793 BorrowKind::Mut { .. } => Mutability::Mut,
794 }
795 }
796
797 pub fn allows_two_phase_borrow(&self) -> bool {
800 match *self {
801 BorrowKind::Shared
802 | BorrowKind::Fake(_)
803 | BorrowKind::Mut { kind: MutBorrowKind::Default | MutBorrowKind::ClosureCapture } => {
804 false
805 }
806 BorrowKind::Mut { kind: MutBorrowKind::TwoPhaseBorrow } => true,
807 }
808 }
809
810 pub fn to_mutbl_lossy(self) -> hir::Mutability {
811 match self {
812 BorrowKind::Mut { .. } => hir::Mutability::Mut,
813 BorrowKind::Shared => hir::Mutability::Not,
814
815 BorrowKind::Fake(_) => hir::Mutability::Not,
818 }
819 }
820}
821
822impl<'tcx> NullOp<'tcx> {
823 pub fn ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
824 match self {
825 NullOp::SizeOf | NullOp::AlignOf | NullOp::OffsetOf(_) => tcx.types.usize,
826 NullOp::UbChecks | NullOp::ContractChecks => tcx.types.bool,
827 }
828 }
829}
830
831impl<'tcx> UnOp {
832 pub fn ty(&self, tcx: TyCtxt<'tcx>, arg_ty: Ty<'tcx>) -> Ty<'tcx> {
833 match self {
834 UnOp::Not | UnOp::Neg => arg_ty,
835 UnOp::PtrMetadata => arg_ty.pointee_metadata_ty_or_projection(tcx),
836 }
837 }
838}
839
840impl<'tcx> BinOp {
841 pub fn ty(&self, tcx: TyCtxt<'tcx>, lhs_ty: Ty<'tcx>, rhs_ty: Ty<'tcx>) -> Ty<'tcx> {
842 match self {
844 &BinOp::Add
845 | &BinOp::AddUnchecked
846 | &BinOp::Sub
847 | &BinOp::SubUnchecked
848 | &BinOp::Mul
849 | &BinOp::MulUnchecked
850 | &BinOp::Div
851 | &BinOp::Rem
852 | &BinOp::BitXor
853 | &BinOp::BitAnd
854 | &BinOp::BitOr => {
855 assert_eq!(lhs_ty, rhs_ty);
857 lhs_ty
858 }
859 &BinOp::AddWithOverflow | &BinOp::SubWithOverflow | &BinOp::MulWithOverflow => {
860 assert_eq!(lhs_ty, rhs_ty);
862 Ty::new_tup(tcx, &[lhs_ty, tcx.types.bool])
863 }
864 &BinOp::Shl
865 | &BinOp::ShlUnchecked
866 | &BinOp::Shr
867 | &BinOp::ShrUnchecked
868 | &BinOp::Offset => {
869 lhs_ty }
871 &BinOp::Eq | &BinOp::Lt | &BinOp::Le | &BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
872 tcx.types.bool
873 }
874 &BinOp::Cmp => {
875 assert_eq!(lhs_ty, rhs_ty);
877 tcx.ty_ordering_enum(DUMMY_SP)
878 }
879 }
880 }
881 pub(crate) fn to_hir_binop(self) -> hir::BinOpKind {
882 match self {
883 BinOp::Add | BinOp::AddWithOverflow => hir::BinOpKind::Add,
886 BinOp::Sub | BinOp::SubWithOverflow => hir::BinOpKind::Sub,
887 BinOp::Mul | BinOp::MulWithOverflow => hir::BinOpKind::Mul,
888 BinOp::Div => hir::BinOpKind::Div,
889 BinOp::Rem => hir::BinOpKind::Rem,
890 BinOp::BitXor => hir::BinOpKind::BitXor,
891 BinOp::BitAnd => hir::BinOpKind::BitAnd,
892 BinOp::BitOr => hir::BinOpKind::BitOr,
893 BinOp::Shl => hir::BinOpKind::Shl,
894 BinOp::Shr => hir::BinOpKind::Shr,
895 BinOp::Eq => hir::BinOpKind::Eq,
896 BinOp::Ne => hir::BinOpKind::Ne,
897 BinOp::Lt => hir::BinOpKind::Lt,
898 BinOp::Gt => hir::BinOpKind::Gt,
899 BinOp::Le => hir::BinOpKind::Le,
900 BinOp::Ge => hir::BinOpKind::Ge,
901 BinOp::Cmp
903 | BinOp::AddUnchecked
904 | BinOp::SubUnchecked
905 | BinOp::MulUnchecked
906 | BinOp::ShlUnchecked
907 | BinOp::ShrUnchecked
908 | BinOp::Offset => {
909 unreachable!()
910 }
911 }
912 }
913
914 pub fn overflowing_to_wrapping(self) -> Option<BinOp> {
916 Some(match self {
917 BinOp::AddWithOverflow => BinOp::Add,
918 BinOp::SubWithOverflow => BinOp::Sub,
919 BinOp::MulWithOverflow => BinOp::Mul,
920 _ => return None,
921 })
922 }
923
924 pub fn is_overflowing(self) -> bool {
926 self.overflowing_to_wrapping().is_some()
927 }
928
929 pub fn wrapping_to_overflowing(self) -> Option<BinOp> {
931 Some(match self {
932 BinOp::Add => BinOp::AddWithOverflow,
933 BinOp::Sub => BinOp::SubWithOverflow,
934 BinOp::Mul => BinOp::MulWithOverflow,
935 _ => return None,
936 })
937 }
938}
939
940impl From<Mutability> for RawPtrKind {
941 fn from(other: Mutability) -> Self {
942 match other {
943 Mutability::Mut => RawPtrKind::Mut,
944 Mutability::Not => RawPtrKind::Const,
945 }
946 }
947}
948
949impl RawPtrKind {
950 pub fn is_fake(self) -> bool {
951 match self {
952 RawPtrKind::Mut | RawPtrKind::Const => false,
953 RawPtrKind::FakeForPtrMetadata => true,
954 }
955 }
956
957 pub fn to_mutbl_lossy(self) -> Mutability {
958 match self {
959 RawPtrKind::Mut => Mutability::Mut,
960 RawPtrKind::Const => Mutability::Not,
961
962 RawPtrKind::FakeForPtrMetadata => Mutability::Not,
965 }
966 }
967
968 pub fn ptr_str(self) -> &'static str {
969 match self {
970 RawPtrKind::Mut => "mut",
971 RawPtrKind::Const => "const",
972 RawPtrKind::FakeForPtrMetadata => "const (fake)",
973 }
974 }
975}