Skip to content

Commit 9beea6f

Browse files
committed
Fix SubtypeFormat sorting for unformmated code
1 parent a041ee2 commit 9beea6f

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub enum DefIdOrName {
139139

140140
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
141141
enum ErrorSortKey {
142-
SubtypeFormat(Reverse<usize>),
142+
SubtypeFormat(Reverse<(usize, usize)>),
143143
OtherKind,
144144
ClauseTraitSized,
145145
Coerce,
@@ -175,18 +175,21 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
175175
errors.sort_by_key(|e| {
176176
let span = e.obligation.cause.span;
177177
let outer_expn_data = span.ctxt().outer_expn_data();
178+
let source_span = outer_expn_data.call_site.source_callsite();
179+
let source_map = self.tcx.sess.source_map();
178180

179181
match e.obligation.predicate.kind().skip_binder() {
180182
ty::PredicateKind::Subtype(_)
181183
if let Some(def_id) = outer_expn_data.macro_def_id
184+
&& let (Some(span_file), row, col, ..) =
185+
source_map.span_to_location_info(span)
186+
&& let (Some(source_file), ..) =
187+
source_map.span_to_location_info(source_span)
182188
&& (self.tcx.is_diagnostic_item(sym::format_args_nl_macro, def_id)
183189
|| self.tcx.is_diagnostic_item(sym::format_args_macro, def_id))
184-
&& self.is_source_span(span) =>
190+
&& span_file.src_hash == source_file.src_hash =>
185191
{
186-
let source_map = self.tcx.sess.source_map();
187-
let (_, _, lo_col, _, _) = source_map.span_to_location_info(span);
188-
189-
ErrorSortKey::SubtypeFormat(Reverse(lo_col))
192+
ErrorSortKey::SubtypeFormat(Reverse((row, col)))
190193
}
191194
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))
192195
if self.tcx.is_lang_item(pred.def_id(), LangItem::Sized) =>
@@ -278,21 +281,6 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
278281
reported.unwrap_or_else(|| self.dcx().delayed_bug("failed to report fulfillment errors"))
279282
}
280283

281-
fn is_source_span(&self, span: Span) -> bool {
282-
let source_map = self.tcx.sess.source_map();
283-
284-
let outer_expn_data = span.ctxt().outer_expn_data();
285-
let outer_callsite = outer_expn_data.call_site.source_callsite();
286-
287-
let span_info = source_map.span_to_location_info(span);
288-
let outer_info = source_map.span_to_location_info(outer_callsite);
289-
290-
match (span_info, outer_info) {
291-
((Some(sf1), _, _, _, _), (Some(sf2), _, _, _, _)) => sf1.src_hash == sf2.src_hash,
292-
_ => false,
293-
}
294-
}
295-
296284
#[instrument(skip(self), level = "debug")]
297285
fn report_fulfillment_error(&self, error: &FulfillmentError<'tcx>) -> ErrorGuaranteed {
298286
let mut error = FulfillmentError {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,25 @@ fn check_format_args_nl() {
88
//~^ ERROR type annotations needed
99
}
1010

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+
[],
28+
//~^ ERROR type annotations needed
29+
a = 1 + 1);
30+
}
31+
1132
fn main() {}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ LL | println!("{:?} {a} {a:?}", [], a = 1 + 1);
1414
|
1515
= 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

17-
error: aborting due to 2 previous errors
17+
error[E0282]: type annotations needed
18+
--> $DIR/span-format_args-issue-140578.rs:12:39
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:64
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:27:2
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
1842

1943
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)