]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/interpret/operand.rs
Rollup merge of #67501 - oli-obk:test-slice-patterns, r=RalfJung
[rust.git] / src / librustc_mir / interpret / operand.rs
1 //! Functions concerning immediate values and operands, and reading from operands.
2 //! All high-level functions to read from memory work on operands as sources.
3
4 use std::convert::{TryFrom, TryInto};
5
6 use rustc::ty::layout::{
7     self, HasDataLayout, IntegerExt, LayoutOf, PrimitiveExt, Size, TyLayout, VariantIdx,
8 };
9 use rustc::{mir, ty};
10
11 use super::{InterpCx, MPlaceTy, Machine, MemPlace, Place, PlaceTy};
12 pub use rustc::mir::interpret::ScalarMaybeUndef;
13 use rustc::mir::interpret::{
14     sign_extend, truncate, AllocId, ConstValue, GlobalId, InterpResult, Pointer, Scalar,
15 };
16 use rustc_macros::HashStable;
17 use syntax::ast;
18
19 /// An `Immediate` represents a single immediate self-contained Rust value.
20 ///
21 /// For optimization of a few very common cases, there is also a representation for a pair of
22 /// primitive values (`ScalarPair`). It allows Miri to avoid making allocations for checked binary
23 /// operations and wide pointers. This idea was taken from rustc's codegen.
24 /// In particular, thanks to `ScalarPair`, arithmetic operations and casts can be entirely
25 /// defined on `Immediate`, and do not have to work with a `Place`.
26 #[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, Hash)]
27 pub enum Immediate<Tag = (), Id = AllocId> {
28     Scalar(ScalarMaybeUndef<Tag, Id>),
29     ScalarPair(ScalarMaybeUndef<Tag, Id>, ScalarMaybeUndef<Tag, Id>),
30 }
31
32 impl<Tag> From<ScalarMaybeUndef<Tag>> for Immediate<Tag> {
33     #[inline(always)]
34     fn from(val: ScalarMaybeUndef<Tag>) -> Self {
35         Immediate::Scalar(val)
36     }
37 }
38
39 impl<Tag> From<Scalar<Tag>> for Immediate<Tag> {
40     #[inline(always)]
41     fn from(val: Scalar<Tag>) -> Self {
42         Immediate::Scalar(val.into())
43     }
44 }
45
46 impl<Tag> From<Pointer<Tag>> for Immediate<Tag> {
47     #[inline(always)]
48     fn from(val: Pointer<Tag>) -> Self {
49         Immediate::Scalar(Scalar::from(val).into())
50     }
51 }
52
53 impl<'tcx, Tag> Immediate<Tag> {
54     pub fn new_slice(val: Scalar<Tag>, len: u64, cx: &impl HasDataLayout) -> Self {
55         Immediate::ScalarPair(
56             val.into(),
57             Scalar::from_uint(len, cx.data_layout().pointer_size).into(),
58         )
59     }
60
61     pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self {
62         Immediate::ScalarPair(val.into(), vtable.into())
63     }
64
65     #[inline]
66     pub fn to_scalar_or_undef(self) -> ScalarMaybeUndef<Tag> {
67         match self {
68             Immediate::Scalar(val) => val,
69             Immediate::ScalarPair(..) => bug!("Got a wide pointer where a scalar was expected"),
70         }
71     }
72
73     #[inline]
74     pub fn to_scalar(self) -> InterpResult<'tcx, Scalar<Tag>> {
75         self.to_scalar_or_undef().not_undef()
76     }
77
78     #[inline]
79     pub fn to_scalar_pair(self) -> InterpResult<'tcx, (Scalar<Tag>, Scalar<Tag>)> {
80         match self {
81             Immediate::Scalar(..) => bug!("Got a thin pointer where a scalar pair was expected"),
82             Immediate::ScalarPair(a, b) => Ok((a.not_undef()?, b.not_undef()?)),
83         }
84     }
85 }
86
87 // ScalarPair needs a type to interpret, so we often have an immediate and a type together
88 // as input for binary and cast operations.
89 #[derive(Copy, Clone, Debug)]
90 pub struct ImmTy<'tcx, Tag = ()> {
91     pub(crate) imm: Immediate<Tag>,
92     pub layout: TyLayout<'tcx>,
93 }
94
95 // `Tag: Copy` because some methods on `Scalar` consume them by value
96 impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
97     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98         match &self.imm {
99             Immediate::Scalar(ScalarMaybeUndef::Scalar(s)) => match s.to_bits(self.layout.size) {
100                 Ok(s) => {
101                     match self.layout.ty.kind {
102                         ty::Int(_) => {
103                             return write!(
104                                 fmt,
105                                 "{}",
106                                 super::sign_extend(s, self.layout.size) as i128,
107                             );
108                         }
109                         ty::Uint(_) => return write!(fmt, "{}", s),
110                         ty::Bool if s == 0 => return fmt.write_str("false"),
111                         ty::Bool if s == 1 => return fmt.write_str("true"),
112                         ty::Char => {
113                             if let Some(c) = u32::try_from(s).ok().and_then(std::char::from_u32) {
114                                 return write!(fmt, "{}", c);
115                             }
116                         }
117                         ty::Float(ast::FloatTy::F32) => {
118                             if let Ok(u) = u32::try_from(s) {
119                                 return write!(fmt, "{}", f32::from_bits(u));
120                             }
121                         }
122                         ty::Float(ast::FloatTy::F64) => {
123                             if let Ok(u) = u64::try_from(s) {
124                                 return write!(fmt, "{}", f64::from_bits(u));
125                             }
126                         }
127                         _ => {}
128                     }
129                     write!(fmt, "{:x}", s)
130                 }
131                 Err(_) => fmt.write_str("{pointer}"),
132             },
133             Immediate::Scalar(ScalarMaybeUndef::Undef) => fmt.write_str("{undef}"),
134             Immediate::ScalarPair(..) => fmt.write_str("{wide pointer or tuple}"),
135         }
136     }
137 }
138
139 impl<'tcx, Tag> ::std::ops::Deref for ImmTy<'tcx, Tag> {
140     type Target = Immediate<Tag>;
141     #[inline(always)]
142     fn deref(&self) -> &Immediate<Tag> {
143         &self.imm
144     }
145 }
146
147 /// An `Operand` is the result of computing a `mir::Operand`. It can be immediate,
148 /// or still in memory. The latter is an optimization, to delay reading that chunk of
149 /// memory and to avoid having to store arbitrary-sized data here.
150 #[derive(Copy, Clone, Debug, PartialEq, Eq, HashStable, Hash)]
151 pub enum Operand<Tag = (), Id = AllocId> {
152     Immediate(Immediate<Tag, Id>),
153     Indirect(MemPlace<Tag, Id>),
154 }
155
156 #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
157 pub struct OpTy<'tcx, Tag = ()> {
158     op: Operand<Tag>, // Keep this private; it helps enforce invariants.
159     pub layout: TyLayout<'tcx>,
160 }
161
162 impl<'tcx, Tag> ::std::ops::Deref for OpTy<'tcx, Tag> {
163     type Target = Operand<Tag>;
164     #[inline(always)]
165     fn deref(&self) -> &Operand<Tag> {
166         &self.op
167     }
168 }
169
170 impl<'tcx, Tag: Copy> From<MPlaceTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
171     #[inline(always)]
172     fn from(mplace: MPlaceTy<'tcx, Tag>) -> Self {
173         OpTy { op: Operand::Indirect(*mplace), layout: mplace.layout }
174     }
175 }
176
177 impl<'tcx, Tag> From<ImmTy<'tcx, Tag>> for OpTy<'tcx, Tag> {
178     #[inline(always)]
179     fn from(val: ImmTy<'tcx, Tag>) -> Self {
180         OpTy { op: Operand::Immediate(val.imm), layout: val.layout }
181     }
182 }
183
184 impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag> {
185     #[inline]
186     pub fn from_scalar(val: Scalar<Tag>, layout: TyLayout<'tcx>) -> Self {
187         ImmTy { imm: val.into(), layout }
188     }
189
190     #[inline]
191     pub fn try_from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Option<Self> {
192         Some(Self::from_scalar(Scalar::try_from_uint(i, layout.size)?, layout))
193     }
194     #[inline]
195     pub fn from_uint(i: impl Into<u128>, layout: TyLayout<'tcx>) -> Self {
196         Self::from_scalar(Scalar::from_uint(i, layout.size), layout)
197     }
198
199     #[inline]
200     pub fn try_from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Option<Self> {
201         Some(Self::from_scalar(Scalar::try_from_int(i, layout.size)?, layout))
202     }
203
204     #[inline]
205     pub fn from_int(i: impl Into<i128>, layout: TyLayout<'tcx>) -> Self {
206         Self::from_scalar(Scalar::from_int(i, layout.size), layout)
207     }
208
209     #[inline]
210     pub fn to_bits(self) -> InterpResult<'tcx, u128> {
211         self.to_scalar()?.to_bits(self.layout.size)
212     }
213 }
214
215 // Use the existing layout if given (but sanity check in debug mode),
216 // or compute the layout.
217 #[inline(always)]
218 pub(super) fn from_known_layout<'tcx>(
219     layout: Option<TyLayout<'tcx>>,
220     compute: impl FnOnce() -> InterpResult<'tcx, TyLayout<'tcx>>,
221 ) -> InterpResult<'tcx, TyLayout<'tcx>> {
222     match layout {
223         None => compute(),
224         Some(layout) => {
225             if cfg!(debug_assertions) {
226                 let layout2 = compute()?;
227                 assert_eq!(
228                     layout.details, layout2.details,
229                     "mismatch in layout of supposedly equal-layout types {:?} and {:?}",
230                     layout.ty, layout2.ty
231                 );
232             }
233             Ok(layout)
234         }
235     }
236 }
237
238 impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
239     /// Normalice `place.ptr` to a `Pointer` if this is a place and not a ZST.
240     /// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.
241     #[inline]
242     pub fn force_op_ptr(
243         &self,
244         op: OpTy<'tcx, M::PointerTag>,
245     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
246         match op.try_as_mplace(self) {
247             Ok(mplace) => Ok(self.force_mplace_ptr(mplace)?.into()),
248             Err(imm) => Ok(imm.into()), // Nothing to cast/force
249         }
250     }
251
252     /// Try reading an immediate in memory; this is interesting particularly for `ScalarPair`.
253     /// Returns `None` if the layout does not permit loading this as a value.
254     fn try_read_immediate_from_mplace(
255         &self,
256         mplace: MPlaceTy<'tcx, M::PointerTag>,
257     ) -> InterpResult<'tcx, Option<ImmTy<'tcx, M::PointerTag>>> {
258         if mplace.layout.is_unsized() {
259             // Don't touch unsized
260             return Ok(None);
261         }
262
263         let ptr = match self
264             .check_mplace_access(mplace, None)
265             .expect("places should be checked on creation")
266         {
267             Some(ptr) => ptr,
268             None => {
269                 return Ok(Some(ImmTy {
270                     // zero-sized type
271                     imm: Scalar::zst().into(),
272                     layout: mplace.layout,
273                 }));
274             }
275         };
276
277         match mplace.layout.abi {
278             layout::Abi::Scalar(..) => {
279                 let scalar = self.memory.get_raw(ptr.alloc_id)?.read_scalar(
280                     self,
281                     ptr,
282                     mplace.layout.size,
283                 )?;
284                 Ok(Some(ImmTy { imm: scalar.into(), layout: mplace.layout }))
285             }
286             layout::Abi::ScalarPair(ref a, ref b) => {
287                 // We checked `ptr_align` above, so all fields will have the alignment they need.
288                 // We would anyway check against `ptr_align.restrict_for_offset(b_offset)`,
289                 // which `ptr.offset(b_offset)` cannot possibly fail to satisfy.
290                 let (a, b) = (&a.value, &b.value);
291                 let (a_size, b_size) = (a.size(self), b.size(self));
292                 let a_ptr = ptr;
293                 let b_offset = a_size.align_to(b.align(self).abi);
294                 assert!(b_offset.bytes() > 0); // we later use the offset to tell apart the fields
295                 let b_ptr = ptr.offset(b_offset, self)?;
296                 let a_val = self.memory.get_raw(ptr.alloc_id)?.read_scalar(self, a_ptr, a_size)?;
297                 let b_val = self.memory.get_raw(ptr.alloc_id)?.read_scalar(self, b_ptr, b_size)?;
298                 Ok(Some(ImmTy { imm: Immediate::ScalarPair(a_val, b_val), layout: mplace.layout }))
299             }
300             _ => Ok(None),
301         }
302     }
303
304     /// Try returning an immediate for the operand.
305     /// If the layout does not permit loading this as an immediate, return where in memory
306     /// we can find the data.
307     /// Note that for a given layout, this operation will either always fail or always
308     /// succeed!  Whether it succeeds depends on whether the layout can be represented
309     /// in a `Immediate`, not on which data is stored there currently.
310     pub(crate) fn try_read_immediate(
311         &self,
312         src: OpTy<'tcx, M::PointerTag>,
313     ) -> InterpResult<'tcx, Result<ImmTy<'tcx, M::PointerTag>, MPlaceTy<'tcx, M::PointerTag>>> {
314         Ok(match src.try_as_mplace(self) {
315             Ok(mplace) => {
316                 if let Some(val) = self.try_read_immediate_from_mplace(mplace)? {
317                     Ok(val)
318                 } else {
319                     Err(mplace)
320                 }
321             }
322             Err(val) => Ok(val),
323         })
324     }
325
326     /// Read an immediate from a place, asserting that that is possible with the given layout.
327     #[inline(always)]
328     pub fn read_immediate(
329         &self,
330         op: OpTy<'tcx, M::PointerTag>,
331     ) -> InterpResult<'tcx, ImmTy<'tcx, M::PointerTag>> {
332         if let Ok(imm) = self.try_read_immediate(op)? {
333             Ok(imm)
334         } else {
335             bug!("primitive read failed for type: {:?}", op.layout.ty);
336         }
337     }
338
339     /// Read a scalar from a place
340     pub fn read_scalar(
341         &self,
342         op: OpTy<'tcx, M::PointerTag>,
343     ) -> InterpResult<'tcx, ScalarMaybeUndef<M::PointerTag>> {
344         Ok(self.read_immediate(op)?.to_scalar_or_undef())
345     }
346
347     // Turn the wide MPlace into a string (must already be dereferenced!)
348     pub fn read_str(&self, mplace: MPlaceTy<'tcx, M::PointerTag>) -> InterpResult<'tcx, &str> {
349         let len = mplace.len(self)?;
350         let bytes = self.memory.read_bytes(mplace.ptr, Size::from_bytes(len as u64))?;
351         let str = ::std::str::from_utf8(bytes)
352             .map_err(|err| err_unsup!(ValidationFailure(err.to_string())))?;
353         Ok(str)
354     }
355
356     /// Projection functions
357     pub fn operand_field(
358         &self,
359         op: OpTy<'tcx, M::PointerTag>,
360         field: u64,
361     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
362         let base = match op.try_as_mplace(self) {
363             Ok(mplace) => {
364                 // The easy case
365                 let field = self.mplace_field(mplace, field)?;
366                 return Ok(field.into());
367             }
368             Err(value) => value,
369         };
370
371         let field = field.try_into().unwrap();
372         let field_layout = op.layout.field(self, field)?;
373         if field_layout.is_zst() {
374             let immediate = Scalar::zst().into();
375             return Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout });
376         }
377         let offset = op.layout.fields.offset(field);
378         let immediate = match *base {
379             // the field covers the entire type
380             _ if offset.bytes() == 0 && field_layout.size == op.layout.size => *base,
381             // extract fields from types with `ScalarPair` ABI
382             Immediate::ScalarPair(a, b) => {
383                 let val = if offset.bytes() == 0 { a } else { b };
384                 Immediate::from(val)
385             }
386             Immediate::Scalar(val) => {
387                 bug!("field access on non aggregate {:#?}, {:#?}", val, op.layout)
388             }
389         };
390         Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout })
391     }
392
393     pub fn operand_downcast(
394         &self,
395         op: OpTy<'tcx, M::PointerTag>,
396         variant: VariantIdx,
397     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
398         // Downcasts only change the layout
399         Ok(match op.try_as_mplace(self) {
400             Ok(mplace) => self.mplace_downcast(mplace, variant)?.into(),
401             Err(..) => {
402                 let layout = op.layout.for_variant(self, variant);
403                 OpTy { layout, ..op }
404             }
405         })
406     }
407
408     pub fn operand_projection(
409         &self,
410         base: OpTy<'tcx, M::PointerTag>,
411         proj_elem: &mir::PlaceElem<'tcx>,
412     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
413         use rustc::mir::ProjectionElem::*;
414         Ok(match *proj_elem {
415             Field(field, _) => self.operand_field(base, field.index() as u64)?,
416             Downcast(_, variant) => self.operand_downcast(base, variant)?,
417             Deref => self.deref_operand(base)?.into(),
418             Subslice { .. } | ConstantIndex { .. } | Index(_) => {
419                 // The rest should only occur as mplace, we do not use Immediates for types
420                 // allowing such operations.  This matches place_projection forcing an allocation.
421                 let mplace = base.assert_mem_place(self);
422                 self.mplace_projection(mplace, proj_elem)?.into()
423             }
424         })
425     }
426
427     /// This is used by [priroda](https://github.com/oli-obk/priroda) to get an OpTy from a local
428     pub fn access_local(
429         &self,
430         frame: &super::Frame<'mir, 'tcx, M::PointerTag, M::FrameExtra>,
431         local: mir::Local,
432         layout: Option<TyLayout<'tcx>>,
433     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
434         assert_ne!(local, mir::RETURN_PLACE);
435         let layout = self.layout_of_local(frame, local, layout)?;
436         let op = if layout.is_zst() {
437             // Do not read from ZST, they might not be initialized
438             Operand::Immediate(Scalar::zst().into())
439         } else {
440             M::access_local(&self, frame, local)?
441         };
442         Ok(OpTy { op, layout })
443     }
444
445     /// Every place can be read from, so we can turn them into an operand
446     #[inline(always)]
447     pub fn place_to_op(
448         &self,
449         place: PlaceTy<'tcx, M::PointerTag>,
450     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
451         let op = match *place {
452             Place::Ptr(mplace) => Operand::Indirect(mplace),
453             Place::Local { frame, local } => *self.access_local(&self.stack[frame], local, None)?,
454         };
455         Ok(OpTy { op, layout: place.layout })
456     }
457
458     // Evaluate a place with the goal of reading from it.  This lets us sometimes
459     // avoid allocations.
460     pub fn eval_place_to_op(
461         &self,
462         place: &mir::Place<'tcx>,
463         layout: Option<TyLayout<'tcx>>,
464     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
465         use rustc::mir::PlaceBase;
466
467         let base_op = match &place.base {
468             PlaceBase::Local(mir::RETURN_PLACE) => throw_unsup!(ReadFromReturnPointer),
469             PlaceBase::Local(local) => {
470                 // Do not use the layout passed in as argument if the base we are looking at
471                 // here is not the entire place.
472                 // FIXME use place_projection.is_empty() when is available
473                 let layout = if place.projection.is_empty() { layout } else { None };
474
475                 self.access_local(self.frame(), *local, layout)?
476             }
477             PlaceBase::Static(place_static) => self.eval_static_to_mplace(&place_static)?.into(),
478         };
479
480         let op = place
481             .projection
482             .iter()
483             .try_fold(base_op, |op, elem| self.operand_projection(op, elem))?;
484
485         trace!("eval_place_to_op: got {:?}", *op);
486         Ok(op)
487     }
488
489     /// Evaluate the operand, returning a place where you can then find the data.
490     /// If you already know the layout, you can save two table lookups
491     /// by passing it in here.
492     pub fn eval_operand(
493         &self,
494         mir_op: &mir::Operand<'tcx>,
495         layout: Option<TyLayout<'tcx>>,
496     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
497         use rustc::mir::Operand::*;
498         let op = match *mir_op {
499             // FIXME: do some more logic on `move` to invalidate the old location
500             Copy(ref place) | Move(ref place) => self.eval_place_to_op(place, layout)?,
501
502             Constant(ref constant) => {
503                 let val = self.subst_from_frame_and_normalize_erasing_regions(constant.literal);
504                 self.eval_const_to_op(val, layout)?
505             }
506         };
507         trace!("{:?}: {:?}", mir_op, *op);
508         Ok(op)
509     }
510
511     /// Evaluate a bunch of operands at once
512     pub(super) fn eval_operands(
513         &self,
514         ops: &[mir::Operand<'tcx>],
515     ) -> InterpResult<'tcx, Vec<OpTy<'tcx, M::PointerTag>>> {
516         ops.into_iter().map(|op| self.eval_operand(op, None)).collect()
517     }
518
519     // Used when the miri-engine runs into a constant and for extracting information from constants
520     // in patterns via the `const_eval` module
521     /// The `val` and `layout` are assumed to already be in our interpreter
522     /// "universe" (param_env).
523     crate fn eval_const_to_op(
524         &self,
525         val: &'tcx ty::Const<'tcx>,
526         layout: Option<TyLayout<'tcx>>,
527     ) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
528         let tag_scalar = |scalar| match scalar {
529             Scalar::Ptr(ptr) => Scalar::Ptr(self.tag_static_base_pointer(ptr)),
530             Scalar::Raw { data, size } => Scalar::Raw { data, size },
531         };
532         // Early-return cases.
533         let val_val = match val.val {
534             ty::ConstKind::Param(_) => throw_inval!(TooGeneric),
535             ty::ConstKind::Unevaluated(def_id, substs) => {
536                 let instance = self.resolve(def_id, substs)?;
537                 // We use `const_eval` here and `const_eval_raw` elsewhere in mir interpretation.
538                 // The reason we use `const_eval_raw` everywhere else is to prevent cycles during
539                 // validation, because validation automatically reads through any references, thus
540                 // potentially requiring the current static to be evaluated again. This is not a
541                 // problem here, because we are building an operand which means an actual read is
542                 // happening.
543                 // FIXME(oli-obk): eliminate all the `const_eval_raw` usages when we get rid of
544                 // `StaticKind` once and for all.
545                 return self.const_eval(GlobalId { instance, promoted: None });
546             }
547             ty::ConstKind::Infer(..)
548             | ty::ConstKind::Bound(..)
549             | ty::ConstKind::Placeholder(..) => {
550                 bug!("eval_const_to_op: Unexpected ConstKind {:?}", val)
551             }
552             ty::ConstKind::Value(val_val) => val_val,
553         };
554         // Other cases need layout.
555         let layout = from_known_layout(layout, || self.layout_of(val.ty))?;
556         let op = match val_val {
557             ConstValue::ByRef { alloc, offset } => {
558                 let id = self.tcx.alloc_map.lock().create_memory_alloc(alloc);
559                 // We rely on mutability being set correctly in that allocation to prevent writes
560                 // where none should happen.
561                 let ptr = self.tag_static_base_pointer(Pointer::new(id, offset));
562                 Operand::Indirect(MemPlace::from_ptr(ptr, layout.align.abi))
563             }
564             ConstValue::Scalar(x) => Operand::Immediate(tag_scalar(x).into()),
565             ConstValue::Slice { data, start, end } => {
566                 // We rely on mutability being set correctly in `data` to prevent writes
567                 // where none should happen.
568                 let ptr = Pointer::new(
569                     self.tcx.alloc_map.lock().create_memory_alloc(data),
570                     Size::from_bytes(start as u64), // offset: `start`
571                 );
572                 Operand::Immediate(Immediate::new_slice(
573                     self.tag_static_base_pointer(ptr).into(),
574                     (end - start) as u64, // len: `end - start`
575                     self,
576                 ))
577             }
578         };
579         Ok(OpTy { op, layout })
580     }
581
582     /// Read discriminant, return the runtime value as well as the variant index.
583     pub fn read_discriminant(
584         &self,
585         rval: OpTy<'tcx, M::PointerTag>,
586     ) -> InterpResult<'tcx, (u128, VariantIdx)> {
587         trace!("read_discriminant_value {:#?}", rval.layout);
588
589         let (discr_layout, discr_kind, discr_index) = match rval.layout.variants {
590             layout::Variants::Single { index } => {
591                 let discr_val = rval
592                     .layout
593                     .ty
594                     .discriminant_for_variant(*self.tcx, index)
595                     .map_or(index.as_u32() as u128, |discr| discr.val);
596                 return Ok((discr_val, index));
597             }
598             layout::Variants::Multiple {
599                 discr: ref discr_layout,
600                 ref discr_kind,
601                 discr_index,
602                 ..
603             } => (discr_layout, discr_kind, discr_index),
604         };
605
606         // read raw discriminant value
607         let discr_op = self.operand_field(rval, discr_index as u64)?;
608         let discr_val = self.read_immediate(discr_op)?;
609         let raw_discr = discr_val.to_scalar_or_undef();
610         trace!("discr value: {:?}", raw_discr);
611         // post-process
612         Ok(match *discr_kind {
613             layout::DiscriminantKind::Tag => {
614                 let bits_discr = raw_discr
615                     .not_undef()
616                     .and_then(|raw_discr| self.force_bits(raw_discr, discr_val.layout.size))
617                     .map_err(|_| err_ub!(InvalidDiscriminant(raw_discr.erase_tag())))?;
618                 let real_discr = if discr_val.layout.ty.is_signed() {
619                     // going from layout tag type to typeck discriminant type
620                     // requires first sign extending with the discriminant layout
621                     let sexted = sign_extend(bits_discr, discr_val.layout.size) as i128;
622                     // and then zeroing with the typeck discriminant type
623                     let discr_ty = rval
624                         .layout
625                         .ty
626                         .ty_adt_def()
627                         .expect("tagged layout corresponds to adt")
628                         .repr
629                         .discr_type();
630                     let size = layout::Integer::from_attr(self, discr_ty).size();
631                     let truncatee = sexted as u128;
632                     truncate(truncatee, size)
633                 } else {
634                     bits_discr
635                 };
636                 // Make sure we catch invalid discriminants
637                 let index = match rval.layout.ty.kind {
638                     ty::Adt(adt, _) => {
639                         adt.discriminants(self.tcx.tcx).find(|(_, var)| var.val == real_discr)
640                     }
641                     ty::Generator(def_id, substs, _) => {
642                         let substs = substs.as_generator();
643                         substs
644                             .discriminants(def_id, self.tcx.tcx)
645                             .find(|(_, var)| var.val == real_discr)
646                     }
647                     _ => bug!("tagged layout for non-adt non-generator"),
648                 }
649                 .ok_or_else(|| err_ub!(InvalidDiscriminant(raw_discr.erase_tag())))?;
650                 (real_discr, index.0)
651             }
652             layout::DiscriminantKind::Niche {
653                 dataful_variant,
654                 ref niche_variants,
655                 niche_start,
656             } => {
657                 let variants_start = niche_variants.start().as_u32();
658                 let variants_end = niche_variants.end().as_u32();
659                 let raw_discr = raw_discr
660                     .not_undef()
661                     .map_err(|_| err_ub!(InvalidDiscriminant(ScalarMaybeUndef::Undef)))?;
662                 match raw_discr.to_bits_or_ptr(discr_val.layout.size, self) {
663                     Err(ptr) => {
664                         // The niche must be just 0 (which an inbounds pointer value never is)
665                         let ptr_valid = niche_start == 0
666                             && variants_start == variants_end
667                             && !self.memory.ptr_may_be_null(ptr);
668                         if !ptr_valid {
669                             throw_ub!(InvalidDiscriminant(raw_discr.erase_tag().into()))
670                         }
671                         (dataful_variant.as_u32() as u128, dataful_variant)
672                     }
673                     Ok(raw_discr) => {
674                         // We need to use machine arithmetic to get the relative variant idx:
675                         // variant_index_relative = discr_val - niche_start_val
676                         let discr_layout =
677                             self.layout_of(discr_layout.value.to_int_ty(*self.tcx))?;
678                         let discr_val = ImmTy::from_uint(raw_discr, discr_layout);
679                         let niche_start_val = ImmTy::from_uint(niche_start, discr_layout);
680                         let variant_index_relative_val =
681                             self.binary_op(mir::BinOp::Sub, discr_val, niche_start_val)?;
682                         let variant_index_relative = variant_index_relative_val
683                             .to_scalar()?
684                             .assert_bits(discr_val.layout.size);
685                         // Check if this is in the range that indicates an actual discriminant.
686                         if variant_index_relative <= u128::from(variants_end - variants_start) {
687                             let variant_index_relative = u32::try_from(variant_index_relative)
688                                 .expect("we checked that this fits into a u32");
689                             // Then computing the absolute variant idx should not overflow any more.
690                             let variant_index = variants_start
691                                 .checked_add(variant_index_relative)
692                                 .expect("oveflow computing absolute variant idx");
693                             assert!(
694                                 (variant_index as usize)
695                                     < rval
696                                         .layout
697                                         .ty
698                                         .ty_adt_def()
699                                         .expect("tagged layout for non adt")
700                                         .variants
701                                         .len()
702                             );
703                             (u128::from(variant_index), VariantIdx::from_u32(variant_index))
704                         } else {
705                             (u128::from(dataful_variant.as_u32()), dataful_variant)
706                         }
707                     }
708                 }
709             }
710         })
711     }
712 }