Skip to content

Commit e6455ea

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 eac0b26 + ce1da84 commit e6455ea

10 files changed

+13
-10
lines changed

clippy_lints/src/attrs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,8 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
527527
return;
528528
}
529529

530-
let begin_of_attr_to_item = Span::new(attr.span.lo(), item.span.lo(), item.span.ctxt());
531-
let end_of_attr_to_item = Span::new(attr.span.hi(), item.span.lo(), item.span.ctxt());
530+
let begin_of_attr_to_item = Span::new(attr.span.lo(), item.span.lo(), item.span.ctxt(), item.span.parent());
531+
let end_of_attr_to_item = Span::new(attr.span.hi(), item.span.lo(), item.span.ctxt(), item.span.parent());
532532

533533
if let Some(snippet) = snippet_opt(cx, end_of_attr_to_item) {
534534
let lines = snippet.split('\n').collect::<Vec<_>>();

clippy_lints/src/cognitive_complexity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl CognitiveComplexity {
9595
});
9696

9797
if let Some((low, high)) = pos {
98-
Span::new(low, high, header_span.ctxt())
98+
Span::new(low, high, header_span.ctxt(), header_span.parent())
9999
} else {
100100
return;
101101
}

clippy_lints/src/copies.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ fn emit_branches_sharing_code_lint(
472472

473473
let mut span = moved_start.to(span_end);
474474
// Improve formatting if the inner block has indention (i.e. normal Rust formatting)
475-
let test_span = Span::new(span.lo() - BytePos(4), span.lo(), span.ctxt());
475+
let test_span = Span::new(span.lo() - BytePos(4), span.lo(), span.ctxt(), span.parent());
476476
if snippet_opt(cx, test_span)
477477
.map(|snip| snip == " ")
478478
.unwrap_or_default()

clippy_lints/src/doc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ fn check_text(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, text: &str
665665
span.lo() + BytePos::from_usize(offset),
666666
span.lo() + BytePos::from_usize(offset + word.len()),
667667
span.ctxt(),
668+
span.parent(),
668669
);
669670

670671
check_word(cx, word, span);

clippy_lints/src/implicit_hasher.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
130130
let pos = snippet_opt(cx, item.span.until(target.span()))
131131
.and_then(|snip| Some(item.span.lo() + BytePos(snip.find("impl")? as u32 + 4)));
132132
if let Some(pos) = pos {
133-
Span::new(pos, pos, item.span.data().ctxt)
133+
Span::new(pos, pos, item.span.ctxt(), item.span.parent())
134134
} else {
135135
return;
136136
}
@@ -173,7 +173,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
173173
Some(item.span.lo() + BytePos((i + (&snip[i..]).find('(')?) as u32))
174174
})
175175
.expect("failed to create span for type parameters");
176-
Span::new(pos, pos, item.span.data().ctxt)
176+
Span::new(pos, pos, item.span.ctxt(), item.span.parent())
177177
});
178178

179179
let mut ctr_vis = ImplicitHasherConstructorVisitor::new(cx, target);

clippy_lints/src/large_const_arrays.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
6363
hi_pos - BytePos::from_usize("const".len()),
6464
hi_pos,
6565
item.span.ctxt(),
66+
item.span.parent(),
6667
);
6768
span_lint_and_then(
6869
cx,

clippy_lints/src/methods/manual_split_once.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ fn parse_iter_usage(
182182
},
183183
_,
184184
) => {
185-
let parent_span = e.span.parent().unwrap();
185+
let parent_span = e.span.parent_callsite().unwrap();
186186
if parent_span.ctxt() == ctxt {
187187
(Some(UnwrapKind::QuestionMark), parent_span)
188188
} else {

clippy_lints/src/module_style.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ impl EarlyLintPass for ModStyle {
120120
correct.push("mod.rs");
121121
cx.struct_span_lint(
122122
SELF_NAMED_MODULE_FILES,
123-
Span::new(file.start_pos, file.start_pos, SyntaxContext::root()),
123+
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
124124
|build| {
125125
let mut lint =
126126
build.build(&format!("`mod.rs` files are required, found `{}`", path.display()));
@@ -167,7 +167,7 @@ fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &Source
167167

168168
cx.struct_span_lint(
169169
MOD_MODULE_FILES,
170-
Span::new(file.start_pos, file.start_pos, SyntaxContext::root()),
170+
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
171171
|build| {
172172
let mut lint = build.build(&format!("`mod.rs` files are not allowed, found `{}`", path.display()));
173173
lint.help(&format!("move `{}` to `{}`", path.display(), mod_file.display(),));

clippy_lints/src/regex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
8787
let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
8888
let start = base.lo() + BytePos(u32::try_from(c.start.offset).expect("offset too large") + offset);
8989
assert!(start <= end);
90-
Span::new(start, end, base.ctxt())
90+
Span::new(start, end, base.ctxt(), base.parent())
9191
}
9292

9393
fn const_str<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<String> {

clippy_lints/src/tabs_in_doc_comments.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ impl TabsInDocComments {
6969
attr.span.lo() + BytePos(3 + lo),
7070
attr.span.lo() + BytePos(3 + hi),
7171
attr.span.ctxt(),
72+
attr.span.parent(),
7273
);
7374
span_lint_and_sugg(
7475
cx,

0 commit comments

Comments
 (0)