]> git.lizzy.rs Git - rust.git/blob - src/librustc/mir/interpret/value.rs
Update const_forget.rs
[rust.git] / src / librustc / mir / interpret / value.rs
1 use rustc_apfloat::{
2     ieee::{Double, Single},
3     Float,
4 };
5 use rustc_macros::HashStable;
6 use std::fmt;
7
8 use crate::ty::{
9     layout::{HasDataLayout, Size},
10     ParamEnv, Ty, TyCtxt,
11 };
12
13 use super::{sign_extend, truncate, AllocId, Allocation, InterpResult, Pointer, PointerArithmetic};
14
15 /// Represents the result of a raw const operation, pre-validation.
16 #[derive(Clone, HashStable)]
17 pub struct RawConst<'tcx> {
18     // the value lives here, at offset 0, and that allocation definitely is a `AllocKind::Memory`
19     // (so you can use `AllocMap::unwrap_memory`).
20     pub alloc_id: AllocId,
21     pub ty: Ty<'tcx>,
22 }
23
24 /// Represents a constant value in Rust. `Scalar` and `Slice` are optimizations for
25 /// array length computations, enum discriminants and the pattern matching logic.
26 #[derive(
27     Copy,
28     Clone,
29     Debug,
30     Eq,
31     PartialEq,
32     PartialOrd,
33     Ord,
34     RustcEncodable,
35     RustcDecodable,
36     Hash,
37     HashStable
38 )]
39 pub enum ConstValue<'tcx> {
40     /// Used only for types with `layout::abi::Scalar` ABI and ZSTs.
41     ///
42     /// Not using the enum `Value` to encode that this must not be `Undef`.
43     Scalar(Scalar),
44
45     /// Used only for `&[u8]` and `&str`
46     Slice { data: &'tcx Allocation, start: usize, end: usize },
47
48     /// A value not represented/representable by `Scalar` or `Slice`
49     ByRef {
50         /// The backing memory of the value, may contain more memory than needed for just the value
51         /// in order to share `Allocation`s between values
52         alloc: &'tcx Allocation,
53         /// Offset into `alloc`
54         offset: Size,
55     },
56 }
57
58 #[cfg(target_arch = "x86_64")]
59 static_assert_size!(ConstValue<'_>, 32);
60
61 impl<'tcx> ConstValue<'tcx> {
62     #[inline]
63     pub fn try_to_scalar(&self) -> Option<Scalar> {
64         match *self {
65             ConstValue::ByRef { .. } | ConstValue::Slice { .. } => None,
66             ConstValue::Scalar(val) => Some(val),
67         }
68     }
69
70     pub fn try_to_bits(&self, size: Size) -> Option<u128> {
71         self.try_to_scalar()?.to_bits(size).ok()
72     }
73
74     pub fn try_to_bits_for_ty(
75         &self,
76         tcx: TyCtxt<'tcx>,
77         param_env: ParamEnv<'tcx>,
78         ty: Ty<'tcx>,
79     ) -> Option<u128> {
80         let size = tcx.layout_of(param_env.with_reveal_all().and(ty)).ok()?.size;
81         self.try_to_bits(size)
82     }
83
84     pub fn from_bool(b: bool) -> Self {
85         ConstValue::Scalar(Scalar::from_bool(b))
86     }
87
88     pub fn from_u64(i: u64) -> Self {
89         ConstValue::Scalar(Scalar::from_u64(i))
90     }
91
92     pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
93         ConstValue::Scalar(Scalar::from_machine_usize(i, cx))
94     }
95 }
96
97 /// A `Scalar` represents an immediate, primitive value existing outside of a
98 /// `memory::Allocation`. It is in many ways like a small chunk of a `Allocation`, up to 8 bytes in
99 /// size. Like a range of bytes in an `Allocation`, a `Scalar` can either represent the raw bytes
100 /// of a simple value or a pointer into another `Allocation`
101 #[derive(
102     Clone,
103     Copy,
104     Eq,
105     PartialEq,
106     Ord,
107     PartialOrd,
108     RustcEncodable,
109     RustcDecodable,
110     Hash,
111     HashStable
112 )]
113 pub enum Scalar<Tag = (), Id = AllocId> {
114     /// The raw bytes of a simple value.
115     Raw {
116         /// The first `size` bytes of `data` are the value.
117         /// Do not try to read less or more bytes than that. The remaining bytes must be 0.
118         data: u128,
119         size: u8,
120     },
121
122     /// A pointer into an `Allocation`. An `Allocation` in the `memory` module has a list of
123     /// relocations, but a `Scalar` is only large enough to contain one, so we just represent the
124     /// relocation and its associated offset together as a `Pointer` here.
125     Ptr(Pointer<Tag, Id>),
126 }
127
128 #[cfg(target_arch = "x86_64")]
129 static_assert_size!(Scalar, 24);
130
131 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
132     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
133         match self {
134             Scalar::Ptr(ptr) => write!(f, "{:?}", ptr),
135             &Scalar::Raw { data, size } => {
136                 Scalar::check_data(data, size);
137                 if size == 0 {
138                     write!(f, "<ZST>")
139                 } else {
140                     // Format as hex number wide enough to fit any value of the given `size`.
141                     // So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
142                     write!(f, "0x{:>0width$x}", data, width = (size * 2) as usize)
143                 }
144             }
145         }
146     }
147 }
148
149 impl<Tag> fmt::Display for Scalar<Tag> {
150     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151         match self {
152             Scalar::Ptr(_) => write!(f, "a pointer"),
153             Scalar::Raw { data, .. } => write!(f, "{}", data),
154         }
155     }
156 }
157
158 impl<Tag> From<Single> for Scalar<Tag> {
159     #[inline(always)]
160     fn from(f: Single) -> Self {
161         Scalar::from_f32(f)
162     }
163 }
164
165 impl<Tag> From<Double> for Scalar<Tag> {
166     #[inline(always)]
167     fn from(f: Double) -> Self {
168         Scalar::from_f64(f)
169     }
170 }
171
172 impl Scalar<()> {
173     #[inline(always)]
174     fn check_data(data: u128, size: u8) {
175         debug_assert_eq!(
176             truncate(data, Size::from_bytes(size as u64)),
177             data,
178             "Scalar value {:#x} exceeds size of {} bytes",
179             data,
180             size
181         );
182     }
183
184     /// Tag this scalar with `new_tag` if it is a pointer, leave it unchanged otherwise.
185     ///
186     /// Used by `MemPlace::replace_tag`.
187     #[inline]
188     pub fn with_tag<Tag>(self, new_tag: Tag) -> Scalar<Tag> {
189         match self {
190             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.with_tag(new_tag)),
191             Scalar::Raw { data, size } => Scalar::Raw { data, size },
192         }
193     }
194 }
195
196 impl<'tcx, Tag> Scalar<Tag> {
197     /// Erase the tag from the scalar, if any.
198     ///
199     /// Used by error reporting code to avoid having the error type depend on `Tag`.
200     #[inline]
201     pub fn erase_tag(self) -> Scalar {
202         match self {
203             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_tag()),
204             Scalar::Raw { data, size } => Scalar::Raw { data, size },
205         }
206     }
207
208     #[inline]
209     pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
210         Scalar::Raw { data: 0, size: cx.data_layout().pointer_size.bytes() as u8 }
211     }
212
213     #[inline]
214     pub fn zst() -> Self {
215         Scalar::Raw { data: 0, size: 0 }
216     }
217
218     #[inline]
219     pub fn ptr_offset(self, i: Size, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
220         let dl = cx.data_layout();
221         match self {
222             Scalar::Raw { data, size } => {
223                 assert_eq!(size as u64, dl.pointer_size.bytes());
224                 Ok(Scalar::Raw { data: dl.offset(data as u64, i.bytes())? as u128, size })
225             }
226             Scalar::Ptr(ptr) => ptr.offset(i, dl).map(Scalar::Ptr),
227         }
228     }
229
230     #[inline]
231     pub fn ptr_wrapping_offset(self, i: Size, cx: &impl HasDataLayout) -> Self {
232         let dl = cx.data_layout();
233         match self {
234             Scalar::Raw { data, size } => {
235                 assert_eq!(size as u64, dl.pointer_size.bytes());
236                 Scalar::Raw { data: dl.overflowing_offset(data as u64, i.bytes()).0 as u128, size }
237             }
238             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_offset(i, dl)),
239         }
240     }
241
242     #[inline]
243     pub fn ptr_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> InterpResult<'tcx, Self> {
244         let dl = cx.data_layout();
245         match self {
246             Scalar::Raw { data, size } => {
247                 assert_eq!(size as u64, dl.pointer_size().bytes());
248                 Ok(Scalar::Raw { data: dl.signed_offset(data as u64, i)? as u128, size })
249             }
250             Scalar::Ptr(ptr) => ptr.signed_offset(i, dl).map(Scalar::Ptr),
251         }
252     }
253
254     #[inline]
255     pub fn ptr_wrapping_signed_offset(self, i: i64, cx: &impl HasDataLayout) -> Self {
256         let dl = cx.data_layout();
257         match self {
258             Scalar::Raw { data, size } => {
259                 assert_eq!(size as u64, dl.pointer_size.bytes());
260                 Scalar::Raw {
261                     data: dl.overflowing_signed_offset(data as u64, i128::from(i)).0 as u128,
262                     size,
263                 }
264             }
265             Scalar::Ptr(ptr) => Scalar::Ptr(ptr.wrapping_signed_offset(i, dl)),
266         }
267     }
268
269     #[inline]
270     pub fn from_bool(b: bool) -> Self {
271         Scalar::Raw { data: b as u128, size: 1 }
272     }
273
274     #[inline]
275     pub fn from_char(c: char) -> Self {
276         Scalar::Raw { data: c as u128, size: 4 }
277     }
278
279     #[inline]
280     pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
281         let i = i.into();
282         if truncate(i, size) == i {
283             Some(Scalar::Raw { data: i, size: size.bytes() as u8 })
284         } else {
285             None
286         }
287     }
288
289     #[inline]
290     pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
291         let i = i.into();
292         Self::try_from_uint(i, size)
293             .unwrap_or_else(|| bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits()))
294     }
295
296     #[inline]
297     pub fn from_u8(i: u8) -> Self {
298         Scalar::Raw { data: i as u128, size: 1 }
299     }
300
301     #[inline]
302     pub fn from_u16(i: u16) -> Self {
303         Scalar::Raw { data: i as u128, size: 2 }
304     }
305
306     #[inline]
307     pub fn from_u32(i: u32) -> Self {
308         Scalar::Raw { data: i as u128, size: 4 }
309     }
310
311     #[inline]
312     pub fn from_u64(i: u64) -> Self {
313         Scalar::Raw { data: i as u128, size: 8 }
314     }
315
316     #[inline]
317     pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
318         Self::from_uint(i, cx.data_layout().pointer_size)
319     }
320
321     #[inline]
322     pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
323         let i = i.into();
324         // `into` performed sign extension, we have to truncate
325         let truncated = truncate(i as u128, size);
326         if sign_extend(truncated, size) as i128 == i {
327             Some(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
328         } else {
329             None
330         }
331     }
332
333     #[inline]
334     pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
335         let i = i.into();
336         Self::try_from_int(i, size)
337             .unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
338     }
339
340     #[inline]
341     pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
342         Self::from_int(i, cx.data_layout().pointer_size)
343     }
344
345     #[inline]
346     pub fn from_f32(f: Single) -> Self {
347         // We trust apfloat to give us properly truncated data.
348         Scalar::Raw { data: f.to_bits(), size: 4 }
349     }
350
351     #[inline]
352     pub fn from_f64(f: Double) -> Self {
353         // We trust apfloat to give us properly truncated data.
354         Scalar::Raw { data: f.to_bits(), size: 8 }
355     }
356
357     /// This is very rarely the method you want!  You should dispatch on the type
358     /// and use `force_bits`/`assert_bits`/`force_ptr`/`assert_ptr`.
359     /// This method only exists for the benefit of low-level memory operations
360     /// as well as the implementation of the `force_*` methods.
361     #[inline]
362     pub fn to_bits_or_ptr(
363         self,
364         target_size: Size,
365         cx: &impl HasDataLayout,
366     ) -> Result<u128, Pointer<Tag>> {
367         match self {
368             Scalar::Raw { data, size } => {
369                 assert_eq!(target_size.bytes(), size as u64);
370                 assert_ne!(size, 0, "you should never look at the bits of a ZST");
371                 Scalar::check_data(data, size);
372                 Ok(data)
373             }
374             Scalar::Ptr(ptr) => {
375                 assert_eq!(target_size, cx.data_layout().pointer_size);
376                 Err(ptr)
377             }
378         }
379     }
380
381     #[inline(always)]
382     pub fn check_raw(data: u128, size: u8, target_size: Size) {
383         assert_eq!(target_size.bytes(), size as u64);
384         assert_ne!(size, 0, "you should never look at the bits of a ZST");
385         Scalar::check_data(data, size);
386     }
387
388     /// Do not call this method!  Use either `assert_bits` or `force_bits`.
389     #[inline]
390     pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
391         match self {
392             Scalar::Raw { data, size } => {
393                 Self::check_raw(data, size, target_size);
394                 Ok(data)
395             }
396             Scalar::Ptr(_) => throw_unsup!(ReadPointerAsBytes),
397         }
398     }
399
400     #[inline(always)]
401     pub fn assert_bits(self, target_size: Size) -> u128 {
402         self.to_bits(target_size).expect("expected Raw bits but got a Pointer")
403     }
404
405     /// Do not call this method!  Use either `assert_ptr` or `force_ptr`.
406     /// This method is intentionally private, do not make it public.
407     #[inline]
408     fn to_ptr(self) -> InterpResult<'tcx, Pointer<Tag>> {
409         match self {
410             Scalar::Raw { data: 0, .. } => throw_unsup!(InvalidNullPointerUsage),
411             Scalar::Raw { .. } => throw_unsup!(ReadBytesAsPointer),
412             Scalar::Ptr(p) => Ok(p),
413         }
414     }
415
416     #[inline(always)]
417     pub fn assert_ptr(self) -> Pointer<Tag> {
418         self.to_ptr().expect("expected a Pointer but got Raw bits")
419     }
420
421     /// Do not call this method!  Dispatch based on the type instead.
422     #[inline]
423     pub fn is_bits(self) -> bool {
424         match self {
425             Scalar::Raw { .. } => true,
426             _ => false,
427         }
428     }
429
430     /// Do not call this method!  Dispatch based on the type instead.
431     #[inline]
432     pub fn is_ptr(self) -> bool {
433         match self {
434             Scalar::Ptr(_) => true,
435             _ => false,
436         }
437     }
438
439     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
440         match self {
441             Scalar::Raw { data: 0, size: 1 } => Ok(false),
442             Scalar::Raw { data: 1, size: 1 } => Ok(true),
443             _ => throw_unsup!(InvalidBool),
444         }
445     }
446
447     pub fn to_char(self) -> InterpResult<'tcx, char> {
448         let val = self.to_u32()?;
449         match ::std::char::from_u32(val) {
450             Some(c) => Ok(c),
451             None => throw_unsup!(InvalidChar(val as u128)),
452         }
453     }
454
455     #[inline]
456     fn to_unsigned_with_bit_width(self, bits: u64) -> InterpResult<'static, u128> {
457         let sz = Size::from_bits(bits);
458         self.to_bits(sz)
459     }
460
461     /// Converts the scalar to produce an `u8`. Fails if the scalar is a pointer.
462     pub fn to_u8(self) -> InterpResult<'static, u8> {
463         self.to_unsigned_with_bit_width(8).map(|v| v as u8)
464     }
465
466     /// Converts the scalar to produce an `u16`. Fails if the scalar is a pointer.
467     pub fn to_u16(self) -> InterpResult<'static, u16> {
468         self.to_unsigned_with_bit_width(16).map(|v| v as u16)
469     }
470
471     /// Converts the scalar to produce an `u32`. Fails if the scalar is a pointer.
472     pub fn to_u32(self) -> InterpResult<'static, u32> {
473         self.to_unsigned_with_bit_width(32).map(|v| v as u32)
474     }
475
476     /// Converts the scalar to produce an `u64`. Fails if the scalar is a pointer.
477     pub fn to_u64(self) -> InterpResult<'static, u64> {
478         self.to_unsigned_with_bit_width(64).map(|v| v as u64)
479     }
480
481     pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
482         let b = self.to_bits(cx.data_layout().pointer_size)?;
483         Ok(b as u64)
484     }
485
486     #[inline]
487     fn to_signed_with_bit_width(self, bits: u64) -> InterpResult<'static, i128> {
488         let sz = Size::from_bits(bits);
489         let b = self.to_bits(sz)?;
490         Ok(sign_extend(b, sz) as i128)
491     }
492
493     /// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
494     pub fn to_i8(self) -> InterpResult<'static, i8> {
495         self.to_signed_with_bit_width(8).map(|v| v as i8)
496     }
497
498     /// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
499     pub fn to_i16(self) -> InterpResult<'static, i16> {
500         self.to_signed_with_bit_width(16).map(|v| v as i16)
501     }
502
503     /// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
504     pub fn to_i32(self) -> InterpResult<'static, i32> {
505         self.to_signed_with_bit_width(32).map(|v| v as i32)
506     }
507
508     /// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
509     pub fn to_i64(self) -> InterpResult<'static, i64> {
510         self.to_signed_with_bit_width(64).map(|v| v as i64)
511     }
512
513     pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
514         let sz = cx.data_layout().pointer_size;
515         let b = self.to_bits(sz)?;
516         let b = sign_extend(b, sz) as i128;
517         Ok(b as i64)
518     }
519
520     #[inline]
521     pub fn to_f32(self) -> InterpResult<'static, Single> {
522         // Going through `u32` to check size and truncation.
523         Ok(Single::from_bits(self.to_u32()? as u128))
524     }
525
526     #[inline]
527     pub fn to_f64(self) -> InterpResult<'static, Double> {
528         // Going through `u64` to check size and truncation.
529         Ok(Double::from_bits(self.to_u64()? as u128))
530     }
531 }
532
533 impl<Tag> From<Pointer<Tag>> for Scalar<Tag> {
534     #[inline(always)]
535     fn from(ptr: Pointer<Tag>) -> Self {
536         Scalar::Ptr(ptr)
537     }
538 }
539
540 #[derive(Clone, Copy, Eq, PartialEq, RustcEncodable, RustcDecodable, HashStable, Hash)]
541 pub enum ScalarMaybeUndef<Tag = (), Id = AllocId> {
542     Scalar(Scalar<Tag, Id>),
543     Undef,
544 }
545
546 impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
547     #[inline(always)]
548     fn from(s: Scalar<Tag>) -> Self {
549         ScalarMaybeUndef::Scalar(s)
550     }
551 }
552
553 impl<Tag> From<Pointer<Tag>> for ScalarMaybeUndef<Tag> {
554     #[inline(always)]
555     fn from(s: Pointer<Tag>) -> Self {
556         ScalarMaybeUndef::Scalar(s.into())
557     }
558 }
559
560 impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
561     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
562         match self {
563             ScalarMaybeUndef::Undef => write!(f, "Undef"),
564             ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
565         }
566     }
567 }
568
569 impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
570     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
571         match self {
572             ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),
573             ScalarMaybeUndef::Scalar(s) => write!(f, "{}", s),
574         }
575     }
576 }
577
578 impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
579     /// Erase the tag from the scalar, if any.
580     ///
581     /// Used by error reporting code to avoid having the error type depend on `Tag`.
582     #[inline]
583     pub fn erase_tag(self) -> ScalarMaybeUndef {
584         match self {
585             ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.erase_tag()),
586             ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
587         }
588     }
589
590     #[inline]
591     pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
592         match self {
593             ScalarMaybeUndef::Scalar(scalar) => Ok(scalar),
594             ScalarMaybeUndef::Undef => throw_unsup!(ReadUndefBytes(Size::ZERO)),
595         }
596     }
597
598     /// Do not call this method!  Use either `assert_bits` or `force_bits`.
599     #[inline(always)]
600     pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> {
601         self.not_undef()?.to_bits(target_size)
602     }
603
604     #[inline(always)]
605     pub fn to_bool(self) -> InterpResult<'tcx, bool> {
606         self.not_undef()?.to_bool()
607     }
608
609     #[inline(always)]
610     pub fn to_char(self) -> InterpResult<'tcx, char> {
611         self.not_undef()?.to_char()
612     }
613
614     #[inline(always)]
615     pub fn to_f32(self) -> InterpResult<'tcx, Single> {
616         self.not_undef()?.to_f32()
617     }
618
619     #[inline(always)]
620     pub fn to_f64(self) -> InterpResult<'tcx, Double> {
621         self.not_undef()?.to_f64()
622     }
623
624     #[inline(always)]
625     pub fn to_u8(self) -> InterpResult<'tcx, u8> {
626         self.not_undef()?.to_u8()
627     }
628
629     #[inline(always)]
630     pub fn to_u32(self) -> InterpResult<'tcx, u32> {
631         self.not_undef()?.to_u32()
632     }
633
634     #[inline(always)]
635     pub fn to_u64(self) -> InterpResult<'tcx, u64> {
636         self.not_undef()?.to_u64()
637     }
638
639     #[inline(always)]
640     pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
641         self.not_undef()?.to_machine_usize(cx)
642     }
643
644     #[inline(always)]
645     pub fn to_i8(self) -> InterpResult<'tcx, i8> {
646         self.not_undef()?.to_i8()
647     }
648
649     #[inline(always)]
650     pub fn to_i32(self) -> InterpResult<'tcx, i32> {
651         self.not_undef()?.to_i32()
652     }
653
654     #[inline(always)]
655     pub fn to_i64(self) -> InterpResult<'tcx, i64> {
656         self.not_undef()?.to_i64()
657     }
658
659     #[inline(always)]
660     pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
661         self.not_undef()?.to_machine_isize(cx)
662     }
663 }
664
665 /// Gets the bytes of a constant slice value.
666 pub fn get_slice_bytes<'tcx>(cx: &impl HasDataLayout, val: ConstValue<'tcx>) -> &'tcx [u8] {
667     if let ConstValue::Slice { data, start, end } = val {
668         let len = end - start;
669         data.get_bytes(
670             cx,
671             // invent a pointer, only the offset is relevant anyway
672             Pointer::new(AllocId(0), Size::from_bytes(start as u64)),
673             Size::from_bytes(len as u64),
674         )
675         .unwrap_or_else(|err| bug!("const slice is invalid: {:?}", err))
676     } else {
677         bug!("expected const slice, but found another const value");
678     }
679 }