Skip to content
Myr

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...

authored by David Tolnay · committed by David Tolnay · parent 908f321 · Browse files · 5 files+128 added−8 removed
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

5 files+128 added−8 removed

modified:serde_core/src/de/mod.rs+63 added
156 unmodified lines
157 /// 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 lines
476/// &"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)]486pub 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 lines
545/// [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 lines
624/// 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)]634pub trait DeserializeOwned: for<'de> Deserialize<'de> {}635impl<T> DeserializeOwned for T where T: for<'de> Deserialize<'de> {}636637/// `DeserializeSeed` is the stateful form of the `Deserialize` trait. If you
157 unmodified lines
795/// 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)]805pub trait DeserializeSeed<'de>: Sized {806 /// The type produced by using this seed.807 type Value;808
128 unmodified lines
937/// 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)]947pub trait Deserializer<'de>: Sized {948 /// The error type that can be returned if some error occurs during949 /// deserialization.950 type Error: Error;
348 unmodified lines
1299/// }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)]1309pub trait Visitor<'de>: Sized {1310 /// The value produced by this visitor.1311 type Value;1312
418 unmodified lines
1731/// 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)]1741pub trait SeqAccess<'de> {1742 /// The error type that can be returned if some error occurs during1743 /// deserialization.1744 type Error: Error;
74 unmodified lines
1819/// 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)]1829pub trait MapAccess<'de> {1830 /// The error type that can be returned if some error occurs during1831 /// deserialization.1832 type Error: Error;
184 unmodified lines
2017/// 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)]2027pub trait EnumAccess<'de>: Sized {2028 /// The error type that can be returned if some error occurs during2029 /// deserialization.2030 type Error: Error;
39 unmodified lines
2070/// 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)]2080pub 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 lines
modified:serde_core/src/ser/mod.rs+57 added
137 unmodified lines
138 /// 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 lines
223/// [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 lines
345/// 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)]355pub 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 buffer
1149 unmodified lines
1508/// 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)]1518pub trait SerializeSeq {1519 /// Must match the `Ok` type of our `Serializer`.1520 type Ok;1521
92 unmodified lines
1614/// 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)]1624pub trait SerializeTuple {1625 /// Must match the `Ok` type of our `Serializer`.1626 type Ok;1627
37 unmodified lines
1665/// 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)]1675pub trait SerializeTupleStruct {1676 /// Must match the `Ok` type of our `Serializer`.1677 type Ok;1678
50 unmodified lines
1729/// 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)]1739pub trait SerializeTupleVariant {1740 /// Must match the `Ok` type of our `Serializer`.1741 type Ok;1742
58 unmodified lines
1801/// 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)]1811pub trait SerializeMap {1812 /// Must match the `Ok` type of our `Serializer`.1813 type Ok;1814
82 unmodified lines
1897/// 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)]1907pub trait SerializeStruct {1908 /// Must match the `Ok` type of our `Serializer`.1909 type Ok;1910
56 unmodified lines
1967/// 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)]1977pub trait SerializeStructVariant {1978 /// Must match the `Ok` type of our `Serializer`.1979 type Ok;1980
30 unmodified lines
modified:test_suite/tests/ui/unimplemented/required_by_dependency.stderr+2 added-2 removed
1error[E0277]: the trait bound `MyStruct: serde_core::ser::Serialize` is not satisfied1error[E0277]: the trait bound `MyStruct: serde::Serialize` is not satisfied2 --> tests/ui/unimplemented/required_by_dependency.rs:4:353 |44 | serde_test::assert_ser_tokens(&MyStruct, &[]);5 | ----------------------------- ^^^^^^^^^ unsatisfied trait bound
25 unmodified lines
31 | where32 | T: ?Sized + Serialize,33 | ^^^^^^^^^ required by this bound in `assert_ser_tokens`3435error[E0277]: the trait bound `MyStruct: serde_core::de::Deserialize<'_>` is not satisfied35error[E0277]: the trait bound `MyStruct: serde::Deserialize<'de>` is not satisfied36 --> tests/ui/unimplemented/required_by_dependency.rs:5:3437 |385 | serde_test::assert_de_tokens(&MyStruct, &[]);39 | ---------------------------- ^^^^^^^^^ unsatisfied trait bound
74 unmodified lines
modified:test_suite/tests/ui/unimplemented/required_locally.stderr+2 added-2 removed
1error[E0277]: the trait bound `MyStruct: Serialize` is not satisfied1error[E0277]: the trait bound `MyStruct: serde::Serialize` is not satisfied2 --> tests/ui/unimplemented/required_locally.rs:21:153 |421 | to_string(&MyStruct);5 | --------- ^^^^^^^^^ unsatisfied trait bound
25 unmodified lines
31 5 | where32 6 | T: Serialize,33 | ^^^^^^^^^ required by this bound in `to_string`3435error[E0277]: the trait bound `MyStruct: Deserialize<'_>` is not satisfied35error[E0277]: the trait bound `MyStruct: serde::Deserialize<'de>` is not satisfied36 --> tests/ui/unimplemented/required_locally.rs:22:2337 |3822 | let _: MyStruct = from_str("");39 | ^^^^^^^^^^^^ unsatisfied trait bound
26 unmodified lines
modified:test_suite/tests/ui/with/incorrect_type.stderr+4 added-4 removed
1error[E0277]: the trait bound `&u8: Serializer` is not satisfied1error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied2 --> tests/ui/with/incorrect_type.rs:14:103 |414 | #[derive(Serialize, Deserialize)]5 | ^^^^^^^^^ the trait `Serializer` is not implemented for `&u8`
23 unmodified lines
29 |30 9 | pub fn serialize<T, S: Serializer>(_: S) -> Result<S::Ok, S::Error> {31 | ^^^^^^^^^3233error[E0277]: the trait bound `&u8: Serializer` is not satisfied33error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied34 --> tests/ui/with/incorrect_type.rs:15:2535 |3615 | struct W(#[serde(with = "w")] u8, u8);37 | ^^^ the trait `Serializer` is not implemented for `&u8`
10 unmodified lines
48 | ^^^ expected `u8`, found `()`49 |50 = note: `?` operator cannot convert from `()` to `u8`5152error[E0277]: the trait bound `&u8: Serializer` is not satisfied52error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied53 --> tests/ui/with/incorrect_type.rs:17:1054 |5517 | #[derive(Serialize, Deserialize)]56 | ^^^^^^^^^ the trait `Serializer` is not implemented for `&u8`
23 unmodified lines
80 |81 9 | pub fn serialize<T, S: Serializer>(_: S) -> Result<S::Ok, S::Error> {82 | ^^^^^^^^^8384error[E0277]: the trait bound `&u8: Serializer` is not satisfied84error[E0277]: the trait bound `&u8: serde::Serializer` is not satisfied85 --> tests/ui/with/incorrect_type.rs:18:3586 |8718 | struct S(#[serde(serialize_with = "w::serialize")] u8, u8);88 | ^^^^^^^^^^^^^^ the trait `Serializer` is not implemented for `&u8`
13 unmodified lines