Skip to content

Commit cff0ea5

Browse files
committed
Move HashStable implementations.
1 parent 7256855 commit cff0ea5

File tree

2 files changed

+60
-61
lines changed

2 files changed

+60
-61
lines changed

compiler/rustc_span/src/hygiene.rs

+59-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
use crate::edition::Edition;
2828
use crate::symbol::{kw, sym, Symbol};
2929
use crate::with_session_globals;
30-
use crate::{BytePos, CachingSourceMapView, ExpnIdCache, SourceFile, Span, DUMMY_SP};
30+
use crate::{BytePos, CachingSourceMapView, HashStableContext, SourceFile, Span, DUMMY_SP};
3131

3232
use crate::def_id::{CrateNum, DefId, DefPathHash, CRATE_DEF_INDEX, LOCAL_CRATE};
3333
use rustc_data_structures::fingerprint::Fingerprint;
@@ -36,6 +36,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3636
use rustc_data_structures::sync::{Lock, Lrc};
3737
use rustc_macros::HashStable_Generic;
3838
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
39+
use std::cell::RefCell;
3940
use std::fmt;
4041
use std::hash::Hash;
4142
use std::thread::LocalKey;
@@ -1407,3 +1408,60 @@ fn update_disambiguator(expn_id: ExpnId) {
14071408
};
14081409
}
14091410
}
1411+
1412+
impl<CTX: HashStableContext> HashStable<CTX> for SyntaxContext {
1413+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1414+
const TAG_EXPANSION: u8 = 0;
1415+
const TAG_NO_EXPANSION: u8 = 1;
1416+
1417+
if *self == SyntaxContext::root() {
1418+
TAG_NO_EXPANSION.hash_stable(ctx, hasher);
1419+
} else {
1420+
TAG_EXPANSION.hash_stable(ctx, hasher);
1421+
let (expn_id, transparency) = self.outer_mark();
1422+
expn_id.hash_stable(ctx, hasher);
1423+
transparency.hash_stable(ctx, hasher);
1424+
}
1425+
}
1426+
}
1427+
1428+
pub type ExpnIdCache = RefCell<Vec<Option<Fingerprint>>>;
1429+
1430+
impl<CTX: HashStableContext> HashStable<CTX> for ExpnId {
1431+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
1432+
const TAG_ROOT: u8 = 0;
1433+
const TAG_NOT_ROOT: u8 = 1;
1434+
1435+
if *self == ExpnId::root() {
1436+
TAG_ROOT.hash_stable(ctx, hasher);
1437+
return;
1438+
}
1439+
1440+
// Since the same expansion context is usually referenced many
1441+
// times, we cache a stable hash of it and hash that instead of
1442+
// recursing every time.
1443+
let index = self.as_u32() as usize;
1444+
let res = CTX::expn_id_cache().with(|cache| cache.borrow().get(index).copied().flatten());
1445+
1446+
if let Some(res) = res {
1447+
res.hash_stable(ctx, hasher);
1448+
} else {
1449+
let new_len = index + 1;
1450+
1451+
let mut sub_hasher = StableHasher::new();
1452+
TAG_NOT_ROOT.hash_stable(ctx, &mut sub_hasher);
1453+
self.expn_data().hash_stable(ctx, &mut sub_hasher);
1454+
let sub_hash: Fingerprint = sub_hasher.finish();
1455+
1456+
CTX::expn_id_cache().with(|cache| {
1457+
let mut cache = cache.borrow_mut();
1458+
if cache.len() < new_len {
1459+
cache.resize(new_len, None);
1460+
}
1461+
let prev = cache[index].replace(sub_hash);
1462+
assert_eq!(prev, None, "Cache slot was filled");
1463+
});
1464+
sub_hash.hash_stable(ctx, hasher);
1465+
}
1466+
}
1467+
}

compiler/rustc_span/src/lib.rs

+1-60
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ use source_map::SourceMap;
3636
pub mod edition;
3737
use edition::Edition;
3838
pub mod hygiene;
39-
pub use hygiene::SyntaxContext;
4039
use hygiene::Transparency;
4140
pub use hygiene::{DesugaringKind, ExpnData, ExpnId, ExpnKind, ForLoopLoc, MacroKind};
41+
pub use hygiene::{ExpnIdCache, SyntaxContext};
4242
pub mod def_id;
4343
use def_id::{CrateNum, DefId, DefPathHash, LOCAL_CRATE};
4444
pub mod lev_distance;
@@ -51,12 +51,10 @@ pub use symbol::{sym, Symbol};
5151
mod analyze_source_file;
5252
pub mod fatal_error;
5353

54-
use rustc_data_structures::fingerprint::Fingerprint;
5554
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
5655
use rustc_data_structures::sync::{Lock, Lrc};
5756

5857
use std::borrow::Cow;
59-
use std::cell::RefCell;
6058
use std::cmp::{self, Ordering};
6159
use std::fmt;
6260
use std::hash::Hash;
@@ -2036,60 +2034,3 @@ where
20362034
Hash::hash(&len, hasher);
20372035
}
20382036
}
2039-
2040-
impl<CTX: HashStableContext> HashStable<CTX> for SyntaxContext {
2041-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
2042-
const TAG_EXPANSION: u8 = 0;
2043-
const TAG_NO_EXPANSION: u8 = 1;
2044-
2045-
if *self == SyntaxContext::root() {
2046-
TAG_NO_EXPANSION.hash_stable(ctx, hasher);
2047-
} else {
2048-
TAG_EXPANSION.hash_stable(ctx, hasher);
2049-
let (expn_id, transparency) = self.outer_mark();
2050-
expn_id.hash_stable(ctx, hasher);
2051-
transparency.hash_stable(ctx, hasher);
2052-
}
2053-
}
2054-
}
2055-
2056-
pub type ExpnIdCache = RefCell<Vec<Option<Fingerprint>>>;
2057-
2058-
impl<CTX: HashStableContext> HashStable<CTX> for ExpnId {
2059-
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
2060-
const TAG_ROOT: u8 = 0;
2061-
const TAG_NOT_ROOT: u8 = 1;
2062-
2063-
if *self == ExpnId::root() {
2064-
TAG_ROOT.hash_stable(ctx, hasher);
2065-
return;
2066-
}
2067-
2068-
// Since the same expansion context is usually referenced many
2069-
// times, we cache a stable hash of it and hash that instead of
2070-
// recursing every time.
2071-
let index = self.as_u32() as usize;
2072-
let res = CTX::expn_id_cache().with(|cache| cache.borrow().get(index).copied().flatten());
2073-
2074-
if let Some(res) = res {
2075-
res.hash_stable(ctx, hasher);
2076-
} else {
2077-
let new_len = index + 1;
2078-
2079-
let mut sub_hasher = StableHasher::new();
2080-
TAG_NOT_ROOT.hash_stable(ctx, &mut sub_hasher);
2081-
self.expn_data().hash_stable(ctx, &mut sub_hasher);
2082-
let sub_hash: Fingerprint = sub_hasher.finish();
2083-
2084-
CTX::expn_id_cache().with(|cache| {
2085-
let mut cache = cache.borrow_mut();
2086-
if cache.len() < new_len {
2087-
cache.resize(new_len, None);
2088-
}
2089-
let prev = cache[index].replace(sub_hash);
2090-
assert_eq!(prev, None, "Cache slot was filled");
2091-
});
2092-
sub_hash.hash_stable(ctx, hasher);
2093-
}
2094-
}
2095-
}

0 commit comments

Comments
 (0)