Skip to content

Commit b69058d

Browse files
authored
Rollup merge of #51982 - michaelwoerister:hash-modules-properly, r=nikomatsakis
incr.comp.: Take names of children into account when computing the ICH of a module's HIR. Fixes #40876. Red-green tracking does not make this a problem anymore. We should verify this via a perf-run though. r? @nikomatsakis
2 parents 45cd78a + 447f1f3 commit b69058d

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

src/librustc/hir/map/definitions.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc_data_structures::indexed_vec::{IndexVec};
2323
use rustc_data_structures::stable_hasher::StableHasher;
2424
use serialize::{Encodable, Decodable, Encoder, Decoder};
2525
use session::CrateDisambiguator;
26+
use std::borrow::Borrow;
2627
use std::fmt::Write;
2728
use std::hash::Hash;
2829
use syntax::ast;
@@ -389,6 +390,13 @@ pub struct DefPathHash(pub Fingerprint);
389390

390391
impl_stable_hash_for!(tuple_struct DefPathHash { fingerprint });
391392

393+
impl Borrow<Fingerprint> for DefPathHash {
394+
#[inline]
395+
fn borrow(&self) -> &Fingerprint {
396+
&self.0
397+
}
398+
}
399+
392400
impl Definitions {
393401
/// Create new empty definition map.
394402
pub fn new() -> Definitions {

src/librustc/ich/fingerprint.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ impl Fingerprint {
4545
)
4646
}
4747

48+
// Combines two hashes in an order independent way. Make sure this is what
49+
// you want.
50+
#[inline]
51+
pub fn combine_commutative(self, other: Fingerprint) -> Fingerprint {
52+
let a = (self.1 as u128) << 64 | self.0 as u128;
53+
let b = (other.1 as u128) << 64 | other.0 as u128;
54+
55+
let c = a.wrapping_add(b);
56+
57+
Fingerprint((c >> 64) as u64, c as u64)
58+
}
59+
4860
pub fn to_hex(&self) -> String {
4961
format!("{:x}{:x}", self.0, self.1)
5062
}

src/librustc/ich/impls_hir.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use hir;
1515
use hir::map::DefPathHash;
1616
use hir::def_id::{DefId, LocalDefId, CrateNum, CRATE_DEF_INDEX};
17-
use ich::{StableHashingContext, NodeIdHashingMode};
17+
use ich::{StableHashingContext, NodeIdHashingMode, Fingerprint};
1818
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
1919
StableHasher, StableHasherResult};
2020
use std::mem;
@@ -755,13 +755,34 @@ impl_stable_hash_for!(enum hir::ImplPolarity {
755755
Negative
756756
});
757757

758-
impl_stable_hash_for!(struct hir::Mod {
759-
inner,
760-
// We are not hashing the IDs of the items contained in the module.
761-
// This is harmless and matches the current behavior but it's not
762-
// actually correct. See issue #40876.
763-
item_ids -> _,
764-
});
758+
impl<'a> HashStable<StableHashingContext<'a>> for hir::Mod {
759+
fn hash_stable<W: StableHasherResult>(&self,
760+
hcx: &mut StableHashingContext<'a>,
761+
hasher: &mut StableHasher<W>) {
762+
let hir::Mod {
763+
inner: ref inner_span,
764+
ref item_ids,
765+
} = *self;
766+
767+
inner_span.hash_stable(hcx, hasher);
768+
769+
// Combining the DefPathHashes directly is faster than feeding them
770+
// into the hasher. Because we use a commutative combine, we also don't
771+
// have to sort the array.
772+
let item_ids_hash = item_ids
773+
.iter()
774+
.map(|id| {
775+
let (def_path_hash, local_id) = id.id.to_stable_hash_key(hcx);
776+
debug_assert_eq!(local_id, hir::ItemLocalId(0));
777+
def_path_hash.0
778+
}).fold(Fingerprint::ZERO, |a, b| {
779+
a.combine_commutative(b)
780+
});
781+
782+
item_ids.len().hash_stable(hcx, hasher);
783+
item_ids_hash.hash_stable(hcx, hasher);
784+
}
785+
}
765786

766787
impl_stable_hash_for!(struct hir::ForeignMod {
767788
abi,

src/librustc_codegen_llvm/mono_item.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ use monomorphize::Instance;
2525
use type_of::LayoutLlvmExt;
2626
use rustc::hir;
2727
use rustc::hir::def::Def;
28-
use rustc::hir::def_id::DefId;
28+
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
2929
use rustc::mir::mono::{Linkage, Visibility};
3030
use rustc::ty::TypeFoldable;
3131
use rustc::ty::layout::LayoutOf;
32-
use syntax::attr;
3332
use std::fmt;
3433

3534
pub use rustc::mir::mono::MonoItem;
@@ -173,7 +172,7 @@ fn predefine_fn<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
173172
// visibility as we're going to link this object all over the place but
174173
// don't want the symbols to get exported.
175174
if linkage != Linkage::Internal && linkage != Linkage::Private &&
176-
attr::contains_name(cx.tcx.hir.krate_attrs(), "compiler_builtins") {
175+
cx.tcx.is_compiler_builtins(LOCAL_CRATE) {
177176
unsafe {
178177
llvm::LLVMRustSetVisibility(lldecl, llvm::Visibility::Hidden);
179178
}

0 commit comments

Comments
 (0)