Skip to content

Commit c358fd5

Browse files
authored
[derive] Avoid ambiguities referring to associated items (#2917)
Release 0.8.34. Closes #2915 gherrit-pr-id: Gc3a755f2c72ce9e1d064eccd1101a5c37acb211e
1 parent da40b0a commit c358fd5

26 files changed

+647
-122
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
[package]
1616
edition = "2021"
1717
name = "zerocopy"
18-
version = "0.8.33"
18+
version = "0.8.34"
1919
authors = [
2020
"Joshua Liebow-Feeser <[email protected]>",
2121
"Jack Wrenn <[email protected]>",
@@ -112,13 +112,13 @@ __internal_use_only_features_that_work_on_stable = [
112112
]
113113

114114
[dependencies]
115-
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive", optional = true }
115+
zerocopy-derive = { version = "=0.8.34", path = "zerocopy-derive", optional = true }
116116

117117
# The "associated proc macro pattern" ensures that the versions of zerocopy and
118118
# zerocopy-derive remain equal, even if the 'derive' feature isn't used.
119119
# See: https://github.com/matklad/macro-dep-test
120120
[target.'cfg(any())'.dependencies]
121-
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive" }
121+
zerocopy-derive = { version = "=0.8.34", path = "zerocopy-derive" }
122122

123123
[dev-dependencies]
124124
# FIXME(#381) Remove this dependency once we have our own layout gadgets.
@@ -134,4 +134,4 @@ testutil = { path = "testutil" }
134134
# CI test failures.
135135
trybuild = { version = "=1.0.89", features = ["diff"] }
136136
# In tests, unlike in production, zerocopy-derive is not optional
137-
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive" }
137+
zerocopy-derive = { version = "=0.8.34", path = "zerocopy-derive" }

zerocopy-derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
[package]
1010
edition = "2021"
1111
name = "zerocopy-derive"
12-
version = "0.8.33"
12+
version = "0.8.34"
1313
authors = ["Joshua Liebow-Feeser <[email protected]>", "Jack Wrenn <[email protected]>"]
1414
description = "Custom derive for traits from the zerocopy crate"
1515
license = "BSD-2-Clause OR Apache-2.0 OR MIT"

zerocopy-derive/src/derive/known_layout.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn derive_known_layout_for_repr_c_struct<'a>(
6969
#[inline(always)]
7070
fn raw_from_ptr_len(
7171
bytes: #core::ptr::NonNull<u8>,
72-
meta: Self::PointerMetadata,
72+
meta: <Self as #zerocopy_crate::KnownLayout>::PointerMetadata,
7373
) -> #core::ptr::NonNull<Self> {
7474
let trailing = <#trailing_field_ty as #zerocopy_crate::KnownLayout>::raw_from_ptr_len(bytes, meta);
7575
let slf = trailing.as_ptr() as *mut Self;
@@ -78,7 +78,7 @@ fn derive_known_layout_for_repr_c_struct<'a>(
7878
}
7979

8080
#[inline(always)]
81-
fn pointer_to_metadata(ptr: *mut Self) -> Self::PointerMetadata {
81+
fn pointer_to_metadata(ptr: *mut Self) -> <Self as #zerocopy_crate::KnownLayout>::PointerMetadata {
8282
<#trailing_field_ty>::pointer_to_metadata(ptr as *mut _)
8383
}
8484
}

zerocopy-derive/src/derive/try_from_bytes.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -229,24 +229,26 @@ pub(crate) fn derive_is_bit_valid(
229229
assert!(matches!(vis, syn::Visibility::Inherited));
230230
let variant_struct_field_index = Index::from(idx + 1);
231231
let (_, ty_generics, _) = ctx.ast.generics.split_for_impl();
232+
let has_field_trait = Trait::HasField {
233+
variant_id: parse_quote!({ #zerocopy_crate::ident_id!(#variant_ident) }),
234+
// Since Rust does not presently support explicit visibility
235+
// modifiers on enum fields, any public type is suitable here;
236+
// we use `()`.
237+
field: field.clone(),
238+
field_id: parse_quote!({ #zerocopy_crate::ident_id!(#ident) }),
239+
};
240+
let has_field_path = has_field_trait.crate_path(ctx);
232241
ImplBlockBuilder::new(
233242
ctx,
234243
data,
235-
Trait::HasField {
236-
variant_id: parse_quote!({ #zerocopy_crate::ident_id!(#variant_ident) }),
237-
// Since Rust does not presently support explicit visibility
238-
// modifiers on enum fields, any public type is suitable
239-
// here; we use `()`.
240-
field: field.clone(),
241-
field_id: parse_quote!({ #zerocopy_crate::ident_id!(#ident) }),
242-
},
244+
has_field_trait,
243245
FieldBounds::None,
244246
)
245247
.inner_extras(quote! {
246248
type Type = #ty;
247249

248250
#[inline(always)]
249-
fn project(slf: #zerocopy_crate::pointer::PtrInner<'_, Self>) -> *mut Self::Type {
251+
fn project(slf: #zerocopy_crate::pointer::PtrInner<'_, Self>) -> *mut <Self as #has_field_path>::Type {
250252
use #zerocopy_crate::pointer::cast::{CastSized, Projection};
251253

252254
slf.project::<___ZerocopyRawEnum #ty_generics, CastSized>()
@@ -359,7 +361,7 @@ pub(crate) fn derive_is_bit_valid(
359361
type Type = ___ZerocopyTag;
360362

361363
#[inline(always)]
362-
fn project(slf: #zerocopy_crate::pointer::PtrInner<'_, Self>) -> *mut Self::Type {
364+
fn project(slf: #zerocopy_crate::pointer::PtrInner<'_, Self>) -> *mut <Self as #zerocopy_crate::HasField<(), { #zerocopy_crate::STRUCT_VARIANT_ID }, { #zerocopy_crate::ident_id!(tag) }>>::Type {
363365
slf.as_ptr().cast()
364366
}
365367
}
@@ -437,31 +439,33 @@ fn derive_has_field_struct_union(ctx: &Ctx, data: &dyn DataExt) -> TokenStream {
437439
let field_token = ident!(("ẕ{}", ident), ident.span());
438440
let field: Box<Type> = parse_quote!(#field_token);
439441
let field_id: Box<Expr> = parse_quote!({ #zerocopy_crate::ident_id!(#ident) });
440-
ImplBlockBuilder::new(
441-
ctx,
442-
data,
443-
Trait::HasField {
442+
let has_field_trait = Trait::HasField {
444443
variant_id: variant_id.clone(),
445444
field: field.clone(),
446445
field_id: field_id.clone(),
447-
},
448-
FieldBounds::None,
449-
)
450-
.inner_extras(quote! {
451-
type Type = #ty;
452-
453-
#[inline(always)]
454-
fn project(slf: #zerocopy_crate::pointer::PtrInner<'_, Self>) -> *mut Self::Type {
455-
let slf = slf.as_ptr();
456-
// SAFETY: By invariant on `PtrInner`, `slf` is a non-null
457-
// pointer whose referent is zero-sized or lives in a valid
458-
// allocation. Since `#ident` is a struct or union field of
459-
// `Self`, this projection preserves or shrinks the referent
460-
// size, and so the resulting referent also fits in the same
461-
// allocation.
462-
unsafe { #core::ptr::addr_of_mut!((*slf).#ident) }
463-
}
464-
}).outer_extras(if is_repr_c_union {
446+
};
447+
let has_field_path = has_field_trait.crate_path(ctx);
448+
ImplBlockBuilder::new(
449+
ctx,
450+
data,
451+
has_field_trait,
452+
FieldBounds::None,
453+
)
454+
.inner_extras(quote! {
455+
type Type = #ty;
456+
457+
#[inline(always)]
458+
fn project(slf: #zerocopy_crate::pointer::PtrInner<'_, Self>) -> *mut <Self as #has_field_path>::Type {
459+
let slf = slf.as_ptr();
460+
// SAFETY: By invariant on `PtrInner`, `slf` is a non-null
461+
// pointer whose referent is zero-sized or lives in a valid
462+
// allocation. Since `#ident` is a struct or union field of
463+
// `Self`, this projection preserves or shrinks the referent
464+
// size, and so the resulting referent also fits in the same
465+
// allocation.
466+
unsafe { #core::ptr::addr_of_mut!((*slf).#ident) }
467+
}
468+
}).outer_extras(if is_repr_c_union {
465469
let ident = &ctx.ast.ident;
466470
let (impl_generics, ty_generics, where_clause) = ctx.ast.generics.split_for_impl();
467471
quote! {

zerocopy-derive/src/output_tests/expected/from_bytes_enum.expected.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
non_ascii_idents,
99
clippy::missing_inline_in_public_items,
1010
)]
11+
#[deny(ambiguous_associated_items)]
1112
#[automatically_derived]
1213
const _: () = {
1314
unsafe impl ::zerocopy::TryFromBytes for Foo {
@@ -40,6 +41,7 @@ const _: () = {
4041
non_ascii_idents,
4142
clippy::missing_inline_in_public_items,
4243
)]
44+
#[deny(ambiguous_associated_items)]
4345
#[automatically_derived]
4446
const _: () = {
4547
unsafe impl ::zerocopy::FromZeros for Foo {
@@ -56,6 +58,7 @@ const _: () = {
5658
non_ascii_idents,
5759
clippy::missing_inline_in_public_items,
5860
)]
61+
#[deny(ambiguous_associated_items)]
5962
#[automatically_derived]
6063
const _: () = {
6164
unsafe impl ::zerocopy::FromBytes for Foo {

zerocopy-derive/src/output_tests/expected/from_bytes_struct.expected.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
non_ascii_idents,
99
clippy::missing_inline_in_public_items,
1010
)]
11+
#[deny(ambiguous_associated_items)]
1112
#[automatically_derived]
1213
const _: () = {
1314
unsafe impl ::zerocopy::TryFromBytes for Foo {
@@ -40,6 +41,7 @@ const _: () = {
4041
non_ascii_idents,
4142
clippy::missing_inline_in_public_items,
4243
)]
44+
#[deny(ambiguous_associated_items)]
4345
#[automatically_derived]
4446
const _: () = {
4547
unsafe impl ::zerocopy::FromZeros for Foo {
@@ -56,6 +58,7 @@ const _: () = {
5658
non_ascii_idents,
5759
clippy::missing_inline_in_public_items,
5860
)]
61+
#[deny(ambiguous_associated_items)]
5962
#[automatically_derived]
6063
const _: () = {
6164
unsafe impl ::zerocopy::FromBytes for Foo {

zerocopy-derive/src/output_tests/expected/from_bytes_union.expected.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
non_ascii_idents,
99
clippy::missing_inline_in_public_items,
1010
)]
11+
#[deny(ambiguous_associated_items)]
1112
#[automatically_derived]
1213
const _: () = {
1314
unsafe impl ::zerocopy::TryFromBytes for Foo
@@ -42,6 +43,7 @@ const _: () = {
4243
non_ascii_idents,
4344
clippy::missing_inline_in_public_items,
4445
)]
46+
#[deny(ambiguous_associated_items)]
4547
#[automatically_derived]
4648
const _: () = {
4749
enum ẕa {}
@@ -55,6 +57,7 @@ const _: () = {
5557
non_ascii_idents,
5658
clippy::missing_inline_in_public_items,
5759
)]
60+
#[deny(ambiguous_associated_items)]
5861
#[automatically_derived]
5962
const _: () = {
6063
unsafe impl ::zerocopy::HasField<
@@ -67,7 +70,11 @@ const _: () = {
6770
#[inline(always)]
6871
fn project(
6972
slf: ::zerocopy::pointer::PtrInner<'_, Self>,
70-
) -> *mut Self::Type {
73+
) -> *mut <Self as ::zerocopy::HasField<
74+
ẕa,
75+
{ ::zerocopy::UNION_VARIANT_ID },
76+
{ ::zerocopy::ident_id!(a) },
77+
>>::Type {
7178
let slf = slf.as_ptr();
7279
unsafe {
7380
::zerocopy::util::macro_util::core_reexport::ptr::addr_of_mut!(
@@ -89,6 +96,7 @@ const _: () = {
8996
non_ascii_idents,
9097
clippy::missing_inline_in_public_items,
9198
)]
99+
#[deny(ambiguous_associated_items)]
92100
#[automatically_derived]
93101
const _: () = {
94102
unsafe impl ::zerocopy::FromZeros for Foo
@@ -108,6 +116,7 @@ const _: () = {
108116
non_ascii_idents,
109117
clippy::missing_inline_in_public_items,
110118
)]
119+
#[deny(ambiguous_associated_items)]
111120
#[automatically_derived]
112121
const _: () = {
113122
unsafe impl ::zerocopy::FromBytes for Foo

zerocopy-derive/src/output_tests/expected/from_zeros.expected.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
non_ascii_idents,
99
clippy::missing_inline_in_public_items,
1010
)]
11+
#[deny(ambiguous_associated_items)]
1112
#[automatically_derived]
1213
const _: () = {
1314
unsafe impl ::zerocopy::TryFromBytes for Foo {
@@ -32,6 +33,7 @@ const _: () = {
3233
non_ascii_idents,
3334
clippy::missing_inline_in_public_items,
3435
)]
36+
#[deny(ambiguous_associated_items)]
3537
#[automatically_derived]
3638
const _: () = {
3739
unsafe impl ::zerocopy::FromZeros for Foo {

zerocopy-derive/src/output_tests/expected/immutable.expected.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
non_ascii_idents,
99
clippy::missing_inline_in_public_items,
1010
)]
11+
#[deny(ambiguous_associated_items)]
1112
#[automatically_derived]
1213
const _: () = {
1314
unsafe impl ::zerocopy::Immutable for Foo {

zerocopy-derive/src/output_tests/expected/into_bytes_enum.expected.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
non_ascii_idents,
99
clippy::missing_inline_in_public_items,
1010
)]
11+
#[deny(ambiguous_associated_items)]
1112
#[automatically_derived]
1213
const _: () = {
1314
unsafe impl ::zerocopy::IntoBytes for Foo {

0 commit comments

Comments
 (0)