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 7 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
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ symbols! {
format_args_capture,
format_args_macro,
format_args_nl,
format_args_nl_macro,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no longer needed?

format_argument,
format_arguments,
format_count,
Expand Down
45 changes: 37 additions & 8 deletions compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use rustc_infer::traits::{
};
use rustc_middle::ty::print::{PrintTraitRefExt as _, with_no_trimmed_paths};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_span::{ErrorGuaranteed, ExpnKind, Span};
use rustc_span::{DesugaringKind, ErrorGuaranteed, ExpnKind, Span};
use tracing::{info, instrument};

pub use self::overflow::*;
Expand Down Expand Up @@ -160,9 +160,20 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
})
.collect();

// Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last.
// Ensure `T: Sized`, `T: MetaSized`, `T: PointeeSized` and `T: WF` obligations come last,
// and `Subtype` obligations from `FormatLiteral` desugarings come first.
// This lets us display diagnostics with more relevant type information and hide redundant
// E0282 errors.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
enum ErrorSortKey {
SubtypeFormat(usize, usize),
OtherKind,
SizedTrait,
MetaSizedTrait,
PointeeSizedTrait,
Coerce,
WellFormed,
}
errors.sort_by_key(|e| {
let maybe_sizedness_did = match e.obligation.predicate.kind().skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => Some(pred.def_id()),
Expand All @@ -171,12 +182,30 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
};

match e.obligation.predicate.kind().skip_binder() {
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => 1,
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => 2,
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => 3,
ty::PredicateKind::Coerce(_) => 4,
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => 5,
_ => 0,
ty::PredicateKind::Subtype(_)
if matches!(
e.obligation.cause.span.desugaring_kind(),
Some(DesugaringKind::FormatLiteral { .. })
) =>
{
let (_, row, col, ..) =
self.tcx.sess.source_map().span_to_location_info(e.obligation.cause.span);
ErrorSortKey::SubtypeFormat(row, col)
}
_ if maybe_sizedness_did == self.tcx.lang_items().sized_trait() => {
ErrorSortKey::SizedTrait
}
_ if maybe_sizedness_did == self.tcx.lang_items().meta_sized_trait() => {
ErrorSortKey::MetaSizedTrait
}
_ if maybe_sizedness_did == self.tcx.lang_items().pointee_sized_trait() => {
ErrorSortKey::PointeeSizedTrait
}
ty::PredicateKind::Coerce(_) => ErrorSortKey::Coerce,
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => {
ErrorSortKey::WellFormed
}
_ => ErrorSortKey::OtherKind,
}
});

Expand Down
1 change: 1 addition & 0 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ pub(crate) mod builtin {
reason = "`format_args_nl` is only for internal \
language use and is subject to change"
)]
#[rustc_diagnostic_item = "format_args_nl_macro"]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

no longer needed?

#[allow_internal_unstable(fmt_internals)]
#[rustc_builtin_macro]
#[macro_export]
Expand Down
32 changes: 32 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,32 @@
fn check_format_args() {
print!("{:?} {a} {a:?}", [], a = 1 + 1);
//~^ ERROR type annotations needed
}

fn check_format_args_nl() {
println!("{:?} {a} {a:?}", [], a = 1 + 1);
//~^ ERROR type annotations needed
}

fn check_multi1() {
println!("{:?} {:?} {a} {a:?}", [], [], a = 1 + 1);
//~^ ERROR type annotations needed
}

fn check_multi2() {
println!("{:?} {:?} {a} {a:?} {b:?}", [], [], a = 1 + 1, b = []);
//~^ ERROR type annotations needed
}

fn check_unformatted() {
println!("
{:?} {:?}
{a}
{a:?}",
[],
//~^ ERROR type annotations needed
[],
a = 1 + 1);
}

fn main() {}
43 changes: 43 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,43 @@
error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:2:28
|
LL | print!("{:?} {a} {a:?}", [], a = 1 + 1);
| ^^ cannot infer type
|
= 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)

error[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:7: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[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:12:35
|
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[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:17:41
|
LL | println!("{:?} {:?} {a} {a:?} {b:?}", [], [], a = 1 + 1, b = []);
| ^^ 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[E0282]: type annotations needed
--> $DIR/span-format_args-issue-140578.rs:26:9
|
LL | [],
| ^^ 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 5 previous errors

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