1
Fork 0

Auto merge of #86386 - inquisitivecrystal:better-errors-for-display-traits-v3, r=estebank

Better errors for Debug and Display traits

Currently, if someone tries to pass value that does not implement `Debug` or `Display` to a formatting macro, they get a very verbose and confusing error message. This PR changes the error messages for missing `Debug` and `Display` impls to be less overwhelming in this case, as suggested by #85844. I was a little less aggressive in changing the error message than that issue proposed. Still, this implementation would be enough to reduce the number of messages to be much more manageable.

After this PR, information on the cause of an error involving a `Debug` or `Display` implementation would suppressed if the requirement originated within a standard library macro. My reasoning was that errors originating from within a macro are confusing when they mention details that the programmer can't see, and this is particularly problematic for `Debug` and `Display`, which are most often used via macros. It is possible that either a broader or a narrower criterion would be better. I'm quite open to any feedback.

Fixes #85844.
This commit is contained in:
bors 2021-06-23 03:16:04 +00:00
commit 8cb207ae69
14 changed files with 33 additions and 44 deletions

View file

@ -478,6 +478,7 @@ symbols! {
discriminant_type, discriminant_type,
discriminant_value, discriminant_value,
dispatch_from_dyn, dispatch_from_dyn,
display_trait,
div, div,
div_assign, div_assign,
doc, doc,

View file

@ -514,6 +514,30 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
} }
} }
// Return early if the trait is Debug or Display and the invocation
// originates within a standard library macro, because the output
// is otherwise overwhelming and unhelpful (see #85844 for an
// example).
let trait_is_debug =
self.tcx.is_diagnostic_item(sym::debug_trait, trait_ref.def_id());
let trait_is_display =
self.tcx.is_diagnostic_item(sym::display_trait, trait_ref.def_id());
let in_std_macro =
match obligation.cause.span.ctxt().outer_expn_data().macro_def_id {
Some(macro_def_id) => {
let crate_name = tcx.crate_name(macro_def_id.krate);
crate_name == sym::std || crate_name == sym::core
}
None => false,
};
if in_std_macro && (trait_is_debug || trait_is_display) {
err.emit();
return;
}
err err
} }

View file

@ -564,7 +564,7 @@ impl Display for Arguments<'_> {
on( on(
crate_local, crate_local,
label = "`{Self}` cannot be formatted using `{{:?}}`", label = "`{Self}` cannot be formatted using `{{:?}}`",
note = "add `#[derive(Debug)]` or manually implement `{Debug}`" note = "add `#[derive(Debug)]` to `{Self}` or manually `impl {Debug} for {Self}`"
), ),
message = "`{Self}` doesn't implement `{Debug}`", message = "`{Self}` doesn't implement `{Debug}`",
label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{Debug}`" label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{Debug}`"
@ -662,6 +662,7 @@ pub use macros::Debug;
note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead" note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead"
)] )]
#[doc(alias = "{}")] #[doc(alias = "{}")]
#[rustc_diagnostic_item = "display_trait"]
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
pub trait Display { pub trait Display {
/// Formats the value using the given formatter. /// Formats the value using the given formatter.

View file

@ -14,11 +14,6 @@ error[E0277]: `for<'r> fn(&'r i32) -> &'r i32 {foo}` doesn't implement `Debug`
| |
LL | assert_eq!(foo, y); LL | assert_eq!(foo, y);
| ^^^^^^^^^^^^^^^^^^^ `for<'r> fn(&'r i32) -> &'r i32 {foo}` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^^^^^^^^^^^^^^^^^^^ `for<'r> fn(&'r i32) -> &'r i32 {foo}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
::: $SRC_DIR/core/src/panicking.rs:LL:COL
|
LL | T: fmt::Debug + ?Sized,
| ---------- required by this bound in `core::panicking::assert_failed`
| |
= help: the trait `Debug` is not implemented for `for<'r> fn(&'r i32) -> &'r i32 {foo}` = help: the trait `Debug` is not implemented for `for<'r> fn(&'r i32) -> &'r i32 {foo}`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

View file

@ -4,7 +4,6 @@ error[E0277]: `impl Sized` doesn't implement `Debug`
LL | println!("{:?}", t); LL | println!("{:?}", t);
| ^ `impl Sized` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^ `impl Sized` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound help: consider further restricting this bound
| |
@ -17,7 +16,6 @@ error[E0277]: `T` doesn't implement `Debug`
LL | println!("{:?}", t); LL | println!("{:?}", t);
| ^ `T` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^ `T` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T` help: consider restricting type parameter `T`
| |
@ -30,7 +28,6 @@ error[E0277]: `T` doesn't implement `Debug`
LL | println!("{:?}", t); LL | println!("{:?}", t);
| ^ `T` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^ `T` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound help: consider further restricting this bound
| |
@ -43,7 +40,6 @@ error[E0277]: `Y` doesn't implement `Debug`
LL | println!("{:?} {:?}", x, y); LL | println!("{:?} {:?}", x, y);
| ^ `Y` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^ `Y` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting type parameter `Y` help: consider further restricting type parameter `Y`
| |
@ -56,7 +52,6 @@ error[E0277]: `X` doesn't implement `Debug`
LL | println!("{:?}", x); LL | println!("{:?}", x);
| ^ `X` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^ `X` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting this bound help: consider further restricting this bound
| |
@ -69,7 +64,6 @@ error[E0277]: `X` doesn't implement `Debug`
LL | println!("{:?}", x); LL | println!("{:?}", x);
| ^ `X` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^ `X` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider further restricting type parameter `X` help: consider further restricting type parameter `X`
| |

View file

@ -5,9 +5,7 @@ LL | x: Error
| ^^^^^^^^ `Error` cannot be formatted using `{:?}` | ^^^^^^^^ `Error` cannot be formatted using `{:?}`
| |
= help: the trait `Debug` is not implemented for `Error` = help: the trait `Debug` is not implemented for `Error`
= note: add `#[derive(Debug)]` or manually implement `Debug` = note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
= note: required because of the requirements on the impl of `Debug` for `&Error`
= note: required for the cast to the object type `dyn Debug`
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error error: aborting due to previous error

View file

@ -5,9 +5,7 @@ LL | Error
| ^^^^^ `Error` cannot be formatted using `{:?}` | ^^^^^ `Error` cannot be formatted using `{:?}`
| |
= help: the trait `Debug` is not implemented for `Error` = help: the trait `Debug` is not implemented for `Error`
= note: add `#[derive(Debug)]` or manually implement `Debug` = note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
= note: required because of the requirements on the impl of `Debug` for `&Error`
= note: required for the cast to the object type `dyn Debug`
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error error: aborting due to previous error

View file

@ -5,9 +5,7 @@ LL | x: Error
| ^^^^^^^^ `Error` cannot be formatted using `{:?}` | ^^^^^^^^ `Error` cannot be formatted using `{:?}`
| |
= help: the trait `Debug` is not implemented for `Error` = help: the trait `Debug` is not implemented for `Error`
= note: add `#[derive(Debug)]` or manually implement `Debug` = note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
= note: required because of the requirements on the impl of `Debug` for `&Error`
= note: required for the cast to the object type `dyn Debug`
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error error: aborting due to previous error

View file

@ -5,9 +5,7 @@ LL | Error
| ^^^^^ `Error` cannot be formatted using `{:?}` | ^^^^^ `Error` cannot be formatted using `{:?}`
| |
= help: the trait `Debug` is not implemented for `Error` = help: the trait `Debug` is not implemented for `Error`
= note: add `#[derive(Debug)]` or manually implement `Debug` = note: add `#[derive(Debug)]` to `Error` or manually `impl Debug for Error`
= note: required because of the requirements on the impl of `Debug` for `&Error`
= note: required for the cast to the object type `dyn Debug`
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error error: aborting due to previous error

View file

@ -84,11 +84,6 @@ error[E0277]: `fn(usize) -> Foo {Foo::Bar}` doesn't implement `Debug`
| |
LL | assert_eq!(Foo::Bar, i); LL | assert_eq!(Foo::Bar, i);
| ^^^^^^^^^^^^^^^^^^^^^^^^ `fn(usize) -> Foo {Foo::Bar}` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^^^^^^^^^^^^^^^^^^^^^^^^ `fn(usize) -> Foo {Foo::Bar}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
::: $SRC_DIR/core/src/panicking.rs:LL:COL
|
LL | T: fmt::Debug + ?Sized,
| ---------- required by this bound in `core::panicking::assert_failed`
| |
= help: the trait `Debug` is not implemented for `fn(usize) -> Foo {Foo::Bar}` = help: the trait `Debug` is not implemented for `fn(usize) -> Foo {Foo::Bar}`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)

View file

@ -28,11 +28,6 @@ LL | fn a() -> i32 {
... ...
LL | assert_eq!(a, 0); LL | assert_eq!(a, 0);
| ^^^^^^^^^^^^^^^^^ `fn() -> i32 {a}` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^^^^^^^^^^^^^^^^^ `fn() -> i32 {a}` cannot be formatted using `{:?}` because it doesn't implement `Debug`
|
::: $SRC_DIR/core/src/panicking.rs:LL:COL
|
LL | T: fmt::Debug + ?Sized,
| ---------- required by this bound in `core::panicking::assert_failed`
| |
= help: the trait `Debug` is not implemented for `fn() -> i32 {a}` = help: the trait `Debug` is not implemented for `fn() -> i32 {a}`
= help: use parentheses to call the function: `a()` = help: use parentheses to call the function: `a()`

View file

@ -5,8 +5,7 @@ LL | println!("{:?} {:?}", Foo, Bar);
| ^^^ `Foo` cannot be formatted using `{:?}` | ^^^ `Foo` cannot be formatted using `{:?}`
| |
= help: the trait `Debug` is not implemented for `Foo` = help: the trait `Debug` is not implemented for `Foo`
= note: add `#[derive(Debug)]` or manually implement `Debug` = note: add `#[derive(Debug)]` to `Foo` or manually `impl Debug for Foo`
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `Bar` doesn't implement `Debug` error[E0277]: `Bar` doesn't implement `Debug`
@ -16,7 +15,6 @@ LL | println!("{:?} {:?}", Foo, Bar);
| ^^^ `Bar` cannot be formatted using `{:?}` because it doesn't implement `Debug` | ^^^ `Bar` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| |
= help: the trait `Debug` is not implemented for `Bar` = help: the trait `Debug` is not implemented for `Bar`
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `Foo` doesn't implement `std::fmt::Display` error[E0277]: `Foo` doesn't implement `std::fmt::Display`
@ -27,7 +25,6 @@ LL | println!("{} {}", Foo, Bar);
| |
= help: the trait `std::fmt::Display` is not implemented for `Foo` = help: the trait `std::fmt::Display` is not implemented for `Foo`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `Bar` doesn't implement `std::fmt::Display` error[E0277]: `Bar` doesn't implement `std::fmt::Display`
@ -38,7 +35,6 @@ LL | println!("{} {}", Foo, Bar);
| |
= help: the trait `std::fmt::Display` is not implemented for `Bar` = help: the trait `std::fmt::Display` is not implemented for `Bar`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 4 previous errors error: aborting due to 4 previous errors

View file

@ -5,9 +5,7 @@ LL | let _: NotDebug = dbg!(NotDebug);
| ^^^^^^^^^^^^^^ `NotDebug` cannot be formatted using `{:?}` | ^^^^^^^^^^^^^^ `NotDebug` cannot be formatted using `{:?}`
| |
= help: the trait `Debug` is not implemented for `NotDebug` = help: the trait `Debug` is not implemented for `NotDebug`
= note: add `#[derive(Debug)]` or manually implement `Debug` = note: add `#[derive(Debug)]` to `NotDebug` or manually `impl Debug for NotDebug`
= note: required because of the requirements on the impl of `Debug` for `&NotDebug`
= note: required by `std::fmt::Debug::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error error: aborting due to previous error

View file

@ -6,8 +6,6 @@ LL | println!("{}", path);
| |
= help: the trait `std::fmt::Display` is not implemented for `Path` = help: the trait `std::fmt::Display` is not implemented for `Path`
= note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data = note: call `.display()` or `.to_string_lossy()` to safely print paths, as they may contain non-Unicode data
= note: required because of the requirements on the impl of `std::fmt::Display` for `&Path`
= note: required by `std::fmt::Display::fmt`
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to previous error error: aborting due to previous error