Skip to content

Fix unuseful span in type error in some format_args!() invocations #140916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_span/src/span_encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,16 @@ impl Span {
}
}

#[inline]
pub fn len(self) -> u32 {
(self.len_with_tag_or_marker & !PARENT_TAG) as u32
}
Comment on lines +284 to +287
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: no need in the current logic


#[inline]
pub fn index(self) -> u32 {
self.lo_or_index
}

#[inline]
pub fn data(self) -> SpanData {
let data = self.data_untracked();
Expand Down
20 changes: 16 additions & 4 deletions compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment on lines +167 to +170
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should does this search format_args rather than format_args_nl?

}
_ => false,
},
) =>
{
(-1, e.obligation.cause.span.index())
Copy link
Author

@moatom moatom May 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is -1 appropriate? (Should I use unsigned integers for sorting?)
  2. Is span.index() an appropriate metric to prioritize arguments? (My initial assumption was that a negative index would help prioritize arguments, but that doesn't seem to be the case.)

}
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() {
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/errors/span-format_args-issue-140578.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
println!("{:?} {a} {a:?}", [], a = 1 + 1);
//~^ ERROR type annotations needed
}
11 changes: 11 additions & 0 deletions tests/ui/errors/span-format_args-issue-140578.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading