Skip to content

Commit

Permalink
[derive] Fix bug with KnownLayout on repr(packed) (#2307)
Browse files Browse the repository at this point in the history
Fixes #2302

gherrit-pr-id: If68c1724be15c4df3ec936a12cf854908650a639
  • Loading branch information
joshlf authored Feb 6, 2025
1 parent c93c2c0 commit 0451e53
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
10 changes: 9 additions & 1 deletion zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,15 @@ fn derive_known_layout_inner(ast: &DeriveInput, _top_level: Trait) -> Result<Tok
::zerocopy::util::macro_util::Field<#leading_field_indices>
>::Type
>,)*
<#trailing_field_ty as ::zerocopy::KnownLayout>::MaybeUninit
// NOTE(#2302): We wrap in `ManuallyDrop` here in case the
// type we're operating on is both generic and
// `repr(packed)`. In that case, Rust needs to know that the
// type is *either* `Sized` or has a trivial `Drop`.
// `ManuallyDrop` has a trivial `Drop`, and so satisfies
// this requirement.
::zerocopy::util::macro_util::core_reexport::mem::ManuallyDrop<
<#trailing_field_ty as ::zerocopy::KnownLayout>::MaybeUninit
>
)
where
#trailing_field_ty: ::zerocopy::KnownLayout,
Expand Down
14 changes: 8 additions & 6 deletions zerocopy-derive/src/output_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ fn test_known_layout() {
::zerocopy::util::macro_util::core_reexport::mem::MaybeUninit<
<Foo<T, U> as ::zerocopy::util::macro_util::Field<__Zerocopy_Field_0>>::Type,
>,
<<Foo<
T,
U,
> as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_1,
>>::Type as ::zerocopy::KnownLayout>::MaybeUninit,
::zerocopy::util::macro_util::core_reexport::mem::ManuallyDrop<
<<Foo<
T,
U,
> as ::zerocopy::util::macro_util::Field<
__Zerocopy_Field_1,
>>::Type as ::zerocopy::KnownLayout>::MaybeUninit
>,
)
where
<Foo<
Expand Down
11 changes: 11 additions & 0 deletions zerocopy-derive/tests/struct_known_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@ impl WithSelfReference {
}

util_assert_impl_all!(WithSelfReference: imp::KnownLayout);

// Deriving `KnownLayout` should work with generic `repr(packed)` types. See
// #2302.

#[derive(imp::KnownLayout)]
#[repr(C, packed)]
struct Packet<P> {
payload: P,
}

util_assert_impl_all!(Packet<imp::u8>: imp::KnownLayout);

0 comments on commit 0451e53

Please sign in to comment.