Skip to content

Commit 179478e

Browse files
committed
Auto merge of rust-lang#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 rust-lang#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 rust-lang#85844.
2 parents bed7885 + 68bb31c commit 179478e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

core/src/fmt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl Display for Arguments<'_> {
564564
on(
565565
crate_local,
566566
label = "`{Self}` cannot be formatted using `{{:?}}`",
567-
note = "add `#[derive(Debug)]` or manually implement `{Debug}`"
567+
note = "add `#[derive(Debug)]` to `{Self}` or manually `impl {Debug} for {Self}`"
568568
),
569569
message = "`{Self}` doesn't implement `{Debug}`",
570570
label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{Debug}`"
@@ -662,6 +662,7 @@ pub use macros::Debug;
662662
note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead"
663663
)]
664664
#[doc(alias = "{}")]
665+
#[rustc_diagnostic_item = "display_trait"]
665666
#[stable(feature = "rust1", since = "1.0.0")]
666667
pub trait Display {
667668
/// Formats the value using the given formatter.

0 commit comments

Comments
 (0)