Skip to content

Commit 80049c8

Browse files
committed
Fix unuseful span in type error in some format_args!() invocations
1 parent d51b6f9 commit 80049c8

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod on_unimplemented_format;
77
mod overflow;
88
pub mod suggestions;
99

10+
use std::cmp::Reverse;
1011
use std::{fmt, iter};
1112

1213
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
@@ -136,6 +137,17 @@ pub enum DefIdOrName {
136137
Name(&'static str),
137138
}
138139

140+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
141+
enum ErrorSortKey {
142+
SubtypeFormat(Reverse<usize>),
143+
OtherKind,
144+
SizedTrait,
145+
MetaSizedTrait,
146+
PointeeSizedTrait,
147+
Coerce,
148+
ClauseWellFormed,
149+
}
150+
139151
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
140152
pub fn report_fulfillment_errors(
141153
&self,
@@ -170,13 +182,39 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
170182
_ => None,
171183
};
172184

185+
186+
let span = e.obligation.cause.span;
187+
let outer_expn_data = span.ctxt().outer_expn_data();
188+
173189
match e.obligation.predicate.kind().skip_binder() {
174-
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => 1,
175-
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => 2,
176-
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => 3,
177-
ty::PredicateKind::Coerce(_) => 4,
178-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => 5,
179-
_ => 0,
190+
ty::PredicateKind::Subtype(_)
191+
if matches! (
192+
outer_expn_data.kind,
193+
ExpnKind::Macro(_, name) if matches!(
194+
name.as_str().rsplit("::").next(),
195+
Some("format_args" | "format_args_nl")
196+
)
197+
) =>
198+
{
199+
let sm = self.tcx.sess.source_map();
200+
let lc = sm.span_to_location_info(span);
201+
202+
if sm.span_to_embeddable_string(span)
203+
== sm.span_to_embeddable_string(outer_expn_data.call_site)
204+
{
205+
ErrorSortKey::OtherKind
206+
} else {
207+
ErrorSortKey::SubtypeFormat(Reverse(lc.2))
208+
}
209+
}
210+
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => ErrorSortKey::SizedTrait,
211+
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => ErrorSortKey::MetaSizedTrait,
212+
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => ErrorSortKey::PointeeSizedTrait,
213+
ty::PredicateKind::Coerce(_) => ErrorSortKey::Coerce,
214+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => {
215+
ErrorSortKey::ClauseWellFormed
216+
}
217+
_ => ErrorSortKey::OtherKind,
180218
}
181219
});
182220

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)