Skip to content

Commit 322e9ac

Browse files
committed
[derive] Simplify code, remove obsolete features
Clean up the implementation, especially in `fn impl_block`. Make the following notable changes: - Previously, `syn` didn't support parsing macro invocations in const generics without the `full` feature enabled. To avoid the compile-time overhead of that feature, we worked around it by constructing AST nodes manually. `syn` has since added support for this without requiring the `full` feature, so we make use of it. - We used to need to split types into those that transatively depended upon type generics (like `[T; 2]`) and those that didn't (like `[u8; 2]`). We made a change in #119 that made this distinction irrelevant, but we never removed the code to perform the split. In this commit, we remove that code. That code was the only reason we needed to enable `syn`'s `visit` feature, so we are also able to remove that feature dependency.
1 parent e22c6d0 commit 322e9ac

File tree

4 files changed

+208
-200
lines changed

4 files changed

+208
-200
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
error[E0277]: the trait bound `T: zerocopy::FromZeroes` is not satisfied
2+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:22:37
3+
|
4+
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
5+
| ^^^^^^ the trait `zerocopy::FromZeroes` is not implemented for `T`
6+
|
7+
note: required for `Foo<T>` to implement `zerocopy::FromZeroes`
8+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:10
9+
|
10+
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
11+
| ^^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
12+
note: required by a bound in `_::Subtrait`
13+
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
14+
|
15+
| trait Subtrait: $trait {}
16+
| ^^^^^^ required by this bound in `Subtrait`
17+
|
18+
::: tests/ui-stable/invalid-impls/invalid-impls.rs:22:1
19+
|
20+
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
21+
| ------------------------------------------- in this macro invocation
22+
= note: this error originates in the derive macro `FromZeroes` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
help: consider restricting type parameter `T`
24+
|
25+
22 | impl_or_verify!(T: zerocopy::FromZeroes => FromZeroes for Foo<T>);
26+
| ++++++++++++++++++++++
27+
28+
error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
29+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:23:36
30+
|
31+
23 | impl_or_verify!(T => FromBytes for Foo<T>);
32+
| ^^^^^^ the trait `zerocopy::FromBytes` is not implemented for `T`
33+
|
34+
note: required for `Foo<T>` to implement `zerocopy::FromBytes`
35+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:22
36+
|
37+
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
38+
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
39+
note: required by a bound in `_::Subtrait`
40+
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
41+
|
42+
| trait Subtrait: $trait {}
43+
| ^^^^^^ required by this bound in `Subtrait`
44+
|
45+
::: tests/ui-stable/invalid-impls/invalid-impls.rs:23:1
46+
|
47+
23 | impl_or_verify!(T => FromBytes for Foo<T>);
48+
| ------------------------------------------ in this macro invocation
49+
= note: this error originates in the derive macro `FromBytes` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
50+
help: consider restricting type parameter `T`
51+
|
52+
23 | impl_or_verify!(T: zerocopy::FromBytes => FromBytes for Foo<T>);
53+
| +++++++++++++++++++++
54+
55+
error[E0277]: the trait bound `T: zerocopy::AsBytes` is not satisfied
56+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:24:34
57+
|
58+
24 | impl_or_verify!(T => AsBytes for Foo<T>);
59+
| ^^^^^^ the trait `zerocopy::AsBytes` is not implemented for `T`
60+
|
61+
note: required for `Foo<T>` to implement `zerocopy::AsBytes`
62+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:33
63+
|
64+
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
65+
| ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
66+
note: required by a bound in `_::Subtrait`
67+
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
68+
|
69+
| trait Subtrait: $trait {}
70+
| ^^^^^^ required by this bound in `Subtrait`
71+
|
72+
::: tests/ui-stable/invalid-impls/invalid-impls.rs:24:1
73+
|
74+
24 | impl_or_verify!(T => AsBytes for Foo<T>);
75+
| ---------------------------------------- in this macro invocation
76+
= note: this error originates in the derive macro `AsBytes` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
77+
help: consider restricting type parameter `T`
78+
|
79+
24 | impl_or_verify!(T: zerocopy::AsBytes => AsBytes for Foo<T>);
80+
| +++++++++++++++++++
81+
82+
error[E0277]: the trait bound `T: zerocopy::Unaligned` is not satisfied
83+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:25:36
84+
|
85+
25 | impl_or_verify!(T => Unaligned for Foo<T>);
86+
| ^^^^^^ the trait `zerocopy::Unaligned` is not implemented for `T`
87+
|
88+
note: required for `Foo<T>` to implement `zerocopy::Unaligned`
89+
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:42
90+
|
91+
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
92+
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
93+
note: required by a bound in `_::Subtrait`
94+
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
95+
|
96+
| trait Subtrait: $trait {}
97+
| ^^^^^^ required by this bound in `Subtrait`
98+
|
99+
::: tests/ui-stable/invalid-impls/invalid-impls.rs:25:1
100+
|
101+
25 | impl_or_verify!(T => Unaligned for Foo<T>);
102+
| ------------------------------------------ in this macro invocation
103+
= note: this error originates in the derive macro `Unaligned` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
104+
help: consider restricting type parameter `T`
105+
|
106+
25 | impl_or_verify!(T: zerocopy::Unaligned => Unaligned for Foo<T>);
107+
| +++++++++++++++++++++

zerocopy-derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ proc-macro = true
2020
[dependencies]
2121
proc-macro2 = "1.0.1"
2222
quote = "1.0.10"
23-
syn = { version = "2", features = ["visit"] }
23+
syn = "2.0.31"
2424

2525
[dev-dependencies]
2626
rustversion = "1.0"

zerocopy-derive/src/ext.rs

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
use syn::{Data, DataEnum, DataStruct, DataUnion, Field, Fields, Type};
5+
use syn::{Data, DataEnum, DataStruct, DataUnion, Type};
66

77
pub trait DataExt {
8-
fn nested_types(&self) -> Vec<&Type>;
8+
/// Extract the types of all fields. For enums, extract the types of fields
9+
/// from each variant.
10+
fn field_types(&self) -> Vec<&Type>;
911
}
1012

1113
impl DataExt for Data {
12-
fn nested_types(&self) -> Vec<&Type> {
14+
fn field_types(&self) -> Vec<&Type> {
1315
match self {
14-
Data::Struct(strc) => strc.nested_types(),
15-
Data::Enum(enm) => enm.nested_types(),
16-
Data::Union(un) => un.nested_types(),
16+
Data::Struct(strc) => strc.field_types(),
17+
Data::Enum(enm) => enm.field_types(),
18+
Data::Union(un) => un.field_types(),
1719
}
1820
}
1921
}
2022

2123
impl DataExt for DataStruct {
22-
fn nested_types(&self) -> Vec<&Type> {
23-
fields_to_types(&self.fields)
24+
fn field_types(&self) -> Vec<&Type> {
25+
self.fields.iter().map(|f| &f.ty).collect()
2426
}
2527
}
2628

2729
impl DataExt for DataEnum {
28-
fn nested_types(&self) -> Vec<&Type> {
29-
self.variants.iter().map(|var| fields_to_types(&var.fields)).fold(Vec::new(), |mut a, b| {
30-
a.extend(b);
31-
a
32-
})
30+
fn field_types(&self) -> Vec<&Type> {
31+
self.variants.iter().map(|var| &var.fields).flatten().map(|f| &f.ty).collect()
32+
}
33+
}
34+
35+
impl DataExt for DataUnion {
36+
fn field_types(&self) -> Vec<&Type> {
37+
self.fields.named.iter().map(|f| &f.ty).collect()
3338
}
3439
}
3540

@@ -39,24 +44,6 @@ pub trait EnumExt {
3944

4045
impl EnumExt for DataEnum {
4146
fn is_c_like(&self) -> bool {
42-
self.nested_types().is_empty()
47+
self.field_types().is_empty()
4348
}
4449
}
45-
46-
impl DataExt for DataUnion {
47-
fn nested_types(&self) -> Vec<&Type> {
48-
field_iter_to_types(&self.fields.named)
49-
}
50-
}
51-
52-
fn fields_to_types(fields: &Fields) -> Vec<&Type> {
53-
match fields {
54-
Fields::Named(named) => field_iter_to_types(&named.named),
55-
Fields::Unnamed(unnamed) => field_iter_to_types(&unnamed.unnamed),
56-
Fields::Unit => Vec::new(),
57-
}
58-
}
59-
60-
fn field_iter_to_types<'a, I: IntoIterator<Item = &'a Field>>(fields: I) -> Vec<&'a Type> {
61-
fields.into_iter().map(|f| &f.ty).collect()
62-
}

0 commit comments

Comments
 (0)