Skip to content

Commit ff64b42

Browse files
authored
Merge pull request #387 from ecstatic-morse/eq-hash
Implement `Eq`, `Clone` and `Hash` for MemoryData and Evaluator
2 parents 19e214e + a2f4d84 commit ff64b42

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/lib.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ use rustc::ty::subst::Subst;
2727
use rustc::hir::def_id::DefId;
2828
use rustc::mir;
2929

30+
use rustc_data_structures::fx::FxHasher;
31+
3032
use syntax::ast::Mutability;
3133
use syntax::codemap::Span;
3234

3335
use std::collections::{HashMap, BTreeMap};
36+
use std::hash::{Hash, Hasher};
3437

3538
pub use rustc::mir::interpret::*;
3639
pub use rustc_mir::interpret::*;
@@ -296,7 +299,7 @@ pub fn eval_main<'a, 'tcx: 'a>(
296299
}
297300
}
298301

299-
#[derive(Default)]
302+
#[derive(Clone, Default, PartialEq, Eq)]
300303
pub struct Evaluator<'tcx> {
301304
/// Environment variables set by `setenv`
302305
/// Miri does not expose env vars from the host to the emulated program
@@ -306,15 +309,34 @@ pub struct Evaluator<'tcx> {
306309
pub(crate) suspended: HashMap<DynamicLifetime, Vec<ValidationQuery<'tcx>>>,
307310
}
308311

312+
impl<'tcx> Hash for Evaluator<'tcx> {
313+
fn hash<H: Hasher>(&self, state: &mut H) {
314+
let Evaluator {
315+
env_vars,
316+
suspended: _,
317+
} = self;
318+
319+
env_vars.iter()
320+
.map(|(env, ptr)| {
321+
let mut h = FxHasher::default();
322+
env.hash(&mut h);
323+
ptr.hash(&mut h);
324+
h.finish()
325+
})
326+
.fold(0u64, |acc, hash| acc.wrapping_add(hash))
327+
.hash(state);
328+
}
329+
}
330+
309331
pub type TlsKey = u128;
310332

311-
#[derive(Copy, Clone, Debug)]
333+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
312334
pub struct TlsEntry<'tcx> {
313335
data: Scalar, // Will eventually become a map from thread IDs to `Scalar`s, if we ever support more than one thread.
314336
dtor: Option<ty::Instance<'tcx>>,
315337
}
316338

317-
#[derive(Default)]
339+
#[derive(Clone, Default, PartialEq, Eq)]
318340
pub struct MemoryData<'tcx> {
319341
/// The Key to use for the next thread-local allocation.
320342
next_thread_local: TlsKey,
@@ -331,6 +353,19 @@ pub struct MemoryData<'tcx> {
331353
statics: HashMap<GlobalId<'tcx>, AllocId>,
332354
}
333355

356+
impl<'tcx> Hash for MemoryData<'tcx> {
357+
fn hash<H: Hasher>(&self, state: &mut H) {
358+
let MemoryData {
359+
next_thread_local: _,
360+
thread_local,
361+
locks: _,
362+
statics: _,
363+
} = self;
364+
365+
thread_local.hash(state);
366+
}
367+
}
368+
334369
impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
335370
type MemoryData = MemoryData<'tcx>;
336371
type MemoryKinds = memory::MemoryKind;

src/locks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::ty::layout::Size;
77
////////////////////////////////////////////////////////////////////////////////
88

99
/// Information about a lock that is currently held.
10-
#[derive(Clone, Debug)]
10+
#[derive(Clone, Debug, PartialEq, Eq)]
1111
pub struct LockInfo<'tcx> {
1212
/// Stores for which lifetimes (of the original write lock) we got
1313
/// which suspensions.

src/range_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use std::collections::BTreeMap;
88
use std::ops;
99

10-
#[derive(Clone, Debug)]
10+
#[derive(Clone, Debug, PartialEq, Eq)]
1111
pub struct RangeMap<T> {
1212
map: BTreeMap<Range, T>,
1313
}

0 commit comments

Comments
 (0)