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