]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_const_eval/src/interpret/place.rs
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`.
[rust.git] / compiler / rustc_const_eval / src / interpret / place.rs
1 //! Computations on places -- field projections, going from mir::Place, and writing
2 //! into a place.
3 //! All high-level functions to write to memory work on places as destinations.
4
5 use std::convert::TryFrom;
6 use std::hash::Hash;
7
8 use rustc_ast::Mutability;
9 use rustc_macros::HashStable;
10 use rustc_middle::mir;
11 use rustc_middle::ty::layout::{LayoutOf, PrimitiveExt, TyAndLayout};
12 use rustc_middle::ty::{self, Ty};
13 use rustc_target::abi::{Abi, Align, FieldsShape, TagEncoding};
14 use rustc_target::abi::{HasDataLayout, Size, VariantIdx, Variants};
15
16 use super::{
17     alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckInAllocMsg,
18     ConstAlloc, ImmTy, Immediate, InterpCx, InterpResult, LocalValue, Machine, MemoryKind, OpTy,
19     Operand, Pointer, PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit,
20 };
21
22 #[derive(Copy, Clone, Hash, PartialEq, Eq, HashStable, Debug)]
23 /// Information required for the sound usage of a `MemPlace`.
24 pub enum MemPlaceMeta<Tag: Provenance = AllocId> {
25     /// The unsized payload (e.g. length for slices or vtable pointer for trait objects).
26     Meta(Scalar<Tag>),
27     /// `Sized` types or unsized `extern type`
28     None,
29     /// The address of this place may not be taken. This protects the `MemPlace` from coming from
30     /// a ZST Operand without a backing allocation and being converted to an integer address. This
31     /// should be impossible, because you can't take the address of an operand, but this is a second
32     /// protection layer ensuring that we don't mess up.
33     Poison,
34 }
35
36 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
37 rustc_data_structures::static_assert_size!(MemPlaceMeta, 24);
38
39 impl<Tag: Provenance> MemPlaceMeta<Tag> {
40     pub fn unwrap_meta(self) -> Scalar<Tag> {
41         match self {
42             Self::Meta(s) => s,
43             Self::None | Self::Poison => {
44                 bug!("expected wide pointer extra data (e.g. slice length or trait object vtable)")
45             }
46         }
47     }
48     fn has_meta(self) -> bool {
49         match self {
50             Self::Meta(_) => true,
51             Self::None | Self::Poison => false,
52         }
53     }
54 }
55
56 #[derive(Copy, Clone, Hash, PartialEq, Eq, HashStable, Debug)]
57 pub struct MemPlace<Tag: Provenance = AllocId> {
58     /// The pointer can be a pure integer, with the `None` tag.
59     pub ptr: Pointer<Option<Tag>>,
60     pub align: Align,
61     /// Metadata for unsized places. Interpretation is up to the type.
62     /// Must not be present for sized types, but can be missing for unsized types
63     /// (e.g., `extern type`).
64     pub meta: MemPlaceMeta<Tag>,
65 }
66
67 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
68 rustc_data_structures::static_assert_size!(MemPlace, 48);
69
70 #[derive(Copy, Clone, Hash, PartialEq, Eq, HashStable, Debug)]
71 pub enum Place<Tag: Provenance = AllocId> {
72     /// A place referring to a value allocated in the `Memory` system.
73     Ptr(MemPlace<Tag>),
74
75     /// To support alloc-free locals, we are able to write directly to a local.
76     /// (Without that optimization, we'd just always be a `MemPlace`.)
77     Local { frame: usize, local: mir::Local },
78 }
79
80 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
81 rustc_data_structures::static_assert_size!(Place, 56);
82
83 #[derive(Copy, Clone, Debug)]
84 pub struct PlaceTy<'tcx, Tag: Provenance = AllocId> {
85     place: Place<Tag>, // Keep this private; it helps enforce invariants.
86     pub layout: TyAndLayout<'tcx>,
87 }
88
89 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
90 rustc_data_structures::static_assert_size!(PlaceTy<'_>, 72);
91
92 impl<'tcx, Tag: Provenance> std::ops::Deref for PlaceTy<'tcx, Tag> {
93     type Target = Place<Tag>;
94     #[inline(always)]
95     fn deref(&self) -> &Place<Tag> {
96         &self.place
97     }
98 }
99
100 /// A MemPlace with its layout. Constructing it is only possible in this module.
101 #[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
102 pub struct MPlaceTy<'tcx, Tag: Provenance = AllocId> {
103     mplace: MemPlace<Tag>,
104     pub layout: TyAndLayout<'tcx>,
105 }
106
107 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
108 rustc_data_structures::static_assert_size!(MPlaceTy<'_>, 64);
109
110 impl<'tcx, Tag: Provenance> std::ops::Deref for MPlaceTy<'tcx, Tag> {
111     type Target = MemPlace<Tag>;
112     #[inline(always)]
113     fn deref(&self) -> &MemPlace<Tag> {
114         &self.mplace
115     }
116 }
117
118 impl<'tcx, Tag: Provenance> From<MPlaceTy<'tcx, Tag>> for PlaceTy<'tcx, Tag> {
119     #[inline(always)]
120     fn from(mplace: MPlaceTy<'tcx, Tag>) -> Self {
121         PlaceTy { place: Place::Ptr(mplace.mplace), layout: mplace.layout }
122     }
123 }
124
125 impl<Tag: Provenance> MemPlace<Tag> {
126     #[inline(always)]
127     pub fn from_ptr(ptr: Pointer<Option<Tag>>, align: Align) -> Self {
128         MemPlace { ptr, align, meta: MemPlaceMeta::None }
129     }
130
131     /// Adjust the provenance of the main pointer (metadata is unaffected).
132     pub fn map_provenance(self, f: impl FnOnce(Option<Tag>) -> Option<Tag>) -> Self {
133         MemPlace { ptr: self.ptr.map_provenance(f), ..self }
134     }
135
136     /// Turn a mplace into a (thin or wide) pointer, as a reference, pointing to the same space.
137     /// This is the inverse of `ref_to_mplace`.
138     #[inline(always)]
139     pub fn to_ref(self, cx: &impl HasDataLayout) -> Immediate<Tag> {
140         match self.meta {
141             MemPlaceMeta::None => Immediate::from(Scalar::from_maybe_pointer(self.ptr, cx)),
142             MemPlaceMeta::Meta(meta) => {
143                 Immediate::ScalarPair(Scalar::from_maybe_pointer(self.ptr, cx).into(), meta.into())
144             }
145             MemPlaceMeta::Poison => bug!(
146                 "MPlaceTy::dangling may never be used to produce a \
147                 place that will have the address of its pointee taken"
148             ),
149         }
150     }
151
152     #[inline]
153     pub fn offset<'tcx>(
154         self,
155         offset: Size,
156         meta: MemPlaceMeta<Tag>,
157         cx: &impl HasDataLayout,
158     ) -> InterpResult<'tcx, Self> {
159         Ok(MemPlace {
160             ptr: self.ptr.offset(offset, cx)?,
161             align: self.align.restrict_for_offset(offset),
162             meta,
163         })
164     }
165 }
166
167 impl<'tcx, Tag: Provenance> MPlaceTy<'tcx, Tag> {
168     /// Produces a MemPlace that works for ZST but nothing else
169     #[inline]
170     pub fn dangling(layout: TyAndLayout<'tcx>) -> Self {
171         let align = layout.align.abi;
172         let ptr = Pointer::new(None, Size::from_bytes(align.bytes())); // no provenance, absolute address
173         // `Poison` this to make sure that the pointer value `ptr` is never observable by the program.
174         MPlaceTy { mplace: MemPlace { ptr, align, meta: MemPlaceMeta::Poison }, layout }
175     }
176
177     #[inline]
178     pub fn offset(
179         &self,
180         offset: Size,
181         meta: MemPlaceMeta<Tag>,
182         layout: TyAndLayout<'tcx>,
183         cx: &impl HasDataLayout,
184     ) -> InterpResult<'tcx, Self> {
185         Ok(MPlaceTy { mplace: self.mplace.offset(offset, meta, cx)?, layout })
186     }
187
188     #[inline]
189     pub fn from_aligned_ptr(ptr: Pointer<Option<Tag>>, layout: TyAndLayout<'tcx>) -> Self {
190         MPlaceTy { mplace: MemPlace::from_ptr(ptr, layout.align.abi), layout }
191     }
192
193     #[inline]
194     pub(super) fn len(&self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
195         if self.layout.is_unsized() {
196             // We need to consult `meta` metadata
197             match self.layout.ty.kind() {
198                 ty::Slice(..) | ty::Str => self.mplace.meta.unwrap_meta().to_machine_usize(cx),
199                 _ => bug!("len not supported on unsized type {:?}", self.layout.ty),
200             }
201         } else {
202             // Go through the layout.  There are lots of types that support a length,
203             // e.g., SIMD types. (But not all repr(simd) types even have FieldsShape::Array!)
204             match self.layout.fields {
205                 FieldsShape::Array { count, .. } => Ok(count),
206                 _ => bug!("len not supported on sized type {:?}", self.layout.ty),
207             }
208         }
209     }
210
211     #[inline]
212     pub(super) fn vtable(&self) -> Scalar<Tag> {
213         match self.layout.ty.kind() {
214             ty::Dynamic(..) => self.mplace.meta.unwrap_meta(),
215             _ => bug!("vtable not supported on type {:?}", self.layout.ty),
216         }
217     }
218 }
219
220 // These are defined here because they produce a place.
221 impl<'tcx, Tag: Provenance> OpTy<'tcx, Tag> {
222     #[inline(always)]
223     /// Note: do not call `as_ref` on the resulting place. This function should only be used to
224     /// read from the resulting mplace, not to get its address back.
225     pub fn try_as_mplace(&self) -> Result<MPlaceTy<'tcx, Tag>, ImmTy<'tcx, Tag>> {
226         match **self {
227             Operand::Indirect(mplace) => Ok(MPlaceTy { mplace, layout: self.layout }),
228             Operand::Immediate(_) if self.layout.is_zst() => Ok(MPlaceTy::dangling(self.layout)),
229             Operand::Immediate(imm) => Err(ImmTy::from_immediate(imm, self.layout)),
230         }
231     }
232
233     #[inline(always)]
234     /// Note: do not call `as_ref` on the resulting place. This function should only be used to
235     /// read from the resulting mplace, not to get its address back.
236     pub fn assert_mem_place(&self) -> MPlaceTy<'tcx, Tag> {
237         self.try_as_mplace().unwrap()
238     }
239 }
240
241 impl<Tag: Provenance> Place<Tag> {
242     #[inline]
243     pub fn assert_mem_place(self) -> MemPlace<Tag> {
244         match self {
245             Place::Ptr(mplace) => mplace,
246             _ => bug!("assert_mem_place: expected Place::Ptr, got {:?}", self),
247         }
248     }
249 }
250
251 impl<'tcx, Tag: Provenance> PlaceTy<'tcx, Tag> {
252     #[inline]
253     pub fn assert_mem_place(self) -> MPlaceTy<'tcx, Tag> {
254         MPlaceTy { mplace: self.place.assert_mem_place(), layout: self.layout }
255     }
256 }
257
258 // separating the pointer tag for `impl Trait`, see https://github.com/rust-lang/rust/issues/54385
259 impl<'mir, 'tcx: 'mir, Tag, M> InterpCx<'mir, 'tcx, M>
260 where
261     // FIXME: Working around https://github.com/rust-lang/rust/issues/54385
262     Tag: Provenance + Eq + Hash + 'static,
263     M: Machine<'mir, 'tcx, PointerTag = Tag>,
264 {
265     /// Take a value, which represents a (thin or wide) reference, and make it a place.
266     /// Alignment is just based on the type.  This is the inverse of `MemPlace::to_ref()`.
267     ///
268     /// Only call this if you are sure the place is "valid" (aligned and inbounds), or do not
269     /// want to ever use the place for memory access!
270     /// Generally prefer `deref_operand`.
271     pub fn ref_to_mplace(
272         &self,
273         val: &ImmTy<'tcx, M::PointerTag>,
274     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
275         let pointee_type =
276             val.layout.ty.builtin_deref(true).expect("`ref_to_mplace` called on non-ptr type").ty;
277         let layout = self.layout_of(pointee_type)?;
278         let (ptr, meta) = match **val {
279             Immediate::Scalar(ptr) => (ptr, MemPlaceMeta::None),
280             Immediate::ScalarPair(ptr, meta) => (ptr, MemPlaceMeta::Meta(meta.check_init()?)),
281         };
282
283         let mplace = MemPlace {
284             ptr: self.scalar_to_ptr(ptr.check_init()?),
285             // We could use the run-time alignment here. For now, we do not, because
286             // the point of tracking the alignment here is to make sure that the *static*
287             // alignment information emitted with the loads is correct. The run-time
288             // alignment can only be more restrictive.
289             align: layout.align.abi,
290             meta,
291         };
292         Ok(MPlaceTy { mplace, layout })
293     }
294
295     /// Take an operand, representing a pointer, and dereference it to a place -- that
296     /// will always be a MemPlace.  Lives in `place.rs` because it creates a place.
297     pub fn deref_operand(
298         &self,
299         src: &OpTy<'tcx, M::PointerTag>,
300     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
301         let val = self.read_immediate(src)?;
302         trace!("deref to {} on {:?}", val.layout.ty, *val);
303         let mplace = self.ref_to_mplace(&val)?;
304         self.check_mplace_access(mplace, CheckInAllocMsg::DerefTest)?;
305         Ok(mplace)
306     }
307
308     #[inline]
309     pub(super) fn get_alloc(
310         &self,
311         place: &MPlaceTy<'tcx, M::PointerTag>,
312     ) -> InterpResult<'tcx, Option<AllocRef<'_, 'tcx, M::PointerTag, M::AllocExtra>>> {
313         assert!(!place.layout.is_unsized());
314         assert!(!place.meta.has_meta());
315         let size = place.layout.size;
316         self.memory.get(place.ptr, size, place.align)
317     }
318
319     #[inline]
320     pub(super) fn get_alloc_mut(
321         &mut self,
322         place: &MPlaceTy<'tcx, M::PointerTag>,
323     ) -> InterpResult<'tcx, Option<AllocRefMut<'_, 'tcx, M::PointerTag, M::AllocExtra>>> {
324         assert!(!place.layout.is_unsized());
325         assert!(!place.meta.has_meta());
326         let size = place.layout.size;
327         self.memory.get_mut(place.ptr, size, place.align)
328     }
329
330     /// Check if this mplace is dereferenceable and sufficiently aligned.
331     fn check_mplace_access(
332         &self,
333         mplace: MPlaceTy<'tcx, M::PointerTag>,
334         msg: CheckInAllocMsg,
335     ) -> InterpResult<'tcx> {
336         let (size, align) = self
337             .size_and_align_of_mplace(&mplace)?
338             .unwrap_or((mplace.layout.size, mplace.layout.align.abi));
339         assert!(mplace.mplace.align <= align, "dynamic alignment less strict than static one?");
340         let align = M::enforce_alignment(&self.memory.extra).then_some(align);
341         self.memory.check_ptr_access_align(mplace.ptr, size, align.unwrap_or(Align::ONE), msg)?;
342         Ok(())
343     }
344
345     /// Offset a pointer to project to a field of a struct/union. Unlike `place_field`, this is
346     /// always possible without allocating, so it can take `&self`. Also return the field's layout.
347     /// This supports both struct and array fields.
348     ///
349     /// This also works for arrays, but then the `usize` index type is restricting.
350     /// For indexing into arrays, use `mplace_index`.
351     #[inline(always)]
352     pub fn mplace_field(
353         &self,
354         base: &MPlaceTy<'tcx, M::PointerTag>,
355         field: usize,
356     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
357         let offset = base.layout.fields.offset(field);
358         let field_layout = base.layout.field(self, field);
359
360         // Offset may need adjustment for unsized fields.
361         let (meta, offset) = if field_layout.is_unsized() {
362             // Re-use parent metadata to determine dynamic field layout.
363             // With custom DSTS, this *will* execute user-defined code, but the same
364             // happens at run-time so that's okay.
365             match self.size_and_align_of(&base.meta, &field_layout)? {
366                 Some((_, align)) => (base.meta, offset.align_to(align)),
367                 None => {
368                     // For unsized types with an extern type tail we perform no adjustments.
369                     // NOTE: keep this in sync with `PlaceRef::project_field` in the codegen backend.
370                     assert!(matches!(base.meta, MemPlaceMeta::None));
371                     (base.meta, offset)
372                 }
373             }
374         } else {
375             // base.meta could be present; we might be accessing a sized field of an unsized
376             // struct.
377             (MemPlaceMeta::None, offset)
378         };
379
380         // We do not look at `base.layout.align` nor `field_layout.align`, unlike
381         // codegen -- mostly to see if we can get away with that
382         base.offset(offset, meta, field_layout, self)
383     }
384
385     /// Index into an array.
386     #[inline(always)]
387     pub fn mplace_index(
388         &self,
389         base: &MPlaceTy<'tcx, M::PointerTag>,
390         index: u64,
391     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
392         // Not using the layout method because we want to compute on u64
393         match base.layout.fields {
394             FieldsShape::Array { stride, .. } => {
395                 let len = base.len(self)?;
396                 if index >= len {
397                     // This can only be reached in ConstProp and non-rustc-MIR.
398                     throw_ub!(BoundsCheckFailed { len, index });
399                 }
400                 let offset = stride * index; // `Size` multiplication
401                 // All fields have the same layout.
402                 let field_layout = base.layout.field(self, 0);
403
404                 assert!(!field_layout.is_unsized());
405                 base.offset(offset, MemPlaceMeta::None, field_layout, self)
406             }
407             _ => span_bug!(
408                 self.cur_span(),
409                 "`mplace_index` called on non-array type {:?}",
410                 base.layout.ty
411             ),
412         }
413     }
414
415     // Iterates over all fields of an array. Much more efficient than doing the
416     // same by repeatedly calling `mplace_array`.
417     pub(super) fn mplace_array_fields<'a>(
418         &self,
419         base: &'a MPlaceTy<'tcx, Tag>,
420     ) -> InterpResult<'tcx, impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Tag>>> + 'a>
421     {
422         let len = base.len(self)?; // also asserts that we have a type where this makes sense
423         let stride = match base.layout.fields {
424             FieldsShape::Array { stride, .. } => stride,
425             _ => span_bug!(self.cur_span(), "mplace_array_fields: expected an array layout"),
426         };
427         let layout = base.layout.field(self, 0);
428         let dl = &self.tcx.data_layout;
429         // `Size` multiplication
430         Ok((0..len).map(move |i| base.offset(stride * i, MemPlaceMeta::None, layout, dl)))
431     }
432
433     fn mplace_subslice(
434         &self,
435         base: &MPlaceTy<'tcx, M::PointerTag>,
436         from: u64,
437         to: u64,
438         from_end: bool,
439     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
440         let len = base.len(self)?; // also asserts that we have a type where this makes sense
441         let actual_to = if from_end {
442             if from.checked_add(to).map_or(true, |to| to > len) {
443                 // This can only be reached in ConstProp and non-rustc-MIR.
444                 throw_ub!(BoundsCheckFailed { len: len, index: from.saturating_add(to) });
445             }
446             len.checked_sub(to).unwrap()
447         } else {
448             to
449         };
450
451         // Not using layout method because that works with usize, and does not work with slices
452         // (that have count 0 in their layout).
453         let from_offset = match base.layout.fields {
454             FieldsShape::Array { stride, .. } => stride * from, // `Size` multiplication is checked
455             _ => {
456                 span_bug!(self.cur_span(), "unexpected layout of index access: {:#?}", base.layout)
457             }
458         };
459
460         // Compute meta and new layout
461         let inner_len = actual_to.checked_sub(from).unwrap();
462         let (meta, ty) = match base.layout.ty.kind() {
463             // It is not nice to match on the type, but that seems to be the only way to
464             // implement this.
465             ty::Array(inner, _) => (MemPlaceMeta::None, self.tcx.mk_array(inner, inner_len)),
466             ty::Slice(..) => {
467                 let len = Scalar::from_machine_usize(inner_len, self);
468                 (MemPlaceMeta::Meta(len), base.layout.ty)
469             }
470             _ => {
471                 span_bug!(self.cur_span(), "cannot subslice non-array type: `{:?}`", base.layout.ty)
472             }
473         };
474         let layout = self.layout_of(ty)?;
475         base.offset(from_offset, meta, layout, self)
476     }
477
478     pub(crate) fn mplace_downcast(
479         &self,
480         base: &MPlaceTy<'tcx, M::PointerTag>,
481         variant: VariantIdx,
482     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
483         // Downcasts only change the layout
484         assert!(!base.meta.has_meta());
485         Ok(MPlaceTy { layout: base.layout.for_variant(self, variant), ..*base })
486     }
487
488     /// Project into an mplace
489     pub(super) fn mplace_projection(
490         &self,
491         base: &MPlaceTy<'tcx, M::PointerTag>,
492         proj_elem: mir::PlaceElem<'tcx>,
493     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
494         use rustc_middle::mir::ProjectionElem::*;
495         Ok(match proj_elem {
496             Field(field, _) => self.mplace_field(base, field.index())?,
497             Downcast(_, variant) => self.mplace_downcast(base, variant)?,
498             Deref => self.deref_operand(&base.into())?,
499
500             Index(local) => {
501                 let layout = self.layout_of(self.tcx.types.usize)?;
502                 let n = self.access_local(self.frame(), local, Some(layout))?;
503                 let n = self.read_scalar(&n)?;
504                 let n = n.to_machine_usize(self)?;
505                 self.mplace_index(base, n)?
506             }
507
508             ConstantIndex { offset, min_length, from_end } => {
509                 let n = base.len(self)?;
510                 if n < min_length {
511                     // This can only be reached in ConstProp and non-rustc-MIR.
512                     throw_ub!(BoundsCheckFailed { len: min_length, index: n });
513                 }
514
515                 let index = if from_end {
516                     assert!(0 < offset && offset <= min_length);
517                     n.checked_sub(offset).unwrap()
518                 } else {
519                     assert!(offset < min_length);
520                     offset
521                 };
522
523                 self.mplace_index(base, index)?
524             }
525
526             Subslice { from, to, from_end } => self.mplace_subslice(base, from, to, from_end)?,
527         })
528     }
529
530     /// Converts a repr(simd) place into a place where `place_index` accesses the SIMD elements.
531     /// Also returns the number of elements.
532     pub fn mplace_to_simd(
533         &self,
534         base: &MPlaceTy<'tcx, M::PointerTag>,
535     ) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::PointerTag>, u64)> {
536         // Basically we just transmute this place into an array following simd_size_and_type.
537         // (Transmuting is okay since this is an in-memory place. We also double-check the size
538         // stays the same.)
539         let (len, e_ty) = base.layout.ty.simd_size_and_type(*self.tcx);
540         let array = self.tcx.mk_array(e_ty, len);
541         let layout = self.layout_of(array)?;
542         assert_eq!(layout.size, base.layout.size);
543         Ok((MPlaceTy { layout, ..*base }, len))
544     }
545
546     /// Gets the place of a field inside the place, and also the field's type.
547     /// Just a convenience function, but used quite a bit.
548     /// This is the only projection that might have a side-effect: We cannot project
549     /// into the field of a local `ScalarPair`, we have to first allocate it.
550     pub fn place_field(
551         &mut self,
552         base: &PlaceTy<'tcx, M::PointerTag>,
553         field: usize,
554     ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
555         // FIXME: We could try to be smarter and avoid allocation for fields that span the
556         // entire place.
557         let mplace = self.force_allocation(base)?;
558         Ok(self.mplace_field(&mplace, field)?.into())
559     }
560
561     pub fn place_index(
562         &mut self,
563         base: &PlaceTy<'tcx, M::PointerTag>,
564         index: u64,
565     ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
566         let mplace = self.force_allocation(base)?;
567         Ok(self.mplace_index(&mplace, index)?.into())
568     }
569
570     pub fn place_downcast(
571         &self,
572         base: &PlaceTy<'tcx, M::PointerTag>,
573         variant: VariantIdx,
574     ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
575         // Downcast just changes the layout
576         Ok(match base.place {
577             Place::Ptr(mplace) => {
578                 self.mplace_downcast(&MPlaceTy { mplace, layout: base.layout }, variant)?.into()
579             }
580             Place::Local { .. } => {
581                 let layout = base.layout.for_variant(self, variant);
582                 PlaceTy { layout, ..*base }
583             }
584         })
585     }
586
587     /// Projects into a place.
588     pub fn place_projection(
589         &mut self,
590         base: &PlaceTy<'tcx, M::PointerTag>,
591         &proj_elem: &mir::ProjectionElem<mir::Local, Ty<'tcx>>,
592     ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
593         use rustc_middle::mir::ProjectionElem::*;
594         Ok(match proj_elem {
595             Field(field, _) => self.place_field(base, field.index())?,
596             Downcast(_, variant) => self.place_downcast(base, variant)?,
597             Deref => self.deref_operand(&self.place_to_op(base)?)?.into(),
598             // For the other variants, we have to force an allocation.
599             // This matches `operand_projection`.
600             Subslice { .. } | ConstantIndex { .. } | Index(_) => {
601                 let mplace = self.force_allocation(base)?;
602                 self.mplace_projection(&mplace, proj_elem)?.into()
603             }
604         })
605     }
606
607     /// Converts a repr(simd) place into a place where `place_index` accesses the SIMD elements.
608     /// Also returns the number of elements.
609     pub fn place_to_simd(
610         &mut self,
611         base: &PlaceTy<'tcx, M::PointerTag>,
612     ) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::PointerTag>, u64)> {
613         let mplace = self.force_allocation(base)?;
614         self.mplace_to_simd(&mplace)
615     }
616
617     /// Computes a place. You should only use this if you intend to write into this
618     /// place; for reading, a more efficient alternative is `eval_place_for_read`.
619     pub fn eval_place(
620         &mut self,
621         place: mir::Place<'tcx>,
622     ) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
623         let mut place_ty = PlaceTy {
624             // This works even for dead/uninitialized locals; we check further when writing
625             place: Place::Local { frame: self.frame_idx(), local: place.local },
626             layout: self.layout_of_local(self.frame(), place.local, None)?,
627         };
628
629         for elem in place.projection.iter() {
630             place_ty = self.place_projection(&place_ty, &elem)?
631         }
632
633         trace!("{:?}", self.dump_place(place_ty.place));
634         // Sanity-check the type we ended up with.
635         debug_assert!(mir_assign_valid_types(
636             *self.tcx,
637             self.param_env,
638             self.layout_of(self.subst_from_current_frame_and_normalize_erasing_regions(
639                 place.ty(&self.frame().body.local_decls, *self.tcx).ty
640             )?)?,
641             place_ty.layout,
642         ));
643         Ok(place_ty)
644     }
645
646     /// Write an immediate to a place
647     #[inline(always)]
648     pub fn write_immediate(
649         &mut self,
650         src: Immediate<M::PointerTag>,
651         dest: &PlaceTy<'tcx, M::PointerTag>,
652     ) -> InterpResult<'tcx> {
653         self.write_immediate_no_validate(src, dest)?;
654
655         if M::enforce_validity(self) {
656             // Data got changed, better make sure it matches the type!
657             self.validate_operand(&self.place_to_op(dest)?)?;
658         }
659
660         Ok(())
661     }
662
663     /// Write a scalar to a place
664     #[inline(always)]
665     pub fn write_scalar(
666         &mut self,
667         val: impl Into<ScalarMaybeUninit<M::PointerTag>>,
668         dest: &PlaceTy<'tcx, M::PointerTag>,
669     ) -> InterpResult<'tcx> {
670         self.write_immediate(Immediate::Scalar(val.into()), dest)
671     }
672
673     /// Write a pointer to a place
674     #[inline(always)]
675     pub fn write_pointer(
676         &mut self,
677         ptr: impl Into<Pointer<Option<M::PointerTag>>>,
678         dest: &PlaceTy<'tcx, M::PointerTag>,
679     ) -> InterpResult<'tcx> {
680         self.write_scalar(Scalar::from_maybe_pointer(ptr.into(), self), dest)
681     }
682
683     /// Write an immediate to a place.
684     /// If you use this you are responsible for validating that things got copied at the
685     /// right type.
686     fn write_immediate_no_validate(
687         &mut self,
688         src: Immediate<M::PointerTag>,
689         dest: &PlaceTy<'tcx, M::PointerTag>,
690     ) -> InterpResult<'tcx> {
691         if cfg!(debug_assertions) {
692             // This is a very common path, avoid some checks in release mode
693             assert!(!dest.layout.is_unsized(), "Cannot write unsized data");
694             match src {
695                 Immediate::Scalar(ScalarMaybeUninit::Scalar(Scalar::Ptr(..))) => assert_eq!(
696                     self.pointer_size(),
697                     dest.layout.size,
698                     "Size mismatch when writing pointer"
699                 ),
700                 Immediate::Scalar(ScalarMaybeUninit::Scalar(Scalar::Int(int))) => {
701                     assert_eq!(int.size(), dest.layout.size, "Size mismatch when writing bits")
702                 }
703                 Immediate::Scalar(ScalarMaybeUninit::Uninit) => {} // uninit can have any size
704                 Immediate::ScalarPair(_, _) => {
705                     // FIXME: Can we check anything here?
706                 }
707             }
708         }
709         trace!("write_immediate: {:?} <- {:?}: {}", *dest, src, dest.layout.ty);
710
711         // See if we can avoid an allocation. This is the counterpart to `try_read_immediate`,
712         // but not factored as a separate function.
713         let mplace = match dest.place {
714             Place::Local { frame, local } => {
715                 match M::access_local_mut(self, frame, local)? {
716                     Ok(local) => {
717                         // Local can be updated in-place.
718                         *local = LocalValue::Live(Operand::Immediate(src));
719                         return Ok(());
720                     }
721                     Err(mplace) => {
722                         // The local is in memory, go on below.
723                         mplace
724                     }
725                 }
726             }
727             Place::Ptr(mplace) => mplace, // already referring to memory
728         };
729         let dest = MPlaceTy { mplace, layout: dest.layout };
730
731         // This is already in memory, write there.
732         self.write_immediate_to_mplace_no_validate(src, &dest)
733     }
734
735     /// Write an immediate to memory.
736     /// If you use this you are responsible for validating that things got copied at the
737     /// right type.
738     fn write_immediate_to_mplace_no_validate(
739         &mut self,
740         value: Immediate<M::PointerTag>,
741         dest: &MPlaceTy<'tcx, M::PointerTag>,
742     ) -> InterpResult<'tcx> {
743         // Note that it is really important that the type here is the right one, and matches the
744         // type things are read at. In case `src_val` is a `ScalarPair`, we don't do any magic here
745         // to handle padding properly, which is only correct if we never look at this data with the
746         // wrong type.
747
748         // Invalid places are a thing: the return place of a diverging function
749         let tcx = *self.tcx;
750         let mut alloc = match self.get_alloc_mut(dest)? {
751             Some(a) => a,
752             None => return Ok(()), // zero-sized access
753         };
754
755         // FIXME: We should check that there are dest.layout.size many bytes available in
756         // memory.  The code below is not sufficient, with enough padding it might not
757         // cover all the bytes!
758         match value {
759             Immediate::Scalar(scalar) => {
760                 match dest.layout.abi {
761                     Abi::Scalar(_) => {} // fine
762                     _ => span_bug!(
763                         self.cur_span(),
764                         "write_immediate_to_mplace: invalid Scalar layout: {:#?}",
765                         dest.layout
766                     ),
767                 }
768                 alloc.write_scalar(alloc_range(Size::ZERO, dest.layout.size), scalar)
769             }
770             Immediate::ScalarPair(a_val, b_val) => {
771                 // We checked `ptr_align` above, so all fields will have the alignment they need.
772                 // We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
773                 // which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
774                 let (a, b) = match dest.layout.abi {
775                     Abi::ScalarPair(a, b) => (a.value, b.value),
776                     _ => span_bug!(
777                         self.cur_span(),
778                         "write_immediate_to_mplace: invalid ScalarPair layout: {:#?}",
779                         dest.layout
780                     ),
781                 };
782                 let (a_size, b_size) = (a.size(&tcx), b.size(&tcx));
783                 let b_offset = a_size.align_to(b.align(&tcx).abi);
784
785                 // It is tempting to verify `b_offset` against `layout.fields.offset(1)`,
786                 // but that does not work: We could be a newtype around a pair, then the
787                 // fields do not match the `ScalarPair` components.
788
789                 alloc.write_scalar(alloc_range(Size::ZERO, a_size), a_val)?;
790                 alloc.write_scalar(alloc_range(b_offset, b_size), b_val)
791             }
792         }
793     }
794
795     /// Copies the data from an operand to a place. This does not support transmuting!
796     /// Use `copy_op_transmute` if the layouts could disagree.
797     #[inline(always)]
798     pub fn copy_op(
799         &mut self,
800         src: &OpTy<'tcx, M::PointerTag>,
801         dest: &PlaceTy<'tcx, M::PointerTag>,
802     ) -> InterpResult<'tcx> {
803         self.copy_op_no_validate(src, dest)?;
804
805         if M::enforce_validity(self) {
806             // Data got changed, better make sure it matches the type!
807             self.validate_operand(&self.place_to_op(dest)?)?;
808         }
809
810         Ok(())
811     }
812
813     /// Copies the data from an operand to a place. This does not support transmuting!
814     /// Use `copy_op_transmute` if the layouts could disagree.
815     /// Also, if you use this you are responsible for validating that things get copied at the
816     /// right type.
817     fn copy_op_no_validate(
818         &mut self,
819         src: &OpTy<'tcx, M::PointerTag>,
820         dest: &PlaceTy<'tcx, M::PointerTag>,
821     ) -> InterpResult<'tcx> {
822         // We do NOT compare the types for equality, because well-typed code can
823         // actually "transmute" `&mut T` to `&T` in an assignment without a cast.
824         if !mir_assign_valid_types(*self.tcx, self.param_env, src.layout, dest.layout) {
825             span_bug!(
826                 self.cur_span(),
827                 "type mismatch when copying!\nsrc: {:?},\ndest: {:?}",
828                 src.layout.ty,
829                 dest.layout.ty,
830             );
831         }
832
833         // Let us see if the layout is simple so we take a shortcut, avoid force_allocation.
834         let src = match self.try_read_immediate(src)? {
835             Ok(src_val) => {
836                 assert!(!src.layout.is_unsized(), "cannot have unsized immediates");
837                 // Yay, we got a value that we can write directly.
838                 // FIXME: Add a check to make sure that if `src` is indirect,
839                 // it does not overlap with `dest`.
840                 return self.write_immediate_no_validate(*src_val, dest);
841             }
842             Err(mplace) => mplace,
843         };
844         // Slow path, this does not fit into an immediate. Just memcpy.
845         trace!("copy_op: {:?} <- {:?}: {}", *dest, src, dest.layout.ty);
846
847         // This interprets `src.meta` with the `dest` local's layout, if an unsized local
848         // is being initialized!
849         let (dest, size) = self.force_allocation_maybe_sized(dest, src.meta)?;
850         let size = size.unwrap_or_else(|| {
851             assert!(
852                 !dest.layout.is_unsized(),
853                 "Cannot copy into already initialized unsized place"
854             );
855             dest.layout.size
856         });
857         assert_eq!(src.meta, dest.meta, "Can only copy between equally-sized instances");
858
859         self.memory
860             .copy(src.ptr, src.align, dest.ptr, dest.align, size, /*nonoverlapping*/ true)
861     }
862
863     /// Copies the data from an operand to a place. The layouts may disagree, but they must
864     /// have the same size.
865     pub fn copy_op_transmute(
866         &mut self,
867         src: &OpTy<'tcx, M::PointerTag>,
868         dest: &PlaceTy<'tcx, M::PointerTag>,
869     ) -> InterpResult<'tcx> {
870         if mir_assign_valid_types(*self.tcx, self.param_env, src.layout, dest.layout) {
871             // Fast path: Just use normal `copy_op`
872             return self.copy_op(src, dest);
873         }
874         // We still require the sizes to match.
875         if src.layout.size != dest.layout.size {
876             // FIXME: This should be an assert instead of an error, but if we transmute within an
877             // array length computation, `typeck` may not have yet been run and errored out. In fact
878             // most likey we *are* running `typeck` right now. Investigate whether we can bail out
879             // on `typeck_results().has_errors` at all const eval entry points.
880             debug!("Size mismatch when transmuting!\nsrc: {:#?}\ndest: {:#?}", src, dest);
881             self.tcx.sess.delay_span_bug(
882                 self.cur_span(),
883                 "size-changing transmute, should have been caught by transmute checking",
884             );
885             throw_inval!(TransmuteSizeDiff(src.layout.ty, dest.layout.ty));
886         }
887         // Unsized copies rely on interpreting `src.meta` with `dest.layout`, we want
888         // to avoid that here.
889         assert!(
890             !src.layout.is_unsized() && !dest.layout.is_unsized(),
891             "Cannot transmute unsized data"
892         );
893
894         // The hard case is `ScalarPair`.  `src` is already read from memory in this case,
895         // using `src.layout` to figure out which bytes to use for the 1st and 2nd field.
896         // We have to write them to `dest` at the offsets they were *read at*, which is
897         // not necessarily the same as the offsets in `dest.layout`!
898         // Hence we do the copy with the source layout on both sides.  We also make sure to write
899         // into memory, because if `dest` is a local we would not even have a way to write
900         // at the `src` offsets; the fact that we came from a different layout would
901         // just be lost.
902         let dest = self.force_allocation(dest)?;
903         self.copy_op_no_validate(
904             src,
905             &PlaceTy::from(MPlaceTy { mplace: *dest, layout: src.layout }),
906         )?;
907
908         if M::enforce_validity(self) {
909             // Data got changed, better make sure it matches the type!
910             self.validate_operand(&dest.into())?;
911         }
912
913         Ok(())
914     }
915
916     /// Ensures that a place is in memory, and returns where it is.
917     /// If the place currently refers to a local that doesn't yet have a matching allocation,
918     /// create such an allocation.
919     /// This is essentially `force_to_memplace`.
920     ///
921     /// This supports unsized types and returns the computed size to avoid some
922     /// redundant computation when copying; use `force_allocation` for a simpler, sized-only
923     /// version.
924     pub fn force_allocation_maybe_sized(
925         &mut self,
926         place: &PlaceTy<'tcx, M::PointerTag>,
927         meta: MemPlaceMeta<M::PointerTag>,
928     ) -> InterpResult<'tcx, (MPlaceTy<'tcx, M::PointerTag>, Option<Size>)> {
929         let (mplace, size) = match place.place {
930             Place::Local { frame, local } => {
931                 match M::access_local_mut(self, frame, local)? {
932                     Ok(&mut local_val) => {
933                         // We need to make an allocation.
934
935                         // We need the layout of the local.  We can NOT use the layout we got,
936                         // that might e.g., be an inner field of a struct with `Scalar` layout,
937                         // that has different alignment than the outer field.
938                         let local_layout =
939                             self.layout_of_local(&self.stack()[frame], local, None)?;
940                         // We also need to support unsized types, and hence cannot use `allocate`.
941                         let (size, align) = self
942                             .size_and_align_of(&meta, &local_layout)?
943                             .expect("Cannot allocate for non-dyn-sized type");
944                         let ptr = self.memory.allocate(size, align, MemoryKind::Stack)?;
945                         let mplace = MemPlace { ptr: ptr.into(), align, meta };
946                         if let LocalValue::Live(Operand::Immediate(value)) = local_val {
947                             // Preserve old value.
948                             // We don't have to validate as we can assume the local
949                             // was already valid for its type.
950                             let mplace = MPlaceTy { mplace, layout: local_layout };
951                             self.write_immediate_to_mplace_no_validate(value, &mplace)?;
952                         }
953                         // Now we can call `access_mut` again, asserting it goes well,
954                         // and actually overwrite things.
955                         *M::access_local_mut(self, frame, local).unwrap().unwrap() =
956                             LocalValue::Live(Operand::Indirect(mplace));
957                         (mplace, Some(size))
958                     }
959                     Err(mplace) => (mplace, None), // this already was an indirect local
960                 }
961             }
962             Place::Ptr(mplace) => (mplace, None),
963         };
964         // Return with the original layout, so that the caller can go on
965         Ok((MPlaceTy { mplace, layout: place.layout }, size))
966     }
967
968     #[inline(always)]
969     pub fn force_allocation(
970         &mut self,
971         place: &PlaceTy<'tcx, M::PointerTag>,
972     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
973         Ok(self.force_allocation_maybe_sized(place, MemPlaceMeta::None)?.0)
974     }
975
976     pub fn allocate(
977         &mut self,
978         layout: TyAndLayout<'tcx>,
979         kind: MemoryKind<M::MemoryKind>,
980     ) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> {
981         let ptr = self.memory.allocate(layout.size, layout.align.abi, kind)?;
982         Ok(MPlaceTy::from_aligned_ptr(ptr.into(), layout))
983     }
984
985     /// Returns a wide MPlace of type `&'static [mut] str` to a new 1-aligned allocation.
986     pub fn allocate_str(
987         &mut self,
988         str: &str,
989         kind: MemoryKind<M::MemoryKind>,
990         mutbl: Mutability,
991     ) -> MPlaceTy<'tcx, M::PointerTag> {
992         let ptr = self.memory.allocate_bytes(str.as_bytes(), Align::ONE, kind, mutbl);
993         let meta = Scalar::from_machine_usize(u64::try_from(str.len()).unwrap(), self);
994         let mplace =
995             MemPlace { ptr: ptr.into(), align: Align::ONE, meta: MemPlaceMeta::Meta(meta) };
996
997         let ty = self.tcx.mk_ref(
998             self.tcx.lifetimes.re_static,
999             ty::TypeAndMut { ty: self.tcx.types.str_, mutbl },
1000         );
1001         let layout = self.layout_of(ty).unwrap();
1002         MPlaceTy { mplace, layout }
1003     }
1004
1005     /// Writes the discriminant of the given variant.
1006     pub fn write_discriminant(
1007         &mut self,
1008         variant_index: VariantIdx,
1009         dest: &PlaceTy<'tcx, M::PointerTag>,
1010     ) -> InterpResult<'tcx> {
1011         // This must be an enum or generator.
1012         match dest.layout.ty.kind() {
1013             ty::Adt(adt, _) => assert!(adt.is_enum()),
1014             ty::Generator(..) => {}
1015             _ => span_bug!(
1016                 self.cur_span(),
1017                 "write_discriminant called on non-variant-type (neither enum nor generator)"
1018             ),
1019         }
1020         // Layout computation excludes uninhabited variants from consideration
1021         // therefore there's no way to represent those variants in the given layout.
1022         // Essentially, uninhabited variants do not have a tag that corresponds to their
1023         // discriminant, so we cannot do anything here.
1024         // When evaluating we will always error before even getting here, but ConstProp 'executes'
1025         // dead code, so we cannot ICE here.
1026         if dest.layout.for_variant(self, variant_index).abi.is_uninhabited() {
1027             throw_ub!(UninhabitedEnumVariantWritten)
1028         }
1029
1030         match dest.layout.variants {
1031             Variants::Single { index } => {
1032                 assert_eq!(index, variant_index);
1033             }
1034             Variants::Multiple {
1035                 tag_encoding: TagEncoding::Direct,
1036                 tag: tag_layout,
1037                 tag_field,
1038                 ..
1039             } => {
1040                 // No need to validate that the discriminant here because the
1041                 // `TyAndLayout::for_variant()` call earlier already checks the variant is valid.
1042
1043                 let discr_val =
1044                     dest.layout.ty.discriminant_for_variant(*self.tcx, variant_index).unwrap().val;
1045
1046                 // raw discriminants for enums are isize or bigger during
1047                 // their computation, but the in-memory tag is the smallest possible
1048                 // representation
1049                 let size = tag_layout.value.size(self);
1050                 let tag_val = size.truncate(discr_val);
1051
1052                 let tag_dest = self.place_field(dest, tag_field)?;
1053                 self.write_scalar(Scalar::from_uint(tag_val, size), &tag_dest)?;
1054             }
1055             Variants::Multiple {
1056                 tag_encoding:
1057                     TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start },
1058                 tag: tag_layout,
1059                 tag_field,
1060                 ..
1061             } => {
1062                 // No need to validate that the discriminant here because the
1063                 // `TyAndLayout::for_variant()` call earlier already checks the variant is valid.
1064
1065                 if variant_index != dataful_variant {
1066                     let variants_start = niche_variants.start().as_u32();
1067                     let variant_index_relative = variant_index
1068                         .as_u32()
1069                         .checked_sub(variants_start)
1070                         .expect("overflow computing relative variant idx");
1071                     // We need to use machine arithmetic when taking into account `niche_start`:
1072                     // tag_val = variant_index_relative + niche_start_val
1073                     let tag_layout = self.layout_of(tag_layout.value.to_int_ty(*self.tcx))?;
1074                     let niche_start_val = ImmTy::from_uint(niche_start, tag_layout);
1075                     let variant_index_relative_val =
1076                         ImmTy::from_uint(variant_index_relative, tag_layout);
1077                     let tag_val = self.binary_op(
1078                         mir::BinOp::Add,
1079                         &variant_index_relative_val,
1080                         &niche_start_val,
1081                     )?;
1082                     // Write result.
1083                     let niche_dest = self.place_field(dest, tag_field)?;
1084                     self.write_immediate(*tag_val, &niche_dest)?;
1085                 }
1086             }
1087         }
1088
1089         Ok(())
1090     }
1091
1092     pub fn raw_const_to_mplace(
1093         &self,
1094         raw: ConstAlloc<'tcx>,
1095     ) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
1096         // This must be an allocation in `tcx`
1097         let _ = self.tcx.global_alloc(raw.alloc_id);
1098         let ptr = self.global_base_pointer(Pointer::from(raw.alloc_id))?;
1099         let layout = self.layout_of(raw.ty)?;
1100         Ok(MPlaceTy::from_aligned_ptr(ptr.into(), layout))
1101     }
1102
1103     /// Turn a place with a `dyn Trait` type into a place with the actual dynamic type.
1104     /// Also return some more information so drop doesn't have to run the same code twice.
1105     pub(super) fn unpack_dyn_trait(
1106         &self,
1107         mplace: &MPlaceTy<'tcx, M::PointerTag>,
1108     ) -> InterpResult<'tcx, (ty::Instance<'tcx>, MPlaceTy<'tcx, M::PointerTag>)> {
1109         let vtable = self.scalar_to_ptr(mplace.vtable()); // also sanity checks the type
1110         let (instance, ty) = self.read_drop_type_from_vtable(vtable)?;
1111         let layout = self.layout_of(ty)?;
1112
1113         // More sanity checks
1114         if cfg!(debug_assertions) {
1115             let (size, align) = self.read_size_and_align_from_vtable(vtable)?;
1116             assert_eq!(size, layout.size);
1117             // only ABI alignment is preserved
1118             assert_eq!(align, layout.align.abi);
1119         }
1120
1121         let mplace = MPlaceTy { mplace: MemPlace { meta: MemPlaceMeta::None, ..**mplace }, layout };
1122         Ok((instance, mplace))
1123     }
1124 }