Skip to content

Commit 4479318

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 4479318

File tree

291 files changed

+2937
-2814
lines changed

Some content is hidden

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

291 files changed

+2937
-2814
lines changed

tests/include.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
extern crate zerocopy;
10+
11+
#[allow(unused)]
12+
#[cfg(feature = "derive")]
13+
mod util {
14+
/// A type that doesn't implement any zerocopy traits.
15+
pub struct NotZerocopy<T = ()>(pub T);
16+
17+
/// A `u16` with alignment 2.
18+
///
19+
/// Though `u16` has alignment 2 on some platforms, it's not guaranteed. By
20+
/// contrast, `util::AU16` is guaranteed to have alignment 2.
21+
#[derive(
22+
zerocopy::KnownLayout,
23+
zerocopy::Immutable,
24+
zerocopy::FromBytes,
25+
zerocopy::IntoBytes,
26+
Copy,
27+
Clone,
28+
)]
29+
#[repr(C, align(2))]
30+
pub struct AU16(pub u16);
31+
32+
// Since we can't import these by path (ie, `util::assert_impl_all!`), use a
33+
// name prefix to ensure our derive-emitted code isn't accidentally relying
34+
// on `assert_impl_all!` being in scope.
35+
#[macro_export]
36+
macro_rules! util_assert_impl_all {
37+
($type:ty: $($trait:path),+ $(,)?) => {
38+
const _: fn() = || {
39+
use ::core::prelude::v1::*;
40+
::static_assertions::assert_impl_all!($type: $($trait),+);
41+
};
42+
};
43+
}
44+
45+
// Since we can't import these by path (ie, `util::assert_not_impl_any!`),
46+
// use a name prefix to ensure our derive-emitted code isn't accidentally
47+
// relying on `assert_not_impl_any!` being in scope.
48+
#[macro_export]
49+
macro_rules! util_assert_not_impl_any {
50+
($x:ty: $($t:path),+ $(,)?) => {
51+
const _: fn() = || {
52+
use ::core::prelude::v1::*;
53+
::static_assertions::assert_not_impl_any!($x: $($t),+);
54+
};
55+
};
56+
}
57+
58+
#[macro_export]
59+
macro_rules! test_trivial_is_bit_valid {
60+
($x:ty => $name:ident) => {
61+
#[test]
62+
fn $name() {
63+
util::test_trivial_is_bit_valid::<$x>();
64+
}
65+
};
66+
}
67+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `NotZerocopy: zerocopy::FromBytes` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-from-bytes.rs:18:24
1+
error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied
2+
--> tests/ui-msrv/diagnostic-not-implemented-from-bytes.rs:16:24
33
|
4-
18 | takes_from_bytes::<NotZerocopy>();
5-
| ^^^^^^^^^^^ the trait `zerocopy::FromBytes` is not implemented for `NotZerocopy`
4+
16 | takes_from_bytes::<NotZerocopy>();
5+
| ^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_from_bytes`
8-
--> tests/ui-msrv/diagnostic-not-implemented-from-bytes.rs:21:24
8+
--> tests/ui-msrv/diagnostic-not-implemented-from-bytes.rs:19:24
99
|
10-
21 | fn takes_from_bytes<T: FromBytes>() {}
10+
19 | fn takes_from_bytes<T: FromBytes>() {}
1111
| ^^^^^^^^^ required by this bound in `takes_from_bytes`
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the trait bound `NotZerocopy: FromZeros` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-from-zeros.rs:18:24
2+
--> tests/ui-msrv/diagnostic-not-implemented-from-zeros.rs:16:24
33
|
4-
18 | takes_from_zeros::<NotZerocopy>();
4+
16 | takes_from_zeros::<NotZerocopy>();
55
| ^^^^^^^^^^^ the trait `FromZeros` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_from_zeros`
8-
--> tests/ui-msrv/diagnostic-not-implemented-from-zeros.rs:21:24
8+
--> tests/ui-msrv/diagnostic-not-implemented-from-zeros.rs:19:24
99
|
10-
21 | fn takes_from_zeros<T: FromZeros>() {}
10+
19 | fn takes_from_zeros<T: FromZeros>() {}
1111
| ^^^^^^^^^ required by this bound in `takes_from_zeros`
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `NotZerocopy: zerocopy::Immutable` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-immutable.rs:18:23
1+
error[E0277]: the trait bound `NotZerocopy: Immutable` is not satisfied
2+
--> tests/ui-msrv/diagnostic-not-implemented-immutable.rs:16:23
33
|
4-
18 | takes_immutable::<NotZerocopy>();
5-
| ^^^^^^^^^^^ the trait `zerocopy::Immutable` is not implemented for `NotZerocopy`
4+
16 | takes_immutable::<NotZerocopy>();
5+
| ^^^^^^^^^^^ the trait `Immutable` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_immutable`
8-
--> tests/ui-msrv/diagnostic-not-implemented-immutable.rs:21:23
8+
--> tests/ui-msrv/diagnostic-not-implemented-immutable.rs:19:23
99
|
10-
21 | fn takes_immutable<T: Immutable>() {}
10+
19 | fn takes_immutable<T: Immutable>() {}
1111
| ^^^^^^^^^ required by this bound in `takes_immutable`
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `NotZerocopy: zerocopy::IntoBytes` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-into-bytes.rs:18:24
1+
error[E0277]: the trait bound `NotZerocopy: IntoBytes` is not satisfied
2+
--> tests/ui-msrv/diagnostic-not-implemented-into-bytes.rs:16:24
33
|
4-
18 | takes_into_bytes::<NotZerocopy>();
5-
| ^^^^^^^^^^^ the trait `zerocopy::IntoBytes` is not implemented for `NotZerocopy`
4+
16 | takes_into_bytes::<NotZerocopy>();
5+
| ^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_into_bytes`
8-
--> tests/ui-msrv/diagnostic-not-implemented-into-bytes.rs:21:24
8+
--> tests/ui-msrv/diagnostic-not-implemented-into-bytes.rs:19:24
99
|
10-
21 | fn takes_into_bytes<T: IntoBytes>() {}
10+
19 | fn takes_into_bytes<T: IntoBytes>() {}
1111
| ^^^^^^^^^ required by this bound in `takes_into_bytes`
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `NotZerocopy: zerocopy::Immutable` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-issue-1296.rs:52:19
1+
error[E0277]: the trait bound `NotZerocopy: Immutable` is not satisfied
2+
--> tests/ui-msrv/diagnostic-not-implemented-issue-1296.rs:50:19
33
|
4-
52 | Foo.write_obj(NotZerocopy(()));
5-
| ^^^^^^^^^^^^^^^ the trait `zerocopy::Immutable` is not implemented for `NotZerocopy`
4+
50 | Foo.write_obj(NotZerocopy(()));
5+
| ^^^^^^^^^^^^^^^ the trait `Immutable` is not implemented for `NotZerocopy`
66

7-
error[E0277]: the trait bound `NotZerocopy: zerocopy::IntoBytes` is not satisfied
8-
--> tests/ui-msrv/diagnostic-not-implemented-issue-1296.rs:52:19
7+
error[E0277]: the trait bound `NotZerocopy: IntoBytes` is not satisfied
8+
--> tests/ui-msrv/diagnostic-not-implemented-issue-1296.rs:50:19
99
|
10-
52 | Foo.write_obj(NotZerocopy(()));
11-
| ^^^^^^^^^^^^^^^ the trait `zerocopy::IntoBytes` is not implemented for `NotZerocopy`
10+
50 | Foo.write_obj(NotZerocopy(()));
11+
| ^^^^^^^^^^^^^^^ the trait `IntoBytes` is not implemented for `NotZerocopy`
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `NotZerocopy: zerocopy::KnownLayout` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-known-layout.rs:18:26
1+
error[E0277]: the trait bound `NotZerocopy: KnownLayout` is not satisfied
2+
--> tests/ui-msrv/diagnostic-not-implemented-known-layout.rs:16:26
33
|
4-
18 | takes_known_layout::<NotZerocopy>();
5-
| ^^^^^^^^^^^ the trait `zerocopy::KnownLayout` is not implemented for `NotZerocopy`
4+
16 | takes_known_layout::<NotZerocopy>();
5+
| ^^^^^^^^^^^ the trait `KnownLayout` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_known_layout`
8-
--> tests/ui-msrv/diagnostic-not-implemented-known-layout.rs:21:26
8+
--> tests/ui-msrv/diagnostic-not-implemented-known-layout.rs:19:26
99
|
10-
21 | fn takes_known_layout<T: KnownLayout>() {}
10+
19 | fn takes_known_layout<T: KnownLayout>() {}
1111
| ^^^^^^^^^^^ required by this bound in `takes_known_layout`
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0277]: the trait bound `NotZerocopy: zerocopy::TryFromBytes` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-try-from-bytes.rs:18:28
1+
error[E0277]: the trait bound `NotZerocopy: TryFromBytes` is not satisfied
2+
--> tests/ui-msrv/diagnostic-not-implemented-try-from-bytes.rs:16:28
33
|
4-
18 | takes_try_from_bytes::<NotZerocopy>();
5-
| ^^^^^^^^^^^ the trait `zerocopy::TryFromBytes` is not implemented for `NotZerocopy`
4+
16 | takes_try_from_bytes::<NotZerocopy>();
5+
| ^^^^^^^^^^^ the trait `TryFromBytes` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_try_from_bytes`
8-
--> tests/ui-msrv/diagnostic-not-implemented-try-from-bytes.rs:21:28
8+
--> tests/ui-msrv/diagnostic-not-implemented-try-from-bytes.rs:19:28
99
|
10-
21 | fn takes_try_from_bytes<T: TryFromBytes>() {}
10+
19 | fn takes_try_from_bytes<T: TryFromBytes>() {}
1111
| ^^^^^^^^^^^^ required by this bound in `takes_try_from_bytes`
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0277]: the trait bound `NotZerocopy: zerocopy::Unaligned` is not satisfied
2-
--> tests/ui-msrv/diagnostic-not-implemented-unaligned.rs:18:23
2+
--> tests/ui-msrv/diagnostic-not-implemented-unaligned.rs:16:23
33
|
4-
18 | takes_unaligned::<NotZerocopy>();
4+
16 | takes_unaligned::<NotZerocopy>();
55
| ^^^^^^^^^^^ the trait `zerocopy::Unaligned` is not implemented for `NotZerocopy`
66
|
77
note: required by a bound in `takes_unaligned`
8-
--> tests/ui-msrv/diagnostic-not-implemented-unaligned.rs:21:23
8+
--> tests/ui-msrv/diagnostic-not-implemented-unaligned.rs:19:23
99
|
10-
21 | fn takes_unaligned<T: Unaligned>() {}
10+
19 | fn takes_unaligned<T: Unaligned>() {}
1111
| ^^^^^^^^^ required by this bound in `takes_unaligned`
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
error[E0277]: the trait bound `NotZerocopy<u32>: zerocopy::FromBytes` is not satisfied
2-
--> tests/ui-msrv/include_value_not_from_bytes.rs:19:42
1+
error[E0277]: the trait bound `NotZerocopy<u32>: FromBytes` is not satisfied
2+
--> tests/ui-msrv/include_value_not_from_bytes.rs:17:5
33
|
4-
19 | const NOT_FROM_BYTES: NotZerocopy<u32> = include_value!("../../testdata/include_value/data");
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `zerocopy::FromBytes` is not implemented for `NotZerocopy<u32>`
4+
17 | zerocopy::include_value!("../../testdata/include_value/data");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy<u32>`
66
|
77
note: required by a bound in `NOT_FROM_BYTES::transmute`
8-
--> tests/ui-msrv/include_value_not_from_bytes.rs:19:42
8+
--> tests/ui-msrv/include_value_not_from_bytes.rs:17:5
99
|
10-
19 | const NOT_FROM_BYTES: NotZerocopy<u32> = include_value!("../../testdata/include_value/data");
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
| |
13-
| required by a bound in this
14-
| required by this bound in `NOT_FROM_BYTES::transmute`
10+
17 | zerocopy::include_value!("../../testdata/include_value/data");
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
| |
13+
| required by a bound in this
14+
| required by this bound in `NOT_FROM_BYTES::transmute`
1515
= note: this error originates in the macro `$crate::transmute` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)