Skip to content

Ambiguous macro call #395

Open
Open
@iluuu1994

Description

@iluuu1994

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: macros (general sense)C-bugCategory: bugE-hardDifficulty: might require advanced knowledgeI-incorrectIssue: info is incorrect or misleadingI-terseIssue: info is very terse

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions