Auto merge of #84373 - cjgillot:resolve-span, r=michaelwoerister,petrochenkov
Encode spans relative to the enclosing item The aim of this PR is to avoid recomputing queries when code is moved without modification. MCP at https://github.com/rust-lang/compiler-team/issues/443 This is achieved by : 1. storing the HIR owner LocalDefId information inside the span; 2. encoding and decoding spans relative to the enclosing item in the incremental on-disk cache; 3. marking a dependency to the `source_span(LocalDefId)` query when we translate a span from the short (`Span`) representation to its explicit (`SpanData`) representation. Since all client code uses `Span`, step 3 ensures that all manipulations of span byte positions actually create the dependency edge between the caller and the `source_span(LocalDefId)`. This query return the actual absolute span of the parent item. As a consequence, any source code motion that changes the absolute byte position of a node will either: - modify the distance to the parent's beginning, so change the relative span's hash; - dirty `source_span`, and trigger the incremental recomputation of all code that depends on the span's absolute byte position. With this scheme, I believe the dependency tracking to be accurate. For the moment, the spans are marked during lowering. I'd rather do this during def-collection, but the AST MutVisitor is not practical enough just yet. The only difference is that we attach macro-expanded spans to their expansion point instead of the macro itself.
This commit is contained in:
commit
547d9374d2
68 changed files with 2800 additions and 1170 deletions
|
@ -41,7 +41,7 @@ use hygiene::Transparency;
|
|||
pub use hygiene::{DesugaringKind, ExpnKind, ForLoopLoc, MacroKind};
|
||||
pub use hygiene::{ExpnData, ExpnHash, ExpnId, LocalExpnId, SyntaxContext};
|
||||
pub mod def_id;
|
||||
use def_id::{CrateNum, DefId, DefPathHash, LOCAL_CRATE};
|
||||
use def_id::{CrateNum, DefId, DefPathHash, LocalDefId, LOCAL_CRATE};
|
||||
pub mod lev_distance;
|
||||
mod span_encoding;
|
||||
pub use span_encoding::{Span, DUMMY_SP};
|
||||
|
@ -434,24 +434,38 @@ pub struct SpanData {
|
|||
/// Information about where the macro came from, if this piece of
|
||||
/// code was created by a macro expansion.
|
||||
pub ctxt: SyntaxContext,
|
||||
pub parent: Option<LocalDefId>,
|
||||
}
|
||||
|
||||
impl SpanData {
|
||||
#[inline]
|
||||
pub fn span(&self) -> Span {
|
||||
Span::new(self.lo, self.hi, self.ctxt)
|
||||
Span::new(self.lo, self.hi, self.ctxt, self.parent)
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_lo(&self, lo: BytePos) -> Span {
|
||||
Span::new(lo, self.hi, self.ctxt)
|
||||
Span::new(lo, self.hi, self.ctxt, self.parent)
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_hi(&self, hi: BytePos) -> Span {
|
||||
Span::new(self.lo, hi, self.ctxt)
|
||||
Span::new(self.lo, hi, self.ctxt, self.parent)
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_ctxt(&self, ctxt: SyntaxContext) -> Span {
|
||||
Span::new(self.lo, self.hi, ctxt)
|
||||
Span::new(self.lo, self.hi, ctxt, self.parent)
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_parent(&self, parent: Option<LocalDefId>) -> Span {
|
||||
Span::new(self.lo, self.hi, self.ctxt, parent)
|
||||
}
|
||||
/// Returns `true` if this is a dummy span with any hygienic context.
|
||||
#[inline]
|
||||
pub fn is_dummy(self) -> bool {
|
||||
self.lo.0 == 0 && self.hi.0 == 0
|
||||
}
|
||||
/// Returns `true` if `self` fully encloses `other`.
|
||||
pub fn contains(self, other: Self) -> bool {
|
||||
self.lo <= other.lo && other.hi <= self.hi
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -507,18 +521,25 @@ impl Span {
|
|||
}
|
||||
#[inline]
|
||||
pub fn ctxt(self) -> SyntaxContext {
|
||||
self.data().ctxt
|
||||
self.data_untracked().ctxt
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_ctxt(self, ctxt: SyntaxContext) -> Span {
|
||||
self.data().with_ctxt(ctxt)
|
||||
self.data_untracked().with_ctxt(ctxt)
|
||||
}
|
||||
#[inline]
|
||||
pub fn parent(self) -> Option<LocalDefId> {
|
||||
self.data().parent
|
||||
}
|
||||
#[inline]
|
||||
pub fn with_parent(self, ctxt: Option<LocalDefId>) -> Span {
|
||||
self.data().with_parent(ctxt)
|
||||
}
|
||||
|
||||
/// Returns `true` if this is a dummy span with any hygienic context.
|
||||
#[inline]
|
||||
pub fn is_dummy(self) -> bool {
|
||||
let span = self.data();
|
||||
span.lo.0 == 0 && span.hi.0 == 0
|
||||
self.data_untracked().is_dummy()
|
||||
}
|
||||
|
||||
/// Returns `true` if this span comes from a macro or desugaring.
|
||||
|
@ -534,26 +555,26 @@ impl Span {
|
|||
|
||||
#[inline]
|
||||
pub fn with_root_ctxt(lo: BytePos, hi: BytePos) -> Span {
|
||||
Span::new(lo, hi, SyntaxContext::root())
|
||||
Span::new(lo, hi, SyntaxContext::root(), None)
|
||||
}
|
||||
|
||||
/// Returns a new span representing an empty span at the beginning of this span.
|
||||
#[inline]
|
||||
pub fn shrink_to_lo(self) -> Span {
|
||||
let span = self.data();
|
||||
let span = self.data_untracked();
|
||||
span.with_hi(span.lo)
|
||||
}
|
||||
/// Returns a new span representing an empty span at the end of this span.
|
||||
#[inline]
|
||||
pub fn shrink_to_hi(self) -> Span {
|
||||
let span = self.data();
|
||||
let span = self.data_untracked();
|
||||
span.with_lo(span.hi)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Returns `true` if `hi == lo`.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
let span = self.data();
|
||||
let span = self.data_untracked();
|
||||
span.hi == span.lo
|
||||
}
|
||||
|
||||
|
@ -566,7 +587,7 @@ impl Span {
|
|||
pub fn contains(self, other: Span) -> bool {
|
||||
let span = self.data();
|
||||
let other = other.data();
|
||||
span.lo <= other.lo && other.hi <= span.hi
|
||||
span.contains(other)
|
||||
}
|
||||
|
||||
/// Returns `true` if `self` touches `other`.
|
||||
|
@ -602,7 +623,7 @@ impl Span {
|
|||
|
||||
/// The `Span` for the tokens in the previous macro expansion from which `self` was generated,
|
||||
/// if any.
|
||||
pub fn parent(self) -> Option<Span> {
|
||||
pub fn parent_callsite(self) -> Option<Span> {
|
||||
let expn_data = self.ctxt().outer_expn_data();
|
||||
if !expn_data.is_root() { Some(expn_data.call_site) } else { None }
|
||||
}
|
||||
|
@ -610,7 +631,7 @@ impl Span {
|
|||
/// Walk down the expansion ancestors to find a span that's contained within `outer`.
|
||||
pub fn find_ancestor_inside(mut self, outer: Span) -> Option<Span> {
|
||||
while !outer.contains(self) {
|
||||
self = self.parent()?;
|
||||
self = self.parent_callsite()?;
|
||||
}
|
||||
Some(self)
|
||||
}
|
||||
|
@ -731,6 +752,7 @@ impl Span {
|
|||
cmp::min(span_data.lo, end_data.lo),
|
||||
cmp::max(span_data.hi, end_data.hi),
|
||||
if span_data.ctxt == SyntaxContext::root() { end_data.ctxt } else { span_data.ctxt },
|
||||
if span_data.parent == end_data.parent { span_data.parent } else { None },
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -748,6 +770,7 @@ impl Span {
|
|||
span.hi,
|
||||
end.lo,
|
||||
if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
|
||||
if span.parent == end.parent { span.parent } else { None },
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -765,6 +788,7 @@ impl Span {
|
|||
span.lo,
|
||||
end.lo,
|
||||
if end.ctxt == SyntaxContext::root() { end.ctxt } else { span.ctxt },
|
||||
if span.parent == end.parent { span.parent } else { None },
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -774,6 +798,7 @@ impl Span {
|
|||
span.lo + BytePos::from_usize(inner.start),
|
||||
span.lo + BytePos::from_usize(inner.end),
|
||||
span.ctxt,
|
||||
span.parent,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -812,7 +837,7 @@ impl Span {
|
|||
pub fn remove_mark(&mut self) -> ExpnId {
|
||||
let mut span = self.data();
|
||||
let mark = span.ctxt.remove_mark();
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
|
||||
mark
|
||||
}
|
||||
|
||||
|
@ -820,7 +845,7 @@ impl Span {
|
|||
pub fn adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
|
||||
let mut span = self.data();
|
||||
let mark = span.ctxt.adjust(expn_id);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
|
||||
mark
|
||||
}
|
||||
|
||||
|
@ -828,7 +853,7 @@ impl Span {
|
|||
pub fn normalize_to_macros_2_0_and_adjust(&mut self, expn_id: ExpnId) -> Option<ExpnId> {
|
||||
let mut span = self.data();
|
||||
let mark = span.ctxt.normalize_to_macros_2_0_and_adjust(expn_id);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
|
||||
mark
|
||||
}
|
||||
|
||||
|
@ -836,7 +861,7 @@ impl Span {
|
|||
pub fn glob_adjust(&mut self, expn_id: ExpnId, glob_span: Span) -> Option<Option<ExpnId>> {
|
||||
let mut span = self.data();
|
||||
let mark = span.ctxt.glob_adjust(expn_id, glob_span);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
|
||||
mark
|
||||
}
|
||||
|
||||
|
@ -848,7 +873,7 @@ impl Span {
|
|||
) -> Option<Option<ExpnId>> {
|
||||
let mut span = self.data();
|
||||
let mark = span.ctxt.reverse_glob_adjust(expn_id, glob_span);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt);
|
||||
*self = Span::new(span.lo, span.hi, span.ctxt, span.parent);
|
||||
mark
|
||||
}
|
||||
|
||||
|
@ -900,7 +925,7 @@ impl<D: Decoder> Decodable<D> for Span {
|
|||
let lo = d.read_struct_field("lo", Decodable::decode)?;
|
||||
let hi = d.read_struct_field("hi", Decodable::decode)?;
|
||||
|
||||
Ok(Span::new(lo, hi, SyntaxContext::root()))
|
||||
Ok(Span::new(lo, hi, SyntaxContext::root(), None))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -961,7 +986,7 @@ impl fmt::Debug for Span {
|
|||
|
||||
impl fmt::Debug for SpanData {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
(*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt), f)
|
||||
(*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt, self.parent), f)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1922,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
|
||||
|
@ -1976,6 +2002,7 @@ impl InnerSpan {
|
|||
pub trait HashStableContext {
|
||||
fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
|
||||
fn hash_spans(&self) -> bool;
|
||||
fn def_span(&self, def_id: LocalDefId) -> Span;
|
||||
fn span_data_to_lines_and_cols(
|
||||
&mut self,
|
||||
span: &SpanData,
|
||||
|
@ -1999,22 +2026,35 @@ where
|
|||
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
|
||||
const TAG_VALID_SPAN: u8 = 0;
|
||||
const TAG_INVALID_SPAN: u8 = 1;
|
||||
const TAG_RELATIVE_SPAN: u8 = 2;
|
||||
|
||||
if !ctx.hash_spans() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.ctxt().hash_stable(ctx, hasher);
|
||||
let span = self.data_untracked();
|
||||
span.ctxt.hash_stable(ctx, hasher);
|
||||
span.parent.hash_stable(ctx, hasher);
|
||||
|
||||
if self.is_dummy() {
|
||||
if span.is_dummy() {
|
||||
Hash::hash(&TAG_INVALID_SPAN, hasher);
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(parent) = span.parent {
|
||||
let def_span = ctx.def_span(parent).data_untracked();
|
||||
if def_span.contains(span) {
|
||||
// This span is enclosed in a definition: only hash the relative position.
|
||||
Hash::hash(&TAG_RELATIVE_SPAN, hasher);
|
||||
(span.lo - def_span.lo).to_u32().hash_stable(ctx, hasher);
|
||||
(span.hi - def_span.lo).to_u32().hash_stable(ctx, hasher);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 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 span = self.data();
|
||||
let (file, line_lo, col_lo, line_hi, col_hi) = match ctx.span_data_to_lines_and_cols(&span)
|
||||
{
|
||||
Some(pos) => pos,
|
||||
|
|
|
@ -794,7 +794,7 @@ impl SourceMap {
|
|||
start_of_next_point.checked_add(width - 1).unwrap_or(start_of_next_point);
|
||||
|
||||
let end_of_next_point = BytePos(cmp::max(sp.lo().0 + 1, end_of_next_point));
|
||||
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt())
|
||||
Span::new(BytePos(start_of_next_point), end_of_next_point, sp.ctxt(), None)
|
||||
}
|
||||
|
||||
/// Finds the width of the character, either before or after the end of provided span,
|
||||
|
|
|
@ -4,7 +4,9 @@
|
|||
// The encoding format for inline spans were obtained by optimizing over crates in rustc/libstd.
|
||||
// See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28
|
||||
|
||||
use crate::def_id::LocalDefId;
|
||||
use crate::hygiene::SyntaxContext;
|
||||
use crate::SPAN_TRACK;
|
||||
use crate::{BytePos, SpanData};
|
||||
|
||||
use rustc_data_structures::fx::FxIndexSet;
|
||||
|
@ -54,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,
|
||||
|
@ -70,25 +76,42 @@ pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_zero:
|
|||
|
||||
impl Span {
|
||||
#[inline]
|
||||
pub fn new(mut lo: BytePos, mut hi: BytePos, ctxt: SyntaxContext) -> Self {
|
||||
pub fn new(
|
||||
mut lo: BytePos,
|
||||
mut hi: BytePos,
|
||||
ctxt: SyntaxContext,
|
||||
parent: Option<LocalDefId>,
|
||||
) -> Self {
|
||||
if lo > hi {
|
||||
std::mem::swap(&mut lo, &mut hi);
|
||||
}
|
||||
|
||||
let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
|
||||
|
||||
if len <= MAX_LEN && ctxt2 <= MAX_CTXT {
|
||||
if len <= MAX_LEN && ctxt2 <= MAX_CTXT && parent.is_none() {
|
||||
// Inline format.
|
||||
Span { base_or_index: base, len_or_tag: len as u16, ctxt_or_zero: ctxt2 as u16 }
|
||||
} else {
|
||||
// Interned format.
|
||||
let index = with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt }));
|
||||
let index =
|
||||
with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent }));
|
||||
Span { base_or_index: index, len_or_tag: LEN_TAG, ctxt_or_zero: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn data(self) -> SpanData {
|
||||
let data = self.data_untracked();
|
||||
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 data_untracked(self) -> SpanData {
|
||||
if self.len_or_tag != LEN_TAG {
|
||||
// Inline format.
|
||||
debug_assert!(self.len_or_tag as u32 <= MAX_LEN);
|
||||
|
@ -96,6 +119,7 @@ impl Span {
|
|||
lo: BytePos(self.base_or_index),
|
||||
hi: BytePos(self.base_or_index + self.len_or_tag as u32),
|
||||
ctxt: SyntaxContext::from_u32(self.ctxt_or_zero as u32),
|
||||
parent: None,
|
||||
}
|
||||
} else {
|
||||
// Interned format.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue