#![feature(macro_metavar_expr)]
macro_rules! init_env {
($self:ident) => {
macro_rules! appear {
() => {
$self += 1
};
}
macro_rules! tap {
(i32) => {
$self.abs()
};
}
// ... many macro that use $self
};
}
fn main() {
let x = 0;
init_env!(x);
fn f(x: i32) {
init_env!(x);
tap!(); // <- error: ambiguous
}
}
The tap! is ambiguous as there is a same named macro in parent scope.
How can I make macro expansion to prefer the inner most definition, like normal variable and function?
Or can I remove a macro definition in current scope?
since, indeed, there is a semantic difference between an inlined macro definition and a macro-generated one.
This, on the other hand, is very interesting! This now does hint at having a preprocessor pattern which could alleviate the syntax for the caller:
// Hypothetically, we could have:
#[replace_make_self_with_macro_definition]
fn main ()
{
let x = 0i32;
make_self!(x);
tap!();
fn f (x: i32)
{
make_self!(x);
tap!();
}
}
Where replace_make_self_with_macro_definition would be a proc-macro using a Visitor pattern to locate a