Skip to content

Commit df67202

Browse files
committed
Auto merge of #143882 - cjgillot:relative-span-file, r=<try>
Also hash spans inside the same file as relative. r? `@ghost` for perf
2 parents d2baa49 + 3a47352 commit df67202

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
@@ -2660,12 +2660,12 @@ where
26602660
}
26612661

26622662
if let Some(parent) = span.parent {
2663-
let def_span = ctx.def_span(parent).data_untracked();
2664-
if def_span.contains(span) {
2663+
let parent_span = ctx.def_span(parent).data_untracked();
2664+
if parent_span.contains(span) {
26652665
// This span is enclosed in a definition: only hash the relative position.
26662666
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2667-
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
2668-
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
2667+
Hash::hash(&(span.lo - parent_span.lo), hasher);
2668+
Hash::hash(&(span.hi - parent_span.lo), hasher);
26692669
return;
26702670
}
26712671
}
@@ -2682,6 +2682,25 @@ where
26822682
Hash::hash(&TAG_VALID_SPAN, hasher);
26832683
Hash::hash(&file, hasher);
26842684

2685+
if let Some(parent) = span.parent {
2686+
let parent_span = ctx.def_span(parent).data_untracked();
2687+
let Some((parent_file, ..)) = ctx.span_data_to_lines_and_cols(&parent_span) else {
2688+
Hash::hash(&TAG_INVALID_SPAN, hasher);
2689+
return;
2690+
};
2691+
2692+
if parent_file == file {
2693+
// This span is relative to another span in the same file,
2694+
// only hash the relative position.
2695+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2696+
// Use signed difference as `span` may start before `parent_span`,
2697+
// for instance attributes start before their item's span.
2698+
Hash::hash(&(span.lo.to_u32() as isize - parent_span.lo.to_u32() as isize), hasher);
2699+
Hash::hash(&(span.hi.to_u32() as isize - parent_span.lo.to_u32() as isize), hasher);
2700+
return;
2701+
}
2702+
}
2703+
26852704
// Hash both the length and the end location (line/column) of a span. If we
26862705
// hash only the length, for example, then two otherwise equal spans with
26872706
// different end locations will have the same hash. This can cause a problem

0 commit comments

Comments
 (0)