Skip to content

Commit 640797f

Browse files
committed
Invert flow in impl HashStable of Span.
1 parent 1de5fdb commit 640797f

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/librustc/ich/hcx.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::NodeId {
281281
}
282282
}
283283

284-
impl<'a> HashStable<StableHashingContext<'a>> for Span {
284+
impl<'a> syntax_pos::StableHashingContextLike for StableHashingContext<'a> {
285285
/// Hashes a span in a stable way. We can't directly hash the span's `BytePos`
286286
/// fields (that would be similar to hashing pointers, since those are just
287287
/// offsets into the `SourceMap`). Instead, we hash the (file name, line, column)
@@ -291,25 +291,25 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
291291
/// codepoint offsets. For the purpose of the hash that's sufficient.
292292
/// Also, hashing filenames is expensive so we avoid doing it twice when the
293293
/// span starts and ends in the same file, which is almost always the case.
294-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
294+
fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher) {
295295
const TAG_VALID_SPAN: u8 = 0;
296296
const TAG_INVALID_SPAN: u8 = 1;
297297
const TAG_EXPANSION: u8 = 0;
298298
const TAG_NO_EXPANSION: u8 = 1;
299299

300-
if !hcx.hash_spans {
300+
if !self.hash_spans {
301301
return
302302
}
303303

304-
if *self == DUMMY_SP {
304+
if *span == DUMMY_SP {
305305
return std_hash::Hash::hash(&TAG_INVALID_SPAN, hasher);
306306
}
307307

308308
// If this is not an empty or invalid span, we want to hash the last
309309
// position that belongs to it, as opposed to hashing the first
310310
// position past it.
311-
let span = self.data();
312-
let (file_lo, line_lo, col_lo) = match hcx.source_map()
311+
let span = span.data();
312+
let (file_lo, line_lo, col_lo) = match self.source_map()
313313
.byte_pos_to_line_and_col(span.lo) {
314314
Some(pos) => pos,
315315
None => {
@@ -333,9 +333,9 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
333333
std_hash::Hash::hash(&line_col_len, hasher);
334334

335335
if span.ctxt == SyntaxContext::root() {
336-
TAG_NO_EXPANSION.hash_stable(hcx, hasher);
336+
TAG_NO_EXPANSION.hash_stable(self, hasher);
337337
} else {
338-
TAG_EXPANSION.hash_stable(hcx, hasher);
338+
TAG_EXPANSION.hash_stable(self, hasher);
339339

340340
// Since the same expansion context is usually referenced many
341341
// times, we cache a stable hash of it and hash that instead of
@@ -352,14 +352,14 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
352352
}
353353

354354
let mut hasher = StableHasher::new();
355-
expn_id.expn_data().hash_stable(hcx, &mut hasher);
355+
expn_id.expn_data().hash_stable(self, &mut hasher);
356356
let sub_hash: Fingerprint = hasher.finish();
357357
let sub_hash = sub_hash.to_smaller_hash();
358358
cache.borrow_mut().insert(expn_id, sub_hash);
359359
sub_hash
360360
});
361361

362-
sub_hash.hash_stable(hcx, hasher);
362+
sub_hash.hash_stable(self, hasher);
363363
}
364364
}
365365
}

src/librustc/ich/impls_syntax.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
1717
use smallvec::SmallVec;
1818
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1919

20-
impl<'ctx> syntax_pos::StableHashingContextLike for StableHashingContext<'ctx> {}
2120
impl<'ctx> syntax::StableHashingContextLike for StableHashingContext<'ctx> {}
2221
impl<'ctx> rustc_target::StableHashingContextLike for StableHashingContext<'ctx> {}
2322

src/libsyntax_pos/lib.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub use symbol::{Symbol, sym};
3434
mod analyze_source_file;
3535
pub mod fatal_error;
3636

37-
use rustc_data_structures::stable_hasher::StableHasher;
37+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3838
use rustc_data_structures::sync::{Lrc, Lock};
3939

4040
use std::borrow::Cow;
@@ -245,6 +245,14 @@ impl Ord for Span {
245245
}
246246
}
247247

248+
impl<CTX> HashStable<CTX> for Span
249+
where CTX: StableHashingContextLike
250+
{
251+
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
252+
ctx.hash_stable_span(self, hasher)
253+
}
254+
}
255+
248256
/// A collection of spans. Spans have two orthogonal attributes:
249257
///
250258
/// - They can be *primary spans*. In this case they are the locus of
@@ -1566,4 +1574,6 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
15661574
/// Requirements for a `StableHashingContext` to be used in this crate.
15671575
/// This is a hack to allow using the `HashStable_Generic` derive macro
15681576
/// instead of implementing everything in librustc.
1569-
pub trait StableHashingContextLike {}
1577+
pub trait StableHashingContextLike {
1578+
fn hash_stable_span(&mut self, span: &Span, hasher: &mut StableHasher);
1579+
}

0 commit comments

Comments
 (0)