Skip to content

Commit 9d7e407

Browse files
Rollup merge of rust-lang#142594 - mejrs:new_desugaring, r=chenyukang
Add DesugaringKind::FormatLiteral Implements `DesugaringKind::FormatLiteral` to mark the FormatArgs desugaring of format literals. The main use for this is to stop yapping about about formatting parameters if we're not anywhere near a format literal. The other use case is to fix suggestions such as rust-lang#141350. It might also be useful for new or existing diagnostics that check whether they're in a format-like macro. cc `@xizheyin` `@fmease`
2 parents c620fab + 0d5e35f commit 9d7e407

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

core/src/fmt/mod.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -855,11 +855,13 @@ impl Display for Arguments<'_> {
855855
#[rustc_on_unimplemented(
856856
on(
857857
crate_local,
858-
label = "`{Self}` cannot be formatted using `{{:?}}`",
859858
note = "add `#[derive(Debug)]` to `{Self}` or manually `impl {This} for {Self}`"
860859
),
861-
message = "`{Self}` doesn't implement `{This}`",
862-
label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{This}`"
860+
on(
861+
from_desugaring = "FormatLiteral",
862+
label = "`{Self}` cannot be formatted using `{{:?}}` because it doesn't implement `{This}`"
863+
),
864+
message = "`{Self}` doesn't implement `{This}`"
863865
)]
864866
#[doc(alias = "{:?}")]
865867
#[rustc_diagnostic_item = "Debug"]
@@ -986,11 +988,14 @@ pub use macros::Debug;
986988
any(Self = "std::path::Path", Self = "std::path::PathBuf"),
987989
label = "`{Self}` cannot be formatted with the default formatter; call `.display()` on it",
988990
note = "call `.display()` or `.to_string_lossy()` to safely print paths, \
989-
as they may contain non-Unicode data"
991+
as they may contain non-Unicode data",
992+
),
993+
on(
994+
from_desugaring = "FormatLiteral",
995+
note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead",
996+
label = "`{Self}` cannot be formatted with the default formatter",
990997
),
991-
message = "`{Self}` doesn't implement `{This}`",
992-
label = "`{Self}` cannot be formatted with the default formatter",
993-
note = "in format strings you may be able to use `{{:?}}` (or {{:#?}} for pretty-print) instead"
998+
message = "`{Self}` doesn't implement `{This}`"
994999
)]
9951000
#[doc(alias = "{}")]
9961001
#[rustc_diagnostic_item = "Display"]

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
}

0 commit comments

Comments
 (0)