Override diagnostic::on_unimplemented message of all serde_core traits

This prevents diagnostics being rendered like `serde_core::ser::Serialize`
and `serde_core::de::Deserialize` in projects that have no direct
dependency on serde_core.

The attributes make error messages arguably sometimes worse than
pre-serde_core by always rendering `serde::Serialize` and never
`Serialize` when `use serde::Serialize` is in scope, which rustc's
default message construction knows to recognize. But this is probably
negligible.

I explored the alternative of setting `[lib] name = "serde"` in
serde_core/Cargo.toml which also prevents `serde_core::ser::Serialize`
in messages, but has the unwanted effect of also inserting the following
noise into every such error:

    note: there are multiple different versions of crate `serde` in the dependency graph
        --> $WORKSPACE/serde_core/src/ser/mod.rs:225:1
         |
     225 | pub trait Serialize {
         | ^^^^^^^^^^^^^^^^^^^ this is the required trait
         |
        ::: src/main.rs:1:1
         |
       1 | struct MyStruct;
         | --------------- this type doesn't implement the required trait
    ...
       4 |     let _ = serde_json::to_string(&MyStruct);
         |             ----------
         |             |
         |             one version of crate `serde` used here, as a dependency of crate `serde`
         |             one version of crate `serde` used here, as a dependency of crate `serde_json`
         |
        ::: $WORKSPACE/serde/src/private/de.rs:2347:1
         |
    2347 | pub trait IdentifierDeserializer<'de, E: Error> {
         | ----------------------------------------------- this is the found trait
         = help: you can use `cargo tree` to explore your dependency tree

The "this is the found trait" pointing to `IdentifierDeserializer` seems
like a compiler bug...
This commit is contained in:
David Tolnay
2025-09-12 19:32:00 -07:00
parent 908f32175a
commit 4bddf1b953
5 changed files with 128 additions and 8 deletions
@@ -1,4 +1,4 @@
error[E0277]: the trait bound `&u8: Serializer` is not satisfied
error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied
--> tests/ui/with/incorrect_type.rs:14:10
|
14 | #[derive(Serialize, Deserialize)]
@@ -30,7 +30,7 @@ note: function defined here
9 | pub fn serialize<T, S: Serializer>(_: S) -> Result<S::Ok, S::Error> {
| ^^^^^^^^^
error[E0277]: the trait bound `&u8: Serializer` is not satisfied
error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied
--> tests/ui/with/incorrect_type.rs:15:25
|
15 | struct W(#[serde(with = "w")] u8, u8);
@@ -49,7 +49,7 @@ error[E0308]: `?` operator has incompatible types
|
= note: `?` operator cannot convert from `()` to `u8`
error[E0277]: the trait bound `&u8: Serializer` is not satisfied
error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied
--> tests/ui/with/incorrect_type.rs:17:10
|
17 | #[derive(Serialize, Deserialize)]
@@ -81,7 +81,7 @@ note: function defined here
9 | pub fn serialize<T, S: Serializer>(_: S) -> Result<S::Ok, S::Error> {
| ^^^^^^^^^
error[E0277]: the trait bound `&u8: Serializer` is not satisfied
error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied
--> tests/ui/with/incorrect_type.rs:18:35
|
18 | struct S(#[serde(serialize_with = "w::serialize")] u8, u8);