From ab581789f4aa6a92f8e149f3086040342dfaddcf Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 27 Sep 2025 09:39:10 -0700 Subject: [PATCH] Workaround for RUSTDOCFLAGS='--cfg=docsrs' --- serde/build.rs | 6 ++---- serde_core/build.rs | 2 +- serde_core/src/crate_root.rs | 13 ++++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/serde/build.rs b/serde/build.rs index 152327eb..fa0fd5a0 100644 --- a/serde/build.rs +++ b/serde/build.rs @@ -19,9 +19,7 @@ use serde_core::__private$$ as serde_core_private; fn main() { println!("cargo:rerun-if-changed=build.rs"); - if env::var_os("DOCS_RS").is_some() { - println!("cargo:rustc-cfg=no_serde_core"); - } + println!("cargo:rustc-cfg=if_docsrs_then_no_serde_core"); let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); let patch_version = env::var("CARGO_PKG_VERSION_PATCH").unwrap(); @@ -35,12 +33,12 @@ fn main() { if minor >= 77 { println!("cargo:rustc-check-cfg=cfg(feature, values(\"result\"))"); + println!("cargo:rustc-check-cfg=cfg(if_docsrs_then_no_serde_core)"); println!("cargo:rustc-check-cfg=cfg(no_core_cstr)"); println!("cargo:rustc-check-cfg=cfg(no_core_error)"); println!("cargo:rustc-check-cfg=cfg(no_core_net)"); println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)"); println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)"); - println!("cargo:rustc-check-cfg=cfg(no_serde_core)"); println!("cargo:rustc-check-cfg=cfg(no_serde_derive)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)"); diff --git a/serde_core/build.rs b/serde_core/build.rs index 1c52372c..9a33ab30 100644 --- a/serde_core/build.rs +++ b/serde_core/build.rs @@ -29,12 +29,12 @@ fn main() { }; if minor >= 77 { + println!("cargo:rustc-check-cfg=cfg(if_docsrs_then_no_serde_core)"); println!("cargo:rustc-check-cfg=cfg(no_core_cstr)"); println!("cargo:rustc-check-cfg=cfg(no_core_error)"); println!("cargo:rustc-check-cfg=cfg(no_core_net)"); println!("cargo:rustc-check-cfg=cfg(no_core_num_saturating)"); println!("cargo:rustc-check-cfg=cfg(no_diagnostic_namespace)"); - println!("cargo:rustc-check-cfg=cfg(no_serde_core)"); println!("cargo:rustc-check-cfg=cfg(no_serde_derive)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic)"); println!("cargo:rustc-check-cfg=cfg(no_std_atomic64)"); diff --git a/serde_core/src/crate_root.rs b/serde_core/src/crate_root.rs index 80ef99c7..2cf75a40 100644 --- a/serde_core/src/crate_root.rs +++ b/serde_core/src/crate_root.rs @@ -132,12 +132,12 @@ macro_rules! crate_root { }; } - #[cfg_attr(no_serde_core, path = "core/de/mod.rs")] + #[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/de/mod.rs")] pub mod de; - #[cfg_attr(no_serde_core, path = "core/ser/mod.rs")] + #[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/ser/mod.rs")] pub mod ser; - #[cfg_attr(no_serde_core, path = "core/format.rs")] + #[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/format.rs")] mod format; #[doc(inline)] @@ -147,7 +147,10 @@ macro_rules! crate_root { // Used by generated code. Not public API. #[doc(hidden)] - #[cfg_attr(no_serde_core, path = "core/private/mod.rs")] + #[cfg_attr( + all(docsrs, if_docsrs_then_no_serde_core), + path = "core/private/mod.rs" + )] mod private; // Used by declarative macro generated code. Not public API. @@ -162,7 +165,7 @@ macro_rules! crate_root { include!(concat!(env!("OUT_DIR"), "/private.rs")); #[cfg(all(not(feature = "std"), no_core_error))] - #[cfg_attr(no_serde_core, path = "core/std_error.rs")] + #[cfg_attr(all(docsrs, if_docsrs_then_no_serde_core), path = "core/std_error.rs")] mod std_error; }; }