Skip to content

Commit 8743472

Browse files
committed
Auto merge of rust-lang#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 rust-lang/compiler-team#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.
2 parents e014277 + 127ec9a commit 8743472

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/macros.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,12 @@ impl MacroParser {
12471247
let data = delimited_span.entire().data();
12481248
(
12491249
data.hi,
1250-
Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt),
1250+
Span::new(
1251+
data.lo + BytePos(1),
1252+
data.hi - BytePos(1),
1253+
data.ctxt,
1254+
data.parent,
1255+
),
12511256
delimited_span.entire(),
12521257
)
12531258
}

src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ macro_rules! source {
356356
}
357357

358358
pub(crate) fn mk_sp(lo: BytePos, hi: BytePos) -> Span {
359-
Span::new(lo, hi, SyntaxContext::root())
359+
Span::new(lo, hi, SyntaxContext::root(), None)
360360
}
361361

362362
pub(crate) fn mk_sp_lo_plus_one(lo: BytePos) -> Span {
363-
Span::new(lo, lo + BytePos(1), SyntaxContext::root())
363+
Span::new(lo, lo + BytePos(1), SyntaxContext::root(), None)
364364
}
365365

366366
// Returns `true` if the given span does not intersect with file lines.

0 commit comments

Comments
 (0)