Skip to content

Commit 529df1e

Browse files
committed
[zerocopy-derive] Fix panic with raw identifiers
Release 0.8.33. Applies the same approach as in #2788, but to new lines of code (that should have used this technique to begin with). Introduces a macro that makes this pattern less verbose, and also hopefully encourages future uses to use the correct pattern. gherrit-pr-id: Gc07a814b14897f1beb00786c47a1343e65c9b884
1 parent 8cd798a commit 529df1e

5 files changed

Lines changed: 26 additions & 17 deletions

File tree

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.32"
18+
version = "0.8.33"
1919
authors = [
2020
"Joshua Liebow-Feeser <joshlf@google.com>",
2121
"Jack Wrenn <jswrenn@amazon.com>",
@@ -112,13 +112,13 @@ __internal_use_only_features_that_work_on_stable = [
112112
]
113113

114114
[dependencies]
115-
zerocopy-derive = { version = "=0.8.32", path = "zerocopy-derive", optional = true }
115+
zerocopy-derive = { version = "=0.8.33", 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.32", path = "zerocopy-derive" }
121+
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive" }
122122

123123
[dev-dependencies]
124124
# More recent versions of `either` have an MSRV higher than ours.
@@ -142,4 +142,4 @@ testutil = { path = "testutil" }
142142
# CI test failures.
143143
trybuild = { version = "=1.0.89", features = ["diff"] }
144144
# In tests, unlike in production, zerocopy-derive is not optional
145-
zerocopy-derive = { version = "=0.8.32", path = "zerocopy-derive" }
145+
zerocopy-derive = { version = "=0.8.33", 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.32"
12+
version = "0.8.33"
1313
authors = ["Joshua Liebow-Feeser <joshlf@google.com>", "Jack Wrenn <jswrenn@amazon.com>"]
1414
description = "Custom derive for traits from the zerocopy crate"
1515
license = "BSD-2-Clause OR Apache-2.0 OR MIT"

zerocopy-derive/src/enum.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream
5555
}
5656

5757
fn tag_ident(variant_ident: &Ident) -> Ident {
58-
let variant_ident_str = crate::ext::to_ident_str(variant_ident);
59-
Ident::new(&format!("___ZEROCOPY_TAG_{}", variant_ident_str), variant_ident.span())
58+
ident!(("___ZEROCOPY_TAG_{}", variant_ident), variant_ident.span())
6059
}
6160

6261
/// Generates a constant for the tag associated with each variant of the enum.
@@ -103,8 +102,7 @@ fn generate_tag_consts(data: &DataEnum) -> TokenStream {
103102
}
104103

105104
fn variant_struct_ident(variant_ident: &Ident) -> Ident {
106-
let variant_ident_str = crate::ext::to_ident_str(variant_ident);
107-
Ident::new(&format!("___ZerocopyVariantStruct_{}", variant_ident_str), variant_ident.span())
105+
ident!(("___ZerocopyVariantStruct_{}", variant_ident), variant_ident.span())
108106
}
109107

110108
/// Generates variant structs for the given enum variant.
@@ -171,10 +169,9 @@ fn generate_variant_structs(
171169
}
172170

173171
fn variants_union_field_ident(ident: &Ident) -> Ident {
174-
let variant_ident_str = crate::ext::to_ident_str(ident);
175172
// Field names are prefixed with `__field_` to prevent name collision
176173
// with the `__nonempty` field.
177-
Ident::new(&format!("__field_{}", variant_ident_str), ident.span())
174+
ident!(("__field_{}", ident), ident.span())
178175
}
179176

180177
fn generate_variants_union(

zerocopy-derive/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
)]
3838
#![recursion_limit = "128"]
3939

40+
macro_rules! ident {
41+
(($fmt:literal $(, $arg:expr)*), $span:expr) => {
42+
syn::Ident::new(&format!($fmt $(, crate::ext::to_ident_str($arg))*), $span)
43+
};
44+
}
45+
4046
mod r#enum;
4147
mod ext;
4248
#[cfg(test)]
@@ -322,10 +328,8 @@ fn derive_known_layout_inner(
322328

323329
// Generate a valid ident for a type-level handle to a field of a
324330
// given `name`.
325-
let field_index = |name: &TokenStream| {
326-
let name = to_ident_str(name);
327-
Ident::new(&format!("__Zerocopy_Field_{}", name), ident.span())
328-
};
331+
let field_index =
332+
|name: &TokenStream| ident!(("__Zerocopy_Field_{}", name), ident.span());
329333

330334
let field_indices: Vec<_> =
331335
fields.iter().map(|(_vis, name, _ty)| field_index(name)).collect();
@@ -764,7 +768,7 @@ fn derive_has_field_struct_union(
764768
}
765769

766770
let field_tokens = fields.iter().map(|(vis, ident, _)| {
767-
let ident = Ident::new(&format!("ẕ{}", ident), ident.span());
771+
let ident = ident!(("ẕ{}", ident), ident.span());
768772
quote!(
769773
#vis enum #ident {}
770774
)
@@ -783,7 +787,7 @@ fn derive_has_field_struct_union(
783787
Data::Enum(..) | Data::Struct(..) => false,
784788
};
785789
let has_fields = fields.iter().map(move |(_, ident, ty)| {
786-
let field_token = Ident::new(&format!("ẕ{}", ident), ident.span());
790+
let field_token = ident!(("ẕ{}", ident), ident.span());
787791
let field: Box<Type> = parse_quote!(#field_token);
788792
let field_id: Box<Expr> = parse_quote!({ #zerocopy_crate::ident_id!(#ident) });
789793
ImplBlockBuilder::new(

zerocopy-derive/tests/struct_try_from_bytes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,11 @@ struct A;
249249
struct B {
250250
a: A,
251251
}
252+
253+
#[derive(imp::TryFromBytes)]
254+
#[repr(C)]
255+
struct RawType {
256+
r#type: u8,
257+
}
258+
259+
util_assert_impl_all!(RawType: imp::TryFromBytes);

0 commit comments

Comments
 (0)