Skip to content

Commit 8150494

Browse files
committed
add a deterministic_hash method to DefPath
Produces a deterministic hash, at least for a single platform / compiler-version.
1 parent d4bd054 commit 8150494

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/librustc/hir/map/definitions.rs

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use hir::def_id::{DefId, DefIndex};
1313
use hir::map::def_collector::DefCollector;
1414
use rustc_data_structures::fnv::FnvHashMap;
1515
use std::fmt::Write;
16+
use std::hash::{Hash, Hasher, SipHasher};
1617
use syntax::{ast, visit};
1718
use syntax::parse::token::InternedString;
1819
use ty::TyCtxt;
@@ -133,6 +134,18 @@ impl DefPath {
133134

134135
s
135136
}
137+
138+
pub fn deterministic_hash(&self, tcx: TyCtxt) -> u64 {
139+
let mut state = SipHasher::new();
140+
self.deterministic_hash_to(tcx, &mut state);
141+
state.finish()
142+
}
143+
144+
pub fn deterministic_hash_to<H: Hasher>(&self, tcx: TyCtxt, state: &mut H) {
145+
tcx.crate_name(self.krate).hash(state);
146+
tcx.crate_disambiguator(self.krate).hash(state);
147+
self.data.hash(state);
148+
}
136149
}
137150

138151
/// Root of an inlined item. We track the `DefPath` of the item within

src/librustc_incremental/calculate_svh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod svh_visitor {
144144
}
145145

146146
fn hash_def_path(&mut self, path: &DefPath) {
147-
path.to_string(self.tcx).hash(self.st);
147+
path.deterministic_hash_to(self.tcx, self.st);
148148
}
149149
}
150150

0 commit comments

Comments
 (0)