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

Declarative macro transcription does not take into account precedence #444

Open
scrabsha opened this issue Aug 20, 2023 · 1 comment
Open

Comments

@scrabsha
Copy link
Contributor

Let's consider the following code:

macro_rules! two_times {
    ($e:expr) => {
        2 * $e
    };
}

fn main() {
    let a = two_times!(1 + 1);
    assert_eq!(a, 4);
}

Let's break down its expansion according to the current Ferrocene specs:

  • $e matches the expression 1 + 1 (20.4.1.2:6),
  • When transcribing, $e is replaced with 1 + 1 (20.4.2:3),
  • The final expansion is 2 * 1 + 1.

Well, that's not what currently happens (playground link). rustc -Zunpretty=expanded even adds parenthesis around the 1 + 1.

My understanding as that expr metavariables that match an arithmetic expression should have a higher precedence than the rest of the macro substitution. As far as I can tell, this is not mentioned in Ferrocene.

Side note: I don't have much experience in how Ferrocene specs are structured, so I may have used the wrong word/missed something important. Sorry for that.

@Veykril
Copy link
Member

Veykril commented Aug 20, 2023

Good catch we are not specifying none groups (aka invisible delimiters) at all currently which is in general a somewhat undesigned space in rust still rust-lang/rust#67062. We should be specifying the grouping behavior for expressions at least though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants