rust-lang / rust-lang/rustc-dev-guide

Ambiguous macro call

Open
#395 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macros C-bug E-hard I-incorrect I-terse
Dominant language
HTML
Stars
1.9k
Forks
613
Avg merge
17h 24m
Merged PRs (30d)
39

Description

The guide says right here:

Error: some fatal error has occurred in the parser. For example, this happens if there are more than one pattern match, since that indicates the macro is ambiguous.

This seems not to be the case.

macro_rules! times {
    (a b) => (5000);
    ($x:ident b) => ($x * $x);
    ($x:ident $y:ident) => ($x * $y);
}

fn main() {
    dbg!(times!(a b));
}

Output: 5000

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=bf361f3a9a5ab6fd4d4d30b6d9ae2365

Rust just chooses the first pattern that matches. In fact, you can use the exact same pattern twice and the Rust compiler won't complain.

macro_rules! times {
    (a b) => (5000);
    (a b) => (10000);
}

fn main() {
    dbg!(times!(a b));
}

Output: 5000

Am I misunderstanding the description or is the description wrong?

Contributor guide

No contributing guide indexed for this repository

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

Open src/macro-expansion.md at the linked passage and compare its claim about ambiguous macro patterns with the Rust examples in the issue. Check the relevant macro-matching behavior before revising the explanation. Done means the guide accurately describes which matching patterns are accepted and how Rust selects among them.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.