-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Cannot hygienically create test functions in macro #28335
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
Comments
The |
So we can't hygienically create any functions? That seems odd. |
One workaround is to nest the function in a module: macro_rules! generate_tests {
($m:ident, $t:ty) => {
#[cfg(test)]
mod $m {
#[test]
fn test() { }
}
}
}
struct Foo;
generate_tests!(foo, Foo); |
The problem here is not about hygiene of your macro, macro_!(Foo);
macro_!(Bar); will expand into two functions with same identifier (name) - So, as @lfairy pointed out, you can workaround this, I'll only add that module usage is unnecessary. |
I thought this is what macro hygiene is: The identifiers within your macro don't clash with those outside of it? |
@tbu- That's true, but I think only locals are hygienic |
It says in the book, "This [macro hygiene] holds for let bindings and loop labels, but not for items." |
Triage: it's been 18 months, and it's not clear that there's actually a bug here. Closing! If that's wrong, please open a new bug with the exact details today, thank you! |
Yea, probably not a bug. Sorry for not closing it before. |
Code:
Error:
The text was updated successfully, but these errors were encountered: