rust-lang / rust-lang/rust

False positive `local ambiguity` in macro

Open
#154,594 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

MCVE:

macro_rules! mcve {
    ($($my_lft:lifetime)? $($my_ty:ty)?) => {};
}

mcve!('a);
error: local ambiguity when calling macro `mcve`: multiple parsing options: built-in NTs lifetime ('my_lft') or ty ('my_ty').
 --> src/lib.rs:5:7
  |
5 | mcve!('a);
  |       ^^

This piece of code should only be able to match $my_lft, but it complains about being able to reach $my_ty.
To prove that $my_ty cannot be reached, and that this problem happens with lifetime but not with other types such as block, i created the following playground link:

Meta

Tried it in editions 2024, 2021 and 2018, using nightly, 1.94.1, 1.80.1, 1.70.0, 1.60.0, 1.50.0, 1.40.0 and 1.31.0 (i.e. the first version with edition 2018, where the $(...)? operator wasn't even stable).

Similar issues (some probably too old to be relevant, but still included just in case):

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 MCVE in the linked Rust Playground and compare the lifetime case with the block case. Trace macro parsing behavior for the optional lifetime and type metavariables, then verify that the reported local ambiguity disappears without changing valid ambiguity diagnostics.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.