rustc_middle/mir/
statement.rs

1//! Functionality for statements, operands, places, and things that appear in them.
2
3use tracing::{debug, instrument};
4
5use super::interpret::GlobalAlloc;
6use super::*;
7use crate::ty::CoroutineArgsExt;
8
9///////////////////////////////////////////////////////////////////////////
10// Statements
11
12/// A statement in a basic block, including information about its source code.
13#[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    /// Changes a statement to a nop. This is both faster than deleting instructions and avoids
22    /// invalidating statement indices in `Location`s.
23    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    /// Returns a simple string representation of a `StatementKind` variant, independent of any
34    /// values it might hold (e.g. `StatementKind::Assign` always returns `"Assign"`).
35    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///////////////////////////////////////////////////////////////////////////
69// Places
70
71#[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable)]
72pub struct PlaceTy<'tcx> {
73    pub ty: Ty<'tcx>,
74    /// Downcast to a particular variant of an enum or a coroutine, if included.
75    pub variant_index: Option<VariantIdx>,
76}
77
78// At least on 64 bit systems, `PlaceTy` should not be larger than two or three pointers.
79#[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    /// `place_ty.field_ty(tcx, f)` computes the type of a given field.
89    ///
90    /// Most clients of `PlaceTy` can instead just extract the relevant type
91    /// directly from their `PlaceElem`, but some instances of `ProjectionElem<V, T>`
92    /// do not carry a `Ty` for `T`.
93    ///
94    /// Note that the resulting type has not been normalized.
95    #[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                // Only prefix fields (upvars and current state) are
137                // accessible without a variant index.
138                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    /// Convenience wrapper around `projection_ty_core` for `PlaceElem`,
161    /// where we can just use the `Ty` that is already stored inline on
162    /// field projection elems.
163    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    /// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
168    /// projects `place_ty` onto `elem`, returning the appropriate
169    /// `Ty` or downcast variant corresponding to that projection.
170    /// The `handle_field` callback must map a `FieldIdx` to its `Ty`,
171    /// (which should be trivial when `T` = `Ty`).
172    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            // FIXME(unsafe_binders): Rename `handle_opaque_cast_and_subtype` to be more general.
224            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    /// Returns `true` if the target of this projection may refer to a different region of memory
235    /// than the base.
236    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    /// Returns `true` if the target of this projection always refers to the same memory region
252    /// whatever the state of the program.
253    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    /// Returns `true` if this is a `Downcast` projection with the given `VariantIdx`.
267    pub fn is_downcast_to(&self, v: VariantIdx) -> bool {
268        matches!(*self, Self::Downcast(_, x) if x == v)
269    }
270
271    /// Returns `true` if this is a `Field` projection with the given index.
272    pub fn is_field_to(&self, f: FieldIdx) -> bool {
273        matches!(*self, Self::Field(x, _) if x == f)
274    }
275
276    /// Returns `true` if this is accepted inside `VarDebugInfoContents::Place`.
277    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            // FIXME(unsafe_binders): Figure this out.
290            Self::UnwrapUnsafeBinder(..) => false,
291        }
292    }
293}
294
295/// Alias for projections as they appear in `UserTypeProjection`, where we
296/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
297pub 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
305// Once we stop implementing `Ord` for `DefId`,
306// this impl will be unnecessary. Until then, we'll
307// leave this impl in place to prevent re-adding a
308// dependency on the `Ord` impl for `DefId`
309impl<'tcx> !PartialOrd for PlaceRef<'tcx> {}
310
311impl<'tcx> Place<'tcx> {
312    // FIXME change this to a const fn by also making List::empty a const fn.
313    pub fn return_place() -> Place<'tcx> {
314        Place { local: RETURN_PLACE, projection: List::empty() }
315    }
316
317    /// Returns `true` if this `Place` contains a `Deref` projection.
318    ///
319    /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
320    /// same region of memory as its base.
321    pub fn is_indirect(&self) -> bool {
322        self.projection.iter().any(|elem| elem.is_indirect())
323    }
324
325    /// Returns `true` if this `Place`'s first projection is `Deref`.
326    ///
327    /// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,
328    /// `Deref` projections can only occur as the first projection. In that case this method
329    /// is equivalent to `is_indirect`, but faster.
330    pub fn is_indirect_first_projection(&self) -> bool {
331        self.as_ref().is_indirect_first_projection()
332    }
333
334    /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
335    /// a single deref of a local.
336    #[inline(always)]
337    pub fn local_or_deref_local(&self) -> Option<Local> {
338        self.as_ref().local_or_deref_local()
339    }
340
341    /// If this place represents a local variable like `_X` with no
342    /// projections, return `Some(_X)`.
343    #[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    /// Iterate over the projections in evaluation order, i.e., the first element is the base with
354    /// its projection and then subsequently more projections are added.
355    /// As a concrete example, given the place a.b.c, this would yield:
356    /// - (a, .b)
357    /// - (a.b, .c)
358    ///
359    /// Given a place without projections, the iterator is empty.
360    #[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    /// Generates a new place by appending `more_projections` to the existing ones
368    /// and interning the result.
369    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    /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
406    /// a single deref of a local.
407    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    /// Returns `true` if this `Place` contains a `Deref` projection.
416    ///
417    /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
418    /// same region of memory as its base.
419    pub fn is_indirect(&self) -> bool {
420        self.projection.iter().any(|elem| elem.is_indirect())
421    }
422
423    /// Returns `true` if this `Place`'s first projection is `Deref`.
424    ///
425    /// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,
426    /// `Deref` projections can only occur as the first projection. In that case this method
427    /// is equivalent to `is_indirect`, but faster.
428    pub fn is_indirect_first_projection(&self) -> bool {
429        // To make sure this is not accidentally used in wrong mir phase
430        debug_assert!(
431            self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref)
432        );
433        self.projection.first() == Some(&PlaceElem::Deref)
434    }
435
436    /// If this place represents a local variable like `_X` with no
437    /// projections, return `Some(_X)`.
438    #[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    /// Iterate over the projections in evaluation order, i.e., the first element is the base with
461    /// its projection and then subsequently more projections are added.
462    /// As a concrete example, given the place a.b.c, this would yield:
463    /// - (a, .b)
464    /// - (a.b, .c)
465    ///
466    /// Given a place without projections, the iterator is empty.
467    #[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    /// Generates a new place by appending `more_projections` to the existing ones
478    /// and interning the result.
479    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
513///////////////////////////////////////////////////////////////////////////
514// Operands
515
516impl<'tcx> Operand<'tcx> {
517    /// Convenience helper to make a constant that refers to the fn
518    /// with given `DefId` and args. Since this is used to synthesize
519    /// MIR, assumes `user_ty` is None.
520    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    /// Convenience helper to make a literal-like constant from a given scalar value.
539    /// Since this is used to synthesize MIR, assumes `user_ty` is None.
540    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    /// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
573    /// constant.
574    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    /// Returns the `ConstOperand` that is the target of this `Operand`, or `None` if this `Operand` is a
582    /// place.
583    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    /// Gets the `ty::FnDef` from an operand if it's a constant function item.
591    ///
592    /// While this is unlikely in general, it's the normal case of what you'll
593    /// find as the `func` in a [`TerminatorKind::Call`].
594    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
642///////////////////////////////////////////////////////////////////////////
643/// Rvalues
644
645pub enum RvalueInitializationState {
646    Shallow,
647    Deep,
648}
649
650impl<'tcx> Rvalue<'tcx> {
651    /// Returns true if rvalue can be safely removed when the result is unused.
652    #[inline]
653    pub fn is_safe_to_remove(&self) -> bool {
654        match self {
655            // Pointer to int casts may be side-effects due to exposing the provenance.
656            // While the model is undecided, we should be conservative. See
657            // <https://www.ralfj.de/blog/2022/04/11/provenance-exposed.html>
658            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    /// Returns `true` if this rvalue is deeply initialized (most rvalues) or
746    /// whether its only shallowly initialized (`Rvalue::Box`).
747    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    /// Returns whether borrows represented by this kind are allowed to be split into separate
764    /// Reservation and Activation phases.
765    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            // We have no type corresponding to a shallow borrow, so use
782            // `&` as an approximation.
783            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        // FIXME: handle SIMD correctly
809        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                // these should be integers or floats of the same size.
822                assert_eq!(lhs_ty, rhs_ty);
823                lhs_ty
824            }
825            &BinOp::AddWithOverflow | &BinOp::SubWithOverflow | &BinOp::MulWithOverflow => {
826                // these should be integers of the same size.
827                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 // lhs_ty can be != rhs_ty
836            }
837            &BinOp::Eq | &BinOp::Lt | &BinOp::Le | &BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
838                tcx.types.bool
839            }
840            &BinOp::Cmp => {
841                // these should be integer-like types of the same size.
842                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            // HIR `+`/`-`/`*` can map to either of these MIR BinOp, depending
850            // on whether overflow checks are enabled or not.
851            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            // We don't have HIR syntax for these.
868            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    /// If this is a `FooWithOverflow`, return `Some(Foo)`.
881    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    /// Returns whether this is a `FooWithOverflow`
891    pub fn is_overflowing(self) -> bool {
892        self.overflowing_to_wrapping().is_some()
893    }
894
895    /// If this is a `Foo`, return `Some(FooWithOverflow)`.
896    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            // We have no type corresponding to a fake borrow, so use
929            // `*const` as an approximation.
930            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}