macro_rules! macro_with_format { () => {
fn check_5(arg : usize) -> String {
let s : &str;
if arg < 5 {
s = format!("{arg} < 5");
} else {
s = format!("{arg} >= 5");
}
String::from(s)
}
}}
fn main() {
macro_with_format!();
println!( "{}", check_5(6) );
}
I get the following message (which does not indicate where the problem is in the macro):
--> src/main.rs:14:5
|
14 | macro_with_format!();
| ^^^^^^^^^^^^^^^^^^^^ expected `&str`, found `String`
|
= note: this error originates in the macro `format` which comes from the expansion of the macro `macro_with_format` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0308`.
error: could not compile `package` (bin "package") due to 2 previous errors
Yes, I understand that. My actuall application is in a macro with many formats and I would like the error message to tell me which one has the error, instead of that is is somewhere inside the macro.
The reason it's not printing additional context is because the source of the type error is considered to be withinformat (even though it shouldn't necessarily here), so the compiler expects that giving more information isn't needed. As seen when using -Zmacro-backtrace in a nightly build, the compiler does keep context of every expansion. I think we should rework the logic slightly here to instead of looking at the root span context for whether its info is relevant, we should walk the entire chain, and provide context for the last local macro expansion (in this case, point inside macro_with_format). Could you file a diagnostic ticket requesting this so that we properly track it? I think this is a change that will require some fidgeting to get right, but the code complexity isn't high (just dealing with Spans and macro contexts, which can get confusing).
The following seems to restore the default version:
rustup default stable
RUSTFLAGS=''
I think that this is a good stop gap aproach, if you are using the stable version of rust.
I also think that requesting that back tracing macros when they have an error is really helpful feature and I would like to see it in the stable version rust. Where does one rquest this ?