From 015f470daa3020ebe92abf61fa4010b56bde006d Mon Sep 17 00:00:00 2001 From: Bruno Dutra Date: Sat, 4 Aug 2018 17:41:03 +0200 Subject: [PATCH] Move EvalSnapshot into its own module --- src/librustc_mir/interpret/eval_context.rs | 44 +------------------- src/librustc_mir/interpret/mod.rs | 1 + src/librustc_mir/interpret/snapshot.rs | 47 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/librustc_mir/interpret/snapshot.rs diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 8d4f3baf3a9b..b6a84ed8404e 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -9,7 +9,6 @@ // except according to those terms. use std::fmt::Write; -use std::hash::{Hash, Hasher}; use std::mem; use rustc::hir::def_id::DefId; @@ -40,6 +39,8 @@ use super::{ Memory, Machine }; +use super::snapshot::EvalSnapshot; + pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> { /// Stores the `Machine` instance. pub machine: M, @@ -207,47 +208,6 @@ impl_stable_hash_for!(enum self::LocalValue { Live(x), }); -/// The virtual machine state during const-evaluation at a given point in time. -#[derive(Eq, PartialEq)] -struct EvalSnapshot<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> { - machine: M, - memory: Memory<'a, 'mir, 'tcx, M>, - stack: Vec>, -} - -impl<'a, 'mir, 'tcx, M> EvalSnapshot<'a, 'mir, 'tcx, M> - where M: Machine<'mir, 'tcx>, -{ - fn new<'b>(machine: &M, memory: &Memory<'a, 'mir, 'tcx, M>, stack: &[Frame<'mir, 'tcx>]) -> Self { - EvalSnapshot { - machine: machine.clone(), - memory: memory.clone(), - stack: stack.into(), - } - } -} - -impl<'a, 'mir, 'tcx, M> Hash for EvalSnapshot<'a, 'mir, 'tcx, M> - where M: Machine<'mir, 'tcx>, -{ - fn hash(&self, state: &mut H) { - // Implement in terms of hash stable, so that k1 == k2 -> hash(k1) == hash(k2) - let mut hcx = self.memory.tcx.get_stable_hashing_context(); - let mut hasher = StableHasher::::new(); - self.hash_stable(&mut hcx, &mut hasher); - hasher.finish().hash(state) - } -} - -impl<'a, 'b, 'mir, 'tcx, M> HashStable> for EvalSnapshot<'a, 'mir, 'tcx, M> - where M: Machine<'mir, 'tcx>, -{ - fn hash_stable(&self, hcx: &mut StableHashingContext<'b>, hasher: &mut StableHasher) { - let EvalSnapshot{ machine, memory, stack } = self; - (machine, &memory.data, stack).hash_stable(hcx, hasher); - } -} - pub(super) struct InfiniteLoopDetector<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> { /// The set of all `EvalSnapshot` *hashes* observed by this detector. /// diff --git a/src/librustc_mir/interpret/mod.rs b/src/librustc_mir/interpret/mod.rs index 462c4b8889dd..1e8de0292324 100644 --- a/src/librustc_mir/interpret/mod.rs +++ b/src/librustc_mir/interpret/mod.rs @@ -17,6 +17,7 @@ mod operand; mod machine; mod memory; mod operator; +mod snapshot; mod step; mod terminator; mod traits; diff --git a/src/librustc_mir/interpret/snapshot.rs b/src/librustc_mir/interpret/snapshot.rs new file mode 100644 index 000000000000..45574da74102 --- /dev/null +++ b/src/librustc_mir/interpret/snapshot.rs @@ -0,0 +1,47 @@ +use std::hash::{Hash, Hasher}; + +use rustc::ich::{StableHashingContext, StableHashingContextProvider}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult}; + +use super::{Frame, Memory, Machine}; + +/// 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>, + stack: Vec>, +} + +impl<'a, 'mir, 'tcx, M> EvalSnapshot<'a, 'mir, 'tcx, M> + where M: Machine<'mir, 'tcx>, +{ + pub fn new(machine: &M, memory: &Memory<'a, 'mir, 'tcx, M>, stack: &[Frame<'mir, 'tcx>]) -> Self { + EvalSnapshot { + machine: machine.clone(), + memory: memory.clone(), + stack: stack.into(), + } + } +} + +impl<'a, 'mir, 'tcx, M> Hash for EvalSnapshot<'a, 'mir, 'tcx, M> + where M: Machine<'mir, 'tcx>, +{ + fn hash(&self, state: &mut H) { + // Implement in terms of hash stable, so that k1 == k2 -> hash(k1) == hash(k2) + let mut hcx = self.memory.tcx.get_stable_hashing_context(); + let mut hasher = StableHasher::::new(); + self.hash_stable(&mut hcx, &mut hasher); + hasher.finish().hash(state) + } +} + +impl<'a, 'b, 'mir, 'tcx, M> HashStable> for EvalSnapshot<'a, 'mir, 'tcx, M> + where M: Machine<'mir, 'tcx>, +{ + fn hash_stable(&self, hcx: &mut StableHashingContext<'b>, hasher: &mut StableHasher) { + let EvalSnapshot{ machine, memory, stack } = self; + (machine, &memory.data, stack).hash_stable(hcx, hasher); + } +}