Skip to content

Commit 3af9bfe

Browse files
make Span's HashStable impl actually valid i think
1 parent f471d5f commit 3af9bfe

File tree

3 files changed

+10
-34
lines changed

3 files changed

+10
-34
lines changed

compiler/rustc_query_system/src/ich/hcx.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
use std::sync::Arc;
2-
31
use rustc_ast as ast;
42
use rustc_data_structures::stable_hasher::{HashStable, HashingControls, StableHasher};
53
use rustc_hir::def_id::{DefId, LocalDefId};
64
use rustc_hir::definitions::DefPathHash;
75
use rustc_session::Session;
86
use rustc_session::cstore::Untracked;
97
use rustc_span::source_map::SourceMap;
10-
use rustc_span::{BytePos, CachingSourceMapView, DUMMY_SP, SourceFile, Span, SpanData, Symbol};
8+
use rustc_span::{CachingSourceMapView, DUMMY_SP, Span, SpanData, StableSourceFileId, Symbol};
119

1210
use crate::ich;
1311

@@ -115,11 +113,8 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
115113
}
116114

117115
#[inline]
118-
fn span_data_to_lines_and_cols(
119-
&mut self,
120-
span: &SpanData,
121-
) -> Option<(Arc<SourceFile>, usize, BytePos, usize, BytePos)> {
122-
self.source_map().span_data_to_lines_and_cols(span)
116+
fn span_to_file(&mut self, span: &SpanData) -> Option<StableSourceFileId> {
117+
Some(self.source_map().span_data_to_lines_and_cols(span)?.0.stable_id)
123118
}
124119

125120
#[inline]

compiler/rustc_span/src/caching_source_map_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<'sm> CachingSourceMapView<'sm> {
118118
self.time_stamp += 1;
119119

120120
// Check if lo and hi are in the cached lines.
121-
let lo_cache_idx: isize = self.cache_entry_index(span_data.lo);
121+
let lo_cache_idx = self.cache_entry_index(span_data.lo);
122122
let hi_cache_idx = self.cache_entry_index(span_data.hi);
123123

124124
if lo_cache_idx != -1 && hi_cache_idx != -1 {

compiler/rustc_span/src/lib.rs

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,10 +2597,7 @@ pub trait HashStableContext {
25972597
/// we don't have easy access to a `Session`
25982598
fn unstable_opts_incremental_ignore_spans(&self) -> bool;
25992599
fn def_span(&self, def_id: LocalDefId) -> Span;
2600-
fn span_data_to_lines_and_cols(
2601-
&mut self,
2602-
span: &SpanData,
2603-
) -> Option<(Arc<SourceFile>, usize, BytePos, usize, BytePos)>;
2600+
fn span_to_file(&mut self, span: &SpanData) -> Option<StableSourceFileId>;
26042601
fn hashing_controls(&self) -> HashingControls;
26052602
}
26062603

@@ -2641,6 +2638,7 @@ where
26412638
if def_span.contains(span) {
26422639
// This span is enclosed in a definition: only hash the relative position.
26432640
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
2641+
parent.hash_stable(ctx, hasher);
26442642
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
26452643
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
26462644
return;
@@ -2650,32 +2648,15 @@ where
26502648
// If this is not an empty or invalid span, we want to hash the last
26512649
// position that belongs to it, as opposed to hashing the first
26522650
// position past it.
2653-
let Some((file, line_lo, col_lo, line_hi, col_hi)) = ctx.span_data_to_lines_and_cols(&span)
2654-
else {
2651+
let Some(stable_id) = ctx.span_to_file(&span) else {
26552652
Hash::hash(&TAG_INVALID_SPAN, hasher);
26562653
return;
26572654
};
26582655

26592656
Hash::hash(&TAG_VALID_SPAN, hasher);
2660-
Hash::hash(&file.stable_id, hasher);
2661-
2662-
// Hash both the length and the end location (line/column) of a span. If we
2663-
// hash only the length, for example, then two otherwise equal spans with
2664-
// different end locations will have the same hash. This can cause a problem
2665-
// during incremental compilation wherein a previous result for a query that
2666-
// depends on the end location of a span will be incorrectly reused when the
2667-
// end location of the span it depends on has changed (see issue #74890). A
2668-
// similar analysis applies if some query depends specifically on the length
2669-
// of the span, but we only hash the end location. So hash both.
2670-
2671-
let col_lo_trunc = (col_lo.0 as u64) & 0xFF;
2672-
let line_lo_trunc = ((line_lo as u64) & 0xFF_FF_FF) << 8;
2673-
let col_hi_trunc = (col_hi.0 as u64) & 0xFF << 32;
2674-
let line_hi_trunc = ((line_hi as u64) & 0xFF_FF_FF) << 40;
2675-
let col_line = col_lo_trunc | line_lo_trunc | col_hi_trunc | line_hi_trunc;
2676-
let len = (span.hi - span.lo).0;
2677-
Hash::hash(&col_line, hasher);
2678-
Hash::hash(&len, hasher);
2657+
Hash::hash(&stable_id, hasher);
2658+
Hash::hash(&span.lo.0, hasher);
2659+
Hash::hash(&span.hi.0, hasher);
26792660
}
26802661
}
26812662

0 commit comments

Comments
 (0)