Stop dbg! macro yapping about format modifiers

This commit is contained in:
mejrs 2025-06-17 19:37:29 +02:00
parent b1d18129d1
commit 29ce695cd4
5 changed files with 33 additions and 10 deletions

View File

@ -363,7 +363,14 @@ macro_rules! dbg {
match $val {
tmp => {
$crate::eprintln!("[{}:{}:{}] {} = {:#?}",
$crate::file!(), $crate::line!(), $crate::column!(), $crate::stringify!($val), &tmp);
$crate::file!(),
$crate::line!(),
$crate::column!(),
$crate::stringify!($val),
// The `&T: Debug` check happens here (not in the format literal desugaring)
// to avoid format literal related messages and suggestions.
&&tmp as &dyn $crate::fmt::Debug,
);
tmp
}
}

View File

@ -2,12 +2,10 @@ error[E0277]: `Dummy` doesn't implement `Debug`
--> $DIR/issue-107649.rs:105:5
|
105 | dbg!(lib::Dummy);
| ^^^^^^^^^^^^^^^^ `Dummy` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| ^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `Dummy`
|
= help: the trait `Debug` is not implemented for `Dummy`
= note: add `#[derive(Debug)]` to `Dummy` or manually `impl Debug for Dummy`
= note: required for `&Dummy` to implement `Debug`
= 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)
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `Dummy` with `#[derive(Debug)]`
--> $DIR/auxiliary/dummy_lib.rs:2:1
|

View File

@ -0,0 +1,17 @@
/// Check that only `&X: Debug` is required, not `X: Debug`
//@check-pass
use std::fmt::Debug;
use std::fmt::Formatter;
struct X;
impl Debug for &X {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
f.write_str("X")
}
}
fn main() {
dbg!(X);
}

View File

@ -1,4 +1,7 @@
// Test ensuring that `dbg!(expr)` requires the passed type to implement `Debug`.
//
// `dbg!` shouldn't tell the user about format literal syntax; the user didn't write one.
//@ forbid-output: cannot be formatted using
struct NotDebug;

View File

@ -1,13 +1,11 @@
error[E0277]: `NotDebug` doesn't implement `Debug`
--> $DIR/dbg-macro-requires-debug.rs:6:23
--> $DIR/dbg-macro-requires-debug.rs:9:23
|
LL | let _: NotDebug = dbg!(NotDebug);
| ^^^^^^^^^^^^^^ `NotDebug` cannot be formatted using `{:?}` because it doesn't implement `Debug`
| ^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `NotDebug`
|
= help: the trait `Debug` is not implemented for `NotDebug`
= note: add `#[derive(Debug)]` to `NotDebug` or manually `impl Debug for NotDebug`
= note: required for `&NotDebug` to implement `Debug`
= 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)
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider annotating `NotDebug` with `#[derive(Debug)]`
|
LL + #[derive(Debug)]