Skip to content

Commit 406a35a

Browse files
authored
[cargo-zerocopy] Pass --cfg __ZEROCOPY_INTERNAL_USE_ONLY_DEV_MODE (#2960)
This allows zerocopy code to detect when it's being compiled during development or in CI. In this commit, we use this to disable compiling the `doctests` module in production builds. gherrit-pr-id: G2bd9f86557e00ed04dc9587f4d5f4b0dbb6513ad
1 parent 36e8abe commit 406a35a

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ fn main() {
9191
println!(
9292
"cargo:rustc-check-cfg=cfg(__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS)"
9393
);
94+
println!("cargo:rustc-check-cfg=cfg(__ZEROCOPY_INTERNAL_USE_ONLY_DEV_MODE)");
9495
println!("cargo:rustc-check-cfg=cfg(coverage_nightly)");
9596
}
9697

src/doctests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// those terms.
99

1010
#![cfg(feature = "derive")] // Required for derives on `SliceDst`
11-
#![allow(dead_code)]
11+
#![allow(dead_code, missing_docs, missing_debug_implementations, missing_copy_implementations)]
1212

1313
//! Our UI test framework, built on the `trybuild` crate, does not support
1414
//! testing for post-monomorphization errors. Instead, we use doctests, which
@@ -18,7 +18,6 @@ use crate::*;
1818

1919
#[derive(KnownLayout, FromBytes, IntoBytes, Immutable)]
2020
#[repr(C)]
21-
#[allow(missing_debug_implementations, missing_copy_implementations)]
2221
pub struct SliceDst<T, U> {
2322
pub t: T,
2423
pub u: [U],

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub mod byte_slice;
338338
pub mod byteorder;
339339
mod deprecated;
340340

341-
#[doc(hidden)]
341+
#[cfg(__ZEROCOPY_INTERNAL_USE_ONLY_DEV_MODE)]
342342
pub mod doctests;
343343

344344
// This module is `pub` so that zerocopy's error types and error handling

tools/cargo-zerocopy/src/main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,21 @@ fn install_toolchain_or_exit(versions: &Versions, name: &str) -> Result<(), Erro
164164
Ok(())
165165
}
166166

167-
fn get_rustflags(name: &str) -> &'static str {
167+
fn get_rustflags(name: &str) -> String {
168168
// See #1792 for context on zerocopy_derive_union_into_bytes.
169+
let mut flags =
170+
"--cfg zerocopy_derive_union_into_bytes --cfg __ZEROCOPY_INTERNAL_USE_ONLY_DEV_MODE"
171+
.to_string();
172+
169173
if name == "nightly" {
170-
"--cfg __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS --cfg zerocopy_derive_union_into_bytes "
171-
} else {
172-
"--cfg zerocopy_derive_union_into_bytes "
174+
flags += " --cfg __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS";
173175
}
176+
177+
flags
174178
}
175179

176180
fn get_toolchain_rustflags(name: &str) -> String {
177-
format!("--cfg __ZEROCOPY_TOOLCHAIN=\"{}\" ", name)
181+
format!("--cfg __ZEROCOPY_TOOLCHAIN=\"{}\"", name)
178182
}
179183

180184
fn rustup<'a>(args: impl IntoIterator<Item = &'a str>, env: Option<(&str, &str)>) -> Command {
@@ -228,7 +232,7 @@ fn delegate_cargo() -> Result<(), Error> {
228232
.unwrap_or_default();
229233

230234
let rustflags = format!(
231-
"{}{}{}",
235+
"{} {} {}",
232236
get_rustflags(name),
233237
get_toolchain_rustflags(name),
234238
env_rustflags,

0 commit comments

Comments
 (0)