add a deterministic_hash method to DefPath

Produces a deterministic hash, at least for a single platform /
compiler-version.
This commit is contained in:
Niko Matsakis 2016-08-05 20:12:20 -04:00
parent d4bd0544ca
commit 8150494ac2
2 changed files with 14 additions and 1 deletions

View File

@ -13,6 +13,7 @@
use hir::map::def_collector::DefCollector;
use rustc_data_structures::fnv::FnvHashMap;
use std::fmt::Write;
use std::hash::{Hash, Hasher, SipHasher};
use syntax::{ast, visit};
use syntax::parse::token::InternedString;
use ty::TyCtxt;
@ -133,6 +134,18 @@ pub fn to_string(&self, tcx: TyCtxt) -> String {
s
}
pub fn deterministic_hash(&self, tcx: TyCtxt) -> u64 {
let mut state = SipHasher::new();
self.deterministic_hash_to(tcx, &mut state);
state.finish()
}
pub fn deterministic_hash_to<H: Hasher>(&self, tcx: TyCtxt, state: &mut H) {
tcx.crate_name(self.krate).hash(state);
tcx.crate_disambiguator(self.krate).hash(state);
self.data.hash(state);
}
}
/// Root of an inlined item. We track the `DefPath` of the item within

View File

@ -144,7 +144,7 @@ pub fn new(st: &'a mut SipHasher,
}
fn hash_def_path(&mut self, path: &DefPath) {
path.to_string(self.tcx).hash(self.st);
path.deterministic_hash_to(self.tcx, self.st);
}
}