Skip to main content

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    /// Visits the given type after it has been found to have no variants.
45    #[inline(always)]
46    fn visit_variantless(&mut self, _v: &Self::V) -> InterpResult<'tcx> {
47        interp_ok(())
48    }
49
50    /// Called each time we recurse down to a field of a "product-like" aggregate
51    /// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
52    /// and new (inner) value.
53    /// This gives the visitor the chance to track the stack of nested fields that
54    /// we are descending through.
55    #[inline(always)]
56    fn visit_field(
57        &mut self,
58        _old_val: &Self::V,
59        _field: usize,
60        new_val: &Self::V,
61    ) -> InterpResult<'tcx> {
62        self.visit_value(new_val)
63    }
64    /// Called when recursing into an enum variant.
65    /// This gives the visitor the chance to track the stack of nested fields that
66    /// we are descending through.
67    #[inline(always)]
68    fn visit_variant(
69        &mut self,
70        _old_val: &Self::V,
71        _variant: VariantIdx,
72        new_val: &Self::V,
73    ) -> InterpResult<'tcx> {
74        self.visit_value(new_val)
75    }
76
77    /// Traversal logic; should not be overloaded.
78    fn walk_value(&mut self, v: &Self::V) -> InterpResult<'tcx> {
79        let ty = v.layout().ty;
80        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/visitor.rs:80",
                        "rustc_const_eval::interpret::visitor",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/visitor.rs"),
                        ::tracing_core::__macro_support::Option::Some(80u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::visitor"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("walk_value: type: {0}",
                                                    ty) as &dyn Value))])
            });
    } else { ; }
};trace!("walk_value: type: {ty}");
81
82        // Special treatment for special types, where the (static) layout is not sufficient.
83        match *ty.kind() {
84            // If it is a trait object, switch to the real type that was used to create it.
85            ty::Dynamic(data, _) => {
86                // Dyn types. This is unsized, and the actual dynamic type of the data is given by the
87                // vtable stored in the place metadata.
88                // unsized values are never immediate, so we can assert_mem_place
89                let op = v.to_op(self.ecx())?;
90                let dest = op.assert_mem_place();
91                let inner_mplace = self.ecx().unpack_dyn_trait(&dest, data)?;
92                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/visitor.rs:92",
                        "rustc_const_eval::interpret::visitor",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/visitor.rs"),
                        ::tracing_core::__macro_support::Option::Some(92u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::visitor"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("walk_value: dyn object layout: {0:#?}",
                                                    inner_mplace.layout) as &dyn Value))])
            });
    } else { ; }
};trace!("walk_value: dyn object layout: {:#?}", inner_mplace.layout);
93                // recurse with the inner type
94                return self.visit_field(v, 0, &inner_mplace.into());
95            }
96            // Slices do not need special handling here: they have `Array` field
97            // placement with length 0, so we enter the `Array` case below which
98            // indirectly uses the metadata to determine the actual length.
99
100            // However, `Box`... let's talk about `Box`.
101            ty::Adt(def, ..) if def.is_box() => {
102                // `Box` is a hybrid primitive-library-defined type that one the one hand is
103                // a dereferenceable pointer, on the other hand has *basically arbitrary
104                // user-defined layout* since the user controls the 'allocator' field. So it
105                // cannot be treated like a normal pointer, since it does not fit into an
106                // `Immediate`. Yeah, it is quite terrible. But many visitors want to do
107                // something with "all boxed pointers", so we handle this mess for them.
108                //
109                // When we hit a `Box`, we do not do the usual field recursion; instead,
110                // we (a) call `visit_box` on the pointer value, and (b) recurse on the
111                // allocator field. We also assert tons of things to ensure we do not miss
112                // any other fields.
113
114                // `Box` has two fields: the pointer we care about, and the allocator.
115                match (&v.layout().fields.count(), &2) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::Some(format_args!("`Box` must have exactly 2 fields")));
        }
    }
};assert_eq!(v.layout().fields.count(), 2, "`Box` must have exactly 2 fields");
116                let [unique_ptr, alloc] =
117                    self.ecx().project_fields(v, [FieldIdx::ZERO, FieldIdx::ONE])?;
118
119                // Unfortunately there is some type junk in the way here: `unique_ptr` is a `Unique`...
120                // (which means another 2 fields, the second of which is a `PhantomData`)
121                match (&unique_ptr.layout().fields.count(), &2) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(unique_ptr.layout().fields.count(), 2);
122                let [nonnull_ptr, phantom] =
123                    self.ecx().project_fields(&unique_ptr, [FieldIdx::ZERO, FieldIdx::ONE])?;
124                if !phantom.layout().ty.ty_adt_def().is_some_and(|adt| adt.is_phantom_data())
    {
    {
        ::core::panicking::panic_fmt(format_args!("2nd field of `Unique` should be PhantomData but is {0:?}",
                phantom.layout().ty));
    }
};assert!(
125                    phantom.layout().ty.ty_adt_def().is_some_and(|adt| adt.is_phantom_data()),
126                    "2nd field of `Unique` should be PhantomData but is {:?}",
127                    phantom.layout().ty,
128                );
129
130                // ... that contains a `NonNull` whose only field finally is a raw ptr we can
131                // dereference.
132                match (&nonnull_ptr.layout().fields.count(), &1) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(nonnull_ptr.layout().fields.count(), 1);
133                let pat_ptr = self.ecx().project_field(&nonnull_ptr, FieldIdx::ZERO)?; // `*mut T is !null`
134                let base = match *pat_ptr.layout().ty.kind() {
135                    ty::Pat(base, _) => self.ecx().layout_of(base)?,
136                    _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
137                };
138                let raw_ptr = pat_ptr.transmute(base, self.ecx())?; // The actual raw pointer
139
140                // Hand this actual pointer to the visitor.
141                self.visit_box(ty, &raw_ptr)?;
142
143                // The second `Box` field is the allocator, which we recursively check for validity
144                // like in regular structs.
145                self.visit_field(v, 1, &alloc)?;
146
147                // We visited all parts of this one.
148                return interp_ok(());
149            }
150
151            // Non-normalized types should never show up here.
152            ty::Param(..)
153            | ty::Alias(..)
154            | ty::Bound(..)
155            | ty::Placeholder(..)
156            | ty::Infer(..)
157            | ty::Error(..) => do yeet ::rustc_middle::mir::interpret::InterpErrorKind::InvalidProgram(::rustc_middle::mir::interpret::InvalidProgramInfo::TooGeneric)throw_inval!(TooGeneric),
158
159            // The rest is handled below.
160            _ => {}
161        };
162
163        // Visit the fields of this value.
164        match &v.layout().fields {
165            FieldsShape::Primitive => {}
166            &FieldsShape::Union(fields) => {
167                self.visit_union(v, fields)?;
168            }
169            FieldsShape::Arbitrary { in_memory_order, .. } => {
170                for idx in in_memory_order.iter().copied() {
171                    let field = self.ecx().project_field(v, idx)?;
172                    self.visit_field(v, idx.as_usize(), &field)?;
173                }
174            }
175            FieldsShape::Array { .. } => {
176                let mut iter = self.ecx().project_array_fields(v)?;
177                while let Some((idx, field)) = iter.next(self.ecx())? {
178                    self.visit_field(v, idx.try_into().unwrap(), &field)?;
179                }
180            }
181        }
182
183        match v.layout().variants {
184            // If this is a multi-variant layout, find the right variant and proceed
185            // with *its* fields.
186            Variants::Multiple { .. } => {
187                let idx = self.read_discriminant(v)?;
188                // There are 3 cases where downcasts can turn a Scalar/ScalarPair into a different ABI which
189                // could be a problem for `ImmTy` (see layout_sanity_check):
190                // - variant.size == Size::ZERO: works fine because `ImmTy::offset` has a special case for
191                //   zero-sized layouts.
192                // - variant.fields.count() == 0: works fine because `ImmTy::offset` has a special case for
193                //   zero-field aggregates.
194                // - variant.abi.is_uninhabited(): triggers UB in `read_discriminant` so we never get here.
195                let inner = self.ecx().project_downcast(v, idx)?;
196                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_const_eval/src/interpret/visitor.rs:196",
                        "rustc_const_eval::interpret::visitor",
                        ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_const_eval/src/interpret/visitor.rs"),
                        ::tracing_core::__macro_support::Option::Some(196u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_const_eval::interpret::visitor"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("walk_value: variant layout: {0:#?}",
                                                    inner.layout()) as &dyn Value))])
            });
    } else { ; }
};trace!("walk_value: variant layout: {:#?}", inner.layout());
197                // recurse with the inner type
198                self.visit_variant(v, idx, &inner)?;
199            }
200            // For single-variant layouts, we already did everything there is to do.
201            Variants::Single { .. } => {}
202            // Non-variant layouts need special treatment by the visitor.
203            Variants::Empty => {
204                self.visit_variantless(v)?;
205            }
206        }
207
208        interp_ok(())
209    }
210}