Skip to content

Commit a27fb55

Browse files
Don't use InternedString as sorting key during stable hashing.
1 parent 1768d4a commit a27fb55

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/librustc/ich/impls_syntax.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use ich::StableHashingContext;
1515

16+
use std::cmp;
1617
use std::hash as std_hash;
1718
use std::mem;
1819

@@ -29,6 +30,30 @@ use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
2930
StableHasher, StableHasherResult};
3031
use rustc_data_structures::accumulate_vec::AccumulateVec;
3132

33+
/// InternedString is not compared lexicographically but by pointer value. So,
34+
/// in order to use it as a stable key we introduce a wrapper type with
35+
/// the correct Ord, Eq, and Hash implementations.
36+
#[derive(Eq, Ord, Clone, Copy)]
37+
pub struct InternedStringSortStable(InternedString);
38+
39+
impl std_hash::Hash for InternedStringSortStable {
40+
fn hash<H: std_hash::Hasher>(&self, state: &mut H) {
41+
std_hash::Hash::hash(&self.0[..], state);
42+
}
43+
}
44+
45+
impl cmp::PartialOrd for InternedStringSortStable {
46+
fn partial_cmp(&self, other: &InternedStringSortStable) -> Option<cmp::Ordering> {
47+
(&self.0[..]).partial_cmp(&other.0[..])
48+
}
49+
}
50+
51+
impl cmp::PartialEq for InternedStringSortStable {
52+
fn eq(&self, other: &InternedStringSortStable) -> bool {
53+
(&self.0[..]).eq(&other.0[..])
54+
}
55+
}
56+
3257
impl<'a> HashStable<StableHashingContext<'a>> for InternedString {
3358
#[inline]
3459
fn hash_stable<W: StableHasherResult>(&self,
@@ -39,14 +64,16 @@ impl<'a> HashStable<StableHashingContext<'a>> for InternedString {
3964
}
4065
}
4166

67+
impl_stable_hash_for!(tuple_struct self::InternedStringSortStable { inner });
68+
4269
impl<'a> ToStableHashKey<StableHashingContext<'a>> for InternedString {
43-
type KeyType = InternedString;
70+
type KeyType = InternedStringSortStable;
4471

4572
#[inline]
4673
fn to_stable_hash_key(&self,
4774
_: &StableHashingContext<'a>)
48-
-> InternedString {
49-
self.clone()
75+
-> InternedStringSortStable {
76+
InternedStringSortStable(self.clone())
5077
}
5178
}
5279

@@ -60,13 +87,13 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
6087
}
6188

6289
impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::Name {
63-
type KeyType = InternedString;
90+
type KeyType = InternedStringSortStable;
6491

6592
#[inline]
6693
fn to_stable_hash_key(&self,
6794
_: &StableHashingContext<'a>)
68-
-> InternedString {
69-
self.as_str()
95+
-> InternedStringSortStable {
96+
InternedStringSortStable(self.as_str())
7097
}
7198
}
7299

0 commit comments

Comments
 (0)