1 use std::convert::{TryFrom, TryInto};
2 use std::fmt;
3 
4 use rustc_apfloat::{
5     ieee::{Double, Single},
6     Float,
7 };
8 use rustc_macros::HashStable;
9 use rustc_target::abi::{HasDataLayout, Size};
10 
11 use crate::ty::{Lift, ParamEnv, ScalarInt, Ty, TyCtxt};
12 
13 use super::{
14     AllocId, AllocRange, Allocation, InterpResult, Pointer, PointerArithmetic, Provenance,
15 };
16 
17 /// Represents the result of const evaluation via the `eval_to_allocation` query.
18 #[derive(Copy, Clone, HashStable, TyEncodable, TyDecodable, Debug, Hash, Eq, PartialEq)]
19 pub struct ConstAlloc<'tcx> {
20     // the value lives here, at offset 0, and that allocation definitely is an `AllocKind::Memory`
21     // (so you can use `AllocMap::unwrap_memory`).
22     pub alloc_id: AllocId,
23     pub ty: Ty<'tcx>,
24 }
25 
26 /// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
27 /// array length computations, enum discriminants and the pattern matching logic.
28 #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
29 #[derive(HashStable)]
30 pub enum ConstValue<'tcx> {
31     /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
32     ///
33     /// Not using the enum `Value` to encode that this must not be `Uninit`.
34     Scalar(Scalar),
35 
36     /// Used only for `&[u8]` and `&str`
37     Slice { data: &'tcx Allocation, start: usize, end: usize },
38 
39     /// A value not represented/representable by `Scalar` or `Slice`
40     ByRef {
41         /// The backing memory of the value, may contain more memory than needed for just the value
42         /// in order to share `Allocation`s between values
43         alloc: &'tcx Allocation,
44         /// Offset into `alloc`
45         offset: Size,
46     },
47 }
48 
49 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
50 static_assert_size!(ConstValue<'_>, 32);
51 
52 impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> {
53     type Lifted = ConstValue<'tcx>;
lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ConstValue<'tcx>>54     fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<ConstValue<'tcx>> {
55         Some(match self {
56             ConstValue::Scalar(s) => ConstValue::Scalar(s),
57             ConstValue::Slice { data, start, end } => {
58                 ConstValue::Slice { data: tcx.lift(data)?, start, end }
59             }
60             ConstValue::ByRef { alloc, offset } => {
61                 ConstValue::ByRef { alloc: tcx.lift(alloc)?, offset }
62             }
63         })
64     }
65 }
66 
67 impl<'tcx> ConstValue<'tcx> {
68     #[inline]
try_to_scalar(&self) -> Option<Scalar<AllocId>>69     pub fn try_to_scalar(&self) -> Option<Scalar<AllocId>> {
70         match *self {
71             ConstValue::ByRef { .. } | ConstValue::Slice { .. } => None,
72             ConstValue::Scalar(val) => Some(val),
73         }
74     }
75 
try_to_scalar_int(&self) -> Option<ScalarInt>76     pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
77         Some(self.try_to_scalar()?.assert_int())
78     }
79 
try_to_bits(&self, size: Size) -> Option<u128>80     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
81         self.try_to_scalar_int()?.to_bits(size).ok()
82     }
83 
try_to_bool(&self) -> Option<bool>84     pub fn try_to_bool(&self) -> Option<bool> {
85         self.try_to_scalar_int()?.try_into().ok()
86     }
87 
try_to_machine_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64>88     pub fn try_to_machine_usize(&self, tcx: TyCtxt<'tcx>) -> Option<u64> {
89         self.try_to_scalar_int()?.try_to_machine_usize(tcx).ok()
90     }
91 
try_to_bits_for_ty( &self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>, ) -> Option<u128>92     pub fn try_to_bits_for_ty(
93         &self,
94         tcx: TyCtxt<'tcx>,
95         param_env: ParamEnv<'tcx>,
96         ty: Ty<'tcx>,
97     ) -> Option<u128> {
98         let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
99         self.try_to_bits(size)
100     }
101 
from_bool(b: bool) -> Self102     pub fn from_bool(b: bool) -> Self {
103         ConstValue::Scalar(Scalar::from_bool(b))
104     }
105 
from_u64(i: u64) -> Self106     pub fn from_u64(i: u64) -> Self {
107         ConstValue::Scalar(Scalar::from_u64(i))
108     }
109 
from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self110     pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
111         ConstValue::Scalar(Scalar::from_machine_usize(i, cx))
112     }
113 }
114 
115 /// A `Scalar` represents an immediate, primitive value existing outside of a
116 /// `memory::Allocation`. It is in many ways like a small chunk of an `Allocation`, up to 16 bytes in
117 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
118 /// of a simple value or a pointer into another `Allocation`
119 ///
120 /// These variants would be private if there was a convenient way to achieve that in Rust.
121 /// Do *not* match on a `Scalar`! Use the various `to_*` methods instead.
122 #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, TyEncodable, TyDecodable, Hash)]
123 #[derive(HashStable)]
124 pub enum Scalar<Tag = AllocId> {
125     /// The raw bytes of a simple value.
126     Int(ScalarInt),
127 
128     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
129     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
130     /// relocation and its associated offset together as a `Pointer` here.
131     ///
132     /// We also store the size of the pointer, such that a `Scalar` always knows how big it is.
133     /// The size is always the pointer size of the current target, but this is not information
134     /// that we always have readily available.
135     Ptr(Pointer<Tag>, u8),
136 }
137 
138 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
139 static_assert_size!(Scalar, 24);
140 
141 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
142 // all the Miri types.
143 impl<Tag: Provenance> fmt::Debug for Scalar<Tag> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result144     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145         match self {
146             Scalar::Ptr(ptr, _size) => write!(f, "{:?}", ptr),
147             Scalar::Int(int) => write!(f, "{:?}", int),
148         }
149     }
150 }
151 
152 impl<Tag: Provenance> fmt::Display for Scalar<Tag> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result153     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154         match self {
155             Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr),
156             Scalar::Int(int) => write!(f, "{:?}", int),
157         }
158     }
159 }
160 
161 impl<Tag> From<Single> for Scalar<Tag> {
162     #[inline(always)]
from(f: Single) -> Self163     fn from(f: Single) -> Self {
164         Scalar::from_f32(f)
165     }
166 }
167 
168 impl<Tag> From<Double> for Scalar<Tag> {
169     #[inline(always)]
from(f: Double) -> Self170     fn from(f: Double) -> Self {
171         Scalar::from_f64(f)
172     }
173 }
174 
175 impl<Tag> From<ScalarInt> for Scalar<Tag> {
176     #[inline(always)]
from(ptr: ScalarInt) -> Self177     fn from(ptr: ScalarInt) -> Self {
178         Scalar::Int(ptr)
179     }
180 }
181 
182 impl<Tag> Scalar<Tag> {
183     pub const ZST: Self = Scalar::Int(ScalarInt::ZST);
184 
185     #[inline(always)]
from_pointer(ptr: Pointer<Tag>, cx: &impl HasDataLayout) -> Self186     pub fn from_pointer(ptr: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
187         Scalar::Ptr(ptr, u8::try_from(cx.pointer_size().bytes()).unwrap())
188     }
189 
190     /// Create a Scalar from a pointer with an `Option<_>` tag (where `None` represents a plain integer).
from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self191     pub fn from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self {
192         match ptr.into_parts() {
193             (Some(tag), offset) => Scalar::from_pointer(Pointer::new(tag, offset), cx),
194             (None, offset) => {
195                 Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
196             }
197         }
198     }
199 
200     #[inline]
null_ptr(cx: &impl HasDataLayout) -> Self201     pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
202         Scalar::Int(ScalarInt::null(cx.pointer_size()))
203     }
204 
205     #[inline]
from_bool(b: bool) -> Self206     pub fn from_bool(b: bool) -> Self {
207         Scalar::Int(b.into())
208     }
209 
210     #[inline]
from_char(c: char) -> Self211     pub fn from_char(c: char) -> Self {
212         Scalar::Int(c.into())
213     }
214 
215     #[inline]
try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self>216     pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
217         ScalarInt::try_from_uint(i, size).map(Scalar::Int)
218     }
219 
220     #[inline]
from_uint(i: impl Into<u128>, size: Size) -> Self221     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
222         let i = i.into();
223         Self::try_from_uint(i, size)
224             .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
225     }
226 
227     #[inline]
from_u8(i: u8) -> Self228     pub fn from_u8(i: u8) -> Self {
229         Scalar::Int(i.into())
230     }
231 
232     #[inline]
from_u16(i: u16) -> Self233     pub fn from_u16(i: u16) -> Self {
234         Scalar::Int(i.into())
235     }
236 
237     #[inline]
from_u32(i: u32) -> Self238     pub fn from_u32(i: u32) -> Self {
239         Scalar::Int(i.into())
240     }
241 
242     #[inline]
from_u64(i: u64) -> Self243     pub fn from_u64(i: u64) -> Self {
244         Scalar::Int(i.into())
245     }
246 
247     #[inline]
from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self248     pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
249         Self::from_uint(i, cx.data_layout().pointer_size)
250     }
251 
252     #[inline]
try_from_int(i: impl Into<i128>, size: Size) -> Option<Self>253     pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
254         ScalarInt::try_from_int(i, size).map(Scalar::Int)
255     }
256 
257     #[inline]
from_int(i: impl Into<i128>, size: Size) -> Self258     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
259         let i = i.into();
260         Self::try_from_int(i, size)
261             .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
262     }
263 
264     #[inline]
from_i32(i: i32) -> Self265     pub fn from_i32(i: i32) -> Self {
266         Self::from_int(i, Size::from_bits(32))
267     }
268 
269     #[inline]
from_i64(i: i64) -> Self270     pub fn from_i64(i: i64) -> Self {
271         Self::from_int(i, Size::from_bits(64))
272     }
273 
274     #[inline]
from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self275     pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
276         Self::from_int(i, cx.data_layout().pointer_size)
277     }
278 
279     #[inline]
from_f32(f: Single) -> Self280     pub fn from_f32(f: Single) -> Self {
281         Scalar::Int(f.into())
282     }
283 
284     #[inline]
from_f64(f: Double) -> Self285     pub fn from_f64(f: Double) -> Self {
286         Scalar::Int(f.into())
287     }
288 
289     /// This is almost certainly not the method you want!  You should dispatch on the type
290     /// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
291     ///
292     /// This method only exists for the benefit of low-level operations that truly need to treat the
293     /// scalar in whatever form it is.
294     #[inline]
to_bits_or_ptr_internal(self, target_size: Size) -> Result<u128, Pointer<Tag>>295     pub fn to_bits_or_ptr_internal(self, target_size: Size) -> Result<u128, Pointer<Tag>> {
296         assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
297         match self {
298             Scalar::Int(int) => Ok(int.assert_bits(target_size)),
299             Scalar::Ptr(ptr, sz) => {
300                 assert_eq!(target_size.bytes(), u64::from(sz));
301                 Err(ptr)
302             }
303         }
304     }
305 }
306 
307 impl<'tcx, Tag: Provenance> Scalar<Tag> {
308     /// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
309     /// likely want to use instead.
310     ///
311     /// Will perform ptr-to-int casts if needed and possible.
312     /// If that fails, we know the offset is relative, so we return an "erased" Scalar
313     /// (which is useful for error messages but not much else).
314     #[inline]
try_to_int(self) -> Result<ScalarInt, Scalar<AllocId>>315     pub fn try_to_int(self) -> Result<ScalarInt, Scalar<AllocId>> {
316         match self {
317             Scalar::Int(int) => Ok(int),
318             Scalar::Ptr(ptr, sz) => {
319                 if Tag::OFFSET_IS_ADDR {
320                     Ok(ScalarInt::try_from_uint(ptr.offset.bytes(), Size::from_bytes(sz)).unwrap())
321                 } else {
322                     // We know `offset` is relative, since `OFFSET_IS_ADDR == false`.
323                     let (tag, offset) = ptr.into_parts();
324                     Err(Scalar::Ptr(Pointer::new(tag.get_alloc_id(), offset), sz))
325                 }
326             }
327         }
328     }
329 
330     #[inline(always)]
assert_int(self) -> ScalarInt331     pub fn assert_int(self) -> ScalarInt {
332         self.try_to_int().unwrap()
333     }
334 
335     /// This throws UB (instead of ICEing) on a size mismatch since size mismatches can arise in
336     /// Miri when someone declares a function that we shim (such as `malloc`) with a wrong type.
337     #[inline]
to_bits(self, target_size: Size) -> InterpResult<'tcx, u128>338     pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
339         assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
340         self.try_to_int().map_err(|_| err_unsup!(ReadPointerAsBytes))?.to_bits(target_size).map_err(
341             |size| {
342                 err_ub!(ScalarSizeMismatch {
343                     target_size: target_size.bytes(),
344                     data_size: size.bytes(),
345                 })
346                 .into()
347             },
348         )
349     }
350 
351     #[inline(always)]
assert_bits(self, target_size: Size) -> u128352     pub fn assert_bits(self, target_size: Size) -> u128 {
353         self.to_bits(target_size).unwrap()
354     }
355 
to_bool(self) -> InterpResult<'tcx, bool>356     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
357         let val = self.to_u8()?;
358         match val {
359             0 => Ok(false),
360             1 => Ok(true),
361             _ => throw_ub!(InvalidBool(val)),
362         }
363     }
364 
to_char(self) -> InterpResult<'tcx, char>365     pub fn to_char(self) -> InterpResult<'tcx, char> {
366         let val = self.to_u32()?;
367         match std::char::from_u32(val) {
368             Some(c) => Ok(c),
369             None => throw_ub!(InvalidChar(val)),
370         }
371     }
372 
373     #[inline]
to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128>374     fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> {
375         let sz = Size::from_bits(bits);
376         self.to_bits(sz)
377     }
378 
379     /// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
to_u8(self) -> InterpResult<'static, u8>380     pub fn to_u8(self) -> InterpResult<'static, u8> {
381         self.to_unsigned_with_bit_width(8).map(|v| u8::try_from(v).unwrap())
382     }
383 
384     /// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
to_u16(self) -> InterpResult<'static, u16>385     pub fn to_u16(self) -> InterpResult<'static, u16> {
386         self.to_unsigned_with_bit_width(16).map(|v| u16::try_from(v).unwrap())
387     }
388 
389     /// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
to_u32(self) -> InterpResult<'static, u32>390     pub fn to_u32(self) -> InterpResult<'static, u32> {
391         self.to_unsigned_with_bit_width(32).map(|v| u32::try_from(v).unwrap())
392     }
393 
394     /// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
to_u64(self) -> InterpResult<'static, u64>395     pub fn to_u64(self) -> InterpResult<'static, u64> {
396         self.to_unsigned_with_bit_width(64).map(|v| u64::try_from(v).unwrap())
397     }
398 
399     /// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
to_u128(self) -> InterpResult<'static, u128>400     pub fn to_u128(self) -> InterpResult<'static, u128> {
401         self.to_unsigned_with_bit_width(128)
402     }
403 
to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64>404     pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
405         let b = self.to_bits(cx.data_layout().pointer_size)?;
406         Ok(u64::try_from(b).unwrap())
407     }
408 
409     #[inline]
to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128>410     fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> {
411         let sz = Size::from_bits(bits);
412         let b = self.to_bits(sz)?;
413         Ok(sz.sign_extend(b) as i128)
414     }
415 
416     /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
to_i8(self) -> InterpResult<'static, i8>417     pub fn to_i8(self) -> InterpResult<'static, i8> {
418         self.to_signed_with_bit_width(8).map(|v| i8::try_from(v).unwrap())
419     }
420 
421     /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
to_i16(self) -> InterpResult<'static, i16>422     pub fn to_i16(self) -> InterpResult<'static, i16> {
423         self.to_signed_with_bit_width(16).map(|v| i16::try_from(v).unwrap())
424     }
425 
426     /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
to_i32(self) -> InterpResult<'static, i32>427     pub fn to_i32(self) -> InterpResult<'static, i32> {
428         self.to_signed_with_bit_width(32).map(|v| i32::try_from(v).unwrap())
429     }
430 
431     /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
to_i64(self) -> InterpResult<'static, i64>432     pub fn to_i64(self) -> InterpResult<'static, i64> {
433         self.to_signed_with_bit_width(64).map(|v| i64::try_from(v).unwrap())
434     }
435 
436     /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
to_i128(self) -> InterpResult<'static, i128>437     pub fn to_i128(self) -> InterpResult<'static, i128> {
438         self.to_signed_with_bit_width(128)
439     }
440 
to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64>441     pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
442         let sz = cx.data_layout().pointer_size;
443         let b = self.to_bits(sz)?;
444         let b = sz.sign_extend(b) as i128;
445         Ok(i64::try_from(b).unwrap())
446     }
447 
448     #[inline]
to_f32(self) -> InterpResult<'static, Single>449     pub fn to_f32(self) -> InterpResult<'static, Single> {
450         // Going through `u32` to check size and truncation.
451         Ok(Single::from_bits(self.to_u32()?.into()))
452     }
453 
454     #[inline]
to_f64(self) -> InterpResult<'static, Double>455     pub fn to_f64(self) -> InterpResult<'static, Double> {
456         // Going through `u64` to check size and truncation.
457         Ok(Double::from_bits(self.to_u64()?.into()))
458     }
459 }
460 
461 #[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
462 pub enum ScalarMaybeUninit<Tag = AllocId> {
463     Scalar(Scalar<Tag>),
464     Uninit,
465 }
466 
467 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
468 static_assert_size!(ScalarMaybeUninit, 24);
469 
470 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUninit<Tag> {
471     #[inline(always)]
from(s: Scalar<Tag>) -> Self472     fn from(s: Scalar<Tag>) -> Self {
473         ScalarMaybeUninit::Scalar(s)
474     }
475 }
476 
477 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
478 // all the Miri types.
479 impl<Tag: Provenance> fmt::Debug for ScalarMaybeUninit<Tag> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result480     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
481         match self {
482             ScalarMaybeUninit::Uninit => write!(f, "<uninitialized>"),
483             ScalarMaybeUninit::Scalar(s) => write!(f, "{:?}", s),
484         }
485     }
486 }
487 
488 impl<Tag: Provenance> fmt::Display for ScalarMaybeUninit<Tag> {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result489     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
490         match self {
491             ScalarMaybeUninit::Uninit => write!(f, "uninitialized bytes"),
492             ScalarMaybeUninit::Scalar(s) => write!(f, "{}", s),
493         }
494     }
495 }
496 
497 impl<Tag> ScalarMaybeUninit<Tag> {
498     #[inline]
from_pointer(ptr: Pointer<Tag>, cx: &impl HasDataLayout) -> Self499     pub fn from_pointer(ptr: Pointer<Tag>, cx: &impl HasDataLayout) -> Self {
500         ScalarMaybeUninit::Scalar(Scalar::from_pointer(ptr, cx))
501     }
502 
503     #[inline]
from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self504     pub fn from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self {
505         ScalarMaybeUninit::Scalar(Scalar::from_maybe_pointer(ptr, cx))
506     }
507 
508     #[inline]
check_init(self) -> InterpResult<'static, Scalar<Tag>>509     pub fn check_init(self) -> InterpResult<'static, Scalar<Tag>> {
510         match self {
511             ScalarMaybeUninit::Scalar(scalar) => Ok(scalar),
512             ScalarMaybeUninit::Uninit => throw_ub!(InvalidUninitBytes(None)),
513         }
514     }
515 }
516 
517 impl<'tcx, Tag: Provenance> ScalarMaybeUninit<Tag> {
518     #[inline(always)]
to_bool(self) -> InterpResult<'tcx, bool>519     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
520         self.check_init()?.to_bool()
521     }
522 
523     #[inline(always)]
to_char(self) -> InterpResult<'tcx, char>524     pub fn to_char(self) -> InterpResult<'tcx, char> {
525         self.check_init()?.to_char()
526     }
527 
528     #[inline(always)]
to_f32(self) -> InterpResult<'tcx, Single>529     pub fn to_f32(self) -> InterpResult<'tcx, Single> {
530         self.check_init()?.to_f32()
531     }
532 
533     #[inline(always)]
to_f64(self) -> InterpResult<'tcx, Double>534     pub fn to_f64(self) -> InterpResult<'tcx, Double> {
535         self.check_init()?.to_f64()
536     }
537 
538     #[inline(always)]
to_u8(self) -> InterpResult<'tcx, u8>539     pub fn to_u8(self) -> InterpResult<'tcx, u8> {
540         self.check_init()?.to_u8()
541     }
542 
543     #[inline(always)]
to_u16(self) -> InterpResult<'tcx, u16>544     pub fn to_u16(self) -> InterpResult<'tcx, u16> {
545         self.check_init()?.to_u16()
546     }
547 
548     #[inline(always)]
to_u32(self) -> InterpResult<'tcx, u32>549     pub fn to_u32(self) -> InterpResult<'tcx, u32> {
550         self.check_init()?.to_u32()
551     }
552 
553     #[inline(always)]
to_u64(self) -> InterpResult<'tcx, u64>554     pub fn to_u64(self) -> InterpResult<'tcx, u64> {
555         self.check_init()?.to_u64()
556     }
557 
558     #[inline(always)]
to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64>559     pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
560         self.check_init()?.to_machine_usize(cx)
561     }
562 
563     #[inline(always)]
to_i8(self) -> InterpResult<'tcx, i8>564     pub fn to_i8(self) -> InterpResult<'tcx, i8> {
565         self.check_init()?.to_i8()
566     }
567 
568     #[inline(always)]
to_i16(self) -> InterpResult<'tcx, i16>569     pub fn to_i16(self) -> InterpResult<'tcx, i16> {
570         self.check_init()?.to_i16()
571     }
572 
573     #[inline(always)]
to_i32(self) -> InterpResult<'tcx, i32>574     pub fn to_i32(self) -> InterpResult<'tcx, i32> {
575         self.check_init()?.to_i32()
576     }
577 
578     #[inline(always)]
to_i64(self) -> InterpResult<'tcx, i64>579     pub fn to_i64(self) -> InterpResult<'tcx, i64> {
580         self.check_init()?.to_i64()
581     }
582 
583     #[inline(always)]
to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64>584     pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
585         self.check_init()?.to_machine_isize(cx)
586     }
587 }
588 
589 /// Gets the bytes of a constant slice value.
get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8]590 pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
591     if let ConstValue::Slice { data, start, end } = val {
592         let len = end - start;
593         data.get_bytes(
594             cx,
595             AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) },
596         )
597         .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
598     } else {
599         bug!("expected const slice, but found another const value");
600     }
601 }
602