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<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    /// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
172    /// projects `place_ty` onto `elem`, returning the appropriate
173    /// `Ty` or downcast variant corresponding to that projection.
174    /// The `handle_field` callback must map a `FieldIdx` to its `Ty`,
175    /// (which should be trivial when `T` = `Ty`).
176    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            // FIXME(unsafe_binders): Rename `handle_opaque_cast_and_subtype` to be more general.
228            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    /// Returns `true` if the target of this projection may refer to a different region of memory
239    /// than the base.
240    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    /// Returns `true` if the target of this projection always refers to the same memory region
256    /// whatever the state of the program.
257    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    /// Returns `true` if this is a `Downcast` projection with the given `VariantIdx`.
271    pub fn is_downcast_to(&self, v: VariantIdx) -> bool {
272        matches!(*self, Self::Downcast(_, x) if x == v)
273    }
274
275    /// Returns `true` if this is a `Field` projection with the given index.
276    pub fn is_field_to(&self, f: FieldIdx) -> bool {
277        matches!(*self, Self::Field(x, _) if x == f)
278    }
279
280    /// Returns `true` if this is accepted inside `VarDebugInfoContents::Place`.
281    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            // FIXME(unsafe_binders): Figure this out.
294            Self::UnwrapUnsafeBinder(..) => false,
295        }
296    }
297
298    /// Returns the `ProjectionKind` associated to this projection.
299    pub fn kind(self) -> ProjectionKind {
300        self.try_map(|_| Some(()), |_| ()).unwrap()
301    }
302
303    /// Apply functions to types and values in this projection and return the result.
304    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
329/// Alias for projections as they appear in `UserTypeProjection`, where we
330/// need neither the `V` parameter for `Index` nor the `T` for `Field`.
331pub 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
339// Once we stop implementing `Ord` for `DefId`,
340// this impl will be unnecessary. Until then, we'll
341// leave this impl in place to prevent re-adding a
342// dependency on the `Ord` impl for `DefId`
343impl<'tcx> !PartialOrd for PlaceRef<'tcx> {}
344
345impl<'tcx> Place<'tcx> {
346    // FIXME change this to a const fn by also making List::empty a const fn.
347    pub fn return_place() -> Place<'tcx> {
348        Place { local: RETURN_PLACE, projection: List::empty() }
349    }
350
351    /// Returns `true` if this `Place` contains a `Deref` projection.
352    ///
353    /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
354    /// same region of memory as its base.
355    pub fn is_indirect(&self) -> bool {
356        self.projection.iter().any(|elem| elem.is_indirect())
357    }
358
359    /// Returns `true` if this `Place`'s first projection is `Deref`.
360    ///
361    /// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,
362    /// `Deref` projections can only occur as the first projection. In that case this method
363    /// is equivalent to `is_indirect`, but faster.
364    pub fn is_indirect_first_projection(&self) -> bool {
365        self.as_ref().is_indirect_first_projection()
366    }
367
368    /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
369    /// a single deref of a local.
370    #[inline(always)]
371    pub fn local_or_deref_local(&self) -> Option<Local> {
372        self.as_ref().local_or_deref_local()
373    }
374
375    /// If this place represents a local variable like `_X` with no
376    /// projections, return `Some(_X)`.
377    #[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    /// Iterate over the projections in evaluation order, i.e., the first element is the base with
388    /// its projection and then subsequently more projections are added.
389    /// As a concrete example, given the place a.b.c, this would yield:
390    /// - (a, .b)
391    /// - (a.b, .c)
392    ///
393    /// Given a place without projections, the iterator is empty.
394    #[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    /// Generates a new place by appending `more_projections` to the existing ones
402    /// and interning the result.
403    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    /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
440    /// a single deref of a local.
441    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    /// Returns `true` if this `Place` contains a `Deref` projection.
450    ///
451    /// If `Place::is_indirect` returns false, the caller knows that the `Place` refers to the
452    /// same region of memory as its base.
453    pub fn is_indirect(&self) -> bool {
454        self.projection.iter().any(|elem| elem.is_indirect())
455    }
456
457    /// Returns `true` if this `Place`'s first projection is `Deref`.
458    ///
459    /// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later,
460    /// `Deref` projections can only occur as the first projection. In that case this method
461    /// is equivalent to `is_indirect`, but faster.
462    pub fn is_indirect_first_projection(&self) -> bool {
463        // To make sure this is not accidentally used in wrong mir phase
464        debug_assert!(
465            self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref)
466        );
467        self.projection.first() == Some(&PlaceElem::Deref)
468    }
469
470    /// If this place represents a local variable like `_X` with no
471    /// projections, return `Some(_X)`.
472    #[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    /// Iterate over the projections in evaluation order, i.e., the first element is the base with
495    /// its projection and then subsequently more projections are added.
496    /// As a concrete example, given the place a.b.c, this would yield:
497    /// - (a, .b)
498    /// - (a.b, .c)
499    ///
500    /// Given a place without projections, the iterator is empty.
501    #[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    /// Generates a new place by appending `more_projections` to the existing ones
512    /// and interning the result.
513    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
547///////////////////////////////////////////////////////////////////////////
548// Operands
549
550impl<'tcx> Operand<'tcx> {
551    /// Convenience helper to make a constant that refers to the fn
552    /// with given `DefId` and args. Since this is used to synthesize
553    /// MIR, assumes `user_ty` is None.
554    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    /// Convenience helper to make a literal-like constant from a given scalar value.
573    /// Since this is used to synthesize MIR, assumes `user_ty` is None.
574    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    /// Returns the `Place` that is the target of this `Operand`, or `None` if this `Operand` is a
607    /// constant.
608    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    /// Returns the `ConstOperand` that is the target of this `Operand`, or `None` if this `Operand` is a
616    /// place.
617    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    /// Gets the `ty::FnDef` from an operand if it's a constant function item.
625    ///
626    /// While this is unlikely in general, it's the normal case of what you'll
627    /// find as the `func` in a [`TerminatorKind::Call`].
628    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
676///////////////////////////////////////////////////////////////////////////
677/// Rvalues
678
679pub enum RvalueInitializationState {
680    Shallow,
681    Deep,
682}
683
684impl<'tcx> Rvalue<'tcx> {
685    /// Returns true if rvalue can be safely removed when the result is unused.
686    #[inline]
687    pub fn is_safe_to_remove(&self) -> bool {
688        match self {
689            // Pointer to int casts may be side-effects due to exposing the provenance.
690            // While the model is undecided, we should be conservative. See
691            // <https://www.ralfj.de/blog/2022/04/11/provenance-exposed.html>
692            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    /// Returns `true` if this rvalue is deeply initialized (most rvalues) or
780    /// whether its only shallowly initialized (`Rvalue::Box`).
781    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    /// Returns whether borrows represented by this kind are allowed to be split into separate
798    /// Reservation and Activation phases.
799    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            // We have no type corresponding to a shallow borrow, so use
816            // `&` as an approximation.
817            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        // FIXME: handle SIMD correctly
843        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                // these should be integers or floats of the same size.
856                assert_eq!(lhs_ty, rhs_ty);
857                lhs_ty
858            }
859            &BinOp::AddWithOverflow | &BinOp::SubWithOverflow | &BinOp::MulWithOverflow => {
860                // these should be integers of the same size.
861                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 // lhs_ty can be != rhs_ty
870            }
871            &BinOp::Eq | &BinOp::Lt | &BinOp::Le | &BinOp::Ne | &BinOp::Ge | &BinOp::Gt => {
872                tcx.types.bool
873            }
874            &BinOp::Cmp => {
875                // these should be integer-like types of the same size.
876                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            // HIR `+`/`-`/`*` can map to either of these MIR BinOp, depending
884            // on whether overflow checks are enabled or not.
885            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            // We don't have HIR syntax for these.
902            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    /// If this is a `FooWithOverflow`, return `Some(Foo)`.
915    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    /// Returns whether this is a `FooWithOverflow`
925    pub fn is_overflowing(self) -> bool {
926        self.overflowing_to_wrapping().is_some()
927    }
928
929    /// If this is a `Foo`, return `Some(FooWithOverflow)`.
930    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            // We have no type corresponding to a fake borrow, so use
963            // `*const` as an approximation.
964            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}