rust-lang / rust-lang/rust-analyzer

Completions in macros should consider the fragment type they are part of

Open
#11,059 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-completion A-macro C-Architecture E-unknown S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

macro_rules! tuple_to_record_closure {
    ($ident:ident { $( $field:ident ),+ $(,)? }) => {
        |($( $field ),+)| $ident { $( $field ),+ }
    };
}
struct Foo { foo: () }
fn main() {
    tuple_to_record_closure! {
        Foo {
            f$0
        }
    }
}

Typing at the cursor position gives us various completions, one of which is the Foo { foo }: Foo pattern completion as the identifier is used in a pattern position in the expansion. This completion is wrong though, as applying it will lead to syntax errors. In this case we need to consider that the identifier we are completing is being captures as an $ident fragment, as such only stupid identifier completions should be allowed. OTOH if it was a $pat fragment capture, this completion would be fine.

We currently lack the ability of figuring out what kind of fragment type a token got captured by, so that needs to be done first. https://github.com/rust-analyzer/rust-analyzer/issues/11183

cc https://github.com/rust-analyzer/rust-analyzer/issues/11058

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 with the reproducer in the issue and read issue #11183, which tracks determining the fragment type captured by a macro token. Trace how completion handles the identifier inside the macro expansion. Done means an $ident capture offers only identifier completions, while a $pat capture can offer pattern completions without producing syntax errors.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.