Skip to content

Commit 694fe05

Browse files
committed
Use the #[diagnostic::on_unimplemented] attribute when possible
This change enables the `#[diagnostic::on_unimplemented]` attribute for the `Serialize` and `Deserialize` trait to point the user to the relevant derives and point out that they might want to check crates features for external types by adding the relevant hints an note.
1 parent f3dfd2a commit 694fe05

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

serde/build.rs

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn main() {
2525
println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)");
2626
println!("cargo:rustc-check-cfg=cfg(no_systemtime_checked_add)");
2727
println!("cargo:rustc-check-cfg=cfg(no_target_has_atomic)");
28+
println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)");
2829
}
2930

3031
let target = env::var("TARGET").unwrap();
@@ -84,6 +85,12 @@ fn main() {
8485
if minor < 74 {
8586
println!("cargo:rustc-cfg=no_core_num_saturating");
8687
}
88+
89+
// Support for the `#[diagnostic]` tool attribute namespace
90+
// https://blog.rust-lang.org/2024/05/02/Rust-1.78.0.html#diagnostic-attributes
91+
if minor < 78 {
92+
println!("cargo:rustc-cfg=no_diagnostic_namespace");
93+
}
8794
}
8895

8996
fn rustc_minor_version() -> Option<u32> {

serde/src/de/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,13 @@ impl<'a> Display for Expected + 'a {
532532
/// deserializer lifetimes] for a more detailed explanation of these lifetimes.
533533
///
534534
/// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html
535+
#[cfg_attr(
536+
not(no_diagnostic_namespace),
537+
diagnostic::on_unimplemented(
538+
note = "for local types consider adding `#[derive(serde::Deserialize)]` to your `{Self}` type",
539+
note = "for types from other crates check whether the crate offers a `serde` feature flag",
540+
)
541+
)]
535542
pub trait Deserialize<'de>: Sized {
536543
/// Deserialize this value from the given Serde deserializer.
537544
///

serde/src/ser/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ declare_error_trait!(Error: Sized + Debug + Display);
215215
/// [`linked-hash-map`]: https://crates.io/crates/linked-hash-map
216216
/// [`serde_derive`]: https://crates.io/crates/serde_derive
217217
/// [derive section of the manual]: https://serde.rs/derive.html
218+
#[cfg_attr(
219+
not(no_diagnostic_namespace),
220+
diagnostic::on_unimplemented(
221+
note = "for local types consider adding `#[derive(serde::Serialize)]` to your `{Self}` type",
222+
note = "for types from other crates check whether the crate offers a `serde` feature flag",
223+
)
224+
)]
218225
pub trait Serialize {
219226
/// Serialize this value into the given Serde serializer.
220227
///

0 commit comments

Comments
 (0)