Skip to content

Commit fe2a07d

Browse files
Paolo TranquilliCopilot
andcommitted
Rust: Document in_expansion invariant and test inner attribute macros
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: b98227f5d3a24d73abe15df037acd68e
1 parent 8761570 commit fe2a07d

8 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
applyTo: "rust/extractor/src/translate/**/*.rs"
3+
---
4+
Emit any syntax obtained from a rust-analyzer macro expansion API (`Semantics::expand_*` and similar) within `Translator::in_expansion`, otherwise its locations are silently wrong. Built-in derive expansions are the exception: they are located through `builtin_derive_span_map` instead.

‎rust/extractor/src/translate/base.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,11 @@ impl<'db> Translator<'db> {
403403
}
404404
}
405405

406+
/// Runs `f`, which emits macro-expanded syntax.
407+
///
408+
/// All emission of syntax obtained from `Semantics::expand_*` (or any other expansion API) must
409+
/// go through this: `text_range_for_node` trusts nodes emitted outside of it to belong to the
410+
/// extracted file, so an expansion emitted outside of it silently gets wrong locations.
406411
fn in_expansion<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
407412
self.expansion_depth += 1;
408413
let ret = f(self);

‎rust/ql/test/extractor-tests/macro-expansion-inner-attribute/Cargo.lock‎

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mod m {
2+
#![proc_macro::add_marker]
3+
4+
pub fn f() {}
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# custom inner attributes are nightly-only in rustc, but rust-analyzer expands them anyway
2+
qltest_cargo_check: false
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use proc_macro::TokenStream;
2+
use quote::quote;
3+
4+
#[proc_macro_attribute]
5+
pub fn add_marker(_attr: TokenStream, item: TokenStream) -> TokenStream {
6+
let item = syn::parse_macro_input!(item as syn::Item);
7+
quote! {
8+
#item
9+
pub fn marker() {}
10+
}
11+
.into()
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| inner.rs:1:1:5:1 | mod | 0 | inner.rs:1:1:5:1 | mod m |
2+
| inner.rs:1:1:5:1 | mod | 1 | inner.rs:2:8:2:29 | fn marker |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import rust
2+
3+
query predicate attribute_macros(Item i, int index, Item expanded) {
4+
i.fromSource() and expanded = i.getAttributeMacroExpansion().getItem(index)
5+
}

0 commit comments

Comments
 (0)