Skip to content

Commit 67c84e0

Browse files
incr.comp.: Use StableHash impls instead of functions for hashing most maps.
1 parent b9816c5 commit 67c84e0

File tree

8 files changed

+118
-247
lines changed

8 files changed

+118
-247
lines changed

src/librustc/ich/hcx.rs

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ use hir::map::DefPathHash;
1414
use ich::{self, CachingCodemapView};
1515
use session::config::DebugInfoLevel::NoDebugInfo;
1616
use ty::{self, TyCtxt, fast_reject};
17-
use util::nodemap::{NodeMap, NodeSet, ItemLocalMap};
1817

1918
use std::cmp::Ord;
2019
use std::hash as std_hash;
21-
use std::collections::{HashMap, HashSet, BTreeMap};
20+
use std::collections::HashMap;
2221

2322
use syntax::ast;
2423
use syntax::attr;
@@ -337,111 +336,6 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Span {
337336
}
338337
}
339338

340-
pub fn hash_stable_hashmap<'a, 'gcx, 'tcx, K, V, R, SK, F, W>(
341-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
342-
hasher: &mut StableHasher<W>,
343-
map: &HashMap<K, V, R>,
344-
extract_stable_key: F)
345-
where K: Eq + std_hash::Hash,
346-
V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
347-
R: std_hash::BuildHasher,
348-
SK: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + Ord + Clone,
349-
F: Fn(&mut StableHashingContext<'a, 'gcx, 'tcx>, &K) -> SK,
350-
W: StableHasherResult,
351-
{
352-
let mut keys: Vec<_> = map.keys()
353-
.map(|k| (extract_stable_key(hcx, k), k))
354-
.collect();
355-
keys.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
356-
keys.len().hash_stable(hcx, hasher);
357-
for (stable_key, key) in keys {
358-
stable_key.hash_stable(hcx, hasher);
359-
map[key].hash_stable(hcx, hasher);
360-
}
361-
}
362-
363-
pub fn hash_stable_hashset<'a, 'tcx, 'gcx, K, R, SK, F, W>(
364-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
365-
hasher: &mut StableHasher<W>,
366-
set: &HashSet<K, R>,
367-
extract_stable_key: F)
368-
where K: Eq + std_hash::Hash,
369-
R: std_hash::BuildHasher,
370-
SK: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + Ord + Clone,
371-
F: Fn(&mut StableHashingContext<'a, 'gcx, 'tcx>, &K) -> SK,
372-
W: StableHasherResult,
373-
{
374-
let mut keys: Vec<_> = set.iter()
375-
.map(|k| extract_stable_key(hcx, k))
376-
.collect();
377-
keys.sort_unstable();
378-
keys.hash_stable(hcx, hasher);
379-
}
380-
381-
pub fn hash_stable_nodemap<'a, 'tcx, 'gcx, V, W>(
382-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
383-
hasher: &mut StableHasher<W>,
384-
map: &NodeMap<V>)
385-
where V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
386-
W: StableHasherResult,
387-
{
388-
let definitions = hcx.tcx.hir.definitions();
389-
hash_stable_hashmap(hcx, hasher, map, |_, node_id| {
390-
let hir_id = definitions.node_to_hir_id(*node_id);
391-
let owner_def_path_hash = definitions.def_path_hash(hir_id.owner);
392-
(owner_def_path_hash, hir_id.local_id)
393-
});
394-
}
395-
396-
pub fn hash_stable_nodeset<'a, 'tcx, 'gcx, W>(
397-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
398-
hasher: &mut StableHasher<W>,
399-
map: &NodeSet)
400-
where W: StableHasherResult,
401-
{
402-
let definitions = hcx.tcx.hir.definitions();
403-
hash_stable_hashset(hcx, hasher, map, |_, node_id| {
404-
let hir_id = definitions.node_to_hir_id(*node_id);
405-
let owner_def_path_hash = definitions.def_path_hash(hir_id.owner);
406-
(owner_def_path_hash, hir_id.local_id)
407-
});
408-
}
409-
410-
pub fn hash_stable_itemlocalmap<'a, 'tcx, 'gcx, V, W>(
411-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
412-
hasher: &mut StableHasher<W>,
413-
map: &ItemLocalMap<V>)
414-
where V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
415-
W: StableHasherResult,
416-
{
417-
hash_stable_hashmap(hcx, hasher, map, |_, local_id| {
418-
*local_id
419-
});
420-
}
421-
422-
423-
pub fn hash_stable_btreemap<'a, 'tcx, 'gcx, K, V, SK, F, W>(
424-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
425-
hasher: &mut StableHasher<W>,
426-
map: &BTreeMap<K, V>,
427-
extract_stable_key: F)
428-
where K: Eq + Ord,
429-
V: HashStable<StableHashingContext<'a, 'gcx, 'tcx>>,
430-
SK: HashStable<StableHashingContext<'a, 'gcx, 'tcx>> + Ord + Clone,
431-
F: Fn(&mut StableHashingContext<'a, 'gcx, 'tcx>, &K) -> SK,
432-
W: StableHasherResult,
433-
{
434-
let mut keys: Vec<_> = map.keys()
435-
.map(|k| (extract_stable_key(hcx, k), k))
436-
.collect();
437-
keys.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
438-
keys.len().hash_stable(hcx, hasher);
439-
for (stable_key, key) in keys {
440-
stable_key.hash_stable(hcx, hasher);
441-
map[key].hash_stable(hcx, hasher);
442-
}
443-
}
444-
445339
pub fn hash_stable_trait_impls<'a, 'tcx, 'gcx, W, R>(
446340
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
447341
hasher: &mut StableHasher<W>,

src/librustc/ich/impls_ty.rs

Lines changed: 27 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
//! This module contains `HashStable` implementations for various data types
1212
//! from rustc::ty in no particular order.
1313
14-
use ich::{self, StableHashingContext, NodeIdHashingMode};
15-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
16-
StableHasherResult};
14+
use ich::{StableHashingContext, NodeIdHashingMode};
15+
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
16+
StableHasher, StableHasherResult};
1717
use std::hash as std_hash;
1818
use std::mem;
1919
use middle::region;
@@ -124,9 +124,10 @@ for ty::adjustment::Adjust<'gcx> {
124124

125125
impl_stable_hash_for!(struct ty::adjustment::Adjustment<'tcx> { kind, target });
126126
impl_stable_hash_for!(struct ty::adjustment::OverloadedDeref<'tcx> { region, mutbl });
127-
impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
128127
impl_stable_hash_for!(struct ty::UpvarBorrow<'tcx> { kind, region });
129128

129+
impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
130+
130131
impl_stable_hash_for!(enum ty::BorrowKind {
131132
ImmBorrow,
132133
UniqueImmBorrow,
@@ -513,28 +514,20 @@ impl_stable_hash_for!(enum ty::cast::CastKind {
513514
FnPtrAddrCast
514515
});
515516

516-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
517-
for region::Scope
518-
{
519-
fn hash_stable<W: StableHasherResult>(&self,
520-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
521-
hasher: &mut StableHasher<W>) {
522-
mem::discriminant(self).hash_stable(hcx, hasher);
523-
hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
524-
match *self {
525-
region::Scope::Node(node_id) |
526-
region::Scope::Destruction(node_id) => {
527-
node_id.hash_stable(hcx, hasher);
528-
}
529-
region::Scope::CallSite(body_id) |
530-
region::Scope::Arguments(body_id) => {
531-
body_id.hash_stable(hcx, hasher);
532-
}
533-
region::Scope::Remainder(block_remainder) => {
534-
block_remainder.hash_stable(hcx, hasher);
535-
}
536-
}
537-
})
517+
impl_stable_hash_for!(enum ::middle::region::Scope {
518+
Node(local_id),
519+
Destruction(local_id),
520+
CallSite(local_id),
521+
Arguments(local_id),
522+
Remainder(block_remainder)
523+
});
524+
525+
impl<'a, 'gcx, 'tcx> ToStableHashKey<StableHashingContext<'a, 'gcx, 'tcx>> for region::Scope {
526+
type KeyType = region::Scope;
527+
528+
#[inline]
529+
fn to_stable_hash_key(&self, _: &StableHashingContext<'a, 'gcx, 'tcx>) -> region::Scope {
530+
*self
538531
}
539532
}
540533

@@ -770,10 +763,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::Cr
770763
} = *self;
771764

772765
dependencies.hash_stable(hcx, hasher);
773-
774-
ich::hash_stable_hashmap(hcx, hasher, variances, |hcx, def_id| {
775-
hcx.def_path_hash(*def_id)
776-
});
766+
variances.hash_stable(hcx, hasher);
777767
}
778768
}
779769

@@ -836,25 +826,14 @@ for ::middle::privacy::AccessLevels {
836826
ref map
837827
} = *self;
838828

839-
ich::hash_stable_nodemap(hcx, hasher, map);
829+
map.hash_stable(hcx, hasher);
840830
});
841831
}
842832
}
843833

844-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
845-
for ty::CrateInherentImpls {
846-
fn hash_stable<W: StableHasherResult>(&self,
847-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
848-
hasher: &mut StableHasher<W>) {
849-
let ty::CrateInherentImpls {
850-
ref inherent_impls,
851-
} = *self;
852-
853-
ich::hash_stable_hashmap(hcx, hasher, inherent_impls, |hcx, def_id| {
854-
hcx.def_path_hash(*def_id)
855-
});
856-
}
857-
}
834+
impl_stable_hash_for!(struct ty::CrateInherentImpls {
835+
inherent_impls
836+
});
858837

859838
impl_stable_hash_for!(enum ::session::CompileIncomplete {
860839
Stopped,
@@ -863,14 +842,6 @@ impl_stable_hash_for!(enum ::session::CompileIncomplete {
863842

864843
impl_stable_hash_for!(struct ::util::common::ErrorReported {});
865844

866-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>>
867-
for ::middle::reachable::ReachableSet {
868-
fn hash_stable<W: StableHasherResult>(&self,
869-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
870-
hasher: &mut StableHasher<W>) {
871-
let ::middle::reachable::ReachableSet(ref reachable_set) = *self;
872-
873-
ich::hash_stable_nodeset(hcx, hasher, reachable_set);
874-
}
875-
}
876-
845+
impl_stable_hash_for!(tuple_struct ::middle::reachable::ReachableSet {
846+
reachable_set
847+
});

src/librustc/ich/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
1313
pub use self::fingerprint::Fingerprint;
1414
pub use self::caching_codemap_view::CachingCodemapView;
15-
pub use self::hcx::{StableHashingContext, NodeIdHashingMode, hash_stable_hashmap,
16-
hash_stable_hashset, hash_stable_nodemap, hash_stable_nodeset,
17-
hash_stable_btreemap, hash_stable_itemlocalmap,
15+
pub use self::hcx::{StableHashingContext, NodeIdHashingMode,
1816
hash_stable_trait_impls};
1917
mod fingerprint;
2018
mod caching_codemap_view;

src/librustc/lint/levels.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use std::cmp;
1212

1313
use errors::DiagnosticBuilder;
1414
use hir::HirId;
15-
use ich::{self, StableHashingContext};
15+
use ich::StableHashingContext;
1616
use lint::builtin;
1717
use lint::context::CheckLintNameResult;
1818
use lint::{self, Lint, LintId, Level, LintSource};
19-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher,
20-
StableHasherResult};
19+
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
20+
StableHasher, StableHasherResult};
2121
use session::Session;
2222
use syntax::ast;
2323
use syntax::attr;
@@ -396,10 +396,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintLe
396396
ref id_to_set,
397397
} = *self;
398398

399-
let definitions = hcx.tcx().hir.definitions();
400-
ich::hash_stable_hashmap(hcx, hasher, id_to_set, |_, hir_id| {
401-
(definitions.def_path_hash(hir_id.owner), hir_id.local_id)
402-
});
399+
id_to_set.hash_stable(hcx, hasher);
403400

404401
let LintLevelSets {
405402
ref list,
@@ -418,14 +415,10 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintLe
418415

419416
match *lint_set {
420417
LintSet::CommandLine { ref specs } => {
421-
ich::hash_stable_hashmap(hcx, hasher, specs, |_, lint_id| {
422-
lint_id.lint_name_raw()
423-
});
418+
specs.hash_stable(hcx, hasher);
424419
}
425420
LintSet::Node { ref specs, parent } => {
426-
ich::hash_stable_hashmap(hcx, hasher, specs, |_, lint_id| {
427-
lint_id.lint_name_raw()
428-
});
421+
specs.hash_stable(hcx, hasher);
429422
parent.hash_stable(hcx, hasher);
430423
}
431424
}
@@ -434,12 +427,20 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintLe
434427
}
435428
}
436429

437-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for LintId {
430+
impl<HCX> HashStable<HCX> for LintId {
438431
#[inline]
439432
fn hash_stable<W: StableHasherResult>(&self,
440-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
433+
hcx: &mut HCX,
441434
hasher: &mut StableHasher<W>) {
442435
self.lint_name_raw().hash_stable(hcx, hasher);
443436
}
444437
}
445438

439+
impl<HCX> ToStableHashKey<HCX> for LintId {
440+
type KeyType = &'static str;
441+
442+
#[inline]
443+
fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
444+
self.lint_name_raw()
445+
}
446+
}

src/librustc/middle/region.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! Most of the documentation on regions can be found in
1515
//! `middle/infer/region_inference/README.md`
1616
17-
use ich::{self, StableHashingContext, NodeIdHashingMode};
17+
use ich::{StableHashingContext, NodeIdHashingMode};
1818
use util::nodemap::{FxHashMap, FxHashSet};
1919
use ty;
2020

@@ -1259,22 +1259,11 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ScopeT
12591259
root_parent.hash_stable(hcx, hasher);
12601260
});
12611261

1262-
ich::hash_stable_hashmap(hcx, hasher, parent_map, |hcx, scope| {
1263-
let mut hasher = StableHasher::new();
1264-
scope.hash_stable(hcx, &mut hasher);
1265-
let stable: u64 = hasher.finish();
1266-
stable
1267-
});
1268-
1269-
ich::hash_stable_itemlocalmap(hcx, hasher, var_map);
1270-
ich::hash_stable_itemlocalmap(hcx, hasher, destruction_scopes);
1271-
ich::hash_stable_itemlocalmap(hcx, hasher, rvalue_scopes);
1272-
ich::hash_stable_itemlocalmap(hcx, hasher, closure_tree);
1273-
ich::hash_stable_hashmap(hcx, hasher, yield_in_scope, |hcx, scope| {
1274-
let mut hasher = StableHasher::new();
1275-
scope.hash_stable(hcx, &mut hasher);
1276-
let stable: u64 = hasher.finish();
1277-
stable
1278-
});
1262+
parent_map.hash_stable(hcx, hasher);
1263+
var_map.hash_stable(hcx, hasher);
1264+
destruction_scopes.hash_stable(hcx, hasher);
1265+
rvalue_scopes.hash_stable(hcx, hasher);
1266+
closure_tree.hash_stable(hcx, hasher);
1267+
yield_in_scope.hash_stable(hcx, hasher);
12791268
}
12801269
}

src/librustc/traits/specialize/specialization_graph.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -382,20 +382,7 @@ impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Childr
382382
}
383383
}
384384

385-
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for Graph {
386-
fn hash_stable<W: StableHasherResult>(&self,
387-
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,
388-
hasher: &mut StableHasher<W>) {
389-
let Graph {
390-
ref parent,
391-
ref children,
392-
} = *self;
393-
394-
ich::hash_stable_hashmap(hcx, hasher, parent, |hcx, def_id| {
395-
hcx.def_path_hash(*def_id)
396-
});
397-
ich::hash_stable_hashmap(hcx, hasher, children, |hcx, def_id| {
398-
hcx.def_path_hash(*def_id)
399-
});
400-
}
401-
}
385+
impl_stable_hash_for!(struct self::Graph {
386+
parent,
387+
children
388+
});

0 commit comments

Comments
 (0)