Skip to content

Commit fa30ae5

Browse files
committed
Auto merge of #50441 - kornelski:debughint, r=kornelski
Suggest more helpful formatting string Based on [user feedback](https://users.rust-lang.org/t/ux-feedback-from-a-rust-newbie/17220) the minimal suggestion of `:?` is unclear. Also `{:#?}` is much more readable than the standard debug, so this PR suggests it to help surface this nice feature.
2 parents 2d847dc + 1e38eee commit fa30ae5

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/libcore/fmt/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,10 @@ impl<'a> Display for Arguments<'a> {
542542
/// ```
543543
#[stable(feature = "rust1", since = "1.0.0")]
544544
#[rustc_on_unimplemented(
545-
on(crate_local, label="`{Self}` cannot be formatted using `:?`; \
546-
add `#[derive(Debug)]` or manually implement `{Debug}`"),
545+
on(crate_local, label="`{Self}` cannot be formatted using `{{:?}}`",
546+
note="add `#[derive(Debug)]` or manually implement `{Debug}`"),
547547
message="`{Self}` doesn't implement `{Debug}`",
548-
label="`{Self}` cannot be formatted using `:?` because it doesn't implement `{Debug}`",
548+
label="`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{Debug}`",
549549
)]
550550
#[doc(alias = "{:?}")]
551551
#[lang = "debug_trait"]
@@ -610,8 +610,9 @@ pub trait Debug {
610610
/// ```
611611
#[rustc_on_unimplemented(
612612
message="`{Self}` doesn't implement `{Display}`",
613-
label="`{Self}` cannot be formatted with the default formatter; \
614-
try using `:?` instead if you are using a format string",
613+
label="`{Self}` cannot be formatted with the default formatter",
614+
note="in format strings you may be able to use `{{:?}}` \
615+
(or {{:#?}} for pretty-print) instead",
615616
)]
616617
#[doc(alias = "{}")]
617618
#[stable(feature = "rust1", since = "1.0.0")]

src/test/ui/on-unimplemented/no-debug.stderr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ error[E0277]: `Foo` doesn't implement `std::fmt::Debug`
22
--> $DIR/no-debug.rs:20:27
33
|
44
LL | println!("{:?} {:?}", Foo, Bar);
5-
| ^^^ `Foo` cannot be formatted using `:?`; add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
5+
| ^^^ `Foo` cannot be formatted using `{:?}`
66
|
77
= help: the trait `std::fmt::Debug` is not implemented for `Foo`
8+
= note: add `#[derive(Debug)]` or manually implement `std::fmt::Debug`
89
= note: required by `std::fmt::Debug::fmt`
910

1011
error[E0277]: `no_debug::Bar` doesn't implement `std::fmt::Debug`
1112
--> $DIR/no-debug.rs:20:32
1213
|
1314
LL | println!("{:?} {:?}", Foo, Bar);
14-
| ^^^ `no_debug::Bar` cannot be formatted using `:?` because it doesn't implement `std::fmt::Debug`
15+
| ^^^ `no_debug::Bar` cannot be formatted using `{:?}` because it doesn't implement `std::fmt::Debug`
1516
|
1617
= help: the trait `std::fmt::Debug` is not implemented for `no_debug::Bar`
1718
= note: required by `std::fmt::Debug::fmt`
@@ -20,18 +21,20 @@ error[E0277]: `Foo` doesn't implement `std::fmt::Display`
2021
--> $DIR/no-debug.rs:21:23
2122
|
2223
LL | println!("{} {}", Foo, Bar);
23-
| ^^^ `Foo` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
24+
| ^^^ `Foo` cannot be formatted with the default formatter
2425
|
2526
= help: the trait `std::fmt::Display` is not implemented for `Foo`
27+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
2628
= note: required by `std::fmt::Display::fmt`
2729

2830
error[E0277]: `no_debug::Bar` doesn't implement `std::fmt::Display`
2931
--> $DIR/no-debug.rs:21:28
3032
|
3133
LL | println!("{} {}", Foo, Bar);
32-
| ^^^ `no_debug::Bar` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string
34+
| ^^^ `no_debug::Bar` cannot be formatted with the default formatter
3335
|
3436
= help: the trait `std::fmt::Display` is not implemented for `no_debug::Bar`
37+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
3538
= note: required by `std::fmt::Display::fmt`
3639

3740
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)