Skip to content

Commit 9483f7d

Browse files
Suppress deprecation warnings in generated code (#2797)
* Suppress deprecation warnings in generated code Our derives now properly emit `#[allow(deprecated)]` in all locations where user code might be referenced, suppressing warnings when deriving traits on deprecated types. This change adds regression tests for `ByteHash`, `ByteEq`, and `SplitAt` (and implicitly `KnownLayout` via `SplitAt`) to ensure this behavior is preserved. It also fixes missing suppressions in `KnownLayout`'s internal helpers (`Field` impls and `__ZerocopyKnownLayoutMaybeUninit` struct). * Suppress deprecation warnings in generated code Our derives now properly emit `#[allow(deprecated)]` in all locations where user code might be referenced, suppressing warnings when deriving traits on deprecated types. This change adds regression tests for `ByteHash`, `ByteEq`, and `SplitAt` (and implicitly `KnownLayout` via `SplitAt`) to ensure this behavior is preserved. It also fixes missing suppressions in `KnownLayout`'s internal helpers (`Field` impls and `__ZerocopyKnownLayoutMaybeUninit` struct). * Suppress deprecation warnings in generated code Our derives now properly emit `#[allow(deprecated)]` in all locations where user code might be referenced, suppressing warnings when deriving traits on deprecated types. This change adds regression tests for `ByteHash`, `ByteEq`, and `SplitAt` (and implicitly `KnownLayout` via `SplitAt`) to ensure this behavior is preserved. It also fixes missing suppressions in `KnownLayout`'s internal helpers (`Field` impls and `__ZerocopyKnownLayoutMaybeUninit` struct). --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent c2e43ce commit 9483f7d

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

zerocopy-derive/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ fn derive_known_layout_inner(
337337

338338
let field_impls = field_indices.iter().zip(&fields).map(|(idx, (_, _, ty))| quote! {
339339
// SAFETY: `#ty` is the type of `#ident`'s field at `#idx`.
340+
#[allow(deprecated)]
340341
unsafe impl #impl_generics #zerocopy_crate::util::macro_util::Field<#idx> for #ident #ty_generics
341342
where
342343
#predicates
@@ -377,6 +378,7 @@ fn derive_known_layout_inner(
377378
// triggered when `derive(KnownLayout)` is applied to `repr(C)`
378379
// structs that are generated by macros. See #2177 for details.
379380
#[allow(private_bounds)]
381+
#[allow(deprecated)]
380382
#vis struct __ZerocopyKnownLayoutMaybeUninit<#params> (
381383
#(#zerocopy_crate::util::macro_util::core_reexport::mem::MaybeUninit<
382384
<#ident #ty_generics as
@@ -403,6 +405,7 @@ fn derive_known_layout_inner(
403405
// sound, since `__ZerocopyKnownLayoutMaybeUninit` is guaranteed
404406
// to have the same layout as the derive target type, except
405407
// that `__ZerocopyKnownLayoutMaybeUninit` admits uninit bytes.
408+
#[allow(deprecated)]
406409
unsafe impl #impl_generics #zerocopy_crate::KnownLayout for __ZerocopyKnownLayoutMaybeUninit #ty_generics
407410
where
408411
#trailing_field_ty: #zerocopy_crate::KnownLayout,
@@ -608,8 +611,6 @@ fn derive_hash_inner(
608611
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
609612
let where_predicates = where_clause.map(|clause| &clause.predicates);
610613
Ok(quote! {
611-
// FIXME(#553): Add a test that generates a warning when
612-
// `#[allow(deprecated)]` isn't present.
613614
#[allow(deprecated)]
614615
// While there are not currently any warnings that this suppresses (that
615616
// we're aware of), it's good future-proofing hygiene.
@@ -1840,8 +1841,6 @@ impl<'a, D: DataExt> ImplBlockBuilder<'a, D> {
18401841

18411842
let inner_extras = self.inner_extras;
18421843
let impl_tokens = quote! {
1843-
// FIXME(#553): Add a test that generates a warning when
1844-
// `#[allow(deprecated)]` isn't present.
18451844
#[allow(deprecated)]
18461845
// While there are not currently any warnings that this suppresses
18471846
// (that we're aware of), it's good future-proofing hygiene.

zerocopy-derive/src/output_tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,12 @@ fn test_known_layout() {
183183
struct __Zerocopy_Field_0;
184184
#[allow(non_camel_case_types)]
185185
struct __Zerocopy_Field_1;
186+
#[allow(deprecated)]
186187
unsafe impl<T, U> ::zerocopy::util::macro_util::Field<__Zerocopy_Field_0>
187188
for Foo<T, U> {
188189
type Type = T;
189190
}
191+
#[allow(deprecated)]
190192
unsafe impl<T, U> ::zerocopy::util::macro_util::Field<__Zerocopy_Field_1>
191193
for Foo<T, U> {
192194
type Type = U;
@@ -195,6 +197,7 @@ fn test_known_layout() {
195197
#[repr(align(2))]
196198
#[doc(hidden)]
197199
#[allow(private_bounds)]
200+
#[allow(deprecated)]
198201
struct __ZerocopyKnownLayoutMaybeUninit<T, U>(
199202
::zerocopy::util::macro_util::core_reexport::mem::MaybeUninit<
200203
<Foo<T, U> as ::zerocopy::util::macro_util::Field<__Zerocopy_Field_0>>::Type,
@@ -215,6 +218,7 @@ fn test_known_layout() {
215218
> as ::zerocopy::util::macro_util::Field<
216219
__Zerocopy_Field_1,
217220
>>::Type: ::zerocopy::KnownLayout;
221+
#[allow(deprecated)]
218222
unsafe impl<T, U> ::zerocopy::KnownLayout for __ZerocopyKnownLayoutMaybeUninit<T, U>
219223
where
220224
<Foo<

zerocopy-derive/tests/deprecated.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,80 @@ test!(Enum => #[repr(u8)] enum Enum { A, } => TryFromBytes, FromZeros, KnownLayo
4646
test!(Struct => #[repr(C)] struct Struct; => TryFromBytes, FromZeros, FromBytes, KnownLayout, Immutable, IntoBytes, Unaligned);
4747

4848
test!(Union => #[repr(C)] union Union{ a: (), } => TryFromBytes, FromZeros, FromBytes, KnownLayout, Immutable, IntoBytes, Unaligned);
49+
50+
// Tests for ByteHash and ByteEq which require IntoBytes + Immutable
51+
mod enum_hash_eq {
52+
mod ByteHash {
53+
use super::super::*;
54+
#[deprecated = "do not use"]
55+
#[derive(imp::ByteHash, imp::IntoBytes, imp::Immutable)]
56+
#[repr(u8)]
57+
enum Enum {
58+
A,
59+
}
60+
61+
#[allow(deprecated)]
62+
fn _allow_deprecated() {
63+
util_assert_impl_all!(Enum: ::core::hash::Hash);
64+
}
65+
}
66+
mod ByteEq {
67+
use super::super::*;
68+
#[deprecated = "do not use"]
69+
#[derive(imp::ByteEq, imp::IntoBytes, imp::Immutable)]
70+
#[repr(u8)]
71+
enum Enum {
72+
A,
73+
}
74+
75+
#[allow(deprecated)]
76+
fn _allow_deprecated() {
77+
util_assert_impl_all!(Enum: ::core::cmp::PartialEq, ::core::cmp::Eq);
78+
}
79+
}
80+
}
81+
82+
mod struct_hash_eq {
83+
mod ByteHash {
84+
use super::super::*;
85+
#[deprecated = "do not use"]
86+
#[derive(imp::ByteHash, imp::IntoBytes, imp::Immutable)]
87+
#[repr(C)]
88+
struct Struct;
89+
90+
#[allow(deprecated)]
91+
fn _allow_deprecated() {
92+
util_assert_impl_all!(Struct: ::core::hash::Hash);
93+
}
94+
}
95+
mod ByteEq {
96+
use super::super::*;
97+
#[deprecated = "do not use"]
98+
#[derive(imp::ByteEq, imp::IntoBytes, imp::Immutable)]
99+
#[repr(C)]
100+
struct Struct;
101+
102+
#[allow(deprecated)]
103+
fn _allow_deprecated() {
104+
util_assert_impl_all!(Struct: ::core::cmp::PartialEq, ::core::cmp::Eq);
105+
}
106+
}
107+
}
108+
109+
// Tests for SplitAt which requires repr(C) and at least one field
110+
mod split_at_test {
111+
mod SplitAt {
112+
use super::super::*;
113+
#[deprecated = "do not use"]
114+
#[derive(imp::SplitAt, imp::KnownLayout)]
115+
#[repr(C)]
116+
struct Struct {
117+
a: [u8],
118+
}
119+
120+
#[allow(deprecated)]
121+
fn _allow_deprecated() {
122+
util_assert_impl_all!(Struct: imp::SplitAt);
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)