Skip to content

Commit 97e2d3c

Browse files
authored
Rollup merge of #140916 - moatom:140578, r=chenyukang
Fix unuseful span in type error in some format_args!() invocations Fixed #140578. r? ``@m-ou-se``
2 parents a2db928 + b13b87a commit 97e2d3c

File tree

4 files changed

+115
-11
lines changed

4 files changed

+115
-11
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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
fn check_format_args() {
2+
print!("{:?} {a} {a:?}", [], a = 1 + 1);
3+
//~^ ERROR type annotations needed
4+
}
5+
6+
fn check_format_args_nl() {
7+
println!("{:?} {a} {a:?}", [], a = 1 + 1);
8+
//~^ ERROR type annotations needed
9+
}
10+
11+
fn check_multi1() {
12+
println!("{:?} {:?} {a} {a:?}", [], [], a = 1 + 1);
13+
//~^ ERROR type annotations needed
14+
}
15+
16+
fn check_multi2() {
17+
println!("{:?} {:?} {a} {a:?} {b:?}", [], [], a = 1 + 1, b = []);
18+
//~^ ERROR type annotations needed
19+
}
20+
21+
fn check_unformatted() {
22+
println!("
23+
{:?} {:?}
24+
{a}
25+
{a:?}",
26+
[],
27+
//~^ ERROR type annotations needed
28+
[],
29+
a = 1 + 1);
30+
}
31+
32+
fn main() {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/span-format_args-issue-140578.rs:2:28
3+
|
4+
LL | print!("{:?} {a} {a:?}", [], a = 1 + 1);
5+
| ^^ cannot infer type
6+
|
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)
8+
9+
error[E0282]: type annotations needed
10+
--> $DIR/span-format_args-issue-140578.rs:7:30
11+
|
12+
LL | println!("{:?} {a} {a:?}", [], a = 1 + 1);
13+
| ^^ cannot infer type
14+
|
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)
16+
17+
error[E0282]: type annotations needed
18+
--> $DIR/span-format_args-issue-140578.rs:12:35
19+
|
20+
LL | println!("{:?} {:?} {a} {a:?}", [], [], a = 1 + 1);
21+
| ^^ cannot infer type
22+
|
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)
24+
25+
error[E0282]: type annotations needed
26+
--> $DIR/span-format_args-issue-140578.rs:17:41
27+
|
28+
LL | println!("{:?} {:?} {a} {a:?} {b:?}", [], [], a = 1 + 1, b = []);
29+
| ^^ cannot infer type
30+
|
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)
32+
33+
error[E0282]: type annotations needed
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)
40+
41+
error: aborting due to 5 previous errors
42+
43+
For more information about this error, try `rustc --explain E0282`.

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)