rust-lang / rust-lang/rust-analyzer

Incorrect "Leftover tokens" error with `$(…)?` repetition

Open
#22,431 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-macro C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 0.3.2845-standalone

rustc version: current stable, but shouldn't matter

editor or extension: VS Code

relevant settings: n/a

code snippet to reproduce:

#[macro_export]
macro_rules! attr {
    ($t:ty) => {
        {
            $crate::attr_array!($t; 1);
        }
    };
}

#[macro_export]
macro_rules! attr_array {
    ($( $t:ty; )? $count:expr) => {
        [(); $count]  // expected (), found [(); _]
    };
}

fn make<T>() {
    attr!(T)  // leftover tokens
}

rust-analyzer reports 2 incorrect errors, but rustc accepts this code fine.

Unlike the other issues that mention this particular error, my reproduction seems to depend on a $(…)? or $(…)* repetition being present (a + repetition makes the error go away), so I figured I'd file a separate issue.

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

Reproduce the supplied macro snippet in rust-analyzer and compare its diagnostics with rustc, focusing on the optional $(...)? repetition in attr_array!. Done when rust-analyzer stops reporting the two errors that rustc does not report and still handles the shown macro expansion correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.