rust-lang / rust-lang/rust

macro_rules: Fragment `vis` somehow matches token `[` despite no visibility modifier starting with/having such token

Open
#128,915 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

macro_rules! makro {
    (
        $(  
            [ $( $fn_type: ident )* ] 
        )?
        
        $fn_vis: vis
    ) => { };
}

makro! { [unsafe] pub }

I expected to see this happen: It compiles

Instead, this happened:

   Compiling playground v0.0.1 (/playground)
error: local ambiguity when calling macro `makro`: multiple parsing options: built-in NTs vis ('fn_vis') or 1 other option.
  --> src/lib.rs:13:10
   |
13 | makro! { [unsafe] pub }
   |          ^

error: could not compile `playground` (lib) due to 1 previous error

rustc --version --verbose:

rustc 1.82.0-nightly (cefe1dcef 2024-07-22)
binary: rustc
commit-hash: cefe1dcef0e21f4d0c8ea856ad61c1936dfb7913
commit-date: 2024-07-22
host: x86_64-pc-windows-msvc
release: 1.82.0-nightly
LLVM version: 18.1.7

This also happens in stable Rust, playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b75389208cdb78680bab4ba33bb90efb

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 supplied macro_rules! example in the linked Rust Playground and compare it with the reported stable and nightly behavior. Then trace the compiler's macro_rules! fragment parsing for vis and determine whether the ambiguity matches the language's intended behavior; done means the behavior is explained and the appropriate compiler or documentation change is identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.