rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.2k stars 12.7k forks source link

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

Open Houtamelo opened 2 months ago

Houtamelo commented 2 months ago

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

ehuss commented 2 months ago

I do not believe this is related to vis starting with [ (or any other token). I believe it is because $(...)? can be empty, and so can vis. There are two empty matches in a row. There needs to be something non-empty between them to disambiguate.

(I do not know why empty matchers work that way, I would expect an NFA to be able to handle that. It seems like it is being too greedy skipping over empty matches.)