Skip to content

Commit c832fd8

Browse files
committed
Stop dbg! macro yapping about format modifiers
1 parent 058197d commit c832fd8

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

library/std/src/macros.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,14 @@ macro_rules! dbg {
363363
match $val {
364364
tmp => {
365365
$crate::eprintln!("[{}:{}:{}] {} = {:#?}",
366-
$crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
366+
$crate::file!(),
367+
$crate::line!(),
368+
$crate::column!(),
369+
$crate::stringify!($val),
370+
// The `T: Debug` check happens here (not in the format literal desugaring)
371+
// to avoid format literal related messages and suggestions.
372+
&tmp as &dyn $crate::fmt::Debug,
373+
);
367374
tmp
368375
}
369376
}

tests/ui/modules/issue-107649.stderr

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ error[E0277]: `Dummy` doesn't implement `Debug`
22
--> $DIR/issue-107649.rs:105:5
33
|
44
105 | dbg!(lib::Dummy);
5-
| ^^^^^^^^^^^^^^^^ `Dummy` cannot be formatted using `{:?}` because it doesn't implement `Debug`
5+
| ^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `Dummy`
66
|
7-
= help: the trait `Debug` is not implemented for `Dummy`
87
= note: add `#[derive(Debug)]` to `Dummy` or manually `impl Debug for Dummy`
9-
= note: required for `&Dummy` to implement `Debug`
10-
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
119
help: consider annotating `Dummy` with `#[derive(Debug)]`
1210
--> $DIR/auxiliary/dummy_lib.rs:2:1
1311
|

tests/ui/rfcs/rfc-2361-dbg-macro/dbg-macro-requires-debug.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Test ensuring that `dbg!(expr)` requires the passed type to implement `Debug`.
2+
//
3+
// `dbg!` shouldn't tell the user about format literal syntax; the user didn't write one.
4+
//@ forbid-output: cannot be formatted using
25

36
struct NotDebug;
47

tests/ui/rfcs/rfc-2361-dbg-macro/dbg-macro-requires-debug.stderr

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
error[E0277]: `NotDebug` doesn't implement `Debug`
2-
--> $DIR/dbg-macro-requires-debug.rs:6:23
2+
--> $DIR/dbg-macro-requires-debug.rs:9:23
33
|
44
LL | let _: NotDebug = dbg!(NotDebug);
5-
| ^^^^^^^^^^^^^^ `NotDebug` cannot be formatted using `{:?}` because it doesn't implement `Debug`
5+
| ^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `NotDebug`
66
|
7-
= help: the trait `Debug` is not implemented for `NotDebug`
87
= note: add `#[derive(Debug)]` to `NotDebug` or manually `impl Debug for NotDebug`
9-
= note: required for `&NotDebug` to implement `Debug`
10-
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
119
help: consider annotating `NotDebug` with `#[derive(Debug)]`
1210
|
1311
LL + #[derive(Debug)]

0 commit comments

Comments
 (0)