Skip to content

Unuseful span in type error in some format_args!() invocations #140578

Open
@m-ou-se

Description

@m-ou-se

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:

} else if use_simple_array {
// Generate:
// &[
// <core::fmt::Argument>::new_display(&arg0),
// <core::fmt::Argument>::new_lower_hex(&arg1),
// <core::fmt::Argument>::new_debug(&arg2),
// …
// ]

The more complicated println results in this case in format_args lowering:

} else {
// Generate:
// &match (&arg0, &arg1, &…) {
// args => [
// <core::fmt::Argument>::new_display(args.0),
// <core::fmt::Argument>::new_lower_hex(args.1),
// <core::fmt::Argument>::new_debug(args.0),
// …
// ]
// }

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-fmtArea: `core::fmt`D-imprecise-spansDiagnostics: spans don't point to exactly the erroneous codeE-help-wantedCall for participation: Help is requested to fix this issue.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions