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

Syntax extension to generate unique identifiers #9031

Closed
sfackler opened this issue Sep 7, 2013 · 2 comments
Closed

Syntax extension to generate unique identifiers #9031

sfackler opened this issue Sep 7, 2013 · 2 comments

Comments

@sfackler
Copy link
Member

sfackler commented Sep 7, 2013

When writing a macro, it's common to use a couple of temporary variables, for example m in https://gist.github.com/luqmana/6219956. However, if you do something like

let m = 10;
let h = hashmap! {
    "foo" => m
};

The macro's internal m ends up being inserted, creating a type of infinite size. You can try to get around this issue by changing m to something like __hashmap_temp_var_, but you can still run into issues if, for example, you have nested invocations of a macro or if someone decides to be a jerk and define a variable called __hashmap_temp_var_.

It'd be nice to have a syntax extension that will generate identifiers that are guaranteed to be unique. I'd imagine it'd be used something like

macro_rules! my_macro (
    ($a:expr) => (
        {
            let unique_ident!(m) = HashMap::new();
            let unique_ident!(v) = ~[];
            unique_ident!(v).push($a);
            unique_ident!(m).insert("foo", $a);
            (unique_ident!(v), unique_ident!(m))
        }
    )
)

The implementation would be interesting, since you'd have to make sure that unique_ident!(m) expands to the same thing within a given macro expansion, but to a different thing within any nested expansions. You'd also want to make sure it doesn't shadow any other variables in scope, maybe by using a name that would be illegal to manually define.

@huonw
Copy link
Member

huonw commented Sep 7, 2013

Is this required with @jbclements' wonderful hygiene work?

@sfackler
Copy link
Member Author

sfackler commented Sep 7, 2013

Oh cool, I didn't realize that #9026 covers this!

@sfackler sfackler closed this as completed Sep 7, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Jun 30, 2022
Add [`manual_rem_euclid`] lint

Closes rust-lang#8883

Adds a lint for checking manual use of `rem_euclid(n)`

changelog: Add [`manual_rem_euclid`] lint
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