Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro literal fragment specifiers should forward to exact tokens #124989

Open
tgross35 opened this issue May 10, 2024 · 3 comments
Open

Macro literal fragment specifiers should forward to exact tokens #124989

tgross35 opened this issue May 10, 2024 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-maybe-future-edition Something we may consider for a future edition. T-lang Relevant to the language team, which will review and decide on the PR/issue. WG-macros Working group: Macros

Comments

@tgross35
Copy link
Contributor

tgross35 commented May 10, 2024

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

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 10, 2024
@tgross35
Copy link
Contributor Author

tgross35 commented May 10, 2024

@rustbot label +A-macros +WG-macros +A-maybe-future-edition +T-lang -needs-triage +A-macros-2.0

@rustbot rustbot added T-lang Relevant to the language team, which will review and decide on the PR/issue. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) WG-macros Working group: Macros A-maybe-future-edition Something we may consider for a future edition. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 10, 2024
@tgross35
Copy link
Contributor Author

@rustbot label +A-macros-2.0

@petrochenkov
Copy link
Contributor

This is a consequence of - NUM being supported by literal.
literal may contain multiple tokens, so it's wrapped into a delimited group to preserve parsing priorities, similarly to expr, ty and others.
Only tt and ident always represent single tokens and therefore not wrapped.

Some PR with backround discussion - #92472.
Relevant part of the reference - https://doc.rust-lang.org/reference/procedural-macros.html#declarative-macro-tokens-and-procedural-macro-tokens.

Note that the reference deliberately says

Such token streams may be wrapped into delimited groups (Group) with implicit delimiters (Delimiter::None) when it's necessary for preserving parsing priorities.

to accommodate for potential future changes (e.g. omitting grouping for single-token streams).
Whether it can be done backward compatibly in practice is a separate question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-maybe-future-edition Something we may consider for a future edition. T-lang Relevant to the language team, which will review and decide on the PR/issue. WG-macros Working group: Macros
Projects
None yet
Development

No branches or pull requests

3 participants