Skip to content

Commit 90797a0

Browse files
Auto merge of #143882 - cjgillot:relative-span-file, r=<try>
Also hash spans inside the same file as relative.
2 parents d829133 + aa27d05 commit 90797a0

File tree

4 files changed

+62
-36
lines changed

4 files changed

+62
-36
lines changed

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const TAG_FULL_SPAN: u8 = 0;
3838
// A partial span with no location information, encoded only with a `SyntaxContext`
3939
const TAG_PARTIAL_SPAN: u8 = 1;
4040
const TAG_RELATIVE_SPAN: u8 = 2;
41+
const TAG_RELATIVE_OUTER_SPAN: u8 = 3;
4142

4243
const TAG_SYNTAX_CONTEXT: u8 = 0;
4344
const TAG_EXPN_DATA: u8 = 1;
@@ -654,6 +655,16 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
654655
let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
655656
(enclosing.lo + BytePos::from_u32(dlo), enclosing.lo + BytePos::from_u32(dto))
656657
}
658+
TAG_RELATIVE_OUTER_SPAN => {
659+
let dlo = isize::decode(self);
660+
let dto = isize::decode(self);
661+
662+
let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
663+
let reference = enclosing.lo.to_u32() as isize;
664+
let lo = BytePos::from_usize((reference + dlo) as usize);
665+
let hi = BytePos::from_usize((reference + dto) as usize);
666+
(lo, hi)
667+
}
657668
TAG_FULL_SPAN => {
658669
let file_lo_index = SourceFileIndex::decode(self);
659670
let line_lo = usize::decode(self);
@@ -892,30 +903,33 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
892903
return TAG_PARTIAL_SPAN.encode(self);
893904
}
894905

895-
if let Some(parent) = span_data.parent {
896-
let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
897-
if enclosing.contains(span_data) {
898-
TAG_RELATIVE_SPAN.encode(self);
899-
(span_data.lo - enclosing.lo).to_u32().encode(self);
900-
(span_data.hi - enclosing.lo).to_u32().encode(self);
901-
return;
902-
}
906+
let parent =
907+
span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
908+
if let Some(parent) = parent
909+
&& parent.contains(span_data)
910+
{
911+
TAG_RELATIVE_SPAN.encode(self);
912+
(span_data.lo - parent.lo).to_u32().encode(self);
913+
(span_data.hi - parent.lo).to_u32().encode(self);
914+
return;
903915
}
904916

905-
let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
906-
let partial_span = match &pos {
907-
Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
908-
None => true,
917+
let Some((file_lo, line_lo, col_lo)) =
918+
self.source_map.byte_pos_to_line_and_col(span_data.lo)
919+
else {
920+
return TAG_PARTIAL_SPAN.encode(self);
909921
};
910922

911-
if partial_span {
912-
return TAG_PARTIAL_SPAN.encode(self);
923+
if let Some(parent) = parent
924+
&& file_lo.contains(parent.lo)
925+
{
926+
TAG_RELATIVE_OUTER_SPAN.encode(self);
927+
(span_data.lo.to_u32() as isize - parent.lo.to_u32() as isize).encode(self);
928+
(span_data.hi.to_u32() as isize - parent.lo.to_u32() as isize).encode(self);
929+
return;
913930
}
914931

915-
let (file_lo, line_lo, col_lo) = pos.unwrap();
916-
917932
let len = span_data.hi - span_data.lo;
918-
919933
let source_file_index = self.source_file_index(file_lo);
920934

921935
TAG_FULL_SPAN.encode(self);

compiler/rustc_query_system/src/ich/hcx.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use rustc_hir::definitions::DefPathHash;
55
use rustc_session::Session;
66
use rustc_session::cstore::Untracked;
77
use rustc_span::source_map::SourceMap;
8-
use rustc_span::{
9-
BytePos, CachingSourceMapView, DUMMY_SP, Span, SpanData, StableSourceFileId, Symbol,
10-
};
8+
use rustc_span::{BytePos, CachingSourceMapView, DUMMY_SP, SourceFile, Span, SpanData, Symbol};
119

1210
use crate::ich;
1311

@@ -118,7 +116,7 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
118116
fn span_data_to_lines_and_cols(
119117
&mut self,
120118
span: &SpanData,
121-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)> {
119+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)> {
122120
self.source_map().span_data_to_lines_and_cols(span)
123121
}
124122

compiler/rustc_span/src/caching_source_map_view.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ops::Range;
22
use std::sync::Arc;
33

44
use crate::source_map::SourceMap;
5-
use crate::{BytePos, Pos, RelativeBytePos, SourceFile, SpanData, StableSourceFileId};
5+
use crate::{BytePos, Pos, RelativeBytePos, SourceFile, SpanData};
66

77
#[derive(Clone)]
88
struct CacheEntry {
@@ -114,7 +114,7 @@ impl<'sm> CachingSourceMapView<'sm> {
114114
pub fn span_data_to_lines_and_cols(
115115
&mut self,
116116
span_data: &SpanData,
117-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)> {
117+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)> {
118118
self.time_stamp += 1;
119119

120120
// Check if lo and hi are in the cached lines.
@@ -136,7 +136,7 @@ impl<'sm> CachingSourceMapView<'sm> {
136136
let lo = &self.line_cache[lo_cache_idx as usize];
137137
let hi = &self.line_cache[hi_cache_idx as usize];
138138
return Some((
139-
lo.file.stable_id,
139+
&*lo.file,
140140
lo.line_number,
141141
span_data.lo - lo.line.start,
142142
hi.line_number,
@@ -224,7 +224,7 @@ impl<'sm> CachingSourceMapView<'sm> {
224224
assert_eq!(lo.file_index, hi.file_index);
225225

226226
Some((
227-
lo.file.stable_id,
227+
&*lo.file,
228228
lo.line_number,
229229
span_data.lo - lo.line.start,
230230
hi.line_number,

compiler/rustc_span/src/lib.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,7 @@ pub trait HashStableContext {
26352635
fn span_data_to_lines_and_cols(
26362636
&mut self,
26372637
span: &SpanData,
2638-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)>;
2638+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)>;
26392639
fn hashing_controls(&self) -> HashingControls;
26402640
}
26412641

@@ -2653,6 +2653,7 @@ where
26532653
/// codepoint offsets. For the purpose of the hash that's sufficient.
26542654
/// Also, hashing filenames is expensive so we avoid doing it twice when the
26552655
/// span starts and ends in the same file, which is almost always the case.
2656+
// Important: changes to this method should be reflected in implementations of `SpanEncoder`.
26562657
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
26572658
const TAG_VALID_SPAN: u8 = 0;
26582659
const TAG_INVALID_SPAN: u8 = 1;
@@ -2671,15 +2672,15 @@ where
26712672
return;
26722673
}
26732674

2674-
if let Some(parent) = span.parent {
2675-
let def_span = ctx.def_span(parent).data_untracked();
2676-
if def_span.contains(span) {
2677-
// This span is enclosed in a definition: only hash the relative position.
2678-
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);
2681-
return;
2682-
}
2675+
let parent = span.parent.map(|parent| ctx.def_span(parent).data_untracked());
2676+
if let Some(parent) = parent
2677+
&& parent.contains(span)
2678+
{
2679+
// This span is enclosed in a definition: only hash the relative position.
2680+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2681+
Hash::hash(&(span.lo - parent.lo), hasher);
2682+
Hash::hash(&(span.hi - parent.lo), hasher);
2683+
return;
26832684
}
26842685

26852686
// If this is not an empty or invalid span, we want to hash the last
@@ -2692,7 +2693,20 @@ where
26922693
};
26932694

26942695
Hash::hash(&TAG_VALID_SPAN, hasher);
2695-
Hash::hash(&file, hasher);
2696+
Hash::hash(&file.stable_id, hasher);
2697+
2698+
if let Some(parent) = parent
2699+
&& file.contains(parent.lo)
2700+
{
2701+
// This span is relative to another span in the same file,
2702+
// only hash the relative position.
2703+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2704+
// Use signed difference as `span` may start before `parent`,
2705+
// for instance attributes start before their item's span.
2706+
Hash::hash(&(span.lo.to_u32() as isize - parent.lo.to_u32() as isize), hasher);
2707+
Hash::hash(&(span.hi.to_u32() as isize - parent.lo.to_u32() as isize), hasher);
2708+
return;
2709+
}
26962710

26972711
// Hash both the length and the end location (line/column) of a span. If we
26982712
// hash only the length, for example, then two otherwise equal spans with

0 commit comments

Comments
 (0)