Skip to content

Commit 20ce37d

Browse files
committed
[derive] Improve hygiene testing
In zerocopy-derive tests, import `zerocopy` as `zerocopy_renamed` so that references to `::zerocopy` fail to resolve. This ensures that no generated code hard-codes `zerocopy` or `::zerocopy`, and instead all references make use of the provided custom crate name. gherrit-pr-id: G3cb376b3f2d996151636f7aebb3a33f4134162e6
1 parent f905feb commit 20ce37d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1698
-1534
lines changed

zerocopy-derive/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ testutil = { path = "../testutil" }
5555
# sometimes change the output format slightly, so a version mismatch can cause
5656
# CI test failures.
5757
trybuild = { version = "=1.0.89", features = ["diff"] }
58-
zerocopy = { path = "../", features = ["derive"] }
58+
# We import as `zerocopy-renamed` so that we can refer to the crate as
59+
# `zerocopy-renamed` in the generated code, which allows us to test that the
60+
# `crate` attribute works correctly, and ensures that we never accidentally
61+
# hard-code the name `zerocopy` in the generated code.
62+
zerocopy-renamed = { package = "zerocopy", path = "../", features = ["derive"] }

zerocopy-derive/src/enum.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use crate::{
2121
/// Generates a tag enum for the given enum. This generates an enum with the
2222
/// same non-align `repr`s, variants, and corresponding discriminants, but none
2323
/// of the fields.
24-
pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream {
24+
pub(crate) fn generate_tag_enum(ctx: &Ctx, repr: &EnumRepr, data: &DataEnum) -> TokenStream {
25+
let zerocopy_crate = &ctx.zerocopy_crate;
2526
let variants = data.variants.iter().map(|v| {
2627
let ident = &v.ident;
2728
if let Some((eq, discriminant)) = &v.discriminant {
@@ -48,7 +49,7 @@ pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream
4849

4950
// SAFETY: `___ZerocopyTag` has no fields, and so it does not permit
5051
// interior mutation.
51-
unsafe impl ::zerocopy::Immutable for ___ZerocopyTag {
52+
unsafe impl #zerocopy_crate::Immutable for ___ZerocopyTag {
5253
fn only_derive_is_allowed_to_implement_this_trait() {}
5354
}
5455
}
@@ -240,7 +241,7 @@ pub(crate) fn derive_is_bit_valid(
240241
repr: &EnumRepr,
241242
) -> Result<TokenStream, Error> {
242243
let trait_path = Trait::TryFromBytes.crate_path(&ctx.zerocopy_crate);
243-
let tag_enum = generate_tag_enum(repr, data);
244+
let tag_enum = generate_tag_enum(ctx, repr, data);
244245
let tag_consts = generate_tag_consts(data);
245246

246247
let (outer_tag_type, inner_tag_type) = if repr.is_c() {

zerocopy-derive/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,13 +651,14 @@ fn derive_split_at_inner(ctx: &Ctx, _top_level: Trait) -> Result<TokenStream, Er
651651
return Err(Error::new(Span::call_site(), "must at least one field"));
652652
};
653653

654+
let zerocopy_crate = &ctx.zerocopy_crate;
654655
// SAFETY: `#ty`, per the above checks, is `repr(C)` or `repr(transparent)`
655656
// and is not packed; its trailing field is guaranteed to be well-aligned
656657
// for its type. By invariant on `FieldBounds::TRAILING_SELF`, the trailing
657658
// slice of the trailing field is also well-aligned for its type.
658659
Ok(ImplBlockBuilder::new(ctx, &ctx.ast.data, Trait::SplitAt, FieldBounds::TRAILING_SELF)
659660
.inner_extras(quote! {
660-
type Elem = <#trailing_field as ::zerocopy::SplitAt>::Elem;
661+
type Elem = <#trailing_field as #zerocopy_crate::SplitAt>::Elem;
661662
})
662663
.build())
663664
}
@@ -1219,7 +1220,7 @@ fn derive_into_bytes_enum(ctx: &Ctx, enm: &DataEnum) -> Result<TokenStream, Erro
12191220
));
12201221
}
12211222

1222-
let tag_type_definition = r#enum::generate_tag_enum(&repr, enm);
1223+
let tag_type_definition = r#enum::generate_tag_enum(ctx, &repr, enm);
12231224
Ok(ImplBlockBuilder::new(ctx, enm, Trait::IntoBytes, FieldBounds::ALL_SELF)
12241225
.padding_check(PaddingCheck::Enum { tag_type_definition })
12251226
.build())

zerocopy-derive/src/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ impl Ctx {
3131
if meta.path.is_ident("crate") {
3232
let expr = meta.value().and_then(|value| value.parse());
3333
if let Ok(Expr::Lit(ExprLit { lit: Lit::Str(lit), .. })) = expr {
34-
if let Ok(path_lit) = lit.parse() {
35-
path = path_lit;
34+
if let Ok(path_lit) = lit.parse::<Ident>() {
35+
path = parse_quote!(::#path_lit);
3636
return Ok(());
3737
}
3838
}

zerocopy-derive/tests/crate_path.rs

Lines changed: 0 additions & 189 deletions
This file was deleted.

zerocopy-derive/tests/deprecated.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ macro_rules! test {
2626

2727
#[deprecated = "do not use"]
2828
#[derive(imp::$trait)]
29+
#[zerocopy(crate = "zerocopy_renamed")]
2930
$ty
3031

3132
#[allow(deprecated)]
@@ -53,6 +54,7 @@ mod enum_hash_eq {
5354
use super::super::*;
5455
#[deprecated = "do not use"]
5556
#[derive(imp::ByteHash, imp::IntoBytes, imp::Immutable)]
57+
#[zerocopy(crate = "zerocopy_renamed")]
5658
#[repr(u8)]
5759
enum Enum {
5860
A,
@@ -67,6 +69,7 @@ mod enum_hash_eq {
6769
use super::super::*;
6870
#[deprecated = "do not use"]
6971
#[derive(imp::ByteEq, imp::IntoBytes, imp::Immutable)]
72+
#[zerocopy(crate = "zerocopy_renamed")]
7073
#[repr(u8)]
7174
enum Enum {
7275
A,
@@ -84,6 +87,7 @@ mod struct_hash_eq {
8487
use super::super::*;
8588
#[deprecated = "do not use"]
8689
#[derive(imp::ByteHash, imp::IntoBytes, imp::Immutable)]
90+
#[zerocopy(crate = "zerocopy_renamed")]
8791
#[repr(C)]
8892
struct Struct;
8993

@@ -96,6 +100,7 @@ mod struct_hash_eq {
96100
use super::super::*;
97101
#[deprecated = "do not use"]
98102
#[derive(imp::ByteEq, imp::IntoBytes, imp::Immutable)]
103+
#[zerocopy(crate = "zerocopy_renamed")]
99104
#[repr(C)]
100105
struct Struct;
101106

@@ -112,6 +117,7 @@ mod split_at_test {
112117
use super::super::*;
113118
#[deprecated = "do not use"]
114119
#[derive(imp::SplitAt, imp::KnownLayout)]
120+
#[zerocopy(crate = "zerocopy_renamed")]
115121
#[repr(C)]
116122
struct Struct {
117123
a: [u8],

zerocopy-derive/tests/enum_from_bytes.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ fn test_trivial_is_bit_valid() {
4848
// Make sure no deprecation warning is generated from our derive (see #553).
4949
#[deprecated = "do not use"]
5050
#[derive(imp::FromBytes)]
51+
#[zerocopy(crate = "zerocopy_renamed")]
5152
#[repr(u8)]
5253
enum FooU8 {
5354
Variant0,
@@ -309,6 +310,7 @@ enum FooU8 {
309310
}
310311

311312
#[derive(imp::FromBytes)]
313+
#[zerocopy(crate = "zerocopy_renamed")]
312314
#[repr(u8)]
313315
enum FooU8WithData {
314316
Variant0(u32),
@@ -575,6 +577,7 @@ fn _allow_deprecated() {
575577
}
576578

577579
#[derive(imp::FromBytes)]
580+
#[zerocopy(crate = "zerocopy_renamed")]
578581
#[repr(i8)]
579582
enum FooI8 {
580583
Variant0,
@@ -838,6 +841,7 @@ enum FooI8 {
838841
util_assert_impl_all!(FooI8: imp::FromBytes);
839842

840843
#[derive(imp::FromBytes)]
844+
#[zerocopy(crate = "zerocopy_renamed")]
841845
#[repr(u8, align(2))]
842846
enum FooU8Align {
843847
Variant0,
@@ -1101,6 +1105,7 @@ enum FooU8Align {
11011105
util_assert_impl_all!(FooU8Align: imp::FromBytes);
11021106

11031107
#[derive(imp::FromBytes)]
1108+
#[zerocopy(crate = "zerocopy_renamed")]
11041109
#[repr(i8, align(2))]
11051110
enum FooI8Align {
11061111
Variant0,
@@ -1364,6 +1369,7 @@ enum FooI8Align {
13641369
util_assert_impl_all!(FooI8Align: imp::FromBytes);
13651370

13661371
#[derive(imp::FromBytes)]
1372+
#[zerocopy(crate = "zerocopy_renamed")]
13671373
#[repr(u16)]
13681374
enum FooU16 {
13691375
Variant0,
@@ -66907,6 +66913,7 @@ enum FooU16 {
6690766913
util_assert_impl_all!(FooU16: imp::FromBytes);
6690866914

6690966915
#[derive(imp::FromBytes)]
66916+
#[zerocopy(crate = "zerocopy_renamed")]
6691066917
#[repr(i16)]
6691166918
enum FooI16 {
6691266919
Variant0,

0 commit comments

Comments
 (0)