From 927c709eb99e5243975db2ddc6c40cb06a6f472c Mon Sep 17 00:00:00 2001 From: Bruno Dutra Date: Sat, 18 Aug 2018 14:10:46 +0200 Subject: [PATCH] Impl Eq and PartialEq for EvalSnapshot in terms of the Snapshot trait --- src/librustc_mir/interpret/eval_context.rs | 26 --------- src/librustc_mir/interpret/memory.rs | 21 ------- src/librustc_mir/interpret/snapshot.rs | 67 +++++++++++++++------- 3 files changed, 45 insertions(+), 69 deletions(-) diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 02d87bdb7dc..cc6d6d433f8 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -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> for Frame<'mir, 'tcx> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { let Frame { diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 4a291f164cc..9e61de92936 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -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 { diff --git a/src/librustc_mir/interpret/snapshot.rs b/src/librustc_mir/interpret/snapshot.rs index d23122825aa..66364a7390c 100644 --- a/src/librustc_mir/interpret/snapshot.rs +++ b/src/librustc_mir/interpret/snapshot.rs @@ -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; @@ -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 + where Ctx: SnapshotContext<'a>, + T: Snapshot<'a, Ctx> +{ + type Item = Option<>::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>); @@ -124,22 +138,6 @@ impl<'a, Ctx> Snapshot<'a, Ctx> for Place } } -type PlaceExtraSnapshot<'a> = PlaceExtra>; - -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>; impl<'a, Ctx> Snapshot<'a, Ctx> for Value @@ -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 @@ -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, @@ -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>, { @@ -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>, @@ -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>) { + 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> @@ -319,3 +330,15 @@ impl<'a, 'b, 'mir, 'tcx, M> HashStable> 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() + } +}