]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/mir/interpret/value.rs
CTFE/Miri engine Pointer type overhaul: make Scalar-to-Pointer conversion infallible
[rust.git] / compiler / rustc_middle / src / mir / interpret / value.rs
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, TargetDataLayout};
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 a `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>;
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]
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
76     pub fn try_to_scalar_int(&self) -> Option<ScalarInt> {
77         Some(self.try_to_scalar()?.assert_int())
78     }
79
80     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
81         self.try_to_scalar_int()?.to_bits(size).ok()
82     }
83
84     pub fn try_to_bool(&self) -> Option<bool> {
85         self.try_to_scalar_int()?.try_into().ok()
86     }
87
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
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
102     pub fn from_bool(b: bool) -> Self {
103         ConstValue::Scalar(Scalar::from_bool(b))
104     }
105
106     pub fn from_u64(i: u64) -> Self {
107         ConstValue::Scalar(Scalar::from_u64(i))
108     }
109
110     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 a `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     Ptr(Pointer<Tag>),
132 }
133
134 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
135 //FIXME static_assert_size!(Scalar, 24);
136
137 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
138 // all the Miri types.
139 impl<Tag: Provenance> fmt::Debug for Scalar<Tag> {
140     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141         match self {
142             Scalar::Ptr(ptr) => write!(f, "{:?}", ptr),
143             Scalar::Int(int) => write!(f, "{:?}", int),
144         }
145     }
146 }
147
148 impl<Tag: Provenance> fmt::Display for Scalar<Tag> {
149     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150         match self {
151             Scalar::Ptr(ptr) => write!(f, "pointer to {:?}", ptr),
152             Scalar::Int(int) => write!(f, "{:?}", int),
153         }
154     }
155 }
156
157 impl<Tag> From<Single> for Scalar<Tag> {
158     #[inline(always)]
159     fn from(f: Single) -> Self {
160         Scalar::from_f32(f)
161     }
162 }
163
164 impl<Tag> From<Double> for Scalar<Tag> {
165     #[inline(always)]
166     fn from(f: Double) -> Self {
167         Scalar::from_f64(f)
168     }
169 }
170
171 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
172     #[inline(always)]
173     fn from(ptr: Pointer<Tag>) -> Self {
174         Scalar::Ptr(ptr)
175     }
176 }
177
178 impl<Tag> From<ScalarInt> for Scalar<Tag> {
179     #[inline(always)]
180     fn from(ptr: ScalarInt) -> Self {
181         Scalar::Int(ptr)
182     }
183 }
184
185 impl<'tcx, Tag> Scalar<Tag> {
186     pub const ZST: Self = Scalar::Int(ScalarInt::ZST);
187
188     #[inline]
189     pub fn null_ptr(cx: &impl HasDataLayout) -> Self {
190         Scalar::Int(ScalarInt::null(cx.data_layout().pointer_size))
191     }
192
193     /// Create a Scalar from a pointer with an `Option<_>` tag (where `None` represents a plain integer).
194     pub fn from_maybe_pointer(ptr: Pointer<Option<Tag>>, cx: &impl HasDataLayout) -> Self {
195         match ptr.into_parts() {
196             (Some(tag), offset) => Scalar::Ptr(Pointer::new(tag, offset)),
197             (None, offset) => {
198                 Scalar::Int(ScalarInt::try_from_uint(offset.bytes(), cx.pointer_size()).unwrap())
199             }
200         }
201     }
202
203     #[inline(always)]
204     fn ptr_op(
205         self,
206         dl: &TargetDataLayout,
207         f_int: impl FnOnce(u64) -> InterpResult<'tcx, u64>,
208         f_ptr: impl FnOnce(Pointer<Tag>) -> InterpResult<'tcx, Pointer<Tag>>,
209     ) -> InterpResult<'tcx, Self> {
210         match self {
211             Scalar::Int(int) => Ok(Scalar::Int(int.ptr_sized_op(dl, f_int)?)),
212             Scalar::Ptr(ptr) => Ok(Scalar::Ptr(f_ptr(ptr)?)),
213         }
214     }
215
216     #[inline]
217     pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
218         let dl = cx.data_layout();
219         self.ptr_op(dl, |int| dl.offset(int, i.bytes()), |ptr| ptr.offset(i, dl))
220     }
221
222     #[inline]
223     pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
224         let dl = cx.data_layout();
225         self.ptr_op(
226             dl,
227             |int| Ok(dl.overflowing_offset(int, i.bytes()).0),
228             |ptr| Ok(ptr.wrapping_offset(i, dl)),
229         )
230         .unwrap()
231     }
232
233     #[inline]
234     pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
235         let dl = cx.data_layout();
236         self.ptr_op(dl, |int| dl.signed_offset(int, i), |ptr| ptr.signed_offset(i, dl))
237     }
238
239     #[inline]
240     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
241         let dl = cx.data_layout();
242         self.ptr_op(
243             dl,
244             |int| Ok(dl.overflowing_signed_offset(int, i).0),
245             |ptr| Ok(ptr.wrapping_signed_offset(i, dl)),
246         )
247         .unwrap()
248     }
249
250     #[inline]
251     pub fn from_bool(b: bool) -> Self {
252         Scalar::Int(b.into())
253     }
254
255     #[inline]
256     pub fn from_char(c: char) -> Self {
257         Scalar::Int(c.into())
258     }
259
260     #[inline]
261     pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
262         ScalarInt::try_from_uint(i, size).map(Scalar::Int)
263     }
264
265     #[inline]
266     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
267         let i = i.into();
268         Self::try_from_uint(i, size)
269             .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
270     }
271
272     #[inline]
273     pub fn from_u8(i: u8) -> Self {
274         Scalar::Int(i.into())
275     }
276
277     #[inline]
278     pub fn from_u16(i: u16) -> Self {
279         Scalar::Int(i.into())
280     }
281
282     #[inline]
283     pub fn from_u32(i: u32) -> Self {
284         Scalar::Int(i.into())
285     }
286
287     #[inline]
288     pub fn from_u64(i: u64) -> Self {
289         Scalar::Int(i.into())
290     }
291
292     #[inline]
293     pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
294         Self::from_uint(i, cx.data_layout().pointer_size)
295     }
296
297     #[inline]
298     pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
299         ScalarInt::try_from_int(i, size).map(Scalar::Int)
300     }
301
302     #[inline]
303     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
304         let i = i.into();
305         Self::try_from_int(i, size)
306             .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
307     }
308
309     #[inline]
310     pub fn from_i32(i: i32) -> Self {
311         Self::from_int(i, Size::from_bits(32))
312     }
313
314     #[inline]
315     pub fn from_i64(i: i64) -> Self {
316         Self::from_int(i, Size::from_bits(64))
317     }
318
319     #[inline]
320     pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
321         Self::from_int(i, cx.data_layout().pointer_size)
322     }
323
324     #[inline]
325     pub fn from_f32(f: Single) -> Self {
326         Scalar::Int(f.into())
327     }
328
329     #[inline]
330     pub fn from_f64(f: Double) -> Self {
331         Scalar::Int(f.into())
332     }
333
334     /// This is almost certainly not the method you want!  You should dispatch on the type
335     /// and use `to_{u8,u16,...}`/`scalar_to_ptr` to perform ptr-to-int / int-to-ptr casts as needed.
336     ///
337     /// This method only exists for the benefit of low-level memory operations
338     /// as well as the implementation of the above methods.
339     #[inline]
340     pub fn to_bits_or_ptr(
341         self,
342         target_size: Size,
343         cx: &impl HasDataLayout,
344     ) -> Result<u128, Pointer<Tag>> {
345         assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
346         match self {
347             Scalar::Int(int) => Ok(int.assert_bits(target_size)),
348             Scalar::Ptr(ptr) => {
349                 assert_eq!(target_size, cx.data_layout().pointer_size);
350                 Err(ptr)
351             }
352         }
353     }
354
355     /// Do not call this method! It does not do ptr-to-int casts when needed.
356     #[inline(always)]
357     pub fn assert_bits(self, target_size: Size) -> u128 {
358         self.assert_int().assert_bits(target_size)
359     }
360
361     /// Do not call this method! It does not do ptr-to-int casts when needed.
362     #[inline]
363     pub fn assert_int(self) -> ScalarInt {
364         match self {
365             Scalar::Ptr(_) => bug!("expected an int but got an abstract pointer"),
366             Scalar::Int(int) => int,
367         }
368     }
369
370     /// Do not call this method! It does not do int-to-ptr casts when needed.
371     #[inline]
372     pub fn assert_ptr(self) -> Pointer<Tag> {
373         match self {
374             Scalar::Ptr(p) => p,
375             Scalar::Int { .. } => bug!("expected a Pointer but got Raw bits"),
376         }
377     }
378
379     /// Do not call this method!  Dispatch based on the type instead.
380     #[inline]
381     pub fn is_bits(self) -> bool {
382         matches!(self, Scalar::Int { .. })
383     }
384
385     /// Do not call this method!  Dispatch based on the type instead.
386     #[inline]
387     pub fn is_ptr(self) -> bool {
388         matches!(self, Scalar::Ptr(_))
389     }
390 }
391
392 impl<'tcx, Tag: Provenance> Scalar<Tag> {
393     /// Erase the tag from the scalar, if any.
394     ///
395     /// Used by error reporting code to avoid having the error type depend on `Tag`.
396     #[inline]
397     pub fn erase_for_fmt(self) -> Scalar {
398         match self {
399             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_for_fmt()),
400             Scalar::Int(int) => Scalar::Int(int),
401         }
402     }
403
404     /// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
405     /// likely want to use instead.
406     ///
407     /// Will perform ptr-to-int casts if needed and possible.
408     #[inline]
409     pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
410         assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST");
411         match self {
412             Scalar::Int(int) => int.to_bits(target_size).map_err(|size| {
413                 err_ub!(ScalarSizeMismatch {
414                     target_size: target_size.bytes(),
415                     data_size: size.bytes(),
416                 })
417                 .into()
418             }),
419             Scalar::Ptr(ptr) => {
420                 if Tag::OFFSET_IS_ADDR {
421                     Ok(ptr.offset.bytes().into())
422                 } else {
423                     throw_unsup!(ReadPointerAsBytes)
424                 }
425             }
426         }
427     }
428
429     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
430         let val = self.to_u8()?;
431         match val {
432             0 => Ok(false),
433             1 => Ok(true),
434             _ => throw_ub!(InvalidBool(val)),
435         }
436     }
437
438     pub fn to_char(self) -> InterpResult<'tcx, char> {
439         let val = self.to_u32()?;
440         match std::char::from_u32(val) {
441             Some(c) => Ok(c),
442             None => throw_ub!(InvalidChar(val)),
443         }
444     }
445
446     #[inline]
447     fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> {
448         let sz = Size::from_bits(bits);
449         self.to_bits(sz)
450     }
451
452     /// Converts the scalar to produce an `u8`. Fails if the scalar is a pointer.
453     pub fn to_u8(self) -> InterpResult<'static, u8> {
454         self.to_unsigned_with_bit_width(8).map(|v| u8::try_from(v).unwrap())
455     }
456
457     /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer.
458     pub fn to_u16(self) -> InterpResult<'static, u16> {
459         self.to_unsigned_with_bit_width(16).map(|v| u16::try_from(v).unwrap())
460     }
461
462     /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
463     pub fn to_u32(self) -> InterpResult<'static, u32> {
464         self.to_unsigned_with_bit_width(32).map(|v| u32::try_from(v).unwrap())
465     }
466
467     /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
468     pub fn to_u64(self) -> InterpResult<'static, u64> {
469         self.to_unsigned_with_bit_width(64).map(|v| u64::try_from(v).unwrap())
470     }
471
472     /// Converts the scalar to produce an `u128`. Fails if the scalar is a pointer.
473     pub fn to_u128(self) -> InterpResult<'static, u128> {
474         self.to_unsigned_with_bit_width(128)
475     }
476
477     pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
478         let b = self.to_bits(cx.data_layout().pointer_size)?;
479         Ok(u64::try_from(b).unwrap())
480     }
481
482     #[inline]
483     fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> {
484         let sz = Size::from_bits(bits);
485         let b = self.to_bits(sz)?;
486         Ok(sz.sign_extend(b) as i128)
487     }
488
489     /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
490     pub fn to_i8(self) -> InterpResult<'static, i8> {
491         self.to_signed_with_bit_width(8).map(|v| i8::try_from(v).unwrap())
492     }
493
494     /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
495     pub fn to_i16(self) -> InterpResult<'static, i16> {
496         self.to_signed_with_bit_width(16).map(|v| i16::try_from(v).unwrap())
497     }
498
499     /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
500     pub fn to_i32(self) -> InterpResult<'static, i32> {
501         self.to_signed_with_bit_width(32).map(|v| i32::try_from(v).unwrap())
502     }
503
504     /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
505     pub fn to_i64(self) -> InterpResult<'static, i64> {
506         self.to_signed_with_bit_width(64).map(|v| i64::try_from(v).unwrap())
507     }
508
509     /// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
510     pub fn to_i128(self) -> InterpResult<'static, i128> {
511         self.to_signed_with_bit_width(128)
512     }
513
514     pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
515         let sz = cx.data_layout().pointer_size;
516         let b = self.to_bits(sz)?;
517         let b = sz.sign_extend(b) as i128;
518         Ok(i64::try_from(b).unwrap())
519     }
520
521     #[inline]
522     pub fn to_f32(self) -> InterpResult<'static, Single> {
523         // Going through `u32` to check size and truncation.
524         Ok(Single::from_bits(self.to_u32()?.into()))
525     }
526
527     #[inline]
528     pub fn to_f64(self) -> InterpResult<'static, Double> {
529         // Going through `u64` to check size and truncation.
530         Ok(Double::from_bits(self.to_u64()?.into()))
531     }
532 }
533
534 #[derive(Clone, Copy, Eq, PartialEq, TyEncodable, TyDecodable, HashStable, Hash)]
535 pub enum ScalarMaybeUninit<Tag = AllocId> {
536     Scalar(Scalar<Tag>),
537     Uninit,
538 }
539
540 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
541 //FIXME static_assert_size!(ScalarMaybeUninit, 24);
542
543 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUninit<Tag> {
544     #[inline(always)]
545     fn from(s: Scalar<Tag>) -> Self {
546         ScalarMaybeUninit::Scalar(s)
547     }
548 }
549
550 impl<Tag> From<Pointer<Tag>> for ScalarMaybeUninit<Tag> {
551     #[inline(always)]
552     fn from(s: Pointer<Tag>) -> Self {
553         ScalarMaybeUninit::Scalar(s.into())
554     }
555 }
556
557 // We want the `Debug` output to be readable as it is used by `derive(Debug)` for
558 // all the Miri types.
559 impl<Tag: Provenance> fmt::Debug for ScalarMaybeUninit<Tag> {
560     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
561         match self {
562             ScalarMaybeUninit::Uninit => write!(f, "<uninitialized>"),
563             ScalarMaybeUninit::Scalar(s) => write!(f, "{:?}", s),
564         }
565     }
566 }
567
568 impl<Tag: Provenance> fmt::Display for ScalarMaybeUninit<Tag> {
569     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
570         match self {
571             ScalarMaybeUninit::Uninit => write!(f, "uninitialized bytes"),
572             ScalarMaybeUninit::Scalar(s) => write!(f, "{}", s),
573         }
574     }
575 }
576
577 impl<Tag> ScalarMaybeUninit<Tag> {
578     #[inline]
579     pub fn check_init(self) -> InterpResult<'static, Scalar<Tag>> {
580         match self {
581             ScalarMaybeUninit::Scalar(scalar) => Ok(scalar),
582             ScalarMaybeUninit::Uninit => throw_ub!(InvalidUninitBytes(None)),
583         }
584     }
585 }
586
587 impl<'tcx, Tag: Provenance> ScalarMaybeUninit<Tag> {
588     /// Erase the tag from the scalar, if any.
589     ///
590     /// Used by error reporting code to avoid having the error type depend on `Tag`.
591     #[inline]
592     pub fn erase_for_fmt(self) -> ScalarMaybeUninit {
593         match self {
594             ScalarMaybeUninit::Scalar(s) => ScalarMaybeUninit::Scalar(s.erase_for_fmt()),
595             ScalarMaybeUninit::Uninit => ScalarMaybeUninit::Uninit,
596         }
597     }
598
599     #[inline(always)]
600     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
601         self.check_init()?.to_bool()
602     }
603
604     #[inline(always)]
605     pub fn to_char(self) -> InterpResult<'tcx, char> {
606         self.check_init()?.to_char()
607     }
608
609     #[inline(always)]
610     pub fn to_f32(self) -> InterpResult<'tcx, Single> {
611         self.check_init()?.to_f32()
612     }
613
614     #[inline(always)]
615     pub fn to_f64(self) -> InterpResult<'tcx, Double> {
616         self.check_init()?.to_f64()
617     }
618
619     #[inline(always)]
620     pub fn to_u8(self) -> InterpResult<'tcx, u8> {
621         self.check_init()?.to_u8()
622     }
623
624     #[inline(always)]
625     pub fn to_u16(self) -> InterpResult<'tcx, u16> {
626         self.check_init()?.to_u16()
627     }
628
629     #[inline(always)]
630     pub fn to_u32(self) -> InterpResult<'tcx, u32> {
631         self.check_init()?.to_u32()
632     }
633
634     #[inline(always)]
635     pub fn to_u64(self) -> InterpResult<'tcx, u64> {
636         self.check_init()?.to_u64()
637     }
638
639     #[inline(always)]
640     pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
641         self.check_init()?.to_machine_usize(cx)
642     }
643
644     #[inline(always)]
645     pub fn to_i8(self) -> InterpResult<'tcx, i8> {
646         self.check_init()?.to_i8()
647     }
648
649     #[inline(always)]
650     pub fn to_i16(self) -> InterpResult<'tcx, i16> {
651         self.check_init()?.to_i16()
652     }
653
654     #[inline(always)]
655     pub fn to_i32(self) -> InterpResult<'tcx, i32> {
656         self.check_init()?.to_i32()
657     }
658
659     #[inline(always)]
660     pub fn to_i64(self) -> InterpResult<'tcx, i64> {
661         self.check_init()?.to_i64()
662     }
663
664     #[inline(always)]
665     pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
666         self.check_init()?.to_machine_isize(cx)
667     }
668 }
669
670 /// Gets the bytes of a constant slice value.
671 pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
672     if let ConstValue::Slice { data, start, end } = val {
673         let len = end - start;
674         data.get_bytes(
675             cx,
676             AllocRange { start: Size::from_bytes(start), size: Size::from_bytes(len) },
677         )
678         .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
679     } else {
680         bug!("expected const slice, but found another const value");
681     }
682 }