Skip to content

Cache start and end point #141408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} else {
let try_span = this.mark_span_with_reason(
DesugaringKind::TryBlock,
this.tcx.sess.source_map().end_point(body.span),
this.tcx.end_point(body.span),
Some(Arc::clone(&this.allow_try_trait)),
);

Expand Down Expand Up @@ -1968,7 +1968,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
span,
Some(Arc::clone(&self.allow_try_trait)),
);
let try_span = self.tcx.sess.source_map().end_point(span);
let try_span = self.tcx.end_point(span);
let try_span = self.mark_span_with_reason(
DesugaringKind::QuestionMark,
try_span,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
} else {
self.next_node_id()
};
let span = self.tcx.sess.source_map().start_point(t.span).shrink_to_hi();
let span = self.tcx.start_point(t.span).shrink_to_hi();
let region = Lifetime { ident: Ident::new(kw::UnderscoreLifetime, span), id };
(region, LifetimeSyntax::Hidden)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ trivial! {
rustc_span::ExpnHash,
rustc_span::ExpnId,
rustc_span::Span,
(rustc_span::Span, rustc_span::Span),
rustc_span::Symbol,
rustc_span::Ident,
rustc_target::spec::PanicStrategy,
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/query/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ impl Key for () {
}
}

impl Key for Span {
type Cache<V> = DefaultCache<Self, V>;

fn default_span(&self, _: TyCtxt<'_>) -> Span {
*self
}
}

impl<'tcx> Key for ty::InstanceKind<'tcx> {
type Cache<V> = DefaultCache<Self, V>;

Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ rustc_queries! {
desc { "getting the source span" }
}

query start_and_end_point(key: Span) -> (Span, Span) {
desc { "computing the start and end points for span" }
}

/// Represents crate as a whole (as distinct from the top-level crate module).
///
/// If you call `tcx.hir_crate(())` we will have to assume that any change
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,14 @@ impl<'tcx> TyCtxt<'tcx> {
self.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP)
}

pub fn start_point(self, span: Span) -> Span {
self.start_and_end_point(span).0
}

pub fn end_point(self, span: Span) -> Span {
self.start_and_end_point(span).1
}

#[inline(always)]
pub fn with_stable_hashing_context<R>(
self,
Expand Down Expand Up @@ -3430,6 +3438,10 @@ pub fn provide(providers: &mut Providers) {
tcx.lang_items().panic_impl().is_some_and(|did| did.is_local())
};
providers.source_span = |tcx, def_id| tcx.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP);
providers.start_and_end_point = |tcx, span| {
let sm = tcx.sess.source_map();
(sm.start_point(span), sm.end_point(span))
};
}

pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2489,7 +2489,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
});

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

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_mir_build/src/builder/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
if scope.region_scope == region_scope {
let region_scope_span = region_scope.span(self.tcx, self.region_scope_tree);
// Attribute scope exit drops to scope's closing brace.
let scope_end = self.tcx.sess.source_map().end_point(region_scope_span);
let scope_end = self.tcx.end_point(region_scope_span);

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

scope.drops.push(DropData {
source_info: SourceInfo { span: scope_end, scope: scope.source_scope },
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
// Elided lifetime in reference: we resolve as if there was some lifetime `'_` with
// NodeId `ty.id`.
// This span will be used in case of elision failure.
let span = self.r.tcx.sess.source_map().start_point(ty.span);
let span = self.r.tcx.start_point(ty.span);
self.resolve_elided_lifetime(ty.id, span);
visit::walk_ty(self, ty);
}
Expand Down
22 changes: 4 additions & 18 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,7 @@ where
if def_span.contains(span) {
// This span is enclosed in a definition: only hash the relative position.
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
parent.hash_stable(ctx, hasher);
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
return;
Expand All @@ -2650,31 +2651,16 @@ where
// If this is not an empty or invalid span, we want to hash the last
// position that belongs to it, as opposed to hashing the first
// position past it.
let Some((file, line_lo, col_lo, line_hi, col_hi)) = ctx.span_data_to_lines_and_cols(&span)
else {
let Some((file, line_lo, col_lo, ..)) = ctx.span_data_to_lines_and_cols(&span) else {
Hash::hash(&TAG_INVALID_SPAN, hasher);
return;
};

Hash::hash(&TAG_VALID_SPAN, hasher);
Hash::hash(&file.stable_id, hasher);

// Hash both the length and the end location (line/column) of a span. If we
// hash only the length, for example, then two otherwise equal spans with
// different end locations will have the same hash. This can cause a problem
// during incremental compilation wherein a previous result for a query that
// depends on the end location of a span will be incorrectly reused when the
// end location of the span it depends on has changed (see issue #74890). A
// similar analysis applies if some query depends specifically on the length
// of the span, but we only hash the end location. So hash both.

let col_lo_trunc = (col_lo.0 as u64) & 0xFF;
let line_lo_trunc = ((line_lo as u64) & 0xFF_FF_FF) << 8;
let col_hi_trunc = (col_hi.0 as u64) & 0xFF << 32;
let line_hi_trunc = ((line_hi as u64) & 0xFF_FF_FF) << 40;
let col_line = col_lo_trunc | line_lo_trunc | col_hi_trunc | line_hi_trunc;
Hash::hash(&line_lo, hasher);
Hash::hash(&col_lo, hasher);
let len = (span.hi - span.lo).0;
Hash::hash(&col_line, hasher);
Hash::hash(&len, hasher);
}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,7 @@ impl SourceMap {
/// Returns a new span representing just the first character of the given span.
pub fn start_point(&self, sp: Span) -> Span {
let width = {
let sp = sp.data();
let local_begin = self.lookup_byte_offset(sp.lo);
let local_begin = self.lookup_byte_offset(sp.lo());
let start_index = local_begin.pos.to_usize();
let src = local_begin.sf.external_src.read();

Expand Down
Loading