Skip to content

Commit 6faae4d

Browse files
Add erase_alloc_id method to various Miri primitives
This method is based on `erase_tag`, and replaces `AllocId`s with `()`.
1 parent db7ca25 commit 6faae4d

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/librustc/mir/interpret/pointer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ impl<'tcx, Tag> Pointer<Tag> {
201201
Pointer { alloc_id: self.alloc_id, offset: self.offset, tag: () }
202202
}
203203

204+
/// Replace any allocation IDs in this type with `()`.
205+
///
206+
/// Used when comparing heap snapshots.
207+
pub fn erase_alloc_id(self) -> Pointer<Tag, ()> {
208+
Pointer { alloc_id: (), offset: self.offset, tag: self.tag }
209+
}
210+
204211
/// Test if the pointer is "inbounds" of an allocation of the given size.
205212
/// A pointer is "inbounds" even if its offset is equal to the size; this is
206213
/// a "one-past-the-end" pointer.

src/librustc/mir/interpret/value.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ impl<'tcx, Tag> Scalar<Tag> {
153153
}
154154
}
155155

156+
/// Replace any allocation IDs in this type with `()`.
157+
///
158+
/// Used when comparing heap snapshots.
159+
pub fn erase_alloc_id(self) -> Scalar<Tag, ()> {
160+
match self {
161+
Scalar::Ptr(ptr) => Scalar::Ptr(ptr.erase_alloc_id()),
162+
Scalar::Raw { data, size } => Scalar::Raw { data, size },
163+
}
164+
}
165+
156166
#[inline]
157167
pub fn ptr_null(cx: &impl HasDataLayout) -> Self {
158168
Scalar::Raw {
@@ -509,6 +519,16 @@ impl<'tcx, Tag> ScalarMaybeUndef<Tag> {
509519
}
510520
}
511521

522+
/// Replace any allocation IDs in this type with `()`.
523+
///
524+
/// Used when comparing heap snapshots.
525+
pub fn erase_alloc_id(self) -> ScalarMaybeUndef<Tag, ()> {
526+
match self {
527+
ScalarMaybeUndef::Scalar(s) => ScalarMaybeUndef::Scalar(s.erase_alloc_id()),
528+
ScalarMaybeUndef::Undef => ScalarMaybeUndef::Undef,
529+
}
530+
}
531+
512532
#[inline]
513533
pub fn not_undef(self) -> InterpResult<'static, Scalar<Tag>> {
514534
match self {

src/librustc_mir/interpret/operand.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ impl<'tcx, Tag> Immediate<Tag> {
9191
Immediate::ScalarPair(a, b) => Ok((a.not_undef()?, b.not_undef()?))
9292
}
9393
}
94+
95+
/// Replace any allocation IDs in this type with `()`.
96+
///
97+
/// Used when comparing heap snapshots.
98+
pub(super) fn erase_alloc_id(self) -> Immediate<Tag, ()> {
99+
match self {
100+
Immediate::Scalar(a) => Immediate::Scalar(a.erase_alloc_id()),
101+
Immediate::ScalarPair(a, b)
102+
=> Immediate::ScalarPair(a.erase_alloc_id(), b.erase_alloc_id()),
103+
}
104+
}
94105
}
95106

96107
// ScalarPair needs a type to interpret, so we often have an immediate and a type together
@@ -176,6 +187,16 @@ impl<Tag> Operand<Tag> {
176187

177188
}
178189
}
190+
191+
/// Replace any allocation IDs in this type with `()`.
192+
///
193+
/// Used when comparing heap snapshots.
194+
pub(super) fn erase_alloc_id(self) -> Operand<Tag, ()> {
195+
match self {
196+
Operand::Immediate(imm) => Operand::Immediate(imm.erase_alloc_id()),
197+
Operand::Indirect(place) => Operand::Indirect(place.erase_alloc_id()),
198+
}
199+
}
179200
}
180201

181202
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]

src/librustc_mir/interpret/place.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ impl<Tag> MemPlace<Tag> {
105105
}
106106
}
107107

108+
/// Replace any allocation IDs in this type with `()`.
109+
///
110+
/// Used when comparing heap snapshots.
111+
pub fn erase_alloc_id(self) -> MemPlace<Tag, ()> {
112+
MemPlace {
113+
ptr: self.ptr.erase_alloc_id(),
114+
align: self.align,
115+
meta: self.meta.map(Scalar::erase_alloc_id),
116+
}
117+
}
118+
108119
#[inline(always)]
109120
pub fn from_scalar_ptr(ptr: Scalar<Tag>, align: Align) -> Self {
110121
MemPlace {

0 commit comments

Comments
 (0)