Skip to content

Commit

Permalink
Impl Eq and PartialEq for EvalSnapshot in terms of the Snapshot trait
Browse files Browse the repository at this point in the history
  • Loading branch information
brunocodutra committed Sep 3, 2018
1 parent bf6ba97 commit 927c709
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 69 deletions.
26 changes: 0 additions & 26 deletions src/librustc_mir/interpret/eval_context.rs
Expand Up @@ -111,32 +111,6 @@ pub struct Frame<'mir, 'tcx: 'mir> {
pub stmt: usize,
}

impl<'mir, 'tcx: 'mir> Eq for Frame<'mir, 'tcx> {}

impl<'mir, 'tcx: 'mir> PartialEq for Frame<'mir, 'tcx> {
fn eq(&self, other: &Self) -> bool {
let Frame {
mir: _,
instance,
span: _,
return_to_block,
return_place,
locals,
block,
stmt,
} = self;

// Some of these are constant during evaluation, but are included
// anyways for correctness.
*instance == other.instance
&& *return_to_block == other.return_to_block
&& *return_place == other.return_place
&& *locals == other.locals
&& *block == other.block
&& *stmt == other.stmt
}
}

impl<'a, 'mir, 'tcx: 'mir> HashStable<StableHashingContext<'a>> for Frame<'mir, 'tcx> {
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher<W>) {
let Frame {
Expand Down
21 changes: 0 additions & 21 deletions src/librustc_mir/interpret/memory.rs
Expand Up @@ -69,27 +69,6 @@ impl<'a, 'b, 'c, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout
}
}

impl<'a, 'mir, 'tcx, M> Eq for Memory<'a, 'mir, 'tcx, M>
where M: Machine<'mir, 'tcx>,
'tcx: 'a + 'mir,
{}

impl<'a, 'mir, 'tcx, M> PartialEq for Memory<'a, 'mir, 'tcx, M>
where M: Machine<'mir, 'tcx>,
'tcx: 'a + 'mir,
{
fn eq(&self, other: &Self) -> bool {
let Memory {
data,
alloc_map,
tcx: _,
} = self;

*data == other.data
&& *alloc_map == other.alloc_map
}
}

impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
pub fn new(tcx: TyCtxtAt<'a, 'tcx, 'tcx>, data: M::MemoryData) -> Self {
Memory {
Expand Down
67 changes: 45 additions & 22 deletions src/librustc_mir/interpret/snapshot.rs
Expand Up @@ -11,7 +11,7 @@ use syntax::ast::Mutability;
use syntax::source_map::Span;

use super::eval_context::{LocalValue, StackPopCleanup};
use super::{Frame, Memory, Machine, Operand, MemPlace, Place, PlaceExtra, Value};
use super::{Frame, Memory, Machine, Operand, MemPlace, Place, Value};

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

impl<'a, Ctx, T> Snapshot<'a, Ctx> for Option<T>
where Ctx: SnapshotContext<'a>,
T: Snapshot<'a, Ctx>
{
type Item = Option<<T as Snapshot<'a, Ctx>>::Item>;

fn snapshot(&self, ctx: &'a Ctx) -> Self::Item {
match self {
Some(x) => Some(x.snapshot(ctx)),
None => None,
}
}
}

#[derive(Eq, PartialEq)]
struct AllocIdSnapshot<'a>(Option<AllocationSnapshot<'a>>);

Expand Down Expand Up @@ -124,22 +138,6 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for Place
}
}

type PlaceExtraSnapshot<'a> = PlaceExtra<AllocIdSnapshot<'a>>;

impl<'a, Ctx> Snapshot<'a, Ctx> for PlaceExtra
where Ctx: SnapshotContext<'a, To=Allocation, From=AllocId>,
{
type Item = PlaceExtraSnapshot<'a>;

fn snapshot(&self, ctx: &'a Ctx) -> Self::Item {
match self {
PlaceExtra::Vtable(p) => PlaceExtra::Vtable(p.snapshot(ctx)),
PlaceExtra::Length(l) => PlaceExtra::Length(*l),
PlaceExtra::None => PlaceExtra::None,
}
}
}

type ValueSnapshot<'a> = Value<AllocIdSnapshot<'a>>;

impl<'a, Ctx> Snapshot<'a, Ctx> for Value
Expand Down Expand Up @@ -203,7 +201,7 @@ struct AllocationSnapshot<'a> {
relocations: RelocationsSnapshot<'a>,
undef_mask: &'a UndefMask,
align: &'a Align,
runtime_mutability: &'a Mutability,
mutability: &'a Mutability,
}

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

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

AllocationSnapshot {
bytes,
undef_mask,
align,
runtime_mutability,
mutability,
relocations: relocations.snapshot(ctx),
}
}
}

#[derive(Eq, PartialEq)]
struct FrameSnapshot<'a, 'tcx> {
struct FrameSnapshot<'a, 'tcx: 'a> {
instance: &'a ty::Instance<'tcx>,
span: &'a Span,
return_to_block: &'a StackPopCleanup,
Expand Down Expand Up @@ -269,6 +267,15 @@ struct MemorySnapshot<'a, 'mir: 'a, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx> + 'a
data: &'a M::MemoryData,
}

impl<'a, 'mir, 'tcx, M> Memory<'a, 'mir, 'tcx, M>
where M: Machine<'mir, 'tcx>,
{
fn snapshot<'b: 'a>(&'b self) -> MemorySnapshot<'b, 'mir, 'tcx, M> {
let Memory { data, .. } = self;
MemorySnapshot { data }
}
}

impl<'a, 'b, 'mir, 'tcx, M> SnapshotContext<'b> for Memory<'a, 'mir, 'tcx, M>
where M: Machine<'mir, 'tcx>,
{
Expand All @@ -280,7 +287,6 @@ impl<'a, 'b, 'mir, 'tcx, M> SnapshotContext<'b> for Memory<'a, 'mir, 'tcx, M>
}

/// The virtual machine state during const-evaluation at a given point in time.
#[derive(Eq, PartialEq)]
pub struct EvalSnapshot<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
machine: M,
memory: Memory<'a, 'mir, 'tcx, M>,
Expand All @@ -297,6 +303,11 @@ impl<'a, 'mir, 'tcx, M> EvalSnapshot<'a, 'mir, 'tcx, M>
stack: stack.into(),
}
}

fn snapshot<'b: 'a>(&'b self) -> (&'b M, MemorySnapshot<'b, 'mir, 'tcx, M>, Vec<FrameSnapshot<'a, 'tcx>>) {
let EvalSnapshot{ machine, memory, stack } = self;
(&machine, memory.snapshot(), stack.iter().map(|frame| frame.snapshot(memory)).collect())
}
}

impl<'a, 'mir, 'tcx, M> Hash for EvalSnapshot<'a, 'mir, 'tcx, M>
Expand All @@ -319,3 +330,15 @@ impl<'a, 'b, 'mir, 'tcx, M> HashStable<StableHashingContext<'b>> for EvalSnapsho
(machine, &memory.data, stack).hash_stable(hcx, hasher);
}
}

impl<'a, 'mir, 'tcx, M> Eq for EvalSnapshot<'a, 'mir, 'tcx, M>
where M: Machine<'mir, 'tcx>,
{}

impl<'a, 'mir, 'tcx, M> PartialEq for EvalSnapshot<'a, 'mir, 'tcx, M>
where M: Machine<'mir, 'tcx>,
{
fn eq(&self, other: &Self) -> bool {
self.snapshot() == other.snapshot()
}
}

0 comments on commit 927c709

Please sign in to comment.