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

Idea: outside consts colliding local variables in macros #85

Open
oxalica opened this issue Aug 28, 2024 · 0 comments
Open

Idea: outside consts colliding local variables in macros #85

oxalica opened this issue Aug 28, 2024 · 0 comments

Comments

@oxalica
Copy link

oxalica commented Aug 28, 2024

In quiz 35 we use colored-local-vars analogy for both variables inside and outside macros, and in quiz 24 we mentioned consts are uncolored. From these, I thought local variables inside macros would still be safe from collision because they are "colored". But that's not the case. Maybe it worths another quiz, or could we expanding quiz 24?

fn main() {
    macro_rules! m {
        () => {
            // could also be `let foo = 42;` but it would give a compile error.
            match Some(42i32) {
                Some(foo) => println!("some"),
                _ => println!("other"),
            }
        };
    }

    { m!(); }
    {
        const foo: i32 = 24;
        m!();
    }
    {
        let foo = 24;
        m!();
    }
}

This code prints:

some
other
some
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

1 participant