From 3c747e4585e49166629dea551fceea7af8227d0d Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 13 Sep 2025 09:34:08 -0700 Subject: [PATCH 1/2] Relocate serde_derive version constraint from serde to serde_core This disallows using an old version of serde_derive against a new version of serde_core (with a rename to serde in Cargo.toml). --- serde/Cargo.toml | 8 -------- serde_core/Cargo.toml | 8 ++++++++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/serde/Cargo.toml b/serde/Cargo.toml index c00fcd66..d58a7dbc 100644 --- a/serde/Cargo.toml +++ b/serde/Cargo.toml @@ -34,14 +34,6 @@ rustdoc-args = [ "--extern-html-root-url=std=https://doc.rust-lang.org", ] -# This cfg cannot be enabled, but it still forces Cargo to keep serde_derive's -# version in lockstep with serde's, even if someone depends on the two crates -# separately with serde's "derive" feature disabled. Every serde_derive release -# is compatible with exactly one serde release because the generated code -# involves nonpublic APIs which are not bound by semver. -[target.'cfg(any())'.dependencies] -serde_derive = { version = "=1.0.219", path = "../serde_derive" } - ### FEATURES ################################################################# diff --git a/serde_core/Cargo.toml b/serde_core/Cargo.toml index 79aca909..02e0ad50 100644 --- a/serde_core/Cargo.toml +++ b/serde_core/Cargo.toml @@ -29,6 +29,14 @@ rustdoc-args = [ "--extern-html-root-url=std=https://doc.rust-lang.org", ] +# This cfg cannot be enabled, but it still forces Cargo to keep serde_derive's +# version in lockstep with serde's, even if someone depends on the two crates +# separately with serde's "derive" feature disabled. Every serde_derive release +# is compatible with exactly one serde release because the generated code +# involves nonpublic APIs which are not bound by semver. +[target.'cfg(any())'.dependencies] +serde_derive = { version = "=1.0.219", path = "../serde_derive" } + ### FEATURES ################################################################# From 8909fc0c603949ad60ee503d1d49a440b7b6f414 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 13 Sep 2025 09:39:58 -0700 Subject: [PATCH 2/2] Enforce derive cannot be used against serde_core Even for a simple data structure that would ordinarily expand to an impl that only refers to the public Serde traits without serde::__private, we still disallow a (renamed) serde_core dependency. This leaves the liberty to new usage of private helpers in such impls over time. For example, similar to the optimization of the standard library's derive(Debug) that introduced debug_struct_field1_finish to improve compile time of derived impls. --- serde/src/lib.rs | 6 ++++++ serde_core/src/lib.rs | 10 ++++++++++ serde_derive/src/dummy.rs | 3 +++ 3 files changed, 19 insertions(+) diff --git a/serde/src/lib.rs b/serde/src/lib.rs index 54b0d408..6f53ed11 100644 --- a/serde/src/lib.rs +++ b/serde/src/lib.rs @@ -256,3 +256,9 @@ extern crate serde_derive; #[cfg(feature = "serde_derive")] #[cfg_attr(docsrs, doc(cfg(feature = "derive")))] pub use serde_derive::{Deserialize, Serialize}; + +#[macro_export] +#[doc(hidden)] +macro_rules! __require_serde_not_serde_core { + () => {}; +} diff --git a/serde_core/src/lib.rs b/serde_core/src/lib.rs index a409d286..e566bb50 100644 --- a/serde_core/src/lib.rs +++ b/serde_core/src/lib.rs @@ -266,3 +266,13 @@ mod seed; #[cfg(all(not(feature = "std"), no_core_error))] mod std_error; + +#[macro_export] +#[doc(hidden)] +macro_rules! __require_serde_not_serde_core { + () => { + ::core::compile_error!( + "Serde derive requires a dependency on the serde crate, not serde_core" + ); + }; +} diff --git a/serde_derive/src/dummy.rs b/serde_derive/src/dummy.rs index 8fd5494b..e0bca647 100644 --- a/serde_derive/src/dummy.rs +++ b/serde_derive/src/dummy.rs @@ -22,6 +22,9 @@ pub fn wrap_in_const(serde_path: Option<&syn::Path>, code: TokenStream) -> Token )] const _: () = { #use_serde + + _serde::__require_serde_not_serde_core!(); + #code }; }