Skip to content

Commit 0dddae1

Browse files
committed
Update on-disk cache span encoder.
1 parent 3481584 commit 0dddae1

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 34 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;
@@ -659,6 +660,19 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
659660
parent,
660661
);
661662

663+
return span;
664+
} else if tag == TAG_RELATIVE_OUTER_SPAN {
665+
let dlo = isize::decode(self);
666+
let dto = isize::decode(self);
667+
668+
let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
669+
let span = Span::new(
670+
BytePos::from_usize((enclosing.lo.to_u32() as isize + dlo) as usize),
671+
BytePos::from_usize((enclosing.lo.to_u32() as isize + dto) as usize),
672+
ctxt,
673+
parent,
674+
);
675+
662676
return span;
663677
} else {
664678
debug_assert_eq!(tag, TAG_FULL_SPAN);
@@ -897,30 +911,33 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
897911
return TAG_PARTIAL_SPAN.encode(self);
898912
}
899913

900-
if let Some(parent) = span_data.parent {
901-
let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
902-
if enclosing.contains(span_data) {
903-
TAG_RELATIVE_SPAN.encode(self);
904-
(span_data.lo - enclosing.lo).to_u32().encode(self);
905-
(span_data.hi - enclosing.lo).to_u32().encode(self);
906-
return;
907-
}
914+
let parent =
915+
span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
916+
if let Some(parent) = parent
917+
&& parent.contains(span_data)
918+
{
919+
TAG_RELATIVE_SPAN.encode(self);
920+
(span_data.lo - parent.lo).to_u32().encode(self);
921+
(span_data.hi - parent.lo).to_u32().encode(self);
922+
return;
908923
}
909924

910-
let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
911-
let partial_span = match &pos {
912-
Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
913-
None => true,
925+
let Some((file_lo, line_lo, col_lo)) =
926+
self.source_map.byte_pos_to_line_and_col(span_data.lo)
927+
else {
928+
return TAG_PARTIAL_SPAN.encode(self);
914929
};
915930

916-
if partial_span {
917-
return TAG_PARTIAL_SPAN.encode(self);
931+
if let Some(parent) = parent
932+
&& file_lo.contains(parent.lo)
933+
{
934+
TAG_RELATIVE_OUTER_SPAN.encode(self);
935+
(span_data.lo.to_u32() as isize - parent.lo.to_u32() as isize).encode(self);
936+
(span_data.hi.to_u32() as isize - parent.lo.to_u32() as isize).encode(self);
937+
return;
918938
}
919939

920-
let (file_lo, line_lo, col_lo) = pos.unwrap();
921-
922940
let len = span_data.hi - span_data.lo;
923-
924941
let source_file_index = self.source_file_index(file_lo);
925942

926943
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
@@ -2641,6 +2641,7 @@ where
26412641
/// codepoint offsets. For the purpose of the hash that's sufficient.
26422642
/// Also, hashing filenames is expensive so we avoid doing it twice when the
26432643
/// span starts and ends in the same file, which is almost always the case.
2644+
// Important: changes to this method should be reflected in implementations of `SpanEncoder`.
26442645
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
26452646
const TAG_VALID_SPAN: u8 = 0;
26462647
const TAG_INVALID_SPAN: u8 = 1;

0 commit comments

Comments
 (0)