Skip to content

Commit

Permalink
[WIP] Don't assume primitive type alignments
Browse files Browse the repository at this point in the history
Previously, we used the `u16` and `u64` types in a number of tests,
relying on them having the alignments 2 and 8 respectively. While those
types have those alignments on many platforms, those are not guaranteed
to be their alignments on all platforms. In this commit, we replace
most of these uses with types of the same size but guaranteed alignment.
In a few places, we have to leave the uses as-is, but this isn't a big
deal since our tests run in CI on platforms where the alignments are
actually the values we're relying upon.

Closes #116
  • Loading branch information
joshlf committed Nov 3, 2022
1 parent 48b3240 commit f424f1f
Show file tree
Hide file tree
Showing 17 changed files with 338 additions and 242 deletions.
168 changes: 91 additions & 77 deletions src/lib.rs

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion zerocopy-derive/tests/struct_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

#![allow(warnings)]

mod util;

use std::{marker::PhantomData, option::IntoIter};

use zerocopy::AsBytes;

use crate::util::AU16;

struct IsAsBytes<T: AsBytes>(T);

// Fail compilation if `$ty: !AsBytes`.
Expand Down Expand Up @@ -36,7 +40,7 @@ is_as_bytes!(CZst);
struct C {
a: u8,
b: u8,
c: u16,
c: AU16,
}

is_as_bytes!(C);
Expand All @@ -60,6 +64,13 @@ is_as_bytes!(CZstPacked);
#[repr(C, packed)]
struct CPacked {
a: u8,
// NOTE: The `u16` type is not guaranteed to have alignment 2, although it
// does on many platforms. However, to fix this would require a custom type
// with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
// allowed to transitively contain `#[repr(align(...))]` types. Thus, we
// have no choice but to use `u16` here. Luckily, these tests run in CI on
// platforms on which `u16` has alignment 2, so this isn't that big of a
// deal.
b: u16,
}

Expand Down
7 changes: 7 additions & 0 deletions zerocopy-derive/tests/struct_unaligned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ is_unaligned!(Bar);
#[derive(Unaligned)]
#[repr(packed)]
struct Baz {
// NOTE: The `u16` type is not guaranteed to have alignment 2, although it
// does on many platforms. However, to fix this would require a custom type
// with a `#[repr(align(2))]` attribute, and `#[repr(packed)]` types are not
// allowed to transitively contain `#[repr(align(...))]` types. Thus, we
// have no choice but to use `u16` here. Luckily, these tests run in CI on
// platforms on which `u16` has alignment 2, so this isn't that big of a
// deal.
a: u16,
}

Expand Down
41 changes: 16 additions & 25 deletions zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:18:10
--> tests/ui-msrv/late_compile_pass.rs:23:10
|
18 | #[derive(FromBytes)]
23 | #[derive(FromBytes)]
| ^^^^^^^^^ the trait `FromBytes` is not implemented for `&'static str`
|
= help: see issue #48214
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:30:10
--> tests/ui-msrv/late_compile_pass.rs:35:10
|
30 | #[derive(AsBytes)]
35 | #[derive(AsBytes)]
| ^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes`
|
= help: see issue #48214
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:40:10
error[E0277]: the trait bound `AU16: Unaligned` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:45:10
|
40 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
45 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16`
|
= help: the following implementations were found:
<i8 as Unaligned>
<u8 as Unaligned>
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:48:10
error[E0277]: the trait bound `AU16: Unaligned` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:53:10
|
48 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
53 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16`
|
= help: the following implementations were found:
<i8 as Unaligned>
<u8 as Unaligned>
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:55:10
error[E0277]: the trait bound `AU16: Unaligned` is not satisfied
--> tests/ui-msrv/late_compile_pass.rs:60:10
|
55 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
60 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16`
|
= help: the following implementations were found:
<i8 as Unaligned>
<u8 as Unaligned>
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)
32 changes: 16 additions & 16 deletions zerocopy-derive/tests/ui-msrv/struct.stderr
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
error: unsupported on types with type parameters
--> tests/ui-msrv/struct.rs:14:10
--> tests/ui-msrv/struct.rs:19:10
|
14 | #[derive(AsBytes)]
19 | #[derive(AsBytes)]
| ^^^^^^^
|
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/struct.rs:34:11
--> tests/ui-msrv/struct.rs:35:11
|
34 | #[repr(C, align(2))]
35 | #[repr(C, align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/struct.rs:38:21
--> tests/ui-msrv/struct.rs:39:21
|
38 | #[repr(transparent, align(2))]
39 | #[repr(transparent, align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/struct.rs:44:16
--> tests/ui-msrv/struct.rs:45:16
|
44 | #[repr(packed, align(2))]
45 | #[repr(packed, align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/struct.rs:48:18
--> tests/ui-msrv/struct.rs:49:18
|
48 | #[repr(align(1), align(2))]
49 | #[repr(align(1), align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/struct.rs:52:8
--> tests/ui-msrv/struct.rs:53:8
|
52 | #[repr(align(2), align(4))]
53 | #[repr(align(2), align(4))]
| ^^^^^^^^

error[E0692]: transparent struct cannot have other repr hints
--> tests/ui-msrv/struct.rs:38:8
--> tests/ui-msrv/struct.rs:39:8
|
38 | #[repr(transparent, align(2))]
39 | #[repr(transparent, align(2))]
| ^^^^^^^^^^^ ^^^^^^^^

error[E0277]: the trait bound `HasPadding<AsBytes2, true>: ShouldBe<false>` is not satisfied
--> tests/ui-msrv/struct.rs:22:10
--> tests/ui-msrv/struct.rs:23:10
|
22 | #[derive(AsBytes)]
23 | #[derive(AsBytes)]
| ^^^^^^^ the trait `ShouldBe<false>` is not implemented for `HasPadding<AsBytes2, true>`
|
= help: the following implementations were found:
Expand Down
24 changes: 12 additions & 12 deletions zerocopy-derive/tests/ui-msrv/union.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
error: unsupported on types with type parameters
--> tests/ui-msrv/union.rs:16:10
--> tests/ui-msrv/union.rs:21:10
|
16 | #[derive(AsBytes)]
21 | #[derive(AsBytes)]
| ^^^^^^^
|
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/union.rs:34:11
--> tests/ui-msrv/union.rs:39:11
|
34 | #[repr(C, align(2))]
39 | #[repr(C, align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/union.rs:50:16
--> tests/ui-msrv/union.rs:55:16
|
50 | #[repr(packed, align(2))]
55 | #[repr(packed, align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/union.rs:56:18
--> tests/ui-msrv/union.rs:61:18
|
56 | #[repr(align(1), align(2))]
61 | #[repr(align(1), align(2))]
| ^^^^^^^^

error: cannot derive Unaligned with repr(align(N > 1))
--> tests/ui-msrv/union.rs:62:8
--> tests/ui-msrv/union.rs:67:8
|
62 | #[repr(align(2), align(4))]
67 | #[repr(align(2), align(4))]
| ^^^^^^^^

error[E0277]: the trait bound `HasPadding<AsBytes2, true>: ShouldBe<false>` is not satisfied
--> tests/ui-msrv/union.rs:22:10
--> tests/ui-msrv/union.rs:27:10
|
22 | #[derive(AsBytes)]
27 | #[derive(AsBytes)]
| ^^^^^^^ the trait `ShouldBe<false>` is not implemented for `HasPadding<AsBytes2, true>`
|
= help: the following implementations were found:
Expand Down
67 changes: 44 additions & 23 deletions zerocopy-derive/tests/ui-stable/late_compile_pass.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:18:10
--> tests/ui-stable/late_compile_pass.rs:23:10
|
18 | #[derive(FromBytes)]
23 | #[derive(FromBytes)]
| ^^^^^^^^^ the trait `FromBytes` is not implemented for `&'static str`
|
= help: the following other types implement trait `FromBytes`:
Expand All @@ -18,56 +18,77 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied
= note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:30:10
--> tests/ui-stable/late_compile_pass.rs:35:10
|
30 | #[derive(AsBytes)]
35 | #[derive(AsBytes)]
| ^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes`
|
= help: the following other types implement trait `AsBytes`:
()
AU16
AsBytes1
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
and $N others
= help: see issue #48214
= note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:40:10
error[E0277]: the trait bound `AU16: Unaligned` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:45:10
|
40 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
45 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16`
|
= help: the following other types implement trait `Unaligned`:
i8
u8
()
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
PhantomData<T>
and $N others
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:48:10
error[E0277]: the trait bound `AU16: Unaligned` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:53:10
|
48 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
53 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16`
|
= help: the following other types implement trait `Unaligned`:
i8
u8
()
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
PhantomData<T>
and $N others
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `u16: Unaligned` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:55:10
error[E0277]: the trait bound `AU16: Unaligned` is not satisfied
--> tests/ui-stable/late_compile_pass.rs:60:10
|
55 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `u16`
60 | #[derive(Unaligned)]
| ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16`
|
= help: the following other types implement trait `Unaligned`:
i8
u8
()
F32<O>
F64<O>
I128<O>
I16<O>
I32<O>
I64<O>
PhantomData<T>
and $N others
= help: see issue #48214
= note: this error originates in the derive macro `Unaligned` (in Nightly builds, run with -Z macro-backtrace for more info)
Loading

0 comments on commit f424f1f

Please sign in to comment.