Skip to content

Commit 2efad95

Browse files
committed
Update on-disk cache span encoder.
1 parent 8f1a162 commit 2efad95

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 33 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,17 @@ 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+
(
665+
BytePos::from_usize((reference + dlo) as usize),
666+
BytePos::from_usize((reference + dto) as usize),
667+
)
668+
}
657669
TAG_FULL_SPAN => {
658670
let file_lo_index = SourceFileIndex::decode(self);
659671
let line_lo = usize::decode(self);
@@ -664,6 +676,7 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
664676
let lo = file_lo.lines()[line_lo - 1] + col_lo;
665677
let lo = file_lo.absolute_position(lo);
666678
let hi = lo + len;
679+
667680
(lo, hi)
668681
}
669682
_ => unreachable!(),
@@ -892,30 +905,33 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
892905
return TAG_PARTIAL_SPAN.encode(self);
893906
}
894907

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-
}
908+
let parent =
909+
span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
910+
if let Some(parent) = parent
911+
&& parent.contains(span_data)
912+
{
913+
TAG_RELATIVE_SPAN.encode(self);
914+
(span_data.lo - parent.lo).to_u32().encode(self);
915+
(span_data.hi - parent.lo).to_u32().encode(self);
916+
return;
903917
}
904918

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,
919+
let Some((file_lo, line_lo, col_lo)) =
920+
self.source_map.byte_pos_to_line_and_col(span_data.lo)
921+
else {
922+
return TAG_PARTIAL_SPAN.encode(self);
909923
};
910924

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

915-
let (file_lo, line_lo, col_lo) = pos.unwrap();
916-
917934
let len = span_data.hi - span_data.lo;
918-
919935
let source_file_index = self.source_file_index(file_lo);
920936

921937
TAG_FULL_SPAN.encode(self);

compiler/rustc_span/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

0 commit comments

Comments
 (0)