Skip to content

Commit 8f1a162

Browse files
committed
Do not call span_data_to_lines_and_cols twice.
1 parent cb8adac commit 8f1a162

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

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: 22 additions & 28 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

@@ -2671,15 +2671,15 @@ where
26712671
return;
26722672
}
26732673

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

26852685
// If this is not an empty or invalid span, we want to hash the last
@@ -2692,25 +2692,19 @@ where
26922692
};
26932693

26942694
Hash::hash(&TAG_VALID_SPAN, hasher);
2695-
Hash::hash(&file, hasher);
2695+
Hash::hash(&file.stable_id, hasher);
26962696

2697-
if let Some(parent) = span.parent {
2698-
let parent_span = ctx.def_span(parent).data_untracked();
2699-
let Some((parent_file, ..)) = ctx.span_data_to_lines_and_cols(&parent_span) else {
2700-
Hash::hash(&TAG_INVALID_SPAN, hasher);
2701-
return;
2702-
};
2703-
2704-
if parent_file == file {
2705-
// This span is relative to another span in the same file,
2706-
// only hash the relative position.
2707-
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2708-
// Use signed difference as `span` may start before `parent_span`,
2709-
// for instance attributes start before their item's span.
2710-
Hash::hash(&(span.lo.to_u32() as isize - parent_span.lo.to_u32() as isize), hasher);
2711-
Hash::hash(&(span.hi.to_u32() as isize - parent_span.lo.to_u32() as isize), hasher);
2712-
return;
2713-
}
2697+
if let Some(parent) = parent
2698+
&& file.contains(parent.lo)
2699+
{
2700+
// This span is relative to another span in the same file,
2701+
// only hash the relative position.
2702+
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2703+
// Use signed difference as `span` may start before `parent`,
2704+
// for instance attributes start before their item's span.
2705+
Hash::hash(&(span.lo.to_u32() as isize - parent.lo.to_u32() as isize), hasher);
2706+
Hash::hash(&(span.hi.to_u32() as isize - parent.lo.to_u32() as isize), hasher);
2707+
return;
27142708
}
27152709

27162710
// Hash both the length and the end location (line/column) of a span. If we

0 commit comments

Comments
 (0)