Skip to content

Commit 4ce96aa

Browse files
Hash all spans relatively to their parent
Co-authored-by: Camille Gillot <gillot.camille@gmail.com>
1 parent b49c7d7 commit 4ce96aa

4 files changed

Lines changed: 57 additions & 39 deletions

File tree

compiler/rustc_middle/src/query/on_disk_cache.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use rustc_span::hygiene::{
2020
};
2121
use rustc_span::source_map::Spanned;
2222
use rustc_span::{
23-
BlobDecoder, BytePos, ByteSymbol, CachingSourceMapView, ExpnData, ExpnHash, Pos,
24-
RelativeBytePos, SourceFile, Span, SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
23+
BlobDecoder, BytePos, ByteSymbol, CachingSourceMapView, ExpnData, ExpnHash, RelativeBytePos,
24+
SourceFile, Span, SpanDecoder, SpanEncoder, StableSourceFileId, Symbol,
2525
};
2626

2727
use crate::dep_graph::{DepNodeIndex, SerializedDepNodeIndex};
@@ -652,7 +652,10 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
652652
let dto = u32::decode(self);
653653

654654
let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked();
655-
(enclosing.lo + BytePos::from_u32(dlo), enclosing.lo + BytePos::from_u32(dto))
655+
(
656+
BytePos(enclosing.lo.0.wrapping_add(dlo)),
657+
BytePos(enclosing.lo.0.wrapping_add(dto)),
658+
)
656659
}
657660
TAG_FULL_SPAN => {
658661
let file_lo_index = SourceFileIndex::decode(self);
@@ -894,30 +897,33 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> {
894897
return TAG_PARTIAL_SPAN.encode(self);
895898
}
896899

897-
if let Some(parent) = span_data.parent {
898-
let enclosing = self.tcx.source_span_untracked(parent).data_untracked();
899-
if enclosing.contains(span_data) {
900-
TAG_RELATIVE_SPAN.encode(self);
901-
(span_data.lo - enclosing.lo).to_u32().encode(self);
902-
(span_data.hi - enclosing.lo).to_u32().encode(self);
903-
return;
904-
}
900+
let parent =
901+
span_data.parent.map(|parent| self.tcx.source_span_untracked(parent).data_untracked());
902+
if let Some(parent) = parent
903+
&& parent.contains(span_data)
904+
{
905+
TAG_RELATIVE_SPAN.encode(self);
906+
(span_data.lo.0.wrapping_sub(parent.lo.0)).encode(self);
907+
(span_data.hi.0.wrapping_sub(parent.lo.0)).encode(self);
908+
return;
905909
}
906910

907-
let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo);
908-
let partial_span = match &pos {
909-
Some((file_lo, _, _)) => !file_lo.contains(span_data.hi),
910-
None => true,
911+
let Some((file_lo, line_lo, col_lo)) =
912+
self.source_map.byte_pos_to_line_and_col(span_data.lo)
913+
else {
914+
return TAG_PARTIAL_SPAN.encode(self);
911915
};
912916

913-
if partial_span {
914-
return TAG_PARTIAL_SPAN.encode(self);
917+
if let Some(parent) = parent
918+
&& file_lo.contains(parent.lo)
919+
{
920+
TAG_RELATIVE_SPAN.encode(self);
921+
(span_data.lo.0.wrapping_sub(parent.lo.0)).encode(self);
922+
(span_data.hi.0.wrapping_sub(parent.lo.0)).encode(self);
923+
return;
915924
}
916925

917-
let (file_lo, line_lo, col_lo) = pos.unwrap();
918-
919926
let len = span_data.hi - span_data.lo;
920-
921927
let source_file_index = self.source_file_index(file_lo);
922928

923929
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
@@ -2831,7 +2831,7 @@ pub trait HashStableContext {
28312831
fn span_data_to_lines_and_cols(
28322832
&mut self,
28332833
span: &SpanData,
2834-
) -> Option<(StableSourceFileId, usize, BytePos, usize, BytePos)>;
2834+
) -> Option<(&SourceFile, usize, BytePos, usize, BytePos)>;
28352835
fn hashing_controls(&self) -> HashingControls;
28362836
}
28372837

@@ -2849,6 +2849,8 @@ where
28492849
/// codepoint offsets. For the purpose of the hash that's sufficient.
28502850
/// Also, hashing filenames is expensive so we avoid doing it twice when the
28512851
/// span starts and ends in the same file, which is almost always the case.
2852+
///
2853+
/// IMPORTANT: changes to this method should be reflected in implementations of `SpanEncoder`.
28522854
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
28532855
const TAG_VALID_SPAN: u8 = 0;
28542856
const TAG_INVALID_SPAN: u8 = 1;
@@ -2867,15 +2869,16 @@ where
28672869
return;
28682870
}
28692871

2870-
if let Some(parent) = span.parent {
2871-
let def_span = ctx.def_span(parent).data_untracked();
2872-
if def_span.contains(span) {
2873-
// This span is enclosed in a definition: only hash the relative position.
2874-
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2875-
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
2876-
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
2877-
return;
2878-
}
2872+
let parent = span.parent.map(|parent| ctx.def_span(parent).data_untracked());
2873+
if let Some(parent) = parent
2874+
&& parent.contains(span)
2875+
{
2876+
// This span is enclosed in a definition: only hash the relative position.
2877+
// The "in the same file" check below
2878+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2879+
(span.lo - parent.lo).to_u32().hash_stable(ctx, hasher);
2880+
(span.hi - parent.lo).to_u32().hash_stable(ctx, hasher);
2881+
return;
28792882
}
28802883

28812884
// If this is not an empty or invalid span, we want to hash the last
@@ -2887,8 +2890,19 @@ where
28872890
return;
28882891
};
28892892

2893+
if let Some(parent) = parent
2894+
&& file.contains(parent.lo)
2895+
{
2896+
// This span is relative to another span in the same file,
2897+
// only hash the relative position.
2898+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2899+
Hash::hash(&(span.lo.0.wrapping_sub(parent.lo.0)), hasher);
2900+
Hash::hash(&(span.hi.0.wrapping_sub(parent.lo.0)), hasher);
2901+
return;
2902+
}
2903+
28902904
Hash::hash(&TAG_VALID_SPAN, hasher);
2891-
Hash::hash(&file, hasher);
2905+
Hash::hash(&file.stable_id, hasher);
28922906

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

0 commit comments

Comments
 (0)