Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions zerocopy-derive/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ pub(crate) fn derive_is_bit_valid(
// enum's tag corresponds to one of the enum's discriminants. Then, we
// check the bit validity of each field of the corresponding variant.
// Thus, this is a sound implementation of `is_bit_valid`.
fn is_bit_valid<A>(
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
A: ::zerocopy::pointer::invariant::Aliasing
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
{
use ::zerocopy::util::macro_util::core_reexport;
Expand Down
20 changes: 14 additions & 6 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,9 +420,13 @@ fn derive_try_from_bytes_struct(ast: &DeriveInput, strct: &DataStruct) -> proc_m
// validity of a struct is just the composition of the bit
// validities of its fields, so this is a sound implementation of
// `is_bit_valid`.
fn is_bit_valid<A: ::zerocopy::pointer::invariant::Aliasing + ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>>(
mut candidate: ::zerocopy::Maybe<Self, A>,
) -> bool {
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
{
true #(&& {
// SAFETY:
// - `project` is a field projection, and so it addresses a
Expand Down Expand Up @@ -470,9 +474,13 @@ fn derive_try_from_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> proc_macro
// bit validity of a union is not yet well defined in Rust, but it
// is guaranteed to be no more strict than this definition. See #696
// for a more in-depth discussion.
fn is_bit_valid<A: ::zerocopy::pointer::invariant::Aliasing + ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>>(
mut candidate: ::zerocopy::Maybe<Self, A>
) -> bool {
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
{
false #(|| {
// SAFETY:
// - `project` is a field projection, and so it addresses a
Expand Down
51 changes: 27 additions & 24 deletions zerocopy-derive/src/output_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,13 @@ fn test_try_from_bytes() {
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

fn is_bit_valid<
A: ::zerocopy::pointer::invariant::Aliasing
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
>(
mut candidate: ::zerocopy::Maybe<Self, A>,
) -> bool {
{
true
}
}
Expand All @@ -170,12 +171,13 @@ fn test_from_zeros() {
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

fn is_bit_valid<
A: ::zerocopy::pointer::invariant::Aliasing
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
>(
mut candidate: ::zerocopy::Maybe<Self, A>,
) -> bool {
{
true
}
}
Expand All @@ -198,12 +200,13 @@ fn test_from_bytes() {
unsafe impl ::zerocopy::TryFromBytes for Foo {
fn only_derive_is_allowed_to_implement_this_trait() {}

fn is_bit_valid<
A: ::zerocopy::pointer::invariant::Aliasing
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
>(
mut candidate: ::zerocopy::Maybe<Self, A>,
) -> bool {
{
true
}
}
Expand Down Expand Up @@ -303,11 +306,11 @@ fn test_try_from_bytes_enum() {
PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
{
fn only_derive_is_allowed_to_implement_this_trait() {}
fn is_bit_valid<A>(
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
A: ::zerocopy::pointer::invariant::Aliasing
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
{
use ::zerocopy::util::macro_util::core_reexport;
Expand Down Expand Up @@ -445,11 +448,11 @@ fn test_try_from_bytes_enum() {
PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
{
fn only_derive_is_allowed_to_implement_this_trait() {}
fn is_bit_valid<A>(
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
A: ::zerocopy::pointer::invariant::Aliasing
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
{
use ::zerocopy::util::macro_util::core_reexport;
Expand Down Expand Up @@ -587,11 +590,11 @@ fn test_try_from_bytes_enum() {
PhantomData<&'a [(X, Y); N]>: ::zerocopy::TryFromBytes,
{
fn only_derive_is_allowed_to_implement_this_trait() {}
fn is_bit_valid<A>(
mut candidate: ::zerocopy::Maybe<'_, Self, A>,
fn is_bit_valid<___ZerocopyAliasing>(
mut candidate: ::zerocopy::Maybe<'_, Self, ___ZerocopyAliasing>,
) -> ::zerocopy::util::macro_util::core_reexport::primitive::bool
where
A: ::zerocopy::pointer::invariant::Aliasing
___ZerocopyAliasing: ::zerocopy::pointer::invariant::Aliasing
+ ::zerocopy::pointer::invariant::AtLeast<::zerocopy::pointer::invariant::Shared>,
{
use ::zerocopy::util::macro_util::core_reexport;
Expand Down
10 changes: 10 additions & 0 deletions zerocopy-derive/tests/enum_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,13 @@ enum GenericWithLifetimes<'a, 'b, X: 'a, Y: 'b> {
Foo(::core::marker::PhantomData<&'a X>),
Bar(::core::marker::PhantomData<&'b Y>),
}

#[derive(Clone, Copy, imp::TryFromBytes)]
struct A;

#[derive(imp::TryFromBytes)]
#[repr(C)]
enum B {
A(A),
A2 { a: A },
}
Comment on lines +271 to +280
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a UI test for the case where a field type is named ___ZerocopyAliasing? We want to be sure this doesn't regress (i.e., start compiling successfully).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that we can unblock Fuchsia users who are depending on this, I'm going to merge this w/o extra tests, but I added the following to the release roadmap to track (note the last bullet, which links to this comment thread and to #1725):

Screenshot 2024-09-23 at 4 24 38 PM

8 changes: 8 additions & 0 deletions zerocopy-derive/tests/struct_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,11 @@ fn packed_unsized() {
let converted = <CPackedUnsized as imp::TryFromBytes>::try_ref_from_bytes(candidate);
imp::assert!(converted.is_ok());
}

#[derive(imp::TryFromBytes)]
struct A;

#[derive(imp::TryFromBytes)]
struct B {
a: A,
}
8 changes: 8 additions & 0 deletions zerocopy-derive/tests/union_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,11 @@ where
}

util_assert_impl_all!(WithParams<'static, 'static, u8, 42>: imp::TryFromBytes);

#[derive(Clone, Copy, imp::TryFromBytes, imp::Immutable)]
struct A;

#[derive(imp::TryFromBytes)]
union B {
a: A,
}