diff options
Diffstat (limited to 'src/librustc')
| -rw-r--r-- | src/librustc/ich/impls_ty.rs | 2 | ||||
| -rw-r--r-- | src/librustc/mir/interpret/pointer.rs | 2 | ||||
| -rw-r--r-- | src/librustc/mir/interpret/value.rs | 28 | ||||
| -rw-r--r-- | src/librustc/mir/mod.rs | 30 | ||||
| -rw-r--r-- | src/librustc/ty/context.rs | 2 | ||||
| -rw-r--r-- | src/librustc/ty/structural_impls.rs | 2 | ||||
| -rw-r--r-- | src/librustc/ty/sty.rs | 6 |
7 files changed, 39 insertions, 33 deletions
diff --git a/src/librustc/ich/impls_ty.rs b/src/librustc/ich/impls_ty.rs index 79c2b89522d..bd2349161f7 100644 --- a/src/librustc/ich/impls_ty.rs +++ b/src/librustc/ich/impls_ty.rs @@ -302,7 +302,7 @@ impl_stable_hash_for!(struct ty::FieldDef { impl_stable_hash_for!( impl<'tcx> for enum mir::interpret::ConstValue<'tcx> [ mir::interpret::ConstValue ] { Scalar(val), - ScalarPair(a, b), + Slice(a, b), ByRef(id, alloc, offset), } ); diff --git a/src/librustc/mir/interpret/pointer.rs b/src/librustc/mir/interpret/pointer.rs index a046825f088..498c0b5b917 100644 --- a/src/librustc/mir/interpret/pointer.rs +++ b/src/librustc/mir/interpret/pointer.rs @@ -76,6 +76,8 @@ pub struct Pointer<Tag=(),Id=AllocId> { pub tag: Tag, } +static_assert!(POINTER_SIZE: ::std::mem::size_of::<Pointer>() == 16); + /// Produces a `Pointer` which points to the beginning of the Allocation impl From<AllocId> for Pointer { #[inline(always)] diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index 76eb43e73d1..4ac84bcfd19 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -22,22 +22,28 @@ pub enum ConstValue<'tcx> { /// Not using the enum `Value` to encode that this must not be `Undef` Scalar(Scalar), - /// Used only for *fat pointers* with layout::abi::ScalarPair + /// Used only for slices and strings (`&[T]`, `&str`, `*const [T]`, `*mut str`, `Box<str>`, ...) /// - /// Needed for pattern matching code related to slices and strings. - ScalarPair(Scalar, Scalar), + /// Empty slices don't necessarily have an address backed by an `AllocId`, thus we also need to + /// enable integer pointers. The `Scalar` type covers exactly those two cases. While we could + /// create dummy-`AllocId`s, the additional code effort for the conversions doesn't seem worth + /// it. + Slice(Scalar, u64), /// An allocation + offset into the allocation. /// Invariant: The AllocId matches the allocation. ByRef(AllocId, &'tcx Allocation, Size), } +#[cfg(target_arch = "x86_64")] +static_assert!(CONST_SIZE: ::std::mem::size_of::<ConstValue<'static>>() == 40); + impl<'tcx> ConstValue<'tcx> { #[inline] pub fn try_to_scalar(&self) -> Option<Scalar> { match *self { ConstValue::ByRef(..) | - ConstValue::ScalarPair(..) => None, + ConstValue::Slice(..) => None, ConstValue::Scalar(val) => Some(val), } } @@ -56,17 +62,8 @@ impl<'tcx> ConstValue<'tcx> { pub fn new_slice( val: Scalar, len: u64, - cx: &impl HasDataLayout ) -> Self { - ConstValue::ScalarPair(val, Scalar::Bits { - bits: len as u128, - size: cx.data_layout().pointer_size.bytes() as u8, - }) - } - - #[inline] - pub fn new_dyn_trait(val: Scalar, vtable: Pointer) -> Self { - ConstValue::ScalarPair(val, Scalar::Ptr(vtable)) + ConstValue::Slice(val, len) } } @@ -90,6 +87,9 @@ pub enum Scalar<Tag=(), Id=AllocId> { Ptr(Pointer<Tag, Id>), } +#[cfg(target_arch = "x86_64")] +static_assert!(SCALAR_SIZE: ::std::mem::size_of::<Scalar>() == 24); + impl<Tag> fmt::Display for Scalar<Tag> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { diff --git a/src/librustc/mir/mod.rs b/src/librustc/mir/mod.rs index f824ab7e5b3..82083b4f699 100644 --- a/src/librustc/mir/mod.rs +++ b/src/librustc/mir/mod.rs @@ -2702,23 +2702,21 @@ pub fn fmt_const_val(f: &mut impl Write, const_val: ty::Const<'_>) -> fmt::Resul return write!(f, "{}", item_path_str(did)); } // print string literals - if let ConstValue::ScalarPair(ptr, len) = value { + if let ConstValue::Slice(ptr, len) = value { if let Scalar::Ptr(ptr) = ptr { - if let Scalar::Bits { bits: len, .. } = len { - if let Ref(_, &ty::TyS { sty: Str, .. }, _) = ty.sty { - return ty::tls::with(|tcx| { - let alloc = tcx.alloc_map.lock().get(ptr.alloc_id); - if let Some(interpret::AllocKind::Memory(alloc)) = alloc { - assert_eq!(len as usize as u128, len); - let slice = - &alloc.bytes[(ptr.offset.bytes() as usize)..][..(len as usize)]; - let s = ::std::str::from_utf8(slice).expect("non utf8 str from miri"); - write!(f, "{:?}", s) - } else { - write!(f, "pointer to erroneous constant {:?}, {:?}", ptr, len) - } - }); - } + if let Ref(_, &ty::TyS { sty: Str, .. }, _) = ty.sty { + return ty::tls::with(|tcx| { + let alloc = tcx.alloc_map.lock().get(ptr.alloc_id); + if let Some(interpret::AllocKind::Memory(alloc)) = alloc { + assert_eq!(len as usize as u64, len); + let slice = + &alloc.bytes[(ptr.offset.bytes() as usize)..][..(len as usize)]; + let s = ::std::str::from_utf8(slice).expect("non utf8 str from miri"); + write!(f, "{:?}", s) + } else { + write!(f, "pointer to erroneous constant {:?}, {:?}", ptr, len) + } + }); } } } diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index 4c8f8141116..a0da7cf2137 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -1767,7 +1767,7 @@ macro_rules! nop_list_lift { impl<'a, 'tcx> Lift<'tcx> for &'a List<$ty> { type Lifted = &'tcx List<$lifted>; fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> { - if self.is_empty() { + if self.is_empty() { return Some(List::empty()); } if tcx.interners.arena.in_arena(*self as *const _) { diff --git a/src/librustc/ty/structural_impls.rs b/src/librustc/ty/structural_impls.rs index 258470bf6f8..28f5a65374d 100644 --- a/src/librustc/ty/structural_impls.rs +++ b/src/librustc/ty/structural_impls.rs @@ -498,7 +498,7 @@ impl<'a, 'tcx> Lift<'tcx> for ConstValue<'a> { fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<Self::Lifted> { match *self { ConstValue::Scalar(x) => Some(ConstValue::Scalar(x)), - ConstValue::ScalarPair(x, y) => Some(ConstValue::ScalarPair(x, y)), + ConstValue::Slice(x, y) => Some(ConstValue::Slice(x, y)), ConstValue::ByRef(x, alloc, z) => Some(ConstValue::ByRef( x, alloc.lift_to_tcx(tcx)?, z, )), diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index b98369b62ea..671a0fc2d5d 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -2063,6 +2063,9 @@ pub enum LazyConst<'tcx> { Evaluated(Const<'tcx>), } +#[cfg(target_arch = "x86_64")] +static_assert!(LAZY_CONST_SIZE: ::std::mem::size_of::<LazyConst<'static>>() == 56); + impl<'tcx> LazyConst<'tcx> { pub fn map_evaluated<R>(self, f: impl FnOnce(Const<'tcx>) -> Option<R>) -> Option<R> { match self { @@ -2089,6 +2092,9 @@ pub struct Const<'tcx> { pub val: ConstValue<'tcx>, } +#[cfg(target_arch = "x86_64")] +static_assert!(CONST_SIZE: ::std::mem::size_of::<Const<'static>>() == 48); + impl<'tcx> Const<'tcx> { #[inline] pub fn from_scalar( |
