-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
Description
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:
#![feature(macro_rules)]
fn foo() -> bool {
true
}
macro_rules! bar(
() => (foo())
)
#[cfg(test)]
mod test {
fn foo() -> bool {
false
}
#[test]
fn test() {
assert!(bar!());
}
}
Testing this program will result in a test failure because when bar!()
expands to foo()
the foo
is the foo
in the test submodule, whereas it should refer to the foo
in the top-level module where bar!
was defined.
This paper is a good resource for implementing this: http://www.cs.utah.edu/plt/publications/macromod.pdf
Metadata
Metadata
Assignees
Labels
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..)