Skip to content

Declarative macro transcription does not take into account precedence #444

Open
@scrabsha

Description

@scrabsha

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions