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.
4 use rustc_middle
::mir
::interpret
::InterpResult
;
6 use rustc_middle
::ty
::layout
::TyAndLayout
;
7 use rustc_target
::abi
::{FieldsShape, VariantIdx, Variants}
;
9 use std
::num
::NonZeroUsize
;
11 use super::{InterpCx, MPlaceTy, Machine, OpTy, PlaceTy}
;
13 /// A thing that we can project into, and that has a layout.
14 /// This wouldn't have to depend on `Machine` but with the current type inference,
15 /// that's just more convenient to work with (avoids repeating all the `Machine` bounds).
16 pub trait Value
<'mir
, 'tcx
, M
: Machine
<'mir
, 'tcx
>>: Sized
{
17 /// Gets this value's layout.
18 fn layout(&self) -> TyAndLayout
<'tcx
>;
20 /// Makes this into an `OpTy`, in a cheap way that is good for reading.
23 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
24 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>>;
26 /// Makes this into an `OpTy`, in a potentially more expensive way that is good for projections.
29 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
30 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
31 self.to_op_for_read(ecx
)
34 /// Creates this from an `OpTy`.
36 /// If `to_op_for_proj` only ever produces `Indirect` operands, then this one is definitely `Indirect`.
37 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self;
39 /// Projects to the given enum variant.
42 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
44 ) -> InterpResult
<'tcx
, Self>;
46 /// Projects to the n-th field.
49 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
51 ) -> InterpResult
<'tcx
, Self>;
54 /// A thing that we can project into given *mutable* access to `ecx`, and that has a layout.
55 /// This wouldn't have to depend on `Machine` but with the current type inference,
56 /// that's just more convenient to work with (avoids repeating all the `Machine` bounds).
57 pub trait ValueMut
<'mir
, 'tcx
, M
: Machine
<'mir
, 'tcx
>>: Sized
{
58 /// Gets this value's layout.
59 fn layout(&self) -> TyAndLayout
<'tcx
>;
61 /// Makes this into an `OpTy`, in a cheap way that is good for reading.
64 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
65 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>>;
67 /// Makes this into an `OpTy`, in a potentially more expensive way that is good for projections.
70 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
71 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>>;
73 /// Creates this from an `OpTy`.
75 /// If `to_op_for_proj` only ever produces `Indirect` operands, then this one is definitely `Indirect`.
76 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self;
78 /// Projects to the given enum variant.
81 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
83 ) -> InterpResult
<'tcx
, Self>;
85 /// Projects to the n-th field.
88 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
90 ) -> InterpResult
<'tcx
, Self>;
93 // We cannot have a general impl which shows that Value implies ValueMut. (When we do, it says we
94 // cannot `impl ValueMut for PlaceTy` because some downstream crate could `impl Value for PlaceTy`.)
95 // So we have some copy-paste here. (We could have a macro but since we only have 2 types with this
96 // double-impl, that would barely make the code shorter, if at all.)
98 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> Value
<'mir
, 'tcx
, M
> for OpTy
<'tcx
, M
::Provenance
> {
100 fn layout(&self) -> TyAndLayout
<'tcx
> {
107 _ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
108 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
113 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self {
120 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
122 ) -> InterpResult
<'tcx
, Self> {
123 ecx
.operand_downcast(self, variant
)
129 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
131 ) -> InterpResult
<'tcx
, Self> {
132 ecx
.operand_field(self, field
)
136 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> ValueMut
<'mir
, 'tcx
, M
>
137 for OpTy
<'tcx
, M
::Provenance
>
140 fn layout(&self) -> TyAndLayout
<'tcx
> {
147 _ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
148 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
155 _ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
156 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
161 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self {
168 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
170 ) -> InterpResult
<'tcx
, Self> {
171 ecx
.operand_downcast(self, variant
)
177 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
179 ) -> InterpResult
<'tcx
, Self> {
180 ecx
.operand_field(self, field
)
184 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> Value
<'mir
, 'tcx
, M
>
185 for MPlaceTy
<'tcx
, M
::Provenance
>
188 fn layout(&self) -> TyAndLayout
<'tcx
> {
195 _ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
196 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
201 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self {
202 // assert is justified because our `to_op_for_read` only ever produces `Indirect` operands.
203 op
.assert_mem_place()
209 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
211 ) -> InterpResult
<'tcx
, Self> {
212 ecx
.mplace_downcast(self, variant
)
218 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
220 ) -> InterpResult
<'tcx
, Self> {
221 ecx
.mplace_field(self, field
)
225 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> ValueMut
<'mir
, 'tcx
, M
>
226 for MPlaceTy
<'tcx
, M
::Provenance
>
229 fn layout(&self) -> TyAndLayout
<'tcx
> {
236 _ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
237 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
244 _ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
245 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
250 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self {
251 // assert is justified because our `to_op_for_proj` only ever produces `Indirect` operands.
252 op
.assert_mem_place()
258 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
260 ) -> InterpResult
<'tcx
, Self> {
261 ecx
.mplace_downcast(self, variant
)
267 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
269 ) -> InterpResult
<'tcx
, Self> {
270 ecx
.mplace_field(self, field
)
274 impl<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>> ValueMut
<'mir
, 'tcx
, M
>
275 for PlaceTy
<'tcx
, M
::Provenance
>
278 fn layout(&self) -> TyAndLayout
<'tcx
> {
285 ecx
: &InterpCx
<'mir
, 'tcx
, M
>,
286 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
287 // We `force_allocation` here so that `from_op` below can work.
288 ecx
.place_to_op(self)
294 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
295 ) -> InterpResult
<'tcx
, OpTy
<'tcx
, M
::Provenance
>> {
296 // We `force_allocation` here so that `from_op` below can work.
297 Ok(ecx
.force_allocation(self)?
.into())
301 fn from_op(op
: &OpTy
<'tcx
, M
::Provenance
>) -> Self {
302 // assert is justified because our `to_op` only ever produces `Indirect` operands.
303 op
.assert_mem_place().into()
309 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
311 ) -> InterpResult
<'tcx
, Self> {
312 ecx
.place_downcast(self, variant
)
318 ecx
: &mut InterpCx
<'mir
, 'tcx
, M
>,
320 ) -> InterpResult
<'tcx
, Self> {
321 ecx
.place_field(self, field
)
325 macro_rules
! make_value_visitor
{
326 ($visitor_trait
:ident
, $value_trait
:ident
, $
($mutability
:ident
)?
) => {
327 /// How to traverse a value and what to do when we are at the leaves.
328 pub trait $visitor_trait
<'mir
, 'tcx
: 'mir
, M
: Machine
<'mir
, 'tcx
>>: Sized
{
329 type V
: $value_trait
<'mir
, 'tcx
, M
>;
331 /// The visitor must have an `InterpCx` in it.
332 fn ecx(&$
($mutability
)?
self)
333 -> &$
($mutability
)? InterpCx
<'mir
, 'tcx
, M
>;
335 /// `read_discriminant` can be hooked for better error messages.
337 fn read_discriminant(
339 op
: &OpTy
<'tcx
, M
::Provenance
>,
340 ) -> InterpResult
<'tcx
, VariantIdx
> {
341 Ok(self.ecx().read_discriminant(op
)?
.1)
344 // Recursive actions, ready to be overloaded.
345 /// Visits the given value, dispatching as appropriate to more specialized visitors.
347 fn visit_value(&mut self, v
: &Self::V
) -> InterpResult
<'tcx
>
351 /// Visits the given value as a union. No automatic recursion can happen here.
353 fn visit_union(&mut self, _v
: &Self::V
, _fields
: NonZeroUsize
) -> InterpResult
<'tcx
>
357 /// Visits the given value as the pointer of a `Box`. There is nothing to recurse into.
358 /// The type of `v` will be a raw pointer, but this is a field of `Box<T>` and the
359 /// pointee type is the actual `T`.
361 fn visit_box(&mut self, _v
: &Self::V
) -> InterpResult
<'tcx
>
365 /// Visits this value as an aggregate, you are getting an iterator yielding
366 /// all the fields (still in an `InterpResult`, you have to do error handling yourself).
367 /// Recurses into the fields.
372 fields
: impl Iterator
<Item
=InterpResult
<'tcx
, Self::V
>>,
373 ) -> InterpResult
<'tcx
> {
374 self.walk_aggregate(v
, fields
)
377 /// Called each time we recurse down to a field of a "product-like" aggregate
378 /// (structs, tuples, arrays and the like, but not enums), passing in old (outer)
379 /// and new (inner) value.
380 /// This gives the visitor the chance to track the stack of nested fields that
381 /// we are descending through.
388 ) -> InterpResult
<'tcx
> {
389 self.visit_value(new_val
)
391 /// Called when recursing into an enum variant.
392 /// This gives the visitor the chance to track the stack of nested fields that
393 /// we are descending through.
398 _variant
: VariantIdx
,
400 ) -> InterpResult
<'tcx
> {
401 self.visit_value(new_val
)
404 // Default recursors. Not meant to be overloaded.
408 fields
: impl Iterator
<Item
=InterpResult
<'tcx
, Self::V
>>,
409 ) -> InterpResult
<'tcx
> {
410 // Now iterate over it.
411 for (idx
, field_val
) in fields
.enumerate() {
412 self.visit_field(v
, idx
, &field_val?
)?
;
416 fn walk_value(&mut self, v
: &Self::V
) -> InterpResult
<'tcx
>
418 let ty
= v
.layout().ty
;
419 trace
!("walk_value: type: {ty}");
421 // Special treatment for special types, where the (static) layout is not sufficient.
423 // If it is a trait object, switch to the real type that was used to create it.
425 // unsized values are never immediate, so we can assert_mem_place
426 let op
= v
.to_op_for_read(self.ecx())?
;
427 let dest
= op
.assert_mem_place();
428 let inner_mplace
= self.ecx().unpack_dyn_trait(&dest
)?
;
429 trace
!("walk_value: dyn object layout: {:#?}", inner_mplace
.layout
);
430 // recurse with the inner type
431 return self.visit_field(&v
, 0, &$value_trait
::from_op(&inner_mplace
.into()));
433 // Slices do not need special handling here: they have `Array` field
434 // placement with length 0, so we enter the `Array` case below which
435 // indirectly uses the metadata to determine the actual length.
437 // However, `Box`... let's talk about `Box`.
438 ty
::Adt(def
, ..) if def
.is_box() => {
439 // `Box` is a hybrid primitive-library-defined type that one the one hand is
440 // a dereferenceable pointer, on the other hand has *basically arbitrary
441 // user-defined layout* since the user controls the 'allocator' field. So it
442 // cannot be treated like a normal pointer, since it does not fit into an
443 // `Immediate`. Yeah, it is quite terrible. But many visitors want to do
444 // something with "all boxed pointers", so we handle this mess for them.
446 // When we hit a `Box`, we do not do the usual `visit_aggregate`; instead,
447 // we (a) call `visit_box` on the pointer value, and (b) recurse on the
448 // allocator field. We also assert tons of things to ensure we do not miss
451 // `Box` has two fields: the pointer we care about, and the allocator.
452 assert_eq
!(v
.layout().fields
.count(), 2, "`Box` must have exactly 2 fields");
453 let (unique_ptr
, alloc
) =
454 (v
.project_field(self.ecx(), 0)?
, v
.project_field(self.ecx(), 1)?
);
455 // Unfortunately there is some type junk in the way here: `unique_ptr` is a `Unique`...
456 // (which means another 2 fields, the second of which is a `PhantomData`)
457 assert_eq
!(unique_ptr
.layout().fields
.count(), 2);
458 let (nonnull_ptr
, phantom
) = (
459 unique_ptr
.project_field(self.ecx(), 0)?
,
460 unique_ptr
.project_field(self.ecx(), 1)?
,
463 phantom
.layout().ty
.ty_adt_def().is_some_and(|adt
| adt
.is_phantom_data()),
464 "2nd field of `Unique` should be PhantomData but is {:?}",
467 // ... that contains a `NonNull`... (gladly, only a single field here)
468 assert_eq
!(nonnull_ptr
.layout().fields
.count(), 1);
469 let raw_ptr
= nonnull_ptr
.project_field(self.ecx(), 0)?
; // the actual raw ptr
470 // ... whose only field finally is a raw ptr we can dereference.
471 self.visit_box(&raw_ptr
)?
;
473 // The second `Box` field is the allocator, which we recursively check for validity
474 // like in regular structs.
475 self.visit_field(v
, 1, &alloc
)?
;
477 // We visited all parts of this one.
483 // Visit the fields of this value.
484 match &v
.layout().fields
{
485 FieldsShape
::Primitive
=> {}
486 &FieldsShape
::Union(fields
) => {
487 self.visit_union(v
, fields
)?
;
489 FieldsShape
::Arbitrary { offsets, .. }
=> {
490 // FIXME: We collect in a vec because otherwise there are lifetime
491 // errors: Projecting to a field needs access to `ecx`.
492 let fields
: Vec
<InterpResult
<'tcx
, Self::V
>> =
493 (0..offsets
.len()).map(|i
| {
494 v
.project_field(self.ecx(), i
)
497 self.visit_aggregate(v
, fields
.into_iter())?
;
499 FieldsShape
::Array { .. }
=> {
500 // Let's get an mplace (or immediate) first.
501 // This might `force_allocate` if `v` is a `PlaceTy`, but `place_index` does that anyway.
502 let op
= v
.to_op_for_proj(self.ecx())?
;
503 // Now we can go over all the fields.
504 // This uses the *run-time length*, i.e., if we are a slice,
505 // the dynamic info from the metadata is used.
506 let iter
= self.ecx().operand_array_fields(&op
)?
507 .map(|f
| f
.and_then(|f
| {
508 Ok($value_trait
::from_op(&f
))
510 self.visit_aggregate(v
, iter
)?
;
514 match v
.layout().variants
{
515 // If this is a multi-variant layout, find the right variant and proceed
516 // with *its* fields.
517 Variants
::Multiple { .. }
=> {
518 let op
= v
.to_op_for_read(self.ecx())?
;
519 let idx
= self.read_discriminant(&op
)?
;
520 let inner
= v
.project_downcast(self.ecx(), idx
)?
;
521 trace
!("walk_value: variant layout: {:#?}", inner
.layout());
522 // recurse with the inner type
523 self.visit_variant(v
, idx
, &inner
)
525 // For single-variant layouts, we already did anything there is to do.
526 Variants
::Single { .. }
=> Ok(())
533 make_value_visitor
!(ValueVisitor
, Value
,);
534 make_value_visitor
!(MutValueVisitor
, ValueMut
, mut);