Skip to content

Commit b13b87a

Browse files
committed
Fix unuseful span in type error in some format_args!() invocations
1 parent 0fd6f11 commit b13b87a

File tree

4 files changed

+60
-36
lines changed

4 files changed

+60
-36
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_infer::traits::{
2121
};
2222
use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
2323
use rustc_middle::ty::{self, Ty, TyCtxt};
24-
use rustc_span::{ErrorGuaranteed, ExpnKind, Span};
24+
use rustc_span::{DesugaringKind, ErrorGuaranteed, ExpnKind, Span};
2525
use tracing::{info, instrument};
2626

2727
pub use self::overflow::*;
@@ -154,9 +154,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
154154
})
155155
.collect();
156156

157-
// Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last.
157+
// Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last,
158+
// and `Subtype` obligations from `FormatLiteral` desugarings come first.
158159
// This lets us display diagnostics with more relevant type information and hide redundant
159160
// E0282 errors.
161+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
162+
enum ErrorSortKey {
163+
SubtypeFormat(usize, usize),
164+
OtherKind,
165+
SizedTrait,
166+
MetaSizedTrait,
167+
PointeeSizedTrait,
168+
Coerce,
169+
WellFormed,
170+
}
160171
errors.sort_by_key(|e| {
161172
let maybe_sizedness_did = match e.obligation.predicate.kind().skip_binder() {
162173
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => Some(pred.def_id()),
@@ -165,12 +176,30 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
165176
};
166177

167178
match e.obligation.predicate.kind().skip_binder() {
168-
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => 1,
169-
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => 2,
170-
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => 3,
171-
ty::PredicateKind::Coerce(_) => 4,
172-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => 5,
173-
_ => 0,
179+
ty::PredicateKind::Subtype(_)
180+
if matches!(
181+
e.obligation.cause.span.desugaring_kind(),
182+
Some(DesugaringKind::FormatLiteral { .. })
183+
) =>
184+
{
185+
let (_, row, col, ..) =
186+
self.tcx.sess.source_map().span_to_location_info(e.obligation.cause.span);
187+
ErrorSortKey::SubtypeFormat(row, col)
188+
}
189+
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => {
190+
ErrorSortKey::SizedTrait
191+
}
192+
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => {
193+
ErrorSortKey::MetaSizedTrait
194+
}
195+
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => {
196+
ErrorSortKey::PointeeSizedTrait
197+
}
198+
ty::PredicateKind::Coerce(_) => ErrorSortKey::Coerce,
199+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => {
200+
ErrorSortKey::WellFormed
201+
}
202+
_ => ErrorSortKey::OtherKind,
174203
}
175204
});
176205

tests/ui/errors/span-format_args-issue-140578.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ fn check_multi2() {
1919
}
2020

2121
fn check_unformatted() {
22-
println!(" //~ ERROR type annotations needed
22+
println!("
2323
{:?} {:?}
2424
{a}
2525
{a:?}",
2626
[],
27+
//~^ ERROR type annotations needed
2728
[],
2829
a = 1 + 1);
2930
}

tests/ui/errors/span-format_args-issue-140578.stderr

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/span-format_args-issue-140578.rs:2:3
2+
--> $DIR/span-format_args-issue-140578.rs:2:28
33
|
44
LL | print!("{:?} {a} {a:?}", [], a = 1 + 1);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
5+
| ^^ cannot infer type
66
|
7-
= note: this error originates in the macro `print` (in Nightly builds, run with -Z macro-backtrace for more info)
7+
= 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)
88

99
error[E0282]: type annotations needed
10-
--> $DIR/span-format_args-issue-140578.rs:7:3
10+
--> $DIR/span-format_args-issue-140578.rs:7:30
1111
|
1212
LL | println!("{:?} {a} {a:?}", [], a = 1 + 1);
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
13+
| ^^ cannot infer type
1414
|
15-
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
15+
= 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)
1616

1717
error[E0282]: type annotations needed
18-
--> $DIR/span-format_args-issue-140578.rs:12:3
18+
--> $DIR/span-format_args-issue-140578.rs:12:35
1919
|
2020
LL | println!("{:?} {:?} {a} {a:?}", [], [], a = 1 + 1);
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
21+
| ^^ cannot infer type
2222
|
23-
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
= 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)
2424

2525
error[E0282]: type annotations needed
26-
--> $DIR/span-format_args-issue-140578.rs:17:3
26+
--> $DIR/span-format_args-issue-140578.rs:17:41
2727
|
2828
LL | println!("{:?} {:?} {a} {a:?} {b:?}", [], [], a = 1 + 1, b = []);
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
29+
| ^^ cannot infer type
3030
|
31-
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
= 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)
3232

3333
error[E0282]: type annotations needed
34-
--> $DIR/span-format_args-issue-140578.rs:22:3
35-
|
36-
LL | / println!("
37-
LL | | {:?} {:?}
38-
LL | | {a}
39-
LL | | {a:?}",
40-
LL | | [],
41-
LL | | [],
42-
LL | | a = 1 + 1);
43-
| |__________^ cannot infer type
44-
|
45-
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
34+
--> $DIR/span-format_args-issue-140578.rs:26:9
35+
|
36+
LL | [],
37+
| ^^ cannot infer type
38+
|
39+
= 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)
4640

4741
error: aborting due to 5 previous errors
4842

tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/issue-107745-avoid-expr-from-macro-expansion.rs:17:5
2+
--> $DIR/issue-107745-avoid-expr-from-macro-expansion.rs:17:22
33
|
44
LL | println!("{:?}", []);
5-
| ^^^^^^^^^^^^^^^^^^^^ cannot infer type
5+
| ^^ cannot infer type
66
|
7-
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
7+
= 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)
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)