Fix unuseful span in type error in some format_args!() invocations

This commit is contained in:
Tomoaki Kobayashi 2025-09-18 14:42:56 +09:00
parent 0fd6f1113b
commit b13b87a1c3
4 changed files with 58 additions and 34 deletions

View file

@ -21,7 +21,7 @@ use rustc_infer::traits::{
};
use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::{ErrorGuaranteed, ExpnKind, Span};
use rustc_span::{DesugaringKind, ErrorGuaranteed, ExpnKind, Span};
use tracing::{info, instrument};
pub use self::overflow::*;
@ -154,9 +154,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
})
.collect();
// Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last.
// Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last,
// and `Subtype` obligations from `FormatLiteral` desugarings come first.
// This lets us display diagnostics with more relevant type information and hide redundant
// E0282 errors.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum ErrorSortKey {
SubtypeFormat(usize, usize),
OtherKind,
SizedTrait,
MetaSizedTrait,
PointeeSizedTrait,
Coerce,
WellFormed,
}
errors.sort_by_key(|e| {
let maybe_sizedness_did = match e.obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => Some(pred.def_id()),
@ -165,12 +176,30 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
};
match e.obligation.predicate.kind().skip_binder() {
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => 1,
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => 2,
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => 3,
ty::PredicateKind::Coerce(_) => 4,
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => 5,
_ => 0,
ty::PredicateKind::Subtype(_)
if matches!(
e.obligation.cause.span.desugaring_kind(),
Some(DesugaringKind::FormatLiteral { .. })
) =>
{
let (_, row, col, ..) =
self.tcx.sess.source_map().span_to_location_info(e.obligation.cause.span);
ErrorSortKey::SubtypeFormat(row, col)
}
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => {
ErrorSortKey::SizedTrait
}
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => {
ErrorSortKey::MetaSizedTrait
}
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => {
ErrorSortKey::PointeeSizedTrait
}
ty::PredicateKind::Coerce(_) => ErrorSortKey::Coerce,
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => {
ErrorSortKey::WellFormed
}
_ => ErrorSortKey::OtherKind,
}
});

View file

@ -19,11 +19,12 @@ fn check_multi2() {
}
fn check_unformatted() {
println!(" //~ ERROR type annotations needed
println!("
{:?} {:?}
{a}
{a:?}",
[],
//~^ ERROR type annotations needed
[],
a = 1 + 1);
}

View file

@ -1,48 +1,42 @@
error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:2:3
--> $DIR/span-format_args-issue-140578.rs:2:28
|
LL | print!("{:?} {a} {a:?}", [], a = 1 + 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
| ^^ cannot infer type
|
= note: this error originates in the macro `print` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::format_args` which comes from the expansion of the macro `print` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:7:3
--> $DIR/span-format_args-issue-140578.rs:7:30
|
LL | println!("{:?} {a} {a:?}", [], a = 1 + 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
| ^^ cannot infer type
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:12:3
--> $DIR/span-format_args-issue-140578.rs:12:35
|
LL | println!("{:?} {:?} {a} {a:?}", [], [], a = 1 + 1);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
| ^^ cannot infer type
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:17:3
--> $DIR/span-format_args-issue-140578.rs:17:41
|
LL | println!("{:?} {:?} {a} {a:?} {b:?}", [], [], a = 1 + 1, b = []);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
| ^^ cannot infer type
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:22:3
--> $DIR/span-format_args-issue-140578.rs:26:9
|
LL | / println!("
LL | | {:?} {:?}
LL | | {a}
LL | | {a:?}",
LL | | [],
LL | | [],
LL | | a = 1 + 1);
| |__________^ cannot infer type
LL | [],
| ^^ cannot infer type
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 5 previous errors

View file

@ -1,10 +1,10 @@
error[E0282]: type annotations needed
--> $DIR/issue-107745-avoid-expr-from-macro-expansion.rs:17:5
--> $DIR/issue-107745-avoid-expr-from-macro-expansion.rs:17:22
|
LL | println!("{:?}", []);
| ^^^^^^^^^^^^^^^^^^^^ cannot infer type
| ^^ cannot infer type
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error