Skip to content

Commit 80cccb4

Browse files
committed
[derive] IntoBytes on unions requires --cfg
Makes progress on #1792
1 parent a9f09d7 commit 80cccb4

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

tools/cargo-zerocopy/src/main.rs

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

166166
fn get_rustflags(name: &str) -> &'static str {
167+
// See #1792 for context on zerocopy_derive_union_into_bytes.
167168
if name == "nightly" {
168-
"--cfg __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS "
169+
"--cfg __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS --cfg zerocopy_derive_union_into_bytes "
169170
} else {
170-
""
171+
"--cfg zerocopy_derive_union_into_bytes "
171172
}
172173
}
173174

zerocopy-derive/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ repository = "https://github.com/google/zerocopy"
2222
# [1] https://github.com/rust-lang/crater
2323
exclude = [".*", "tests/enum_from_bytes.rs", "tests/ui-nightly/enum_from_bytes_u16_too_few.rs.disabled"]
2424

25+
[lints.rust]
26+
# See #1792 for more context.
27+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(zerocopy_derive_union_into_bytes)'] }
28+
2529
[lib]
2630
proc-macro = true
2731

zerocopy-derive/src/lib.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,17 @@ fn derive_into_bytes_enum(ast: &DeriveInput, enm: &DataEnum) -> Result<TokenStre
934934
/// - `repr(C)`, `repr(transparent)`, or `repr(packed)`
935935
/// - no padding (size of union equals size of each field type)
936936
fn derive_into_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> Result<TokenStream, Error> {
937+
// See #1792 for more context.
938+
let cfg_compile_error = quote!(
939+
const _: () = {
940+
#[cfg(not(zerocopy_derive_union_into_bytes))]
941+
::zerocopy::util::macro_util::core_reexport::compile_error!(
942+
"requires --cfg zerocopy_derive_union_into_bytes;
943+
please let us know you use this feature: https://github.com/google/zerocopy/discussions/1802"
944+
);
945+
};
946+
);
947+
937948
// TODO(#10): Support type parameters.
938949
if !ast.generics.params.is_empty() {
939950
return Err(Error::new(Span::call_site(), "unsupported on types with type parameters"));
@@ -951,15 +962,16 @@ fn derive_into_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> Result<TokenSt
951962
));
952963
}
953964

954-
Ok(impl_block(
965+
let impl_block = impl_block(
955966
ast,
956967
unn,
957968
Trait::IntoBytes,
958969
FieldBounds::ALL_SELF,
959970
SelfBounds::None,
960971
Some(PaddingCheck::Union),
961972
None,
962-
))
973+
);
974+
Ok(quote!(#cfg_compile_error #impl_block))
963975
}
964976

965977
/// A struct is `Unaligned` if:

0 commit comments

Comments
 (0)