Override diagnostic::on_unimplemented message of all serde_core traits
Before adoption: this commit came in with the history the project adopted, so no landing records it.
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
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+128 added−8 removed
- tests:+8 added−8 removed
- other:+120 added−0 removed
Provenance
- Commit
4bddf1b- Tree
3046ba1- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
908f321
5 files changed+128 added−8 removed
modified:serde_core/src/de/mod.rs+63 added
156 unmodified lines157 /// The [example data format] presented on the website shows an error158 /// type appropriate for a basic JSON data format.159 ///160 /// [example data format]: https://serde.rs/data-format.html161+ #[cfg_attr(162+ not(no_diagnostic_namespace),163+ diagnostic::on_unimplemented(164+ message = "the trait bound `{Self}: serde::de::Error` is not satisfied",165+ )166+ )]167 pub trait Error: Sized $(+ $($supertrait)::+)* {168 /// Raised when there is general error when deserializing a type.169 ///170 /// The message should not be capitalized and should not end with a period.305 unmodified lines476 /// &"a negative integer",477 /// ));478 /// # }479 /// ```480+#[cfg_attr(481+ not(no_diagnostic_namespace),482+ diagnostic::on_unimplemented(483+ message = "the trait bound `{Self}: serde::de::Expected` is not satisfied",484+ )485+)]486 pub trait Expected {487 /// Format an explanation of what data was being expected. Same signature as488 /// the `Display` and `Debug` traits.489 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result;55 unmodified lines545 /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html546 #[cfg_attr(547 not(no_diagnostic_namespace),548 diagnostic::on_unimplemented(549+ // Prevents `serde_core::de::Deserialize` appearing in the error message550+ // in projects with no direct dependency on serde_core.551+ message = "the trait bound `{Self}: serde::Deserialize<'de>` is not satisfied",552 note = "for local types consider adding `#[derive(serde::Deserialize)]` to your `{Self}` type",553 note = "for types from other crates check whether the crate offers a `serde` feature flag",554 )555 )]68 unmodified lines624 /// bounds is explained in more detail on the page [Understanding deserializer625 /// lifetimes].626 ///627 /// [Understanding deserializer lifetimes]: https://serde.rs/lifetimes.html628+#[cfg_attr(629+ not(no_diagnostic_namespace),630+ diagnostic::on_unimplemented(631+ message = "the trait bound `{Self}: serde::de::DeserializeOwned` is not satisfied",632+ )633+)]634 pub trait DeserializeOwned: for<'de> Deserialize<'de> {}635 impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}636 637 /// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you157 unmodified lines795 /// let flattened: Vec<u64> = deserializer.deserialize_seq(visitor)?;796 /// # Ok(())797 /// # }798 /// ```799+#[cfg_attr(800+ not(no_diagnostic_namespace),801+ diagnostic::on_unimplemented(802+ message = "the trait bound `{Self}: serde::de::DeserializeSeed<'de>` is not satisfied",803+ )804+)]805 pub trait DeserializeSeed<'de>: Sized {806 /// The type produced by using this seed.807 type Value;808 128 unmodified lines937 /// The [example data format] presented on the website contains example code for938 /// a basic JSON `Deserializer`.939 ///940 /// [example data format]: https://serde.rs/data-format.html941+#[cfg_attr(942+ not(no_diagnostic_namespace),943+ diagnostic::on_unimplemented(944+ message = "the trait bound `{Self}: serde::de::Deserializer<'de>` is not satisfied",945+ )946+)]947 pub trait Deserializer<'de>: Sized {948 /// The error type that can be returned if some error occurs during949 /// deserialization.950 type Error: Error;348 unmodified lines1299 /// }1300 /// }1301 /// }1302 /// ```1303+#[cfg_attr(1304+ not(no_diagnostic_namespace),1305+ diagnostic::on_unimplemented(1306+ message = "the trait bound `{Self}: serde::de::Visitor<'de>` is not satisfied",1307+ )1308+)]1309 pub trait Visitor<'de>: Sized {1310 /// The value produced by this visitor.1311 type Value;1312 418 unmodified lines1731 /// The [example data format] presented on the website demonstrates an1732 /// implementation of `SeqAccess` for a basic JSON data format.1733 ///1734 /// [example data format]: https://serde.rs/data-format.html1735+#[cfg_attr(1736+ not(no_diagnostic_namespace),1737+ diagnostic::on_unimplemented(1738+ message = "the trait bound `{Self}: serde::de::SeqAccess<'de>` is not satisfied",1739+ )1740+)]1741 pub trait SeqAccess<'de> {1742 /// The error type that can be returned if some error occurs during1743 /// deserialization.1744 type Error: Error;74 unmodified lines1819 /// The [example data format] presented on the website demonstrates an1820 /// implementation of `MapAccess` for a basic JSON data format.1821 ///1822 /// [example data format]: https://serde.rs/data-format.html1823+#[cfg_attr(1824+ not(no_diagnostic_namespace),1825+ diagnostic::on_unimplemented(1826+ message = "the trait bound `{Self}: serde::de::MapAccess<'de>` is not satisfied",1827+ )1828+)]1829 pub trait MapAccess<'de> {1830 /// The error type that can be returned if some error occurs during1831 /// deserialization.1832 type Error: Error;184 unmodified lines2017 /// The [example data format] presented on the website demonstrates an2018 /// implementation of `EnumAccess` for a basic JSON data format.2019 ///2020 /// [example data format]: https://serde.rs/data-format.html2021+#[cfg_attr(2022+ not(no_diagnostic_namespace),2023+ diagnostic::on_unimplemented(2024+ message = "the trait bound `{Self}: serde::de::EnumAccess<'de>` is not satisfied",2025+ )2026+)]2027 pub trait EnumAccess<'de>: Sized {2028 /// The error type that can be returned if some error occurs during2029 /// deserialization.2030 type Error: Error;39 unmodified lines2070 /// The [example data format] presented on the website demonstrates an2071 /// implementation of `VariantAccess` for a basic JSON data format.2072 ///2073 /// [example data format]: https://serde.rs/data-format.html2074+#[cfg_attr(2075+ not(no_diagnostic_namespace),2076+ diagnostic::on_unimplemented(2077+ message = "the trait bound `{Self}: serde::de::VariantAccess<'de>` is not satisfied",2078+ )2079+)]2080 pub trait VariantAccess<'de>: Sized {2081 /// The error type that can be returned if some error occurs during2082 /// deserialization. Must match the error type of our `EnumAccess`.2083 type Error: Error;301 unmodified linesmodified:serde_core/src/ser/mod.rs+57 added
137 unmodified lines138 /// The [example data format] presented on the website shows an error139 /// type appropriate for a basic JSON data format.140 ///141 /// [example data format]: https://serde.rs/data-format.html142+ #[cfg_attr(143+ not(no_diagnostic_namespace),144+ diagnostic::on_unimplemented(145+ message = "the trait bound `{Self}: serde::ser::Error` is not satisfied",146+ )147+ )]148 pub trait Error: Sized $(+ $($supertrait)::+)* {149 /// Used when a [`Serialize`] implementation encounters any error150 /// while serializing a type.151 ///71 unmodified lines223 /// [derive section of the manual]: https://serde.rs/derive.html224 #[cfg_attr(225 not(no_diagnostic_namespace),226 diagnostic::on_unimplemented(227+ // Prevents `serde_core::ser::Serialize` appearing in the error message228+ // in projects with no direct dependency on serde_core.229+ message = "the trait bound `{Self}: serde::Serialize` is not satisfied",230 note = "for local types consider adding `#[derive(serde::Serialize)]` to your `{Self}` type",231 note = "for types from other crates check whether the crate offers a `serde` feature flag",232 )233 )]111 unmodified lines345 /// The [example data format] presented on the website contains example code for346 /// a basic JSON `Serializer`.347 ///348 /// [example data format]: https://serde.rs/data-format.html349+#[cfg_attr(350+ not(no_diagnostic_namespace),351+ diagnostic::on_unimplemented(352+ message = "the trait bound `{Self}: serde::Serializer` is not satisfied",353+ )354+)]355 pub trait Serializer: Sized {356 /// The output type produced by this `Serializer` during successful357 /// serialization. Most serializers that produce text or binary output358 /// should set `Ok = ()` and serialize into an [`io::Write`] or buffer1149 unmodified lines1508 /// The [example data format] presented on the website demonstrates an1509 /// implementation of `SerializeSeq` for a basic JSON data format.1510 ///1511 /// [example data format]: https://serde.rs/data-format.html1512+#[cfg_attr(1513+ not(no_diagnostic_namespace),1514+ diagnostic::on_unimplemented(1515+ message = "the trait bound `{Self}: serde::ser::SerializeSeq` is not satisfied",1516+ )1517+)]1518 pub trait SerializeSeq {1519 /// Must match the `Ok` type of our `Serializer`.1520 type Ok;1521 92 unmodified lines1614 /// The [example data format] presented on the website demonstrates an1615 /// implementation of `SerializeTuple` for a basic JSON data format.1616 ///1617 /// [example data format]: https://serde.rs/data-format.html1618+#[cfg_attr(1619+ not(no_diagnostic_namespace),1620+ diagnostic::on_unimplemented(1621+ message = "the trait bound `{Self}: serde::ser::SerializeTuple` is not satisfied",1622+ )1623+)]1624 pub trait SerializeTuple {1625 /// Must match the `Ok` type of our `Serializer`.1626 type Ok;1627 37 unmodified lines1665 /// The [example data format] presented on the website demonstrates an1666 /// implementation of `SerializeTupleStruct` for a basic JSON data format.1667 ///1668 /// [example data format]: https://serde.rs/data-format.html1669+#[cfg_attr(1670+ not(no_diagnostic_namespace),1671+ diagnostic::on_unimplemented(1672+ message = "the trait bound `{Self}: serde::ser::SerializeTupleStruct` is not satisfied",1673+ )1674+)]1675 pub trait SerializeTupleStruct {1676 /// Must match the `Ok` type of our `Serializer`.1677 type Ok;1678 50 unmodified lines1729 /// The [example data format] presented on the website demonstrates an1730 /// implementation of `SerializeTupleVariant` for a basic JSON data format.1731 ///1732 /// [example data format]: https://serde.rs/data-format.html1733+#[cfg_attr(1734+ not(no_diagnostic_namespace),1735+ diagnostic::on_unimplemented(1736+ message = "the trait bound `{Self}: serde::ser::SerializeTupleVariant` is not satisfied",1737+ )1738+)]1739 pub trait SerializeTupleVariant {1740 /// Must match the `Ok` type of our `Serializer`.1741 type Ok;1742 58 unmodified lines1801 /// The [example data format] presented on the website demonstrates an1802 /// implementation of `SerializeMap` for a basic JSON data format.1803 ///1804 /// [example data format]: https://serde.rs/data-format.html1805+#[cfg_attr(1806+ not(no_diagnostic_namespace),1807+ diagnostic::on_unimplemented(1808+ message = "the trait bound `{Self}: serde::ser::SerializeMap` is not satisfied",1809+ )1810+)]1811 pub trait SerializeMap {1812 /// Must match the `Ok` type of our `Serializer`.1813 type Ok;1814 82 unmodified lines1897 /// The [example data format] presented on the website demonstrates an1898 /// implementation of `SerializeStruct` for a basic JSON data format.1899 ///1900 /// [example data format]: https://serde.rs/data-format.html1901+#[cfg_attr(1902+ not(no_diagnostic_namespace),1903+ diagnostic::on_unimplemented(1904+ message = "the trait bound `{Self}: serde::ser::SerializeStruct` is not satisfied",1905+ )1906+)]1907 pub trait SerializeStruct {1908 /// Must match the `Ok` type of our `Serializer`.1909 type Ok;1910 56 unmodified lines1967 /// The [example data format] presented on the website demonstrates an1968 /// implementation of `SerializeStructVariant` for a basic JSON data format.1969 ///1970 /// [example data format]: https://serde.rs/data-format.html1971+#[cfg_attr(1972+ not(no_diagnostic_namespace),1973+ diagnostic::on_unimplemented(1974+ message = "the trait bound `{Self}: serde::ser::SerializeStructVariant` is not satisfied",1975+ )1976+)]1977 pub trait SerializeStructVariant {1978 /// Must match the `Ok` type of our `Serializer`.1979 type Ok;1980 30 unmodified linesmodified:test_suite/tests/ui/unimplemented/required_by_dependency.stderr+2 added-2 removed
1-error[E0277]: the trait bound `MyStruct: serde_core::ser::Serialize` is not satisfied1+error[E0277]: the trait bound `MyStruct: serde::Serialize` is not satisfied2 --> tests/ui/unimplemented/required_by_dependency.rs:4:353 |4 4 | serde_test::assert_ser_tokens(&MyStruct, &[]);5 | ----------------------------- ^^^^^^^^^ unsatisfied trait bound25 unmodified lines31 | where32 | T: ?Sized + Serialize,33 | ^^^^^^^^^ required by this bound in `assert_ser_tokens`34 35-error[E0277]: the trait bound `MyStruct: serde_core::de::Deserialize<'_>` is not satisfied35+error[E0277]: the trait bound `MyStruct: serde::Deserialize<'de>` is not satisfied36 --> tests/ui/unimplemented/required_by_dependency.rs:5:3437 |38 5 | serde_test::assert_de_tokens(&MyStruct, &[]);39 | ---------------------------- ^^^^^^^^^ unsatisfied trait bound74 unmodified linesmodified:test_suite/tests/ui/unimplemented/required_locally.stderr+2 added-2 removed
1-error[E0277]: the trait bound `MyStruct: Serialize` is not satisfied1+error[E0277]: the trait bound `MyStruct: serde::Serialize` is not satisfied2 --> tests/ui/unimplemented/required_locally.rs:21:153 |4 21 | to_string(&MyStruct);5 | --------- ^^^^^^^^^ unsatisfied trait bound25 unmodified lines31 5 | where32 6 | T: Serialize,33 | ^^^^^^^^^ required by this bound in `to_string`34 35-error[E0277]: the trait bound `MyStruct: Deserialize<'_>` is not satisfied35+error[E0277]: the trait bound `MyStruct: serde::Deserialize<'de>` is not satisfied36 --> tests/ui/unimplemented/required_locally.rs:22:2337 |38 22 | let _: MyStruct = from_str("");39 | ^^^^^^^^^^^^ unsatisfied trait bound26 unmodified linesmodified:test_suite/tests/ui/with/incorrect_type.stderr+4 added-4 removed
1-error[E0277]: the trait bound `&u8: Serializer` is not satisfied1+error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied2 --> tests/ui/with/incorrect_type.rs:14:103 |4 14 | #[derive(Serialize, Deserialize)]5 | ^^^^^^^^^ the trait `Serializer` is not implemented for `&u8`23 unmodified lines29 |30 9 | pub fn serialize<T, S: Serializer>(_: S) -> Result<S::Ok, S::Error> {31 | ^^^^^^^^^32 33-error[E0277]: the trait bound `&u8: Serializer` is not satisfied33+error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied34 --> tests/ui/with/incorrect_type.rs:15:2535 |36 15 | struct W(#[serde(with = "w")] u8, u8);37 | ^^^ the trait `Serializer` is not implemented for `&u8`10 unmodified lines48 | ^^^ expected `u8`, found `()`49 |50 = note: `?` operator cannot convert from `()` to `u8`51 52-error[E0277]: the trait bound `&u8: Serializer` is not satisfied52+error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied53 --> tests/ui/with/incorrect_type.rs:17:1054 |55 17 | #[derive(Serialize, Deserialize)]56 | ^^^^^^^^^ the trait `Serializer` is not implemented for `&u8`23 unmodified lines80 |81 9 | pub fn serialize<T, S: Serializer>(_: S) -> Result<S::Ok, S::Error> {82 | ^^^^^^^^^83 84-error[E0277]: the trait bound `&u8: Serializer` is not satisfied84+error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied85 --> tests/ui/with/incorrect_type.rs:18:3586 |87 18 | struct S(#[serde(serialize_with = "w::serialize")] u8, u8);88 | ^^^^^^^^^^^^^^ the trait `Serializer` is not implemented for `&u8`13 unmodified lines