Skip to content

Commit 672f537

Browse files
committed
Also hash spans inside the same file as relative.
1 parent 6d6a08c commit 672f537

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

compiler/rustc_span/src/lib.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,12 +2672,12 @@ where
26722672
}
26732673

26742674
if let Some(parent) = span.parent {
2675-
let def_span = ctx.def_span(parent).data_untracked();
2676-
if def_span.contains(span) {
2675+
let parent_span = ctx.def_span(parent).data_untracked();
2676+
if parent_span.contains(span) {
26772677
// This span is enclosed in a definition: only hash the relative position.
26782678
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2679-
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
2680-
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
2679+
Hash::hash(&(span.lo - parent_span.lo), hasher);
2680+
Hash::hash(&(span.hi - parent_span.lo), hasher);
26812681
return;
26822682
}
26832683
}
@@ -2694,6 +2694,25 @@ where
26942694
Hash::hash(&TAG_VALID_SPAN, hasher);
26952695
Hash::hash(&file, hasher);
26962696

2697+
if let Some(parent) = span.parent {
2698+
let parent_span = ctx.def_span(parent).data_untracked();
2699+
let Some((parent_file, ..)) = ctx.span_data_to_lines_and_cols(&parent_span) else {
2700+
Hash::hash(&TAG_INVALID_SPAN, hasher);
2701+
return;
2702+
};
2703+
2704+
if parent_file == file {
2705+
// This span is relative to another span in the same file,
2706+
// only hash the relative position.
2707+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2708+
// Use signed difference as `span` may start before `parent_span`,
2709+
// for instance attributes start before their item's span.
2710+
Hash::hash(&(span.lo.to_u32() as isize - parent_span.lo.to_u32() as isize), hasher);
2711+
Hash::hash(&(span.hi.to_u32() as isize - parent_span.lo.to_u32() as isize), hasher);
2712+
return;
2713+
}
2714+
}
2715+
26972716
// Hash both the length and the end location (line/column) of a span. If we
26982717
// hash only the length, for example, then two otherwise equal spans with
26992718
// different end locations will have the same hash. This can cause a problem

0 commit comments

Comments
 (0)