Skip to content

Commit

Permalink
[derive] IntoBytes on unions requires --cfg
Browse files Browse the repository at this point in the history
Makes progress on #1792
  • Loading branch information
joshlf committed Oct 3, 2024
1 parent a9f09d7 commit 6fdeca1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tools/cargo-zerocopy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,11 @@ fn install_toolchain_or_exit(versions: &Versions, name: &str) -> Result<(), Erro
}

fn get_rustflags(name: &str) -> &'static str {
// See #1792 for context on zerocopy_derive_union_into_bytes.
if name == "nightly" {
"--cfg __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS "
"--cfg __ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS --cfg zerocopy_derive_union_into_bytes "
} else {
""
"--cfg zerocopy_derive_union_into_bytes "
}
}

Expand Down
4 changes: 4 additions & 0 deletions zerocopy-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ repository = "https://github.com/google/zerocopy"
# [1] https://github.com/rust-lang/crater
exclude = [".*", "tests/enum_from_bytes.rs", "tests/ui-nightly/enum_from_bytes_u16_too_few.rs.disabled"]

[lints.rust]
# See #1792 for more context.
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(zerocopy_derive_union_into_bytes)'] }

[lib]
proc-macro = true

Expand Down
9 changes: 9 additions & 0 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,15 @@ fn derive_into_bytes_enum(ast: &DeriveInput, enm: &DataEnum) -> Result<TokenStre
/// - `repr(C)`, `repr(transparent)`, or `repr(packed)`
/// - no padding (size of union equals size of each field type)
fn derive_into_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> Result<TokenStream, Error> {
if !cfg!(zerocopy_derive_union_into_bytes) {
// See #1792 for more context.
return Err(Error::new(
Span::call_site(),
"requires --cfg zerocopy_derive_union_into_bytes;
please let us know you use this feature: https://github.com/google/zerocopy/discussions/1802",
));
}

// TODO(#10): Support type parameters.
if !ast.generics.params.is_empty() {
return Err(Error::new(Span::call_site(), "unsupported on types with type parameters"));
Expand Down

0 comments on commit 6fdeca1

Please sign in to comment.