Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f471d5f

Browse files
Cache start and end point
1 parent e3892a4 commit f471d5f

File tree

10 files changed

+33
-9
lines changed

10 files changed

+33
-9
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_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/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)