Description
The span of this error is useful:
error[E0282]: type annotations needed
|
# | println!("{:?}", []);
| ^^ cannot infer type
|
However, in a slightly more complicated println, the same error points at the entire macro invocation:
error[E0282]: type annotations needed
|
# | println!("{:?} {a} {a:?}", [], a = 1 + 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
|
It should have just pointed at []
(or perhaps {:?}
), rather than the entire macro call.
The simple println results in this case in format_args lowering:
rust/compiler/rustc_ast_lowering/src/format.rs
Lines 513 to 520 in cb0d6e7
The more complicated println results in this case in format_args lowering:
rust/compiler/rustc_ast_lowering/src/format.rs
Lines 538 to 547 in cb0d6e7
It'd be nice if both cases would cause any type/trait errors to be reported on the argument (or placeholders), instead of on the span of the entire macro invocation.
I'm not sure if this requires changes in rustc_ast_lowering, or in the code that reports the type errors.
Activity
moatom commentedon May 4, 2025
@m-ou-se
Hi, I'd like to try tackling this issue when I have time because I'm interested in understanding the branching logic that determines how the span is handled in these cases.
Is anyone already working on it? If not, I’ll start looking into it.
P.S. This is my first contribution to the Rust compiler, and I assume this could be a PR with an appropriate level of difficulty. Is that correct?
m-ou-se commentedon May 4, 2025
@moatom As far as I know, nobody is working on it yet. Your help is welcome.
I'm not sure how difficult solving this will be. It might just need changing some spans in
rustc_ast_lowering/src/format.rs
, or it might need changing a lot of stuff in the code that generates the "type annotations needed" error.A big part of the work is going to be trying out a bunch of things and digging through the diagnostics code. Enjoy! ^^