From 68af46c1121d14a3d89ec56cd4ed2fef829bae75 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 14 Nov 2022 13:37:08 +0100 Subject: [PATCH] assert that we are (de)seiralizing ProvenanceMap correctly --- .../rustc_const_eval/src/interpret/memory.rs | 4 ++-- .../rustc_middle/src/mir/interpret/allocation.rs | 16 +++++++--------- .../mir/interpret/allocation/provenance_map.rs | 6 ++++-- compiler/rustc_middle/src/mir/pretty.rs | 4 ++-- compiler/rustc_mir_transform/src/const_prop.rs | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_const_eval/src/interpret/memory.rs b/compiler/rustc_const_eval/src/interpret/memory.rs index 4eda9882da3..fdd0e444010 100644 --- a/compiler/rustc_const_eval/src/interpret/memory.rs +++ b/compiler/rustc_const_eval/src/interpret/memory.rs @@ -112,7 +112,7 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> { /// A reference to some allocation that was already bounds-checked for the given region /// and had the on-access machine hooks run. #[derive(Copy, Clone)] -pub struct AllocRef<'a, 'tcx, Prov, Extra> { +pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra> { alloc: &'a Allocation, range: AllocRange, tcx: TyCtxt<'tcx>, @@ -120,7 +120,7 @@ pub struct AllocRef<'a, 'tcx, Prov, Extra> { } /// A reference to some allocation that was already bounds-checked for the given region /// and had the on-access machine hooks run. -pub struct AllocRefMut<'a, 'tcx, Prov, Extra> { +pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra> { alloc: &'a mut Allocation, range: AllocRange, tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index 09a2c3aa5e2..68024070346 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -36,7 +36,7 @@ pub use init_mask::{InitChunk, InitChunkIter}; // hashed. (see the `Hash` impl below for more details), so the impl is not derived. #[derive(Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)] #[derive(HashStable)] -pub struct Allocation { +pub struct Allocation { /// The actual bytes of the allocation. /// Note that the bytes of a pointer represent the offset of the pointer. bytes: Box<[u8]>, @@ -108,9 +108,7 @@ impl hash::Hash for Allocation { /// (`ConstAllocation`) are used quite a bit. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)] #[rustc_pass_by_value] -pub struct ConstAllocation<'tcx, Prov = AllocId, Extra = ()>( - pub Interned<'tcx, Allocation>, -); +pub struct ConstAllocation<'tcx>(pub Interned<'tcx, Allocation>); impl<'tcx> fmt::Debug for ConstAllocation<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -120,8 +118,8 @@ impl<'tcx> fmt::Debug for ConstAllocation<'tcx> { } } -impl<'tcx, Prov, Extra> ConstAllocation<'tcx, Prov, Extra> { - pub fn inner(self) -> &'tcx Allocation { +impl<'tcx> ConstAllocation<'tcx> { + pub fn inner(self) -> &'tcx Allocation { self.0.0 } } @@ -220,7 +218,7 @@ impl AllocRange { } // The constructors are all without extra; the extra gets added by a machine hook later. -impl Allocation { +impl Allocation { /// Creates an allocation initialized by the given bytes pub fn from_bytes<'a>( slice: impl Into>, @@ -278,7 +276,7 @@ impl Allocation { impl Allocation { /// Adjust allocation from the ones in tcx to a custom Machine instance /// with a different Provenance and Extra type. - pub fn adjust_from_tcx( + pub fn adjust_from_tcx( self, cx: &impl HasDataLayout, extra: Extra, @@ -311,7 +309,7 @@ impl Allocation { } /// Raw accessors. Provide access to otherwise private bytes. -impl Allocation { +impl Allocation { pub fn len(&self) -> usize { self.bytes.len() } diff --git a/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs b/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs index 3c78d642e15..19ee209e552 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs @@ -22,15 +22,17 @@ pub struct ProvenanceMap { bytes: Option>>, } -impl> Decodable for ProvenanceMap { +impl> Decodable for ProvenanceMap { fn decode(d: &mut D) -> Self { + assert!(!Prov::OFFSET_IS_ADDR); // only `AllocId` is ever serialized Self { ptrs: Decodable::decode(d), bytes: None } } } -impl> Encodable for ProvenanceMap { +impl> Encodable for ProvenanceMap { fn encode(&self, s: &mut S) { let Self { ptrs, bytes } = self; + assert!(!Prov::OFFSET_IS_ADDR); // only `AllocId` is ever serialized debug_assert!(bytes.is_none()); ptrs.encode(s) } diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index 72035319b8b..2b3f2c02411 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -788,7 +788,7 @@ pub fn write_allocations<'tcx>( /// After the hex dump, an ascii dump follows, replacing all unprintable characters (control /// characters or characters whose value is larger than 127) with a `.` /// This also prints provenance adequately. -pub fn display_allocation<'a, 'tcx, Prov, Extra>( +pub fn display_allocation<'a, 'tcx, Prov: Provenance, Extra>( tcx: TyCtxt<'tcx>, alloc: &'a Allocation, ) -> RenderAllocation<'a, 'tcx, Prov, Extra> { @@ -796,7 +796,7 @@ pub fn display_allocation<'a, 'tcx, Prov, Extra>( } #[doc(hidden)] -pub struct RenderAllocation<'a, 'tcx, Prov, Extra> { +pub struct RenderAllocation<'a, 'tcx, Prov: Provenance, Extra> { tcx: TyCtxt<'tcx>, alloc: &'a Allocation, } diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 4e451588845..42143832367 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -266,7 +266,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> _tcx: TyCtxt<'tcx>, _machine: &Self, _alloc_id: AllocId, - alloc: ConstAllocation<'tcx, Self::Provenance, Self::AllocExtra>, + alloc: ConstAllocation<'tcx>, _static_def_id: Option, is_write: bool, ) -> InterpResult<'tcx> {