rust-lang / rust-lang/rust-analyzer
Breaking change in highlighting of code under a proc-macro
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
This breaking change happened during the transition from version v0.4.2159 -> v0.4.2160. I tested this by switching between different "Pre-release" versions in VSCode:
code snippet to reproduce:
Add this proc macro as a dependency (using a master git dependency is required):
bon = { git = "https://github.com/elastio/bon" }
#[bon::builder]
fn example(arg: u32) {
let _ = arg;
}
Code highlighting in v0.4.2159 works correctly:
Code highlighting in v0.4.2160 is broken:
The problem is that the arg symbol is assigned the semantic token type of an enum:
Internally (ignoring all other generated code), the macro generates the following code, where the arg symbol is used as a name for an enum, however, that enum declaration appears syntactically higher in code:
mod example_builder {
mod members {
pub(in super::super) enum arg {}
}
}
fn __orig_example(arg: u32) {
let _ = arg;
}
Previously I relied on the fact that rust-analyzer assigned the semantic token type to the symbol based on the last occurence of its span in the generated code, so I intentionally put the fn __orig_example(arg: u32) { /**/ } at the end of the proc macro out so I'm sure RA doesn't mess up the syntax highlighting, because the original function is always at the end thus spans of its symbols will guarantee correct highlighting.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the semantic highlighting difference between rust-analyzer v0.4.2159 and v0.4.2160 in VS Code with the supplied bon proc-macro dependency and example. Inspect how semantic tokens are assigned for identifiers across the generated proc-macro code, especially the conflicting arg enum and function parameter spans. Done means the parameter is highlighted consistently as a function argument rather than as an enum.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100