rustc_const_eval/interpret/
visitor.rs

1//! Visitor for a run-time value with a given layout: Traverse enums, structs and other compound
2//! types until we arrive at the leaves, with custom handling for primitive types.
3
4use std::num::NonZero;
5
6use rustc_abi::{FieldIdx, FieldsShape, VariantIdx, Variants};
7use rustc_middle::mir::interpret::InterpResult;
8use rustc_middle::ty::{self, Ty};
9use tracing::trace;
10
11use super::{InterpCx, MPlaceTy, Machine, Projectable, interp_ok, throw_inval};
12
13/// How to traverse a value and what to do when we are at the leaves.
14pub trait ValueVisitor<'tcx, M: Machine<'tcx>>: Sized {
15    type V: Projectable<'tcx, M::Provenance> + From<MPlaceTy<'tcx, M::Provenance>>;
16
17    /// The visitor must have an `InterpCx` in it.
18    fn ecx(&self) -> &InterpCx<'tcx, M>;
19
20    /// `read_discriminant` can be hooked for better error messages.
21    #[inline(always)]
22    fn read_discriminant(&mut self, v: &Self::V) -> InterpResult<'tcx, VariantIdx> {
23        self.ecx().read_discriminant(&v.to_op(self.ecx())?)
24    }
25
26    // Recursive actions, ready to be overloaded.
27    /// Visits the given value, dispatching as appropriate to more specialized visitors.
28    #[inline(always)]
29    fn visit_value(&mut self, v: &Self::V) -> InterpResult<'tcx> {
30        self.walk_value(v)
31    }
32    /// Visits the given value as a union. No automatic recursion can happen here.
33    #[inline(always)]
34    fn visit_union(&mut self, _v: &Self::V, _fields: NonZero<usize>) -> InterpResult<'tcx> {
35        interp_ok(())
36    }
37    /// Visits the given value as the pointer of a `Box`. There is nothing to recurse into.
38    /// The type of `v` will be a raw pointer to `T`, but this is a field of `Box<T>` and the
39    /// pointee type is the actual `T`. `box_ty` provides the full type of the `Box` itself.
40    #[inline(always)]
41    fn visit_box(&mut self, _box_ty: Ty<'tcx>, _v: &Self::V) -> InterpResult<'tcx> {
42        interp_ok(())
43    }
44
45    /// Called each time we recurse down to a field of a "product-like" aggregate
46    /// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
47    /// and new (inner) value.
48    /// This gives the visitor the chance to track the stack of nested fields that
49    /// we are descending through.
50    #[inline(always)]
51    fn visit_field(
52        &mut self,
53        _old_val: &Self::V,
54        _field: usize,
55        new_val: &Self::V,
56    ) -> InterpResult<'tcx> {
57        self.visit_value(new_val)
58    }
59    /// Called when recursing into an enum variant.
60    /// This gives the visitor the chance to track the stack of nested fields that
61    /// we are descending through.
62    #[inline(always)]
63    fn visit_variant(
64        &mut self,
65        _old_val: &Self::V,
66        _variant: VariantIdx,
67        new_val: &Self::V,
68    ) -> InterpResult<'tcx> {
69        self.visit_value(new_val)
70    }
71
72    /// Traversal logic; should not be overloaded.
73    fn walk_value(&mut self, v: &Self::V) -> InterpResult<'tcx> {
74        let ty = v.layout().ty;
75        trace!("walk_value: type: {ty}");
76
77        // Special treatment for special types, where the (static) layout is not sufficient.
78        match *ty.kind() {
79            // If it is a trait object, switch to the real type that was used to create it.
80            ty::Dynamic(data, _) => {
81                // Dyn types. This is unsized, and the actual dynamic type of the data is given by the
82                // vtable stored in the place metadata.
83                // unsized values are never immediate, so we can assert_mem_place
84                let op = v.to_op(self.ecx())?;
85                let dest = op.assert_mem_place();
86                let inner_mplace = self.ecx().unpack_dyn_trait(&dest, data)?;
87                trace!("walk_value: dyn object layout: {:#?}", inner_mplace.layout);
88                // recurse with the inner type
89                return self.visit_field(v, 0, &inner_mplace.into());
90            }
91            // Slices do not need special handling here: they have `Array` field
92            // placement with length 0, so we enter the `Array` case below which
93            // indirectly uses the metadata to determine the actual length.
94
95            // However, `Box`... let's talk about `Box`.
96            ty::Adt(def, ..) if def.is_box() => {
97                // `Box` is a hybrid primitive-library-defined type that one the one hand is
98                // a dereferenceable pointer, on the other hand has *basically arbitrary
99                // user-defined layout* since the user controls the 'allocator' field. So it
100                // cannot be treated like a normal pointer, since it does not fit into an
101                // `Immediate`. Yeah, it is quite terrible. But many visitors want to do
102                // something with "all boxed pointers", so we handle this mess for them.
103                //
104                // When we hit a `Box`, we do not do the usual field recursion; instead,
105                // we (a) call `visit_box` on the pointer value, and (b) recurse on the
106                // allocator field. We also assert tons of things to ensure we do not miss
107                // any other fields.
108
109                // `Box` has two fields: the pointer we care about, and the allocator.
110                assert_eq!(v.layout().fields.count(), 2, "`Box` must have exactly 2 fields");
111                let [unique_ptr, alloc] =
112                    self.ecx().project_fields(v, [FieldIdx::ZERO, FieldIdx::ONE])?;
113
114                // Unfortunately there is some type junk in the way here: `unique_ptr` is a `Unique`...
115                // (which means another 2 fields, the second of which is a `PhantomData`)
116                assert_eq!(unique_ptr.layout().fields.count(), 2);
117                let [nonnull_ptr, phantom] =
118                    self.ecx().project_fields(&unique_ptr, [FieldIdx::ZERO, FieldIdx::ONE])?;
119                assert!(
120                    phantom.layout().ty.ty_adt_def().is_some_and(|adt| adt.is_phantom_data()),
121                    "2nd field of `Unique` should be PhantomData but is {:?}",
122                    phantom.layout().ty,
123                );
124
125                // ... that contains a `NonNull`... (gladly, only a single field here)
126                assert_eq!(nonnull_ptr.layout().fields.count(), 1);
127                let raw_ptr = self.ecx().project_field(&nonnull_ptr, FieldIdx::ZERO)?; // the actual raw ptr
128
129                // ... whose only field finally is a raw ptr we can dereference.
130                self.visit_box(ty, &raw_ptr)?;
131
132                // The second `Box` field is the allocator, which we recursively check for validity
133                // like in regular structs.
134                self.visit_field(v, 1, &alloc)?;
135
136                // We visited all parts of this one.
137                return interp_ok(());
138            }
139
140            // Non-normalized types should never show up here.
141            ty::Param(..)
142            | ty::Alias(..)
143            | ty::Bound(..)
144            | ty::Placeholder(..)
145            | ty::Infer(..)
146            | ty::Error(..) => throw_inval!(TooGeneric),
147
148            // The rest is handled below.
149            _ => {}
150        };
151
152        // Visit the fields of this value.
153        match &v.layout().fields {
154            FieldsShape::Primitive => {}
155            &FieldsShape::Union(fields) => {
156                self.visit_union(v, fields)?;
157            }
158            FieldsShape::Arbitrary { in_memory_order, .. } => {
159                for idx in in_memory_order.iter().copied() {
160                    let field = self.ecx().project_field(v, idx)?;
161                    self.visit_field(v, idx.as_usize(), &field)?;
162                }
163            }
164            FieldsShape::Array { .. } => {
165                let mut iter = self.ecx().project_array_fields(v)?;
166                while let Some((idx, field)) = iter.next(self.ecx())? {
167                    self.visit_field(v, idx.try_into().unwrap(), &field)?;
168                }
169            }
170        }
171
172        match v.layout().variants {
173            // If this is a multi-variant layout, find the right variant and proceed
174            // with *its* fields.
175            Variants::Multiple { .. } => {
176                let idx = self.read_discriminant(v)?;
177                // There are 3 cases where downcasts can turn a Scalar/ScalarPair into a different ABI which
178                // could be a problem for `ImmTy` (see layout_sanity_check):
179                // - variant.size == Size::ZERO: works fine because `ImmTy::offset` has a special case for
180                //   zero-sized layouts.
181                // - variant.fields.count() == 0: works fine because `ImmTy::offset` has a special case for
182                //   zero-field aggregates.
183                // - variant.abi.is_uninhabited(): triggers UB in `read_discriminant` so we never get here.
184                let inner = self.ecx().project_downcast(v, idx)?;
185                trace!("walk_value: variant layout: {:#?}", inner.layout());
186                // recurse with the inner type
187                self.visit_variant(v, idx, &inner)?;
188            }
189            // For single-variant layouts, we already did everything there is to do.
190            Variants::Single { .. } | Variants::Empty => {}
191        }
192
193        interp_ok(())
194    }
195}