Skip to content

Strange order dependency for macro that matches _ and ident #39964

Closed
@larsluthman

Description

@larsluthman

A macro_rules! macro with two cases that match _ and ident might not match _ depending on the order of the cases. This works:

macro_rules! foo {
    (_) => {};
    ($a:ident) => {};
}

fn main() {
    foo!(_);
}

But this doesn't:

macro_rules! foo {
    ($a:ident) => {};
    (_) => {};
}

fn main() {
    foo!(_);
}

...giving the error message:

rustc 1.17.0-nightly (306035c21 2017-02-18)
error: expected ident, found _
 --> <anon>:8:10
  |
8 |     foo!(_);
  |          ^

Why would it expect an ident there? If _ isn't an ident, shouldn't it just move on to the second macro case and match that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions