Skip to content

Macro expanded pretty printed code doesn't follow macro hygiene rules #58031

Closed
@fyrchik

Description

@fyrchik

Hello. I have just started studying rust (and macros), so may not understand fully how it works.

macro_rules! new_bind {
    ($t:ident, $f:expr) => {
        let t = $f;
        println!("3. t = {}", t);
    };
}

macro_rules! existing_ident {
    ($t:ident, $f:expr) => {
        $t = $f;
        println!("1. t = {}", $t);
    };
}

fn main() {
    let mut t = 0;

    existing_ident!(t, 1);
    println!("2. t = {}", t);

    new_bind!(t, 2);
    println!("4. t = {}", t);
}

This code produces following ouptut:

1. t = 1
2. t = 1
3. t = 2
4. t = 1

While I expect following output (also looking at cargo expand results), because new_bind! macro introduces new let binding in the same scope:

1. t = 1
2. t = 1
3. t = 2
4. t = 2

I have found similar issues, but am not sure if they are the same.

#32922
#31856

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-prettyArea: Pretty printing (including `-Z unpretty`)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions