rustc_const_eval/interpret/
projection.rs

1//! This file implements "place projections"; basically a symmetric API for 3 types: MPlaceTy, OpTy, PlaceTy.
2//!
3//! OpTy and PlaceTy generally work by "let's see if we are actually an MPlaceTy, and do something custom if not".
4//! For PlaceTy, the custom thing is basically always to call `force_allocation` and then use the MPlaceTy logic anyway.
5//! For OpTy, the custom thing on field pojections has to be pretty clever (since `Operand::Immediate` can have fields),
6//! but for array/slice operations it only has to worry about `Operand::Uninit`. That makes the value part trivial,
7//! but we still need to do bounds checking and adjust the layout. To not duplicate that with MPlaceTy, we actually
8//! implement the logic on OpTy, and MPlaceTy calls that.
9
10use std::marker::PhantomData;
11use std::ops::Range;
12
13use rustc_abi::{self as abi, Size, VariantIdx};
14use rustc_middle::ty::Ty;
15use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
16use rustc_middle::{bug, mir, span_bug, ty};
17use tracing::{debug, instrument};
18
19use super::{
20    InterpCx, InterpResult, MPlaceTy, Machine, MemPlaceMeta, OpTy, Provenance, Scalar, err_ub,
21    interp_ok, throw_ub, throw_unsup,
22};
23
24/// Describes the constraints placed on offset-projections.
25#[derive(Copy, Clone, Debug)]
26pub enum OffsetMode {
27    /// The offset has to be inbounds, like `ptr::offset`.
28    Inbounds,
29    /// No constraints, just wrap around the edge of the address space.
30    Wrapping,
31}
32
33/// A thing that we can project into, and that has a layout.
34pub trait Projectable<'tcx, Prov: Provenance>: Sized + std::fmt::Debug {
35    /// Get the layout.
36    fn layout(&self) -> TyAndLayout<'tcx>;
37
38    /// Get the metadata of a wide value.
39    fn meta(&self) -> MemPlaceMeta<Prov>;
40
41    /// Get the length of a slice/string/array stored here.
42    fn len<M: Machine<'tcx, Provenance = Prov>>(
43        &self,
44        ecx: &InterpCx<'tcx, M>,
45    ) -> InterpResult<'tcx, u64> {
46        let layout = self.layout();
47        if layout.is_unsized() {
48            // We need to consult `meta` metadata
49            match layout.ty.kind() {
50                ty::Slice(..) | ty::Str => self.meta().unwrap_meta().to_target_usize(ecx),
51                _ => bug!("len not supported on unsized type {:?}", layout.ty),
52            }
53        } else {
54            // Go through the layout. There are lots of types that support a length,
55            // e.g., SIMD types. (But not all repr(simd) types even have FieldsShape::Array!)
56            match layout.fields {
57                abi::FieldsShape::Array { count, .. } => interp_ok(count),
58                _ => bug!("len not supported on sized type {:?}", layout.ty),
59            }
60        }
61    }
62
63    /// Offset the value by the given amount, replacing the layout and metadata.
64    fn offset_with_meta<M: Machine<'tcx, Provenance = Prov>>(
65        &self,
66        offset: Size,
67        mode: OffsetMode,
68        meta: MemPlaceMeta<Prov>,
69        layout: TyAndLayout<'tcx>,
70        ecx: &InterpCx<'tcx, M>,
71    ) -> InterpResult<'tcx, Self>;
72
73    fn offset<M: Machine<'tcx, Provenance = Prov>>(
74        &self,
75        offset: Size,
76        layout: TyAndLayout<'tcx>,
77        ecx: &InterpCx<'tcx, M>,
78    ) -> InterpResult<'tcx, Self> {
79        assert!(layout.is_sized());
80        // We sometimes do pointer arithmetic with this function, disregarding the source type.
81        // So we don't check the sizes here.
82        self.offset_with_meta(offset, OffsetMode::Inbounds, MemPlaceMeta::None, layout, ecx)
83    }
84
85    /// This does an offset-by-zero, which is effectively a transmute. Note however that
86    /// not all transmutes are supported by all projectables -- specifically, if this is an
87    /// `OpTy` or `ImmTy`, the new layout must have almost the same ABI as the old one
88    /// (only changing the `valid_range` is allowed and turning integers into pointers).
89    fn transmute<M: Machine<'tcx, Provenance = Prov>>(
90        &self,
91        layout: TyAndLayout<'tcx>,
92        ecx: &InterpCx<'tcx, M>,
93    ) -> InterpResult<'tcx, Self> {
94        assert!(self.layout().is_sized() && layout.is_sized());
95        assert_eq!(self.layout().size, layout.size);
96        self.offset_with_meta(Size::ZERO, OffsetMode::Wrapping, MemPlaceMeta::None, layout, ecx)
97    }
98
99    /// Convert this to an `OpTy`. This might be an irreversible transformation, but is useful for
100    /// reading from this thing.
101    fn to_op<M: Machine<'tcx, Provenance = Prov>>(
102        &self,
103        ecx: &InterpCx<'tcx, M>,
104    ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>>;
105}
106
107/// A type representing iteration over the elements of an array.
108pub struct ArrayIterator<'a, 'tcx, Prov: Provenance, P: Projectable<'tcx, Prov>> {
109    base: &'a P,
110    range: Range<u64>,
111    stride: Size,
112    field_layout: TyAndLayout<'tcx>,
113    _phantom: PhantomData<Prov>, // otherwise it says `Prov` is never used...
114}
115
116impl<'a, 'tcx, Prov: Provenance, P: Projectable<'tcx, Prov>> ArrayIterator<'a, 'tcx, Prov, P> {
117    /// Should be the same `ecx` on each call, and match the one used to create the iterator.
118    pub fn next<M: Machine<'tcx, Provenance = Prov>>(
119        &mut self,
120        ecx: &InterpCx<'tcx, M>,
121    ) -> InterpResult<'tcx, Option<(u64, P)>> {
122        let Some(idx) = self.range.next() else { return interp_ok(None) };
123        // We use `Wrapping` here since the offset has already been checked when the iterator was created.
124        interp_ok(Some((
125            idx,
126            self.base.offset_with_meta(
127                self.stride * idx,
128                OffsetMode::Wrapping,
129                MemPlaceMeta::None,
130                self.field_layout,
131                ecx,
132            )?,
133        )))
134    }
135}
136
137// FIXME: Working around https://github.com/rust-lang/rust/issues/54385
138impl<'tcx, Prov, M> InterpCx<'tcx, M>
139where
140    Prov: Provenance,
141    M: Machine<'tcx, Provenance = Prov>,
142{
143    /// Offset a pointer to project to a field of a struct/union. Unlike `place_field`, this is
144    /// always possible without allocating, so it can take `&self`. Also return the field's layout.
145    /// This supports both struct and array fields, but not slices!
146    ///
147    /// This also works for arrays, but then the `usize` index type is restricting.
148    /// For indexing into arrays, use `mplace_index`.
149    pub fn project_field<P: Projectable<'tcx, M::Provenance>>(
150        &self,
151        base: &P,
152        field: usize,
153    ) -> InterpResult<'tcx, P> {
154        // Slices nominally have length 0, so they will panic somewhere in `fields.offset`.
155        debug_assert!(
156            !matches!(base.layout().ty.kind(), ty::Slice(..)),
157            "`field` projection called on a slice -- call `index` projection instead"
158        );
159        let offset = base.layout().fields.offset(field);
160        // Computing the layout does normalization, so we get a normalized type out of this
161        // even if the field type is non-normalized (possible e.g. via associated types).
162        let field_layout = base.layout().field(self, field);
163
164        // Offset may need adjustment for unsized fields.
165        let (meta, offset) = if field_layout.is_unsized() {
166            assert!(!base.layout().is_sized());
167            let base_meta = base.meta();
168            // Re-use parent metadata to determine dynamic field layout.
169            // With custom DSTS, this *will* execute user-defined code, but the same
170            // happens at run-time so that's okay.
171            match self.size_and_align_of(&base_meta, &field_layout)? {
172                Some((_, align)) => {
173                    // For packed types, we need to cap alignment.
174                    let align = if let ty::Adt(def, _) = base.layout().ty.kind()
175                        && let Some(packed) = def.repr().pack
176                    {
177                        align.min(packed)
178                    } else {
179                        align
180                    };
181                    (base_meta, offset.align_to(align))
182                }
183                None if offset == Size::ZERO => {
184                    // If the offset is 0, then rounding it up to alignment wouldn't change anything,
185                    // so we can do this even for types where we cannot determine the alignment.
186                    (base_meta, offset)
187                }
188                None => {
189                    // We cannot know the alignment of this field, so we cannot adjust.
190                    throw_unsup!(ExternTypeField)
191                }
192            }
193        } else {
194            // base_meta could be present; we might be accessing a sized field of an unsized
195            // struct.
196            (MemPlaceMeta::None, offset)
197        };
198
199        base.offset_with_meta(offset, OffsetMode::Inbounds, meta, field_layout, self)
200    }
201
202    /// Downcasting to an enum variant.
203    pub fn project_downcast<P: Projectable<'tcx, M::Provenance>>(
204        &self,
205        base: &P,
206        variant: VariantIdx,
207    ) -> InterpResult<'tcx, P> {
208        assert!(!base.meta().has_meta());
209        // Downcasts only change the layout.
210        // (In particular, no check about whether this is even the active variant -- that's by design,
211        // see https://github.com/rust-lang/rust/issues/93688#issuecomment-1032929496.)
212        // So we just "offset" by 0.
213        let layout = base.layout().for_variant(self, variant);
214        // This variant may in fact be uninhabited.
215        // See <https://github.com/rust-lang/rust/issues/120337>.
216
217        // This cannot be `transmute` as variants *can* have a smaller size than the entire enum.
218        base.offset(Size::ZERO, layout, self)
219    }
220
221    /// Compute the offset and field layout for accessing the given index.
222    pub fn project_index<P: Projectable<'tcx, M::Provenance>>(
223        &self,
224        base: &P,
225        index: u64,
226    ) -> InterpResult<'tcx, P> {
227        // Not using the layout method because we want to compute on u64
228        let (offset, field_layout) = match base.layout().fields {
229            abi::FieldsShape::Array { stride, count: _ } => {
230                // `count` is nonsense for slices, use the dynamic length instead.
231                let len = base.len(self)?;
232                if index >= len {
233                    // This can only be reached in ConstProp and non-rustc-MIR.
234                    throw_ub!(BoundsCheckFailed { len, index });
235                }
236                // With raw slices, `len` can be so big that this *can* overflow.
237                let offset = self
238                    .compute_size_in_bytes(stride, index)
239                    .ok_or_else(|| err_ub!(PointerArithOverflow))?;
240
241                // All fields have the same layout.
242                let field_layout = base.layout().field(self, 0);
243                (offset, field_layout)
244            }
245            _ => span_bug!(
246                self.cur_span(),
247                "`mplace_index` called on non-array type {:?}",
248                base.layout().ty
249            ),
250        };
251
252        base.offset(offset, field_layout, self)
253    }
254
255    /// Converts a repr(simd) value into an array of the right size, such that `project_index`
256    /// accesses the SIMD elements. Also returns the number of elements.
257    pub fn project_to_simd<P: Projectable<'tcx, M::Provenance>>(
258        &self,
259        base: &P,
260    ) -> InterpResult<'tcx, (P, u64)> {
261        assert!(base.layout().ty.ty_adt_def().unwrap().repr().simd());
262        // SIMD types must be newtypes around arrays, so all we have to do is project to their only field.
263        let array = self.project_field(base, 0)?;
264        let len = array.len(self)?;
265        interp_ok((array, len))
266    }
267
268    fn project_constant_index<P: Projectable<'tcx, M::Provenance>>(
269        &self,
270        base: &P,
271        offset: u64,
272        min_length: u64,
273        from_end: bool,
274    ) -> InterpResult<'tcx, P> {
275        let n = base.len(self)?;
276        if n < min_length {
277            // This can only be reached in ConstProp and non-rustc-MIR.
278            throw_ub!(BoundsCheckFailed { len: min_length, index: n });
279        }
280
281        let index = if from_end {
282            assert!(0 < offset && offset <= min_length);
283            n.checked_sub(offset).unwrap()
284        } else {
285            assert!(offset < min_length);
286            offset
287        };
288
289        self.project_index(base, index)
290    }
291
292    /// Iterates over all fields of an array. Much more efficient than doing the
293    /// same by repeatedly calling `project_index`.
294    pub fn project_array_fields<'a, P: Projectable<'tcx, M::Provenance>>(
295        &self,
296        base: &'a P,
297    ) -> InterpResult<'tcx, ArrayIterator<'a, 'tcx, M::Provenance, P>> {
298        let abi::FieldsShape::Array { stride, .. } = base.layout().fields else {
299            span_bug!(self.cur_span(), "project_array_fields: expected an array layout");
300        };
301        let len = base.len(self)?;
302        let field_layout = base.layout().field(self, 0);
303        // Ensure that all the offsets are in-bounds once, up-front.
304        debug!("project_array_fields: {base:?} {len}");
305        base.offset(len * stride, self.layout_of(self.tcx.types.unit).unwrap(), self)?;
306        // Create the iterator.
307        interp_ok(ArrayIterator {
308            base,
309            range: 0..len,
310            stride,
311            field_layout,
312            _phantom: PhantomData,
313        })
314    }
315
316    /// Subslicing
317    fn project_subslice<P: Projectable<'tcx, M::Provenance>>(
318        &self,
319        base: &P,
320        from: u64,
321        to: u64,
322        from_end: bool,
323    ) -> InterpResult<'tcx, P> {
324        let len = base.len(self)?; // also asserts that we have a type where this makes sense
325        let actual_to = if from_end {
326            if from.checked_add(to).is_none_or(|to| to > len) {
327                // This can only be reached in ConstProp and non-rustc-MIR.
328                throw_ub!(BoundsCheckFailed { len, index: from.saturating_add(to) });
329            }
330            len.checked_sub(to).unwrap()
331        } else {
332            to
333        };
334
335        // Not using layout method because that works with usize, and does not work with slices
336        // (that have count 0 in their layout).
337        let from_offset = match base.layout().fields {
338            abi::FieldsShape::Array { stride, .. } => stride * from, // `Size` multiplication is checked
339            _ => {
340                span_bug!(
341                    self.cur_span(),
342                    "unexpected layout of index access: {:#?}",
343                    base.layout()
344                )
345            }
346        };
347
348        // Compute meta and new layout
349        let inner_len = actual_to.checked_sub(from).unwrap();
350        let (meta, ty) = match base.layout().ty.kind() {
351            // It is not nice to match on the type, but that seems to be the only way to
352            // implement this.
353            ty::Array(inner, _) => {
354                (MemPlaceMeta::None, Ty::new_array(self.tcx.tcx, *inner, inner_len))
355            }
356            ty::Slice(..) => {
357                let len = Scalar::from_target_usize(inner_len, self);
358                (MemPlaceMeta::Meta(len), base.layout().ty)
359            }
360            _ => {
361                span_bug!(
362                    self.cur_span(),
363                    "cannot subslice non-array type: `{:?}`",
364                    base.layout().ty
365                )
366            }
367        };
368        let layout = self.layout_of(ty)?;
369
370        base.offset_with_meta(from_offset, OffsetMode::Inbounds, meta, layout, self)
371    }
372
373    /// Applying a general projection
374    #[instrument(skip(self), level = "trace")]
375    pub fn project<P>(&self, base: &P, proj_elem: mir::PlaceElem<'tcx>) -> InterpResult<'tcx, P>
376    where
377        P: Projectable<'tcx, M::Provenance> + From<MPlaceTy<'tcx, M::Provenance>> + std::fmt::Debug,
378    {
379        use rustc_middle::mir::ProjectionElem::*;
380        interp_ok(match proj_elem {
381            OpaqueCast(ty) => {
382                span_bug!(self.cur_span(), "OpaqueCast({ty}) encountered after borrowck")
383            }
384            UnwrapUnsafeBinder(target) => base.transmute(self.layout_of(target)?, self)?,
385            // We don't want anything happening here, this is here as a dummy.
386            Subtype(_) => base.transmute(base.layout(), self)?,
387            Field(field, _) => self.project_field(base, field.index())?,
388            Downcast(_, variant) => self.project_downcast(base, variant)?,
389            Deref => self.deref_pointer(&base.to_op(self)?)?.into(),
390            Index(local) => {
391                let layout = self.layout_of(self.tcx.types.usize)?;
392                let n = self.local_to_op(local, Some(layout))?;
393                let n = self.read_target_usize(&n)?;
394                self.project_index(base, n)?
395            }
396            ConstantIndex { offset, min_length, from_end } => {
397                self.project_constant_index(base, offset, min_length, from_end)?
398            }
399            Subslice { from, to, from_end } => self.project_subslice(base, from, to, from_end)?,
400        })
401    }
402}