Skip to content

Override diagnostic::on_unimplemented message for all serde_core traits - #2963

Merged
dtolnay merged 3 commits into
serde-rs:masterfrom
dtolnay:diagnosticpath
Sep 13, 2025
Merged

Override diagnostic::on_unimplemented message for all serde_core traits#2963
dtolnay merged 3 commits into
serde-rs:masterfrom
dtolnay:diagnosticpath

Conversation

@dtolnay

@dtolnay dtolnay commented Sep 13, 2025

Copy link
Copy Markdown
Member

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.

- error[E0277]: the trait bound `MyStruct: serde_core::ser::Serialize` is not satisfied
+ error[E0277]: the trait bound `MyStruct: serde::Serialize` is not satisfied
   --> src/main.rs:5:27
    |
  5 |     serde_json::to_string(&thing);
    |     --------------------- ^^^^^^ unsatisfied trait bound
    |     |
    |     required by a bound introduced by this call

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.

- error[E0277]: the trait bound `MyStruct: Serialize` is not satisfied
+ error[E0277]: the trait bound `MyStruct: serde::Serialize` is not satisfied
   --> src/main.rs:5:27
    |
  5 |     serde_json::to_string(&thing);
    |     --------------------- ^^^^^^ unsatisfied trait bound
    |     |
    |     required by a bound introduced by this call

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 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...
@dtolnay
dtolnay merged commit 66c5d2b into serde-rs:master Sep 13, 2025
14 checks passed
@dtolnay
dtolnay deleted the diagnosticpath branch September 13, 2025 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant