rust-lang / rust-lang/rust-analyzer

Breaking change in highlighting of code under a proc-macro

Open
#18,438 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

C-support
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:
Image

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.