-
Notifications
You must be signed in to change notification settings - Fork 13.4k
No macro hygiene for items #19700
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
Actually, identifiers are protected by hygiene (and will not capture any scope unless the identifier is passed down), but function identifiers aren't. Macros only deal with token trees -- I'm not sure how to manipulate tts to generically refer to a function defined in another crate that may or may not have a different name. |
Only let-bound identifiers are safe. Functions, imports, and statics all have this same issue. The following still fails, though not because of functions: #![feature(macro_rules)]
static BLAHBLAH: bool = true;
macro_rules! bar(
() => ({
BLAHBLAH
})
)
#[cfg(test)]
mod test {
static BLAHBLAH: bool = false;
#[test]
fn test() {
assert!(bar!());
}
} |
This is unfortunate, but it's the documented behavior of See #22462 for a specific manifestation of this problem that I think we can mitigate in time. |
I don't understand why this issue is closed. You're acknowledging it as something that should be fixed so shouldn't it be left open? |
This aspect of We will at some point introduce a new macro system, first in Rust nightlies, then in some stable Rust 1.x. This would happen through the RFC process. It would coexist with the current Until there's a full RFC, we can discuss this further on rust-lang/rfcs#440. |
Uses of identifiers in a macro expansion refer to identifiers in the macro use-site and not the macro-definition site, effectively giving macros dynamic scope.
Minimal example:
Testing this program will result in a test failure because when
bar!()
expands tofoo()
thefoo
is thefoo
in the test submodule, whereas it should refer to thefoo
in the top-level module wherebar!
was defined.This paper is a good resource for implementing this: http://www.cs.utah.edu/plt/publications/macromod.pdf
The text was updated successfully, but these errors were encountered: