Skip to content

Macro literal fragment specifiers should forward to exact tokens #124989

Open
@tgross35

Description

@tgross35

Motivating example (playground link):

macro_rules! foo {
    ($a:literal) => {
        foo!(@inner $a);
    };
    (@inner true) => {
        println!("true");
    };
    (@inner false) => {
        println!("false");
    };
}

fn main() {
    foo!(true);
    foo!(false);
}

The above fails to compile because literal does not forward to exact matches, but it doesn't seem like there is any reason it shouldn't. ident or tt can be used, but loosening restrictions can cause other problems.

This probably couldn't be changed without an edition depending on fallout, but we can probably make sure this works with macros 2.0.

The reference makes a comment about forwarding here: https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-maybe-future-editionSomething we may consider for a future edition.T-langRelevant to the language team, which will review and decide on the PR/issue.WG-macrosWorking group: Macros

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions