Skip to content

Commit 9625da0

Browse files
committed
Auto merge of rust-lang#141408 - compiler-errors:start-and-end-point, r=<try>
Cache start and end point r? `@ghost`
2 parents 2eef478 + 3af9bfe commit 9625da0

File tree

13 files changed

+43
-43
lines changed

13 files changed

+43
-43
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
641641
} else {
642642
let try_span = this.mark_span_with_reason(
643643
DesugaringKind::TryBlock,
644-
this.tcx.sess.source_map().end_point(body.span),
644+
this.tcx.end_point(body.span),
645645
Some(Arc::clone(&this.allow_try_trait)),
646646
);
647647

@@ -1968,7 +1968,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19681968
span,
19691969
Some(Arc::clone(&self.allow_try_trait)),
19701970
);
1971-
let try_span = self.tcx.sess.source_map().end_point(span);
1971+
let try_span = self.tcx.end_point(span);
19721972
let try_span = self.mark_span_with_reason(
19731973
DesugaringKind::QuestionMark,
19741974
try_span,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14021402
} else {
14031403
self.next_node_id()
14041404
};
1405-
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
1405+
let span = self.tcx.start_point(t.span).shrink_to_hi();
14061406
let region = Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id };
14071407
(region, LifetimeSyntax::Hidden)
14081408
}

compiler/rustc_middle/src/query/erase.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ trivial! {
333333
rustc_span::ExpnHash,
334334
rustc_span::ExpnId,
335335
rustc_span::Span,
336+
(rustc_span::Span, rustc_span::Span),
336337
rustc_span::Symbol,
337338
rustc_span::Ident,
338339
rustc_target::spec::PanicStrategy,

compiler/rustc_middle/src/query/keys.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ impl Key for () {
7070
}
7171
}
7272

73+
impl Key for Span {
74+
type Cache<V> = DefaultCache<Self, V>;
75+
76+
fn default_span(&self, _: TyCtxt<'_>) -> Span {
77+
*self
78+
}
79+
}
80+
7381
impl<'tcx> Key for ty::InstanceKind<'tcx> {
7482
type Cache<V> = DefaultCache<Self, V>;
7583

compiler/rustc_middle/src/query/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ rustc_queries! {
159159
desc { "getting the source span" }
160160
}
161161

162+
query start_and_end_point(key: Span) -> (Span, Span) {
163+
desc { "computing the start and end points for span" }
164+
}
165+
162166
/// Represents crate as a whole (as distinct from the top-level crate module).
163167
///
164168
/// If you call `tcx.hir_crate(())` we will have to assume that any change

compiler/rustc_middle/src/ty/context.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,14 @@ impl<'tcx> TyCtxt<'tcx> {
21162116
self.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP)
21172117
}
21182118

2119+
pub fn start_point(self, span: Span) -> Span {
2120+
self.start_and_end_point(span).0
2121+
}
2122+
2123+
pub fn end_point(self, span: Span) -> Span {
2124+
self.start_and_end_point(span).1
2125+
}
2126+
21192127
#[inline(always)]
21202128
pub fn with_stable_hashing_context<R>(
21212129
self,
@@ -3430,6 +3438,10 @@ pub fn provide(providers: &mut Providers) {
34303438
tcx.lang_items().panic_impl().is_some_and(|did| did.is_local())
34313439
};
34323440
providers.source_span = |tcx, def_id| tcx.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP);
3441+
providers.start_and_end_point = |tcx, span| {
3442+
let sm = tcx.sess.source_map();
3443+
(sm.start_point(span), sm.end_point(span))
3444+
};
34333445
}
34343446

34353447
pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2489,7 +2489,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
24892489
});
24902490

24912491
let source_info = self.source_info(guard_span);
2492-
let guard_end = self.source_info(tcx.sess.source_map().end_point(guard_span));
2492+
let guard_end = self.source_info(tcx.end_point(guard_span));
24932493
let guard_frame = self.guard_context.pop().unwrap();
24942494
debug!("Exiting guard building context with locals: {:?}", guard_frame);
24952495

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11651165
if scope.region_scope == region_scope {
11661166
let region_scope_span = region_scope.span(self.tcx, self.region_scope_tree);
11671167
// Attribute scope exit drops to scope's closing brace.
1168-
let scope_end = self.tcx.sess.source_map().end_point(region_scope_span);
1168+
let scope_end = self.tcx.end_point(region_scope_span);
11691169

11701170
scope.drops.push(DropData {
11711171
source_info: SourceInfo { span: scope_end, scope: scope.source_scope },
@@ -1196,7 +1196,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
11961196
scope.invalidate_cache();
11971197
if scope.region_scope == region_scope {
11981198
let region_scope_span = region_scope.span(self.tcx, self.region_scope_tree);
1199-
let scope_end = self.tcx.sess.source_map().end_point(region_scope_span);
1199+
let scope_end = self.tcx.end_point(region_scope_span);
12001200

12011201
scope.drops.push(DropData {
12021202
source_info: SourceInfo { span: scope_end, scope: scope.source_scope },

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_resolve/src/late.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
831831
// Elided lifetime in reference: we resolve as if there was some lifetime `'_` with
832832
// NodeId `ty.id`.
833833
// This span will be used in case of elision failure.
834-
let span = self.r.tcx.sess.source_map().start_point(ty.span);
834+
let span = self.r.tcx.start_point(ty.span);
835835
self.resolve_elided_lifetime(ty.id, span);
836836
visit::walk_ty(self, ty);
837837
}

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

compiler/rustc_span/src/source_map.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,7 @@ impl SourceMap {
886886
/// Returns a new span representing just the first character of the given span.
887887
pub fn start_point(&self, sp: Span) -> Span {
888888
let width = {
889-
let sp = sp.data();
890-
let local_begin = self.lookup_byte_offset(sp.lo);
889+
let local_begin = self.lookup_byte_offset(sp.lo());
891890
let start_index = local_begin.pos.to_usize();
892891
let src = local_begin.sf.external_src.read();
893892

0 commit comments

Comments
 (0)