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