Sub topic 8
Hygiene: which names a macro can see
This chapter assumes you can write a macro_rules! macro with expr and ident fragments and a transcriber. It covers how Rust decides which names a macro body and its caller can see.
The problem hygiene solves
A macro pastes code into the caller's program. If that code used plain text substitution, a local variable inside the macro could collide with a variable the caller already has. Rust avoids this with hygiene. Every identifier carries a record of where it was written, and a name only matches another name from the same origin.
Locals defined inside a macro
macro_rules! shadow {
($e:expr) => {{
let x = 100;
$e + x
}};
}
fn main() {
let x = 1;
println!("{}", shadow!(x));
}This prints 101. The macro's let x = 100 and the caller's x look identical in the expanded text. The compiler still treats them as two different variables. The x inside $e came from main, so it refers to 1. The x in $e + x came from the macro body, so it refers to 100.
Names the caller passes in
A hygienic macro cannot see the caller's locals unless the caller hands them over. A macro body that writes y will not find a y declared in main, and you get cannot find value y in this scope. To work with a caller's variable, take its name as an ident fragment:
macro_rules! add_to {
($v:ident, $n:expr) => {
$v += $n;
};
}
fn main() {
let mut total = 1;
add_to!(total, 4);
println!("{}", total);
}This prints 5. The identifier total was written by the caller, so it resolves in the caller's scope. The same rule lets a macro introduce a binding that the caller can use afterwards:
macro_rules! make_var {
($name:ident, $val:expr) => {
let $name = $val;
};
}
fn main() {
make_var!(answer, 42);
println!("{}", answer);
}This prints 42. The caller chose the name answer, so the binding is visible after the expansion. If the macro had written let answer = ... itself, main could not have used it.
What hygiene does not cover
In macro_rules!, hygiene applies only to local variables and loop labels. Items such as functions, structs, modules, and statics are not hygienic. A macro can define one, and the caller can then use it by name:
macro_rules! define_helper {
() => {
fn helper() -> i32 {
7
}
};
}
define_helper!();
fn main() {
println!("{}", helper());
}This prints 7. The function helper is visible to main even though the caller never wrote its name.
Common mistakes
- Expecting a macro to read a caller's local variable by writing its name in the macro body. Pass the name in as an
identinstead. - Expecting
let x = ...inside a macro to create a variable the caller can use. Take the name as anidentif the caller needs it. - Assuming hygiene protects generated functions or types from name clashes. It does not.
Exercise
What does this program print, and why? macro_rules! twice { ($e:expr) => {{ let n = 2; $e * n }}; } fn main() { let n = 10; println!("{}", twice!(n)); }
Show answer
It prints 20. The n in $e was written in main, so it refers to 10. The n in $e * n was written in the macro body, so it refers to 2. The two never clash, and the result is 10 * 2 = 20.