Skip to content

Commit 927c709

Browse files
committed
Impl Eq and PartialEq for EvalSnapshot in terms of the Snapshot trait
1 parent bf6ba97 commit 927c709

File tree

3 files changed

+45
-69
lines changed

3 files changed

+45
-69
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,32 +111,6 @@ pub struct Frame<'mir, 'tcx: 'mir> {
111111
pub stmt: usize,
112112
}
113113

114-
impl<'mir, 'tcx: 'mir> Eq for Frame<'mir, 'tcx> {}
115-
116-
impl<'mir, 'tcx: 'mir> PartialEq for Frame<'mir, 'tcx> {
117-
fn eq(&self, other: &Self) -> bool {
118-
let Frame {
119-
mir: _,
120-
instance,
121-
span: _,
122-
return_to_block,
123-
return_place,
124-
locals,
125-
block,
126-
stmt,
127-
} = self;
128-
129-
// Some of these are constant during evaluation, but are included
130-
// anyways for correctness.
131-
*instance == other.instance
132-
&& *return_to_block == other.return_to_block
133-
&& *return_place == other.return_place
134-
&& *locals == other.locals
135-
&& *block == other.block
136-
&& *stmt == other.stmt
137-
}
138-
}
139-
140114
impl<'a, 'mir, 'tcx: 'mir> HashStable<StableHashingContext<'a>> for Frame<'mir, 'tcx> {
141115
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher<W>) {
142116
let Frame {

src/librustc_mir/interpret/memory.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,6 @@ impl<'a, 'b, 'c, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout
6969
}
7070
}
7171

72-
impl<'a, 'mir, 'tcx, M> Eq for Memory<'a, 'mir, 'tcx, M>
73-
where M: Machine<'mir, 'tcx>,
74-
'tcx: 'a + 'mir,
75-
{}
76-
77-
impl<'a, 'mir, 'tcx, M> PartialEq for Memory<'a, 'mir, 'tcx, M>
78-
where M: Machine<'mir, 'tcx>,
79-
'tcx: 'a + 'mir,
80-
{
81-
fn eq(&self, other: &Self) -> bool {
82-
let Memory {
83-
data,
84-
alloc_map,
85-
tcx: _,
86-
} = self;
87-
88-
*data == other.data
89-
&& *alloc_map == other.alloc_map
90-
}
91-
}
92-
9372
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
9473
pub fn new(tcx: TyCtxtAt<'a, 'tcx, 'tcx>, data: M::MemoryData) -> Self {
9574
Memory {

src/librustc_mir/interpret/snapshot.rs

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syntax::ast::Mutability;
1111
use syntax::source_map::Span;
1212

1313
use super::eval_context::{LocalValue, StackPopCleanup};
14-
use super::{Frame, Memory, Machine, Operand, MemPlace, Place, PlaceExtra, Value};
14+
use super::{Frame, Memory, Machine, Operand, MemPlace, Place, Value};
1515

1616
trait SnapshotContext<'a> {
1717
type To;
@@ -24,6 +24,20 @@ trait Snapshot<'a, Ctx: SnapshotContext<'a>> {
2424
fn snapshot(&self, ctx: &'a Ctx) -> Self::Item;
2525
}
2626

27+
impl<'a, Ctx, T> Snapshot<'a, Ctx> for Option<T>
28+
where Ctx: SnapshotContext<'a>,
29+
T: Snapshot<'a, Ctx>
30+
{
31+
type Item = Option<<T as Snapshot<'a, Ctx>>::Item>;
32+
33+
fn snapshot(&self, ctx: &'a Ctx) -> Self::Item {
34+
match self {
35+
Some(x) => Some(x.snapshot(ctx)),
36+
None => None,
37+
}
38+
}
39+
}
40+
2741
#[derive(Eq, PartialEq)]
2842
struct AllocIdSnapshot<'a>(Option<AllocationSnapshot<'a>>);
2943

@@ -124,22 +138,6 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for Place
124138
}
125139
}
126140

127-
type PlaceExtraSnapshot<'a> = PlaceExtra<AllocIdSnapshot<'a>>;
128-
129-
impl<'a, Ctx> Snapshot<'a, Ctx> for PlaceExtra
130-
where Ctx: SnapshotContext<'a, To=Allocation, From=AllocId>,
131-
{
132-
type Item = PlaceExtraSnapshot<'a>;
133-
134-
fn snapshot(&self, ctx: &'a Ctx) -> Self::Item {
135-
match self {
136-
PlaceExtra::Vtable(p) => PlaceExtra::Vtable(p.snapshot(ctx)),
137-
PlaceExtra::Length(l) => PlaceExtra::Length(*l),
138-
PlaceExtra::None => PlaceExtra::None,
139-
}
140-
}
141-
}
142-
143141
type ValueSnapshot<'a> = Value<AllocIdSnapshot<'a>>;
144142

145143
impl<'a, Ctx> Snapshot<'a, Ctx> for Value
@@ -203,7 +201,7 @@ struct AllocationSnapshot<'a> {
203201
relocations: RelocationsSnapshot<'a>,
204202
undef_mask: &'a UndefMask,
205203
align: &'a Align,
206-
runtime_mutability: &'a Mutability,
204+
mutability: &'a Mutability,
207205
}
208206

209207
impl<'a, Ctx> Snapshot<'a, Ctx> for &'a Allocation
@@ -212,20 +210,20 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for &'a Allocation
212210
type Item = AllocationSnapshot<'a>;
213211

214212
fn snapshot(&self, ctx: &'a Ctx) -> Self::Item {
215-
let Allocation { bytes, relocations, undef_mask, align, runtime_mutability } = self;
213+
let Allocation { bytes, relocations, undef_mask, align, mutability } = self;
216214

217215
AllocationSnapshot {
218216
bytes,
219217
undef_mask,
220218
align,
221-
runtime_mutability,
219+
mutability,
222220
relocations: relocations.snapshot(ctx),
223221
}
224222
}
225223
}
226224

227225
#[derive(Eq, PartialEq)]
228-
struct FrameSnapshot<'a, 'tcx> {
226+
struct FrameSnapshot<'a, 'tcx: 'a> {
229227
instance: &'a ty::Instance<'tcx>,
230228
span: &'a Span,
231229
return_to_block: &'a StackPopCleanup,
@@ -269,6 +267,15 @@ struct MemorySnapshot<'a, 'mir: 'a, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx> + 'a
269267
data: &'a M::MemoryData,
270268
}
271269

270+
impl<'a, 'mir, 'tcx, M> Memory<'a, 'mir, 'tcx, M>
271+
where M: Machine<'mir, 'tcx>,
272+
{
273+
fn snapshot<'b: 'a>(&'b self) -> MemorySnapshot<'b, 'mir, 'tcx, M> {
274+
let Memory { data, .. } = self;
275+
MemorySnapshot { data }
276+
}
277+
}
278+
272279
impl<'a, 'b, 'mir, 'tcx, M> SnapshotContext<'b> for Memory<'a, 'mir, 'tcx, M>
273280
where M: Machine<'mir, 'tcx>,
274281
{
@@ -280,7 +287,6 @@ impl<'a, 'b, 'mir, 'tcx, M> SnapshotContext<'b> for Memory<'a, 'mir, 'tcx, M>
280287
}
281288

282289
/// The virtual machine state during const-evaluation at a given point in time.
283-
#[derive(Eq, PartialEq)]
284290
pub struct EvalSnapshot<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
285291
machine: M,
286292
memory: Memory<'a, 'mir, 'tcx, M>,
@@ -297,6 +303,11 @@ impl<'a, 'mir, 'tcx, M> EvalSnapshot<'a, 'mir, 'tcx, M>
297303
stack: stack.into(),
298304
}
299305
}
306+
307+
fn snapshot<'b: 'a>(&'b self) -> (&'b M, MemorySnapshot<'b, 'mir, 'tcx, M>, Vec<FrameSnapshot<'a, 'tcx>>) {
308+
let EvalSnapshot{ machine, memory, stack } = self;
309+
(&machine, memory.snapshot(), stack.iter().map(|frame| frame.snapshot(memory)).collect())
310+
}
300311
}
301312

302313
impl<'a, 'mir, 'tcx, M> Hash for EvalSnapshot<'a, 'mir, 'tcx, M>
@@ -319,3 +330,15 @@ impl<'a, 'b, 'mir, 'tcx, M> HashStable<StableHashingContext<'b>> for EvalSnapsho
319330
(machine, &memory.data, stack).hash_stable(hcx, hasher);
320331
}
321332
}
333+
334+
impl<'a, 'mir, 'tcx, M> Eq for EvalSnapshot<'a, 'mir, 'tcx, M>
335+
where M: Machine<'mir, 'tcx>,
336+
{}
337+
338+
impl<'a, 'mir, 'tcx, M> PartialEq for EvalSnapshot<'a, 'mir, 'tcx, M>
339+
where M: Machine<'mir, 'tcx>,
340+
{
341+
fn eq(&self, other: &Self) -> bool {
342+
self.snapshot() == other.snapshot()
343+
}
344+
}

0 commit comments

Comments
 (0)