Skip to content

Commit 68af46c

Browse files
committed
assert that we are (de)seiralizing ProvenanceMap correctly
1 parent 03b2598 commit 68af46c

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ pub struct Memory<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
112112
/// A reference to some allocation that was already bounds-checked for the given region
113113
/// and had the on-access machine hooks run.
114114
#[derive(Copy, Clone)]
115-
pub struct AllocRef<'a, 'tcx, Prov, Extra> {
115+
pub struct AllocRef<'a, 'tcx, Prov: Provenance, Extra> {
116116
alloc: &'a Allocation<Prov, Extra>,
117117
range: AllocRange,
118118
tcx: TyCtxt<'tcx>,
119119
alloc_id: AllocId,
120120
}
121121
/// A reference to some allocation that was already bounds-checked for the given region
122122
/// and had the on-access machine hooks run.
123-
pub struct AllocRefMut<'a, 'tcx, Prov, Extra> {
123+
pub struct AllocRefMut<'a, 'tcx, Prov: Provenance, Extra> {
124124
alloc: &'a mut Allocation<Prov, Extra>,
125125
range: AllocRange,
126126
tcx: TyCtxt<'tcx>,

compiler/rustc_middle/src/mir/interpret/allocation.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub use init_mask::{InitChunk, InitChunkIter};
3636
// hashed. (see the `Hash` impl below for more details), so the impl is not derived.
3737
#[derive(Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
3838
#[derive(HashStable)]
39-
pub struct Allocation<Prov = AllocId, Extra = ()> {
39+
pub struct Allocation<Prov: Provenance = AllocId, Extra = ()> {
4040
/// The actual bytes of the allocation.
4141
/// Note that the bytes of a pointer represent the offset of the pointer.
4242
bytes: Box<[u8]>,
@@ -108,9 +108,7 @@ impl hash::Hash for Allocation {
108108
/// (`ConstAllocation`) are used quite a bit.
109109
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, HashStable)]
110110
#[rustc_pass_by_value]
111-
pub struct ConstAllocation<'tcx, Prov = AllocId, Extra = ()>(
112-
pub Interned<'tcx, Allocation<Prov, Extra>>,
113-
);
111+
pub struct ConstAllocation<'tcx>(pub Interned<'tcx, Allocation>);
114112

115113
impl<'tcx> fmt::Debug for ConstAllocation<'tcx> {
116114
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -120,8 +118,8 @@ impl<'tcx> fmt::Debug for ConstAllocation<'tcx> {
120118
}
121119
}
122120

123-
impl<'tcx, Prov, Extra> ConstAllocation<'tcx, Prov, Extra> {
124-
pub fn inner(self) -> &'tcx Allocation<Prov, Extra> {
121+
impl<'tcx> ConstAllocation<'tcx> {
122+
pub fn inner(self) -> &'tcx Allocation {
125123
self.0.0
126124
}
127125
}
@@ -220,7 +218,7 @@ impl AllocRange {
220218
}
221219

222220
// The constructors are all without extra; the extra gets added by a machine hook later.
223-
impl<Prov> Allocation<Prov> {
221+
impl<Prov: Provenance> Allocation<Prov> {
224222
/// Creates an allocation initialized by the given bytes
225223
pub fn from_bytes<'a>(
226224
slice: impl Into<Cow<'a, [u8]>>,
@@ -278,7 +276,7 @@ impl<Prov> Allocation<Prov> {
278276
impl Allocation {
279277
/// Adjust allocation from the ones in tcx to a custom Machine instance
280278
/// with a different Provenance and Extra type.
281-
pub fn adjust_from_tcx<Prov, Extra, Err>(
279+
pub fn adjust_from_tcx<Prov: Provenance, Extra, Err>(
282280
self,
283281
cx: &impl HasDataLayout,
284282
extra: Extra,
@@ -311,7 +309,7 @@ impl Allocation {
311309
}
312310

313311
/// Raw accessors. Provide access to otherwise private bytes.
314-
impl<Prov, Extra> Allocation<Prov, Extra> {
312+
impl<Prov: Provenance, Extra> Allocation<Prov, Extra> {
315313
pub fn len(&self) -> usize {
316314
self.bytes.len()
317315
}

compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ pub struct ProvenanceMap<Prov = AllocId> {
2222
bytes: Option<Box<SortedMap<Size, Prov>>>,
2323
}
2424

25-
impl<D: Decoder, Prov: Decodable<D>> Decodable<D> for ProvenanceMap<Prov> {
25+
impl<D: Decoder, Prov: Provenance + Decodable<D>> Decodable<D> for ProvenanceMap<Prov> {
2626
fn decode(d: &mut D) -> Self {
27+
assert!(!Prov::OFFSET_IS_ADDR); // only `AllocId` is ever serialized
2728
Self { ptrs: Decodable::decode(d), bytes: None }
2829
}
2930
}
3031

31-
impl<S: Encoder, Prov: Encodable<S>> Encodable<S> for ProvenanceMap<Prov> {
32+
impl<S: Encoder, Prov: Provenance + Encodable<S>> Encodable<S> for ProvenanceMap<Prov> {
3233
fn encode(&self, s: &mut S) {
3334
let Self { ptrs, bytes } = self;
35+
assert!(!Prov::OFFSET_IS_ADDR); // only `AllocId` is ever serialized
3436
debug_assert!(bytes.is_none());
3537
ptrs.encode(s)
3638
}

compiler/rustc_middle/src/mir/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -788,15 +788,15 @@ pub fn write_allocations<'tcx>(
788788
/// After the hex dump, an ascii dump follows, replacing all unprintable characters (control
789789
/// characters or characters whose value is larger than 127) with a `.`
790790
/// This also prints provenance adequately.
791-
pub fn display_allocation<'a, 'tcx, Prov, Extra>(
791+
pub fn display_allocation<'a, 'tcx, Prov: Provenance, Extra>(
792792
tcx: TyCtxt<'tcx>,
793793
alloc: &'a Allocation<Prov, Extra>,
794794
) -> RenderAllocation<'a, 'tcx, Prov, Extra> {
795795
RenderAllocation { tcx, alloc }
796796
}
797797

798798
#[doc(hidden)]
799-
pub struct RenderAllocation<'a, 'tcx, Prov, Extra> {
799+
pub struct RenderAllocation<'a, 'tcx, Prov: Provenance, Extra> {
800800
tcx: TyCtxt<'tcx>,
801801
alloc: &'a Allocation<Prov, Extra>,
802802
}

compiler/rustc_mir_transform/src/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx>
266266
_tcx: TyCtxt<'tcx>,
267267
_machine: &Self,
268268
_alloc_id: AllocId,
269-
alloc: ConstAllocation<'tcx, Self::Provenance, Self::AllocExtra>,
269+
alloc: ConstAllocation<'tcx>,
270270
_static_def_id: Option<DefId>,
271271
is_write: bool,
272272
) -> InterpResult<'tcx> {

0 commit comments

Comments
 (0)