Hi,
I'm working on a macro and I'm getting the error local ambiguity when calling macro
, but I think this is not ambiguous. Let me explain:
macro_rules! my_macro {
($($field:ident : $value:expr),*, $msg:expr $(, $part:expr)* $(,)?) => {{
println!("Options:");
$(
println!(" - {}: {}", stringify!($field), $value);
)*
println!("Message:");
println!(" {}", format!($msg, $($part),*));
println!();
}};
}
fn main() {
// This is ok
my_macro!(foo: "bar", "Hello!");
my_macro!(foo: "bar", "Hello {}!", "world");
my_macro!(foo: "bar", "Hello {} {}!", "fellow", "rusticians");
// This is not
my_macro!(foo: "bar", bu: "baz", "Hello!");
my_macro!(foo: "bar", bu: "baz", "Hello {}!", "world");
my_macro!(foo: "bar", bu: "baz", "Hello {} {}!", "fellow", "rusticians");
}
The error (only for the first failing line):
error: local ambiguity when calling macro `my_macro`: multiple parsing options: built-in NTs expr ('msg') or ident ('field').
--> src/main.rs:20:27
|
20 | my_macro!(foo: "bar", bu: "baz", "Hello!");
| ^^
So I can see that bu
can be seen as an expression and that it might be the msg
but then there is the :
so it should be obvious, that that it can't be the msg
because there is no :
after msg
.
Is this a limitation of the compiler or does it in fact see bu: "baz"
as an expression? (If so, why is only bu
highlighted?)
Is there a way around it without using =>
instead of :
? I'm trying to improve a macro that already has one such option named (cwd: "."
) and I want to add other (optional) options in the same style.