Skip to content

Commit

Permalink
Track span dependency using a callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Sep 10, 2021
1 parent e85ddeb commit b19ae20
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_interface/src/callbacks.rs
Expand Up @@ -25,6 +25,14 @@ fn span_debug(span: rustc_span::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result
})
}

fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
tls::with_opt(|tcx| {
if let Some(tcx) = tcx {
let _ = tcx.source_span(def_id);
}
})
}

/// This is a callback from `rustc_ast` as it cannot access the implicit state
/// in `rustc_middle` otherwise. It is used to when diagnostic messages are
/// emitted and stores them in the current query, if there is one.
Expand Down Expand Up @@ -56,6 +64,7 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
/// TyCtxt in.
pub fn setup_callbacks() {
rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
}
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/hir/mod.rs
Expand Up @@ -153,6 +153,7 @@ pub fn provide(providers: &mut Providers) {
index.parenting.get(&id).copied().unwrap_or(CRATE_HIR_ID)
};
providers.hir_attrs = |tcx, id| AttributeMap { map: &tcx.untracked_crate.attrs, prefix: id };
providers.source_span = |tcx, def_id| tcx.resolutions(()).definitions.def_span(def_id);
providers.def_span = |tcx, def_id| tcx.hir().span_if_local(def_id).unwrap_or(DUMMY_SP);
providers.fn_arg_names = |tcx, id| {
let hir = tcx.hir();
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Expand Up @@ -20,6 +20,14 @@ rustc_queries! {
desc { "get the resolver outputs" }
}

/// Return the span for a definition.
/// Contrary to `def_span` below, this query returns the full absolute span of the definition.
/// This span is meant for dep-tracking rather than diagnostics. It should not be used outside
/// of rustc_middle::hir::source_map.
query source_span(key: LocalDefId) -> Span {
desc { "get the source span" }
}

/// Represents crate as a whole (as distinct from the top-level crate module).
/// If you call `hir_crate` (e.g., indirectly by calling `tcx.hir().krate()`),
/// we will have to assume that any change means that you need to be recompiled.
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_query_impl/src/on_disk_cache.rs
Expand Up @@ -840,7 +840,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Span {
let dlo = u32::decode(decoder)?;
let dto = u32::decode(decoder)?;

let enclosing = decoder.tcx.definitions_untracked().def_span(parent.unwrap()).data();
let enclosing = decoder.tcx.definitions_untracked().def_span(parent.unwrap()).decode();
let span = Span::new(
enclosing.lo + BytePos::from_u32(dlo),
enclosing.lo + BytePos::from_u32(dto),
Expand Down Expand Up @@ -1022,7 +1022,7 @@ where
E: 'a + OpaqueEncoder,
{
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx, E>) -> Result<(), E::Error> {
let span_data = self.data();
let span_data = self.decode();
span_data.ctxt.encode(s)?;
span_data.parent.encode(s)?;

Expand All @@ -1031,7 +1031,7 @@ where
}

if let Some(parent) = span_data.parent {
let enclosing = s.tcx.definitions_untracked().def_span(parent).data();
let enclosing = s.tcx.definitions_untracked().def_span(parent).decode();
if enclosing.contains(span_data) {
TAG_RELATIVE_SPAN.encode(s)?;
(span_data.lo - enclosing.lo).to_u32().encode(s)?;
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_span/src/lib.rs
Expand Up @@ -1947,6 +1947,7 @@ pub struct FileLines {

pub static SPAN_DEBUG: AtomicRef<fn(Span, &mut fmt::Formatter<'_>) -> fmt::Result> =
AtomicRef::new(&(default_span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
pub static SPAN_TRACK: AtomicRef<fn(LocalDefId)> = AtomicRef::new(&((|_| {}) as fn(_)));

// _____________________________________________________________________________
// SpanLinesError, SpanSnippetError, DistinctSources, MalformedSourceMapPositions
Expand Down Expand Up @@ -2031,7 +2032,7 @@ where
return;
}

let span = self.data();
let span = self.decode();
span.ctxt.hash_stable(ctx, hasher);
span.parent.hash_stable(ctx, hasher);

Expand All @@ -2041,7 +2042,7 @@ where
}

if let Some(parent) = span.parent {
let def_span = ctx.def_span(parent).data();
let def_span = ctx.def_span(parent).decode();
if def_span.contains(span) {
// This span is enclosed in a definition: only hash the relative position.
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_span/src/span_encoding.rs
Expand Up @@ -6,6 +6,7 @@

use crate::def_id::LocalDefId;
use crate::hygiene::SyntaxContext;
use crate::SPAN_TRACK;
use crate::{BytePos, SpanData};

use rustc_data_structures::fx::FxIndexSet;
Expand Down Expand Up @@ -55,6 +56,10 @@ use rustc_data_structures::fx::FxIndexSet;
/// the code. No crates in `rustc-perf` need more than 15 bits for `ctxt`,
/// but larger crates might need more than 16 bits.
///
/// In order to reliably use parented spans in incremental compilation,
/// the dependency to the parent definition's span. This is performed
/// using the callback `SPAN_TRACK` to access the query engine.
///
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct Span {
base_or_index: u32,
Expand Down Expand Up @@ -96,6 +101,17 @@ impl Span {

#[inline]
pub fn data(self) -> SpanData {
let data = self.decode();
if let Some(parent) = data.parent {
(*SPAN_TRACK)(parent);
}
data
}

/// Internal function to translate between an encoded span and the expanded representation.
/// This function must not be used outside the incremental engine.
#[inline]
pub fn decode(self) -> SpanData {
if self.len_or_tag != LEN_TAG {
// Inline format.
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
Expand Down

0 comments on commit b19ae20

Please sign in to comment.