Skip to content

Commit f5bb135

Browse files
committed
rdr-perf: make StableHashState generic over HASH_SPANS_AS_PARENTLESS
1 parent 673ed4b commit f5bb135

4 files changed

Lines changed: 27 additions & 30 deletions

File tree

compiler/rustc_data_structures/src/stable_hash.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,5 +620,4 @@ where
620620
#[derive(Clone, Copy, Hash, Eq, PartialEq, Debug)]
621621
pub struct StableHashControls {
622622
pub hash_spans: bool,
623-
pub hash_spans_as_parentless: bool,
624623
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2771,8 +2771,7 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
27712771
dep_node,
27722772
tcx,
27732773
|| {
2774-
tcx.with_stable_hashing_context(|mut hcx| {
2775-
hcx.set_hash_spans_as_parentless(true);
2774+
tcx.with_stable_hashing_context(|hcx| {
27762775
let is_proc_macro = tcx.crate_types().contains(&CrateType::ProcMacro);
27772776
let hash_public_api = tcx.sess.opts.unstable_opts.public_api_hash
27782777
& !is_proc_macro

compiler/rustc_metadata/src/rmeta/encoder/public_api_hasher.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<I: Idx> Default for RDRHashAll<I> {
8787
pub(crate) trait PublicApiHashState<'a> {
8888
fn enabled(&self) -> bool;
8989

90-
fn hcx_mut(&mut self) -> &mut StableHashState<'a>;
90+
fn hcx_mut(&mut self) -> &mut StableHashState<'a, true>;
9191
}
9292

9393
impl<'a, const ENABLED: bool> PublicApiHashState<'a> for PublicApiHashingContext<'a, ENABLED> {
@@ -97,18 +97,18 @@ impl<'a, const ENABLED: bool> PublicApiHashState<'a> for PublicApiHashingContext
9797
}
9898

9999
#[inline(always)]
100-
fn hcx_mut(&mut self) -> &mut StableHashState<'a> {
100+
fn hcx_mut(&mut self) -> &mut StableHashState<'a, true> {
101101
&mut self.hcx
102102
}
103103
}
104104

105105
pub(crate) struct PublicApiHashingContext<'a, const ENABLED: bool> {
106-
pub(crate) hcx: StableHashState<'a>,
106+
pub(crate) hcx: StableHashState<'a, true>,
107107
}
108108

109109
impl<'a, const ENABLED: bool> PublicApiHashingContext<'a, ENABLED> {
110-
pub(crate) fn new(hcx: StableHashState<'a>) -> Self {
111-
Self { hcx }
110+
pub(crate) fn new(hcx: StableHashState<'a, false>) -> Self {
111+
Self { hcx: hcx.hash_spans_as_parentless() }
112112
}
113113
}
114114

compiler/rustc_middle/src/ich.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum CachingSourceMap<'a> {
2020
/// enough information to transform `DefId`s and `HirId`s into stable `DefPath`s (i.e.,
2121
/// a reference to the `TyCtxt`) and it holds a few caches for speeding up various
2222
/// things (e.g., each `DefId`/`DefPath` is only hashed once).
23-
pub struct StableHashState<'a> {
23+
pub struct StableHashState<'a, const HASH_SPANS_AS_PARENTLESS: bool = false> {
2424
untracked: &'a Untracked,
2525
// The value of `-Z incremental-ignore-spans`.
2626
// This field should only be used by `unstable_opts_incremental_ignore_span`
@@ -29,7 +29,20 @@ pub struct StableHashState<'a> {
2929
stable_hash_controls: StableHashControls,
3030
}
3131

32-
impl<'a> StableHashState<'a> {
32+
impl<'a> StableHashState<'a, false> {
33+
pub fn hash_spans_as_parentless(self) -> StableHashState<'a, true> {
34+
let Self { untracked, incremental_ignore_spans, caching_source_map, stable_hash_controls } =
35+
self;
36+
StableHashState {
37+
untracked,
38+
incremental_ignore_spans,
39+
caching_source_map,
40+
stable_hash_controls,
41+
}
42+
}
43+
}
44+
45+
impl<'a, const HASH_SPANS_AS_PARENTLESS: bool> StableHashState<'a, HASH_SPANS_AS_PARENTLESS> {
3346
#[inline]
3447
pub fn new(sess: &'a Session, untracked: &'a Untracked) -> Self {
3548
let hash_spans_initial = !sess.opts.unstable_opts.incremental_ignore_spans;
@@ -38,18 +51,10 @@ impl<'a> StableHashState<'a> {
3851
untracked,
3952
incremental_ignore_spans: sess.opts.unstable_opts.incremental_ignore_spans,
4053
caching_source_map: CachingSourceMap::Unused(sess.source_map()),
41-
stable_hash_controls: StableHashControls {
42-
hash_spans: hash_spans_initial,
43-
hash_spans_as_parentless: false,
44-
},
54+
stable_hash_controls: StableHashControls { hash_spans: hash_spans_initial },
4555
}
4656
}
4757

48-
#[inline]
49-
pub fn set_hash_spans_as_parentless(&mut self, hash_spans_as_parentless: bool) {
50-
self.stable_hash_controls.hash_spans_as_parentless = hash_spans_as_parentless;
51-
}
52-
5358
#[inline]
5459
pub fn while_hashing_spans<F: FnOnce(&mut Self)>(&mut self, hash_spans: bool, f: F) {
5560
let prev_hash_spans = self.stable_hash_controls.hash_spans;
@@ -80,7 +85,9 @@ impl<'a> StableHashState<'a> {
8085
}
8186
}
8287

83-
impl<'a> StableHashCtxt for StableHashState<'a> {
88+
impl<'a, const HASH_SPANS_AS_PARENTLESS: bool> StableHashCtxt
89+
for StableHashState<'a, HASH_SPANS_AS_PARENTLESS>
90+
{
8491
/// Hashes a span in a stable way. We can't directly hash the span's `BytePos` fields (that
8592
/// would be similar to hashing pointers, since those are just offsets into the `SourceMap`).
8693
/// Instead, we hash the (file name, line, column) triple, which stays the same even if the
@@ -104,7 +111,7 @@ impl<'a> StableHashCtxt for StableHashState<'a> {
104111
const TAG_INVALID_SPAN: u8 = 1;
105112
const TAG_RELATIVE_SPAN: u8 = 2;
106113

107-
if self.stable_hash_controls().hash_spans_as_parentless {
114+
if HASH_SPANS_AS_PARENTLESS {
108115
span.parent = None
109116
}
110117
span.ctxt.stable_hash(self, hasher);
@@ -189,15 +196,7 @@ impl<'a> StableHashCtxt for StableHashState<'a> {
189196
#[inline]
190197
fn assert_default_stable_hash_controls(&self, msg: &str) {
191198
let stable_hash_controls = self.stable_hash_controls;
192-
let StableHashControls {
193-
hash_spans,
194-
// This is only used for public api hashing of rmeta.
195-
//
196-
// The expn hashes are encoded in the rmeta and all spans are encoded without their
197-
// parent in rmeta. If it is ok to give the information like this to dependent crates
198-
// it should be ok to ignore this setting here.
199-
hash_spans_as_parentless: _,
200-
} = stable_hash_controls;
199+
let StableHashControls { hash_spans } = stable_hash_controls;
201200

202201
// Note that we require that `hash_spans` be the inverse of the global `-Z
203202
// incremental-ignore-spans` option. Normally, this option is disabled, in which case

0 commit comments

Comments
 (0)