Skip to content

Commit a7eb558

Browse files
committed
WIP: Fix the sorting of errors
1 parent f52791a commit a7eb558

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

compiler/rustc_span/src/span_encoding.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,6 @@ impl Span {
281281
}
282282
}
283283

284-
#[inline]
285-
pub fn index(self) -> u32 {
286-
self.lo_or_index
287-
}
288-
289284
#[inline]
290285
pub fn data(self) -> SpanData {
291286
let data = self.data_untracked();

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

Lines changed: 56 additions & 13 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};
@@ -21,7 +22,7 @@ use rustc_infer::traits::{
2122
};
2223
use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
2324
use rustc_middle::ty::{self, Ty, TyCtxt};
24-
use rustc_span::{ErrorGuaranteed, ExpnKind, Span};
25+
use rustc_span::{CharPos, ErrorGuaranteed, ExpnKind, Span};
2526
use tracing::{info, instrument};
2627

2728
pub use self::overflow::*;
@@ -136,6 +137,15 @@ pub enum DefIdOrName {
136137
Name(&'static str),
137138
}
138139

140+
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
141+
enum ErrorSortKey {
142+
SubtypeFormat(Reverse<CharPos>),
143+
OtherKind,
144+
ClauseTraitSized,
145+
Coerce,
146+
ClauseWellFormed,
147+
}
148+
139149
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
140150
pub fn report_fulfillment_errors(
141151
&self,
@@ -164,25 +174,58 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
164174
// with more relevant type information and hide redundant E0282 errors.
165175
errors.sort_by_key(|e| match e.obligation.predicate.kind().skip_binder() {
166176
ty::PredicateKind::Subtype(_)
167-
if e.obligation.cause.span.macro_backtrace().next().is_some_and(
168-
|trace| match trace.kind {
169-
ExpnKind::Macro(_, name) => {
170-
name.as_str().rsplit("::").next() == Some("format_args_nl")
171-
}
172-
_ => false,
173-
},
177+
if matches! (
178+
e.obligation.cause.span.ctxt().outer_expn_data().kind,
179+
ExpnKind::Macro(_, name) if matches!(
180+
name.as_str().rsplit("::").next(),
181+
Some("format_args" | "format_args_nl")
182+
)
174183
) =>
175184
{
176-
(-1, e.obligation.cause.span.index())
185+
let sm = self.tcx.sess.source_map();
186+
let bpos = e.obligation.cause.span.data().lo;
187+
let loc = sm.lookup_char_pos(bpos);
188+
189+
eprintln!("DEBUG: bpos:{:?}, col:{:?}, cold:{:?}", bpos, loc.col, loc.col_display);
190+
/*
191+
$./build/x86_64-unknown-linux-gnu/stage1/bin/rustc tests/ui/errors/span-format_args-issue-140578.rs
192+
DEBUG: bpos:BytePos(52), col:CharPos(27), cold:27
193+
DEBUG: bpos:BytePos(191660), col:CharPos(27), cold:27
194+
DEBUG: bpos:BytePos(52), col:CharPos(27), cold:27
195+
DEBUG: bpos:BytePos(52), col:CharPos(27), cold:27
196+
error[E0282]: type annotations needed
197+
--> tests/ui/errors/span-format_args-issue-140578.rs:2:3
198+
|
199+
2 | print!("{:?} {a} {a:?}", [], a = 1 + 1);
200+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
201+
|
202+
= 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)
203+
204+
DEBUG: bpos:BytePos(165), col:CharPos(29), cold:29
205+
DEBUG: bpos:BytePos(193512), col:CharPos(27), cold:27
206+
DEBUG: bpos:BytePos(193512), col:CharPos(27), cold:27
207+
DEBUG: bpos:BytePos(193512), col:CharPos(27), cold:27
208+
error[E0282]: type annotations needed
209+
--> tests/ui/errors/span-format_args-issue-140578.rs:7:30
210+
|
211+
7 | println!("{:?} {a} {a:?}", [], a = 1 + 1);
212+
| ^^ cannot infer type
213+
|
214+
= 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)
215+
*/
216+
217+
ErrorSortKey::SubtypeFormat(Reverse(loc.col))
177218
}
178219
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred))
179220
if self.tcx.is_lang_item(pred.def_id(), LangItem::Sized) =>
180221
{
181-
(1, 0)
222+
ErrorSortKey::ClauseTraitSized
223+
}
224+
ty::PredicateKind::Coerce(_) => ErrorSortKey::Coerce,
225+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => {
226+
ErrorSortKey::ClauseWellFormed
182227
}
183-
ty::PredicateKind::Coerce(_) => (2, 0),
184-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => (3, 0),
185-
_ => (0, 0),
228+
_ => ErrorSortKey::OtherKind,
186229
});
187230

188231
for (index, error) in errors.iter().enumerate() {
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
fn main() {
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() {
27
println!("{:?} {a} {a:?}", [], a = 1 + 1);
38
//~^ ERROR type annotations needed
49
}
10+
11+
fn main() {}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
error[E0282]: type annotations needed
2-
--> $DIR/span-format_args-issue-140578.rs:2:30
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:6:30
311
|
412
LL | println!("{:?} {a} {a:?}", [], a = 1 + 1);
513
| ^^ cannot infer type
614
|
715
= 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)
816

9-
error: aborting due to 1 previous error
17+
error: aborting due to 2 previous errors
1018

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

0 commit comments

Comments
 (0)