Skip to content

Commit 015f470

Browse files
committed
Move EvalSnapshot into its own module
1 parent a083aa0 commit 015f470

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

src/librustc_mir/interpret/eval_context.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use std::fmt::Write;
12-
use std::hash::{Hash, Hasher};
1312
use std::mem;
1413

1514
use rustc::hir::def_id::DefId;
@@ -40,6 +39,8 @@ use super::{
4039
Memory, Machine
4140
};
4241

42+
use super::snapshot::EvalSnapshot;
43+
4344
pub struct EvalContext<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
4445
/// Stores the `Machine` instance.
4546
pub machine: M,
@@ -207,47 +208,6 @@ impl_stable_hash_for!(enum self::LocalValue {
207208
Live(x),
208209
});
209210

210-
/// The virtual machine state during const-evaluation at a given point in time.
211-
#[derive(Eq, PartialEq)]
212-
struct EvalSnapshot<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
213-
machine: M,
214-
memory: Memory<'a, 'mir, 'tcx, M>,
215-
stack: Vec<Frame<'mir, 'tcx>>,
216-
}
217-
218-
impl<'a, 'mir, 'tcx, M> EvalSnapshot<'a, 'mir, 'tcx, M>
219-
where M: Machine<'mir, 'tcx>,
220-
{
221-
fn new<'b>(machine: &M, memory: &Memory<'a, 'mir, 'tcx, M>, stack: &[Frame<'mir, 'tcx>]) -> Self {
222-
EvalSnapshot {
223-
machine: machine.clone(),
224-
memory: memory.clone(),
225-
stack: stack.into(),
226-
}
227-
}
228-
}
229-
230-
impl<'a, 'mir, 'tcx, M> Hash for EvalSnapshot<'a, 'mir, 'tcx, M>
231-
where M: Machine<'mir, 'tcx>,
232-
{
233-
fn hash<H: Hasher>(&self, state: &mut H) {
234-
// Implement in terms of hash stable, so that k1 == k2 -> hash(k1) == hash(k2)
235-
let mut hcx = self.memory.tcx.get_stable_hashing_context();
236-
let mut hasher = StableHasher::<u64>::new();
237-
self.hash_stable(&mut hcx, &mut hasher);
238-
hasher.finish().hash(state)
239-
}
240-
}
241-
242-
impl<'a, 'b, 'mir, 'tcx, M> HashStable<StableHashingContext<'b>> for EvalSnapshot<'a, 'mir, 'tcx, M>
243-
where M: Machine<'mir, 'tcx>,
244-
{
245-
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut StableHashingContext<'b>, hasher: &mut StableHasher<W>) {
246-
let EvalSnapshot{ machine, memory, stack } = self;
247-
(machine, &memory.data, stack).hash_stable(hcx, hasher);
248-
}
249-
}
250-
251211
pub(super) struct InfiniteLoopDetector<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
252212
/// The set of all `EvalSnapshot` *hashes* observed by this detector.
253213
///

src/librustc_mir/interpret/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod operand;
1717
mod machine;
1818
mod memory;
1919
mod operator;
20+
mod snapshot;
2021
mod step;
2122
mod terminator;
2223
mod traits;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::hash::{Hash, Hasher};
2+
3+
use rustc::ich::{StableHashingContext, StableHashingContextProvider};
4+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableHasherResult};
5+
6+
use super::{Frame, Memory, Machine};
7+
8+
/// The virtual machine state during const-evaluation at a given point in time.
9+
#[derive(Eq, PartialEq)]
10+
pub struct EvalSnapshot<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
11+
machine: M,
12+
memory: Memory<'a, 'mir, 'tcx, M>,
13+
stack: Vec<Frame<'mir, 'tcx>>,
14+
}
15+
16+
impl<'a, 'mir, 'tcx, M> EvalSnapshot<'a, 'mir, 'tcx, M>
17+
where M: Machine<'mir, 'tcx>,
18+
{
19+
pub fn new(machine: &M, memory: &Memory<'a, 'mir, 'tcx, M>, stack: &[Frame<'mir, 'tcx>]) -> Self {
20+
EvalSnapshot {
21+
machine: machine.clone(),
22+
memory: memory.clone(),
23+
stack: stack.into(),
24+
}
25+
}
26+
}
27+
28+
impl<'a, 'mir, 'tcx, M> Hash for EvalSnapshot<'a, 'mir, 'tcx, M>
29+
where M: Machine<'mir, 'tcx>,
30+
{
31+
fn hash<H: Hasher>(&self, state: &mut H) {
32+
// Implement in terms of hash stable, so that k1 == k2 -> hash(k1) == hash(k2)
33+
let mut hcx = self.memory.tcx.get_stable_hashing_context();
34+
let mut hasher = StableHasher::<u64>::new();
35+
self.hash_stable(&mut hcx, &mut hasher);
36+
hasher.finish().hash(state)
37+
}
38+
}
39+
40+
impl<'a, 'b, 'mir, 'tcx, M> HashStable<StableHashingContext<'b>> for EvalSnapshot<'a, 'mir, 'tcx, M>
41+
where M: Machine<'mir, 'tcx>,
42+
{
43+
fn hash_stable<W: StableHasherResult>(&self, hcx: &mut StableHashingContext<'b>, hasher: &mut StableHasher<W>) {
44+
let EvalSnapshot{ machine, memory, stack } = self;
45+
(machine, &memory.data, stack).hash_stable(hcx, hasher);
46+
}
47+
}

0 commit comments

Comments
 (0)