diff --git a/compiler/rustc_span/src/span_encoding.rs b/compiler/rustc_span/src/span_encoding.rs index a4a47dc99b084..a39235d6e2d59 100644 --- a/compiler/rustc_span/src/span_encoding.rs +++ b/compiler/rustc_span/src/span_encoding.rs @@ -281,6 +281,16 @@ impl Span { } } + #[inline] + pub fn len(self) -> u32 { + (self.len_with_tag_or_marker & !PARENT_TAG) as u32 + } + + #[inline] + pub fn index(self) -> u32 { + self.lo_or_index + } + #[inline] pub fn data(self) -> SpanData { let data = self.data_untracked(); diff --git a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs index 78f9287b407b3..0b4ebf065aab7 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs @@ -163,14 +163,26 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { // Ensure `T: Sized` and `T: WF` obligations come last. This lets us display diagnostics // with more relevant type information and hide redundant E0282 errors. errors.sort_by_key(|e| match e.obligation.predicate.kind().skip_binder() { + ty::PredicateKind::Subtype(_) + if e.obligation.cause.span.macro_backtrace().next().is_some_and( + |trace| match trace.kind { + ExpnKind::Macro(_, name) => { + name.as_str().rsplit("::").next() == Some("format_args_nl") + } + _ => false, + }, + ) => + { + (-1, e.obligation.cause.span.index()) + } ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) if self.tcx.is_lang_item(pred.def_id(), LangItem::Sized) => { - 1 + (1, 0) } - ty::PredicateKind::Coerce(_) => 2, - ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => 3, - _ => 0, + ty::PredicateKind::Coerce(_) => (2, 0), + ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => (3, 0), + _ => (0, 0), }); for (index, error) in errors.iter().enumerate() { diff --git a/tests/ui/errors/span-format_args-issue-140578.rs b/tests/ui/errors/span-format_args-issue-140578.rs new file mode 100644 index 0000000000000..2f7cd5970d079 --- /dev/null +++ b/tests/ui/errors/span-format_args-issue-140578.rs @@ -0,0 +1,4 @@ +fn main() { + println!("{:?} {a} {a:?}", [], a = 1 + 1); + //~^ ERROR type annotations needed +} diff --git a/tests/ui/errors/span-format_args-issue-140578.stderr b/tests/ui/errors/span-format_args-issue-140578.stderr new file mode 100644 index 0000000000000..e53b97e1fc924 --- /dev/null +++ b/tests/ui/errors/span-format_args-issue-140578.stderr @@ -0,0 +1,11 @@ +error[E0282]: type annotations needed + --> $DIR/span-format_args-issue-140578.rs:2:30 + | +LL | println!("{:?} {a} {a:?}", [], a = 1 + 1); + | ^^ cannot infer type + | + = 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) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0282`.