diff --git a/Cargo.toml b/Cargo.toml index 01084b2dc2..89dfcf96ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,4 +43,5 @@ default-features = false [dev-dependencies] rand = "0.6" rustversion = "1.0" -trybuild = "1.0" +# Required for "and $N others" normalization +trybuild = ">=1.0.70" diff --git a/src/lib.rs b/src/lib.rs index 2ed6839707..3e5a5cf50f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,7 +163,29 @@ macro_rules! impl_for_composite_types { ($trait:ident) => { // TODO(#61): Add a "SAFETY" comment and remove this `allow`. #[allow(clippy::undocumented_unsafe_blocks)] - unsafe impl $trait for PhantomData { + unsafe impl $trait for PhantomData { + fn only_derive_is_allowed_to_implement_this_trait() + where + Self: Sized, + { + } + } + // SAFETY: `ManuallyDrop` has the same layout as `T`, and accessing the + // inner value is safe (meaning that it's unsound to leave the inner + // value uninitialized while exposing the `ManuallyDrop` to safe code). + // - `FromBytes`: Since it has the same layout as `T`, any valid `T` is + // a valid `ManuallyDrop`. Since `T: FromBytes`, any sequence of + // bytes is a valid `T`, and thus a valid `ManuallyDrop`. + // - `AsBytes`: Since it has the same layout as `T`, and since it's + // unsound to let safe code access a `ManuallyDrop` whose inner value + // is uninitialized, safe code can only ever access a `ManuallyDrop` + // whose contents are a valid `T`. Since `T: AsBytes`, this means that + // safe code can only ever access a `ManuallyDrop` with all + // initialized bytes. + // - `Unaligned`: `ManuallyDrop` has the same layout (and thus + // alignment) as `T`, and `T: Unaligned` guarantees that that + // alignment is 1. + unsafe impl $trait for ManuallyDrop { fn only_derive_is_allowed_to_implement_this_trait() where Self: Sized, @@ -2493,6 +2515,27 @@ mod tests { U64::::new(u.0).as_bytes().try_into().unwrap() } + // An unsized type. + // + // This is used to test the custom derives of our traits. The `[u8]` type + // gets a hand-rolled impl, so it doesn't exercise our custom derives. + #[derive(Debug, Eq, PartialEq, FromBytes, AsBytes, Unaligned)] + #[repr(transparent)] + struct Unsized([u8]); + + impl Unsized { + fn from_mut_slice(slc: &mut [u8]) -> &mut Unsized { + // SAFETY: This *probably* sound - since the layouts of `[u8]` and + // `Unsized` are the same, so are the layouts of `&mut [u8]` and + // `&mut Unsized`. [1] Even if it turns out that this isn't actually + // guaranteed by the language spec, we can just change this since + // it's in test code. + // + // [1] https://github.com/rust-lang/unsafe-code-guidelines/issues/375 + unsafe { mem::transmute(slc) } + } + } + #[test] fn test_object_safety() { fn _takes_from_bytes(_: &dyn FromBytes) {} @@ -3133,6 +3176,71 @@ mod tests { #[test] fn test_as_bytes_methods() { + /// Run a series of tests by calling `AsBytes` methods on `t`. + /// + /// `bytes` is the expected byte sequence returned from `t.as_bytes()` + /// before `t` has been modified. `post_mutation` is the expected + /// sequence returned from `t.as_bytes()` after `t.as_bytes_mut()[0]` + /// has had its bits flipped (by applying `^= 0xFF`). + /// + /// `N` is the size of `t` in bytes. + fn test( + t: &mut T, + bytes: &[u8], + post_mutation: &T, + ) { + // Test that we can access the underlying bytes, and that we get the + // right bytes and the right number of bytes. + assert_eq!(t.as_bytes(), bytes); + + // Test that changes to the underlying byte slices are reflected in + // the original object. + t.as_bytes_mut()[0] ^= 0xFF; + assert_eq!(t, post_mutation); + t.as_bytes_mut()[0] ^= 0xFF; + + // `write_to` rejects slices that are too small or too large. + assert_eq!(t.write_to(&mut vec![0; N - 1][..]), None); + assert_eq!(t.write_to(&mut vec![0; N + 1][..]), None); + + // `write_to` works as expected. + let mut bytes = [0; N]; + assert_eq!(t.write_to(&mut bytes[..]), Some(())); + assert_eq!(bytes, t.as_bytes()); + + // `write_to_prefix` rejects slices that are too small. + assert_eq!(t.write_to_prefix(&mut vec![0; N - 1][..]), None); + + // `write_to_prefix` works with exact-sized slices. + let mut bytes = [0; N]; + assert_eq!(t.write_to_prefix(&mut bytes[..]), Some(())); + assert_eq!(bytes, t.as_bytes()); + + // `write_to_prefix` works with too-large slices, and any bytes past + // the prefix aren't modified. + let mut too_many_bytes = vec![0; N + 1]; + too_many_bytes[N] = 123; + assert_eq!(t.write_to_prefix(&mut too_many_bytes[..]), Some(())); + assert_eq!(&too_many_bytes[..N], t.as_bytes()); + assert_eq!(too_many_bytes[N], 123); + + // `write_to_suffix` rejects slices that are too small. + assert_eq!(t.write_to_suffix(&mut vec![0; N - 1][..]), None); + + // `write_to_suffix` works with exact-sized slices. + let mut bytes = [0; N]; + assert_eq!(t.write_to_suffix(&mut bytes[..]), Some(())); + assert_eq!(bytes, t.as_bytes()); + + // `write_to_suffix` works with too-large slices, and any bytes + // before the suffix aren't modified. + let mut too_many_bytes = vec![0; N + 1]; + too_many_bytes[0] = 123; + assert_eq!(t.write_to_suffix(&mut too_many_bytes[..]), Some(())); + assert_eq!(&too_many_bytes[1..], t.as_bytes()); + assert_eq!(too_many_bytes[0], 123); + } + #[derive(Debug, Eq, PartialEq, FromBytes, AsBytes)] #[repr(C)] struct Foo { @@ -3141,51 +3249,22 @@ mod tests { c: Option, } - let mut foo = Foo { a: 1, b: Wrapping(2), c: None }; - - // Test that we can access the underlying bytes, and that we get the - // right bytes and the right number of bytes. - let expected: Vec = if cfg!(target_endian = "little") { + let expected_bytes: Vec = if cfg!(target_endian = "little") { vec![1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0] } else { vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0] }; - assert_eq!(foo.as_bytes(), expected.as_bytes()); - - // Test that changes to the underlying byte slices are reflected in the - // original object. - foo.as_bytes_mut()[0] = 3; - let expected_a = if cfg!(target_endian = "little") { 0x00_00_00_03 } else { 0x03_00_00_01 }; - assert_eq!(foo, Foo { a: expected_a, b: Wrapping(2), c: None }); - - // Do the same tests for a slice, which ensures that this logic works - // for unsized types as well. - let foo = &mut [ - Foo { a: 1, b: Wrapping(2), c: None }, - Foo { a: 3, b: Wrapping(4), c: NonZeroU32::new(1) }, - ]; - let expected = if cfg!(target_endian = "little") { - vec![1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0] - } else { - vec![0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 1] - }; - assert_eq!(foo.as_bytes(), expected); - - foo.as_bytes_mut()[8] = 5; - foo.as_bytes_mut()[16] = 6; - let expected_c_1 = NonZeroU32::new(if cfg!(target_endian = "little") { - 0x00_00_00_05 - } else { - 0x05_00_00_00 - }); - let expected_b_2 = - Wrapping(if cfg!(target_endian = "little") { 0x00_00_00_06 } else { 0x06_00_00_04 }); - assert_eq!( - foo, - &[ - Foo { a: 1, b: Wrapping(2), c: expected_c_1 }, - Foo { a: 3, b: expected_b_2, c: NonZeroU32::new(1) }, - ] + let post_mutation_expected_a = + if cfg!(target_endian = "little") { 0x00_00_00_FE } else { 0xFF_00_00_01 }; + test::<12, _>( + &mut Foo { a: 1, b: Wrapping(2), c: None }, + expected_bytes.as_bytes(), + &Foo { a: post_mutation_expected_a, b: Wrapping(2), c: None }, + ); + test::<3, _>( + Unsized::from_mut_slice(&mut [1, 2, 3]), + &[1, 2, 3], + Unsized::from_mut_slice(&mut [0xFE, 2, 3]), ); } @@ -3280,4 +3359,73 @@ mod tests { assert_impls_from_bytes::>(); assert_impls_unaligned::>(); } + + #[test] + fn test_impls() { + macro_rules! assert_impls { + ($ty:ty: $($trait:path),*) => {{ + fn assert_impls_traits() {} + let _ = assert_impls_traits::<$ty>; + }}; + } + + assert_impls!((): FromBytes, AsBytes, Unaligned); + assert_impls!(u8: FromBytes, AsBytes, Unaligned); + assert_impls!(i8: FromBytes, AsBytes, Unaligned); + assert_impls!(u16: FromBytes, AsBytes); + assert_impls!(i16: FromBytes, AsBytes); + assert_impls!(u32: FromBytes, AsBytes); + assert_impls!(i32: FromBytes, AsBytes); + assert_impls!(u64: FromBytes, AsBytes); + assert_impls!(i64: FromBytes, AsBytes); + assert_impls!(u128: FromBytes, AsBytes); + assert_impls!(i128: FromBytes, AsBytes); + assert_impls!(usize: FromBytes, AsBytes); + assert_impls!(isize: FromBytes, AsBytes); + assert_impls!(f32: FromBytes, AsBytes); + assert_impls!(f64: FromBytes, AsBytes); + + assert_impls!(bool: AsBytes, Unaligned); + assert_impls!(char: AsBytes); + assert_impls!(str: AsBytes); + + assert_impls!(NonZeroU8: AsBytes); + assert_impls!(NonZeroI8: AsBytes); + assert_impls!(NonZeroU16: AsBytes); + assert_impls!(NonZeroI16: AsBytes); + assert_impls!(NonZeroU32: AsBytes); + assert_impls!(NonZeroI32: AsBytes); + assert_impls!(NonZeroU64: AsBytes); + assert_impls!(NonZeroI64: AsBytes); + assert_impls!(NonZeroU128: AsBytes); + assert_impls!(NonZeroI128: AsBytes); + assert_impls!(NonZeroUsize: AsBytes); + assert_impls!(NonZeroIsize: AsBytes); + + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + assert_impls!(Option: FromBytes, AsBytes); + + // Implements none of the ZC traits. + struct NotZerocopy; + + assert_impls!(PhantomData: FromBytes, AsBytes, Unaligned); + assert_impls!(PhantomData<[u8]>: FromBytes, AsBytes, Unaligned); + + assert_impls!(ManuallyDrop: FromBytes, AsBytes, Unaligned); + assert_impls!(ManuallyDrop<[u8]>: FromBytes, AsBytes, Unaligned); + + assert_impls!(MaybeUninit: FromBytes); + + assert_impls!(Wrapping: FromBytes, AsBytes, Unaligned); + } } diff --git a/zerocopy-derive/Cargo.toml b/zerocopy-derive/Cargo.toml index 7dc4a1c96e..2ae731fd7f 100644 --- a/zerocopy-derive/Cargo.toml +++ b/zerocopy-derive/Cargo.toml @@ -26,5 +26,6 @@ syn = { version = "1.0.5", features = ["visit"] } [dev-dependencies] rustversion = "1.0" -trybuild = "1.0" +# Required for "and $N others" normalization +trybuild = ">=1.0.70" zerocopy = { path = "../" } diff --git a/zerocopy-derive/src/lib.rs b/zerocopy-derive/src/lib.rs index 0be5aae2bf..533e1e5458 100644 --- a/zerocopy-derive/src/lib.rs +++ b/zerocopy-derive/src/lib.rs @@ -602,7 +602,7 @@ fn impl_block( let use_concrete = if input.generics.params.is_empty() { Some(quote! { const _: () = { - fn must_implement_trait() {} + fn must_implement_trait() {} let _ = must_implement_trait::<#type_ident>; }; }) @@ -612,8 +612,7 @@ fn impl_block( quote! { unsafe impl < #(#params),* > zerocopy::#trait_ident for #type_ident < #(#param_idents),* > #where_clause { - fn only_derive_is_allowed_to_implement_this_trait() where Self: Sized { - } + fn only_derive_is_allowed_to_implement_this_trait() {} } #use_concrete } diff --git a/zerocopy-derive/tests/struct_as_bytes.rs b/zerocopy-derive/tests/struct_as_bytes.rs index 98d7f03b3d..290b4a43ef 100644 --- a/zerocopy-derive/tests/struct_as_bytes.rs +++ b/zerocopy-derive/tests/struct_as_bytes.rs @@ -7,7 +7,7 @@ #[macro_use] mod util; -use std::{marker::PhantomData, option::IntoIter}; +use std::{marker::PhantomData, mem::ManuallyDrop, option::IntoIter}; use zerocopy::AsBytes; @@ -46,12 +46,13 @@ assert_is_as_bytes!(Transparent); #[derive(AsBytes)] #[repr(transparent)] -struct TransparentGeneric { - a: T, - b: CZst, +struct TransparentGeneric { + a: CZst, + b: T, } assert_is_as_bytes!(TransparentGeneric); +assert_is_as_bytes!(TransparentGeneric<[u64]>); #[derive(AsBytes)] #[repr(C, packed)] @@ -77,12 +78,18 @@ assert_is_as_bytes!(CPacked); #[derive(AsBytes)] #[repr(C, packed)] -struct CPackedGeneric { +struct CPackedGeneric { t: T, - u: U, + // Unsized types stored in `repr(packed)` structs must not be dropped + // because dropping them in-place might be unsound depending on the + // alignment of the outer struct. Sized types can be dropped by first being + // moved to an aligned stack variable, but this isn't possible with unsized + // types. + u: ManuallyDrop, } assert_is_as_bytes!(CPackedGeneric); +assert_is_as_bytes!(CPackedGeneric); #[derive(AsBytes)] #[repr(packed)] @@ -102,9 +109,23 @@ assert_is_as_bytes!(Packed); #[derive(AsBytes)] #[repr(packed)] -struct PackedGeneric { +struct PackedGeneric { t: T, - u: U, + // Unsized types stored in `repr(packed)` structs must not be dropped + // because dropping them in-place might be unsound depending on the + // alignment of the outer struct. Sized types can be dropped by first being + // moved to an aligned stack variable, but this isn't possible with unsized + // types. + u: ManuallyDrop, } assert_is_as_bytes!(PackedGeneric); +assert_is_as_bytes!(PackedGeneric); + +#[derive(AsBytes)] +#[repr(transparent)] +struct Unsized { + a: [u8], +} + +assert_is_as_bytes!(Unsized); diff --git a/zerocopy-derive/tests/struct_from_bytes.rs b/zerocopy-derive/tests/struct_from_bytes.rs index 3844d47186..bc15ce4f67 100644 --- a/zerocopy-derive/tests/struct_from_bytes.rs +++ b/zerocopy-derive/tests/struct_from_bytes.rs @@ -8,8 +8,11 @@ mod util; use std::{marker::PhantomData, option::IntoIter}; + use zerocopy::FromBytes; +use crate::util::AU16; + // A struct is `FromBytes` if: // - all fields are `FromBytes` @@ -34,13 +37,22 @@ struct Two { assert_is_from_bytes!(Two); #[derive(FromBytes)] -struct TypeParams<'a, T, I: Iterator> { - a: T, - c: I::Item, - d: u8, - e: PhantomData<&'a [u8]>, - f: PhantomData<&'static str>, - g: PhantomData, +struct Unsized { + a: [u8], +} + +assert_is_from_bytes!(Unsized); + +#[derive(FromBytes)] +struct TypeParams<'a, T: ?Sized, I: Iterator> { + a: I::Item, + b: u8, + c: PhantomData<&'a [u8]>, + d: PhantomData<&'static str>, + e: PhantomData, + f: T, } assert_is_from_bytes!(TypeParams<'static, (), IntoIter<()>>); +assert_is_from_bytes!(TypeParams<'static, AU16, IntoIter<()>>); +assert_is_from_bytes!(TypeParams<'static, [AU16], IntoIter<()>>); diff --git a/zerocopy-derive/tests/struct_unaligned.rs b/zerocopy-derive/tests/struct_unaligned.rs index c91dd4b6b0..7a72c2efa9 100644 --- a/zerocopy-derive/tests/struct_unaligned.rs +++ b/zerocopy-derive/tests/struct_unaligned.rs @@ -8,8 +8,11 @@ mod util; use std::{marker::PhantomData, option::IntoIter}; + use zerocopy::Unaligned; +use crate::util::AU16; + // A struct is `Unaligned` if: // - `repr(align)` is no more than 1 and either // - `repr(C)` or `repr(transparent)` and @@ -55,15 +58,25 @@ struct FooAlign { assert_is_unaligned!(FooAlign); +#[derive(Unaligned)] +#[repr(transparent)] +struct Unsized { + a: [u8], +} + +assert_is_unaligned!(Unsized); + #[derive(Unaligned)] #[repr(C)] -struct TypeParams<'a, T, I: Iterator> { - a: T, - c: I::Item, - d: u8, - e: PhantomData<&'a [u8]>, - f: PhantomData<&'static str>, - g: PhantomData, +struct TypeParams<'a, T: ?Sized, I: Iterator> { + a: I::Item, + b: u8, + c: PhantomData<&'a [u8]>, + d: PhantomData<&'static str>, + e: PhantomData, + f: T, } assert_is_unaligned!(TypeParams<'static, (), IntoIter<()>>); +assert_is_unaligned!(TypeParams<'static, u8, IntoIter<()>>); +assert_is_unaligned!(TypeParams<'static, [u8], IntoIter<()>>); diff --git a/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr b/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr index a16723e834..d26f73672b 100644 --- a/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr +++ b/zerocopy-derive/tests/ui-msrv/derive_transparent.stderr @@ -1,92 +1,83 @@ -error[E0432]: unresolved import `self::util::NotAsBytes` - --> tests/ui-msrv/derive_transparent.rs:11:18 - | -11 | use self::util::{NotAsBytes, AU16}; - | ^^^^^^^^^^ - | | - | no `NotAsBytes` in `util` - | help: a similar name exists in the module: `AsBytes` - -error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied --> tests/ui-msrv/../util.rs | - | const _: fn($ty) -> IsAsBytes<$ty> = IsAsBytes::<$ty>; - | ^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes` + | const _: () = is_as_bytes::<$ty>(); + | ^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy` | - ::: tests/ui-msrv/derive_transparent.rs:33:1 + ::: tests/ui-msrv/derive_transparent.rs:31:1 | -33 | assert_is_as_bytes!(TransparentStruct); - | -------------------------------------------------- in this macro invocation +31 | assert_is_as_bytes!(TransparentStruct); + | --------------------------------------------------- in this macro invocation | -note: required because of the requirements on the impl of `AsBytes` for `TransparentStruct` - --> tests/ui-msrv/derive_transparent.rs:20:10 +note: required because of the requirements on the impl of `AsBytes` for `TransparentStruct` + --> tests/ui-msrv/derive_transparent.rs:21:10 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^ -note: required by a bound in `IsAsBytes` +note: required by a bound in `is_as_bytes` --> tests/ui-msrv/../util.rs | - | struct IsAsBytes(T); - | ^^^^^^^^^^^^^^^^^ required by this bound in `IsAsBytes` + | const fn is_as_bytes() {} + | ^^^^^^^^^^^^^^^^^ required by this bound in `is_as_bytes` | - ::: tests/ui-msrv/derive_transparent.rs:33:1 + ::: tests/ui-msrv/derive_transparent.rs:31:1 | -33 | assert_is_as_bytes!(TransparentStruct); - | -------------------------------------------------- in this macro invocation +31 | assert_is_as_bytes!(TransparentStruct); + | --------------------------------------------------- in this macro invocation = note: this error originates in the macro `assert_is_as_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `char: FromBytes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied --> tests/ui-msrv/../util.rs | - | const _: fn($ty) -> IsFromBytes<$ty> = IsFromBytes::<$ty>; - | ^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `char` + | const _: () = is_from_bytes::<$ty>(); + | ^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` | - ::: tests/ui-msrv/derive_transparent.rs:34:1 + ::: tests/ui-msrv/derive_transparent.rs:32:1 | -34 | assert_is_from_bytes!(TransparentStruct); - | ---------------------------------------------- in this macro invocation +32 | assert_is_from_bytes!(TransparentStruct); + | ----------------------------------------------------- in this macro invocation | -note: required because of the requirements on the impl of `FromBytes` for `TransparentStruct` - --> tests/ui-msrv/derive_transparent.rs:20:19 +note: required because of the requirements on the impl of `FromBytes` for `TransparentStruct` + --> tests/ui-msrv/derive_transparent.rs:21:19 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^^^ -note: required by a bound in `IsFromBytes` +note: required by a bound in `is_from_bytes` --> tests/ui-msrv/../util.rs | - | struct IsFromBytes(T); - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `IsFromBytes` + | const fn is_from_bytes() {} + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `is_from_bytes` | - ::: tests/ui-msrv/derive_transparent.rs:34:1 + ::: tests/ui-msrv/derive_transparent.rs:32:1 | -34 | assert_is_from_bytes!(TransparentStruct); - | ---------------------------------------------- in this macro invocation +32 | assert_is_from_bytes!(TransparentStruct); + | ----------------------------------------------------- in this macro invocation = note: this error originates in the macro `assert_is_from_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `AU16: Unaligned` is not satisfied +error[E0277]: the trait bound `NotZerocopy: Unaligned` is not satisfied --> tests/ui-msrv/../util.rs | - | const _: fn($ty) -> IsUnaligned<$ty> = IsUnaligned::<$ty>; - | ^^^^^^^^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` + | const _: () = is_unaligned::<$ty>(); + | ^^^^^^^^^^^^^^^^^^^ the trait `Unaligned` is not implemented for `NotZerocopy` | - ::: tests/ui-msrv/derive_transparent.rs:35:1 + ::: tests/ui-msrv/derive_transparent.rs:33:1 | -35 | assert_is_unaligned!(TransparentStruct); - | --------------------------------------------- in this macro invocation +33 | assert_is_unaligned!(TransparentStruct); + | ---------------------------------------------------- in this macro invocation | -note: required because of the requirements on the impl of `Unaligned` for `TransparentStruct` - --> tests/ui-msrv/derive_transparent.rs:20:30 +note: required because of the requirements on the impl of `Unaligned` for `TransparentStruct` + --> tests/ui-msrv/derive_transparent.rs:21:30 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^^^ -note: required by a bound in `IsUnaligned` +note: required by a bound in `is_unaligned` --> tests/ui-msrv/../util.rs | - | struct IsUnaligned(T); - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `IsUnaligned` + | const fn is_unaligned() {} + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `is_unaligned` | - ::: tests/ui-msrv/derive_transparent.rs:35:1 + ::: tests/ui-msrv/derive_transparent.rs:33:1 | -35 | assert_is_unaligned!(TransparentStruct); - | --------------------------------------------- in this macro invocation +33 | assert_is_unaligned!(TransparentStruct); + | ---------------------------------------------------- in this macro invocation = note: this error originates in the macro `assert_is_unaligned` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr index cee8b03d3d..417df9be5b 100644 --- a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr @@ -7,37 +7,37 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied = 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:35:10 +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-msrv/late_compile_pass.rs:32:10 | -35 | #[derive(AsBytes)] - | ^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes` +32 | #[derive(AsBytes)] + | ^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy` | = 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 `AU16: Unaligned` is not satisfied - --> tests/ui-msrv/late_compile_pass.rs:45:10 + --> tests/ui-msrv/late_compile_pass.rs:42:10 | -45 | #[derive(Unaligned)] +42 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = 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 `AU16: Unaligned` is not satisfied - --> tests/ui-msrv/late_compile_pass.rs:53:10 + --> tests/ui-msrv/late_compile_pass.rs:50:10 | -53 | #[derive(Unaligned)] +50 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = 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 `AU16: Unaligned` is not satisfied - --> tests/ui-msrv/late_compile_pass.rs:60:10 + --> tests/ui-msrv/late_compile_pass.rs:57:10 | -60 | #[derive(Unaligned)] +57 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: see issue #48214 diff --git a/zerocopy-derive/tests/ui-stable/derive_transparent.stderr b/zerocopy-derive/tests/ui-stable/derive_transparent.stderr index fd51211ccb..425181d5e7 100644 --- a/zerocopy-derive/tests/ui-stable/derive_transparent.stderr +++ b/zerocopy-derive/tests/ui-stable/derive_transparent.stderr @@ -1,22 +1,13 @@ -error[E0432]: unresolved import `self::util::NotAsBytes` - --> tests/ui-stable/derive_transparent.rs:11:18 - | -11 | use self::util::{NotAsBytes, AU16}; - | ^^^^^^^^^^ - | | - | no `NotAsBytes` in `util` - | help: a similar name exists in the module: `AsBytes` - -error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied --> tests/ui-stable/../util.rs | - | const _: fn($ty) -> IsAsBytes<$ty> = IsAsBytes::<$ty>; - | ^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes` + | const _: () = is_as_bytes::<$ty>(); + | ^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy` | - ::: tests/ui-stable/derive_transparent.rs:33:1 + ::: tests/ui-stable/derive_transparent.rs:31:1 | -33 | assert_is_as_bytes!(TransparentStruct); - | -------------------------------------------------- in this macro invocation +31 | assert_is_as_bytes!(TransparentStruct); + | --------------------------------------------------- in this macro invocation | = help: the following other types implement trait `AsBytes`: () @@ -28,71 +19,71 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied I32 I64 and $N others -note: required because of the requirements on the impl of `AsBytes` for `TransparentStruct` - --> tests/ui-stable/derive_transparent.rs:20:10 +note: required because of the requirements on the impl of `AsBytes` for `TransparentStruct` + --> tests/ui-stable/derive_transparent.rs:21:10 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^ -note: required by a bound in `IsAsBytes` +note: required by a bound in `is_as_bytes` --> tests/ui-stable/../util.rs | - | struct IsAsBytes(T); - | ^^^^^^^^^^^^^^^^^ required by this bound in `IsAsBytes` + | const fn is_as_bytes() {} + | ^^^^^^^^^^^^^^^^^ required by this bound in `is_as_bytes` | - ::: tests/ui-stable/derive_transparent.rs:33:1 + ::: tests/ui-stable/derive_transparent.rs:31:1 | -33 | assert_is_as_bytes!(TransparentStruct); - | -------------------------------------------------- in this macro invocation +31 | assert_is_as_bytes!(TransparentStruct); + | --------------------------------------------------- in this macro invocation = note: this error originates in the macro `assert_is_as_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `char: FromBytes` is not satisfied +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied --> tests/ui-stable/../util.rs | - | const _: fn($ty) -> IsFromBytes<$ty> = IsFromBytes::<$ty>; - | ^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `char` + | const _: () = is_from_bytes::<$ty>(); + | ^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` | - ::: tests/ui-stable/derive_transparent.rs:34:1 + ::: tests/ui-stable/derive_transparent.rs:32:1 | -34 | assert_is_from_bytes!(TransparentStruct); - | ---------------------------------------------- in this macro invocation +32 | assert_is_from_bytes!(TransparentStruct); + | ----------------------------------------------------- in this macro invocation | = help: the following other types implement trait `FromBytes`: () + AU16 F32 F64 I128 I16 I32 I64 - MaybeUninit and $N others -note: required because of the requirements on the impl of `FromBytes` for `TransparentStruct` - --> tests/ui-stable/derive_transparent.rs:20:19 +note: required because of the requirements on the impl of `FromBytes` for `TransparentStruct` + --> tests/ui-stable/derive_transparent.rs:21:19 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^^^ -note: required by a bound in `IsFromBytes` +note: required by a bound in `is_from_bytes` --> tests/ui-stable/../util.rs | - | struct IsFromBytes(T); - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `IsFromBytes` + | const fn is_from_bytes() {} + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `is_from_bytes` | - ::: tests/ui-stable/derive_transparent.rs:34:1 + ::: tests/ui-stable/derive_transparent.rs:32:1 | -34 | assert_is_from_bytes!(TransparentStruct); - | ---------------------------------------------- in this macro invocation +32 | assert_is_from_bytes!(TransparentStruct); + | ----------------------------------------------------- in this macro invocation = note: this error originates in the macro `assert_is_from_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `AU16: Unaligned` is not satisfied +error[E0277]: the trait bound `NotZerocopy: Unaligned` is not satisfied --> tests/ui-stable/../util.rs | - | const _: fn($ty) -> IsUnaligned<$ty> = IsUnaligned::<$ty>; - | ^^^^^^^^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` + | const _: () = is_unaligned::<$ty>(); + | ^^^^^^^^^^^^^^^^^^^ the trait `Unaligned` is not implemented for `NotZerocopy` | - ::: tests/ui-stable/derive_transparent.rs:35:1 + ::: tests/ui-stable/derive_transparent.rs:33:1 | -35 | assert_is_unaligned!(TransparentStruct); - | --------------------------------------------- in this macro invocation +33 | assert_is_unaligned!(TransparentStruct); + | ---------------------------------------------------- in this macro invocation | = help: the following other types implement trait `Unaligned`: () @@ -102,21 +93,21 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop and $N others -note: required because of the requirements on the impl of `Unaligned` for `TransparentStruct` - --> tests/ui-stable/derive_transparent.rs:20:30 +note: required because of the requirements on the impl of `Unaligned` for `TransparentStruct` + --> tests/ui-stable/derive_transparent.rs:21:30 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^^^ -note: required by a bound in `IsUnaligned` +note: required by a bound in `is_unaligned` --> tests/ui-stable/../util.rs | - | struct IsUnaligned(T); - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `IsUnaligned` + | const fn is_unaligned() {} + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `is_unaligned` | - ::: tests/ui-stable/derive_transparent.rs:35:1 + ::: tests/ui-stable/derive_transparent.rs:33:1 | -35 | assert_is_unaligned!(TransparentStruct); - | --------------------------------------------- in this macro invocation +33 | assert_is_unaligned!(TransparentStruct); + | ---------------------------------------------------- in this macro invocation = note: this error originates in the macro `assert_is_unaligned` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr b/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr index d2f850680c..994ea609c4 100644 --- a/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr @@ -6,22 +6,22 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied | = help: the following other types implement trait `FromBytes`: () + AU16 F32 F64 FromBytes1 I128 I16 I32 - I64 and $N others = 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-stable/late_compile_pass.rs:35:10 +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui-stable/late_compile_pass.rs:32:10 | -35 | #[derive(AsBytes)] - | ^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes` +32 | #[derive(AsBytes)] + | ^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy` | = help: the following other types implement trait `AsBytes`: () @@ -37,9 +37,9 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied = 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 `AU16: Unaligned` is not satisfied - --> tests/ui-stable/late_compile_pass.rs:45:10 + --> tests/ui-stable/late_compile_pass.rs:42:10 | -45 | #[derive(Unaligned)] +42 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: the following other types implement trait `Unaligned`: @@ -50,15 +50,15 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop 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 `AU16: Unaligned` is not satisfied - --> tests/ui-stable/late_compile_pass.rs:53:10 + --> tests/ui-stable/late_compile_pass.rs:50:10 | -53 | #[derive(Unaligned)] +50 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: the following other types implement trait `Unaligned`: @@ -69,15 +69,15 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop 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 `AU16: Unaligned` is not satisfied - --> tests/ui-stable/late_compile_pass.rs:60:10 + --> tests/ui-stable/late_compile_pass.rs:57:10 | -60 | #[derive(Unaligned)] +57 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: the following other types implement trait `Unaligned`: @@ -88,7 +88,7 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop 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) diff --git a/zerocopy-derive/tests/ui/derive_transparent.rs b/zerocopy-derive/tests/ui/derive_transparent.rs index 294072eb16..d67eadc58a 100644 --- a/zerocopy-derive/tests/ui/derive_transparent.rs +++ b/zerocopy-derive/tests/ui/derive_transparent.rs @@ -8,13 +8,14 @@ extern crate zerocopy; #[macro_use] mod util; -use self::util::{NotAsBytes, AU16}; - -fn main() {} - use core::marker::PhantomData; + use zerocopy::{AsBytes, FromBytes, Unaligned}; +use self::util::NotZerocopy; + +fn main() {} + // Test generic transparent structs #[derive(AsBytes, FromBytes, Unaligned)] @@ -24,12 +25,9 @@ struct TransparentStruct { _phantom: PhantomData<()>, } -// A type that does not implement `AsBytes`. -pub struct NotAsBytes; - // It should be legal to derive these traits on a transparent struct, but it // must also ensure the traits are only implemented when the inner type // implements them. -assert_is_as_bytes!(TransparentStruct); -assert_is_from_bytes!(TransparentStruct); -assert_is_unaligned!(TransparentStruct); +assert_is_as_bytes!(TransparentStruct); +assert_is_from_bytes!(TransparentStruct); +assert_is_unaligned!(TransparentStruct); diff --git a/zerocopy-derive/tests/ui/derive_transparent.stderr b/zerocopy-derive/tests/ui/derive_transparent.stderr index d23896cd8e..6705ab7651 100644 --- a/zerocopy-derive/tests/ui/derive_transparent.stderr +++ b/zerocopy-derive/tests/ui/derive_transparent.stderr @@ -1,22 +1,8 @@ -error[E0432]: unresolved import `self::util::NotAsBytes` - --> tests/ui/derive_transparent.rs:11:18 - | -11 | use self::util::{NotAsBytes, AU16}; - | ^^^^^^^^^^ - | | - | no `NotAsBytes` in `util` - | help: a similar name exists in the module: `AsBytes` - -error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied - --> tests/ui/../util.rs +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui/derive_transparent.rs:31:21 | - | const _: fn($ty) -> IsAsBytes<$ty> = IsAsBytes::<$ty>; - | ^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes` - | - ::: tests/ui/derive_transparent.rs:33:1 - | -33 | assert_is_as_bytes!(TransparentStruct); - | -------------------------------------------------- in this macro invocation +31 | assert_is_as_bytes!(TransparentStruct); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy` | = help: the following other types implement trait `AsBytes`: () @@ -28,71 +14,61 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied I32 I64 and $N others -note: required for `TransparentStruct` to implement `AsBytes` - --> tests/ui/derive_transparent.rs:20:10 +note: required for `TransparentStruct` to implement `AsBytes` + --> tests/ui/derive_transparent.rs:21:10 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^ -note: required by a bound in `IsAsBytes` +note: required by a bound in `is_as_bytes` --> tests/ui/../util.rs | - | struct IsAsBytes(T); - | ^^^^^^^^^^^^^^^^^ required by this bound in `IsAsBytes` + | const fn is_as_bytes() {} + | ^^^^^^^^^^^^^^^^^ required by this bound in `is_as_bytes` | - ::: tests/ui/derive_transparent.rs:33:1 + ::: tests/ui/derive_transparent.rs:31:1 | -33 | assert_is_as_bytes!(TransparentStruct); - | -------------------------------------------------- in this macro invocation - = note: this error originates in the macro `assert_is_as_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) +31 | assert_is_as_bytes!(TransparentStruct); + | --------------------------------------------------- in this macro invocation + = note: this error originates in the derive macro `AsBytes` which comes from the expansion of the macro `assert_is_as_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `char: FromBytes` is not satisfied - --> tests/ui/../util.rs +error[E0277]: the trait bound `NotZerocopy: FromBytes` is not satisfied + --> tests/ui/derive_transparent.rs:32:23 | - | const _: fn($ty) -> IsFromBytes<$ty> = IsFromBytes::<$ty>; - | ^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `char` - | - ::: tests/ui/derive_transparent.rs:34:1 - | -34 | assert_is_from_bytes!(TransparentStruct); - | ---------------------------------------------- in this macro invocation +32 | assert_is_from_bytes!(TransparentStruct); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FromBytes` is not implemented for `NotZerocopy` | = help: the following other types implement trait `FromBytes`: () + AU16 F32 F64 I128 I16 I32 I64 - MaybeUninit and $N others -note: required for `TransparentStruct` to implement `FromBytes` - --> tests/ui/derive_transparent.rs:20:19 +note: required for `TransparentStruct` to implement `FromBytes` + --> tests/ui/derive_transparent.rs:21:19 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^^^ -note: required by a bound in `IsFromBytes` +note: required by a bound in `is_from_bytes` --> tests/ui/../util.rs | - | struct IsFromBytes(T); - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `IsFromBytes` + | const fn is_from_bytes() {} + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `is_from_bytes` | - ::: tests/ui/derive_transparent.rs:34:1 + ::: tests/ui/derive_transparent.rs:32:1 | -34 | assert_is_from_bytes!(TransparentStruct); - | ---------------------------------------------- in this macro invocation - = note: this error originates in the macro `assert_is_from_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) +32 | assert_is_from_bytes!(TransparentStruct); + | ----------------------------------------------------- in this macro invocation + = note: this error originates in the derive macro `FromBytes` which comes from the expansion of the macro `assert_is_from_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `AU16: Unaligned` is not satisfied - --> tests/ui/../util.rs +error[E0277]: the trait bound `NotZerocopy: Unaligned` is not satisfied + --> tests/ui/derive_transparent.rs:33:22 | - | const _: fn($ty) -> IsUnaligned<$ty> = IsUnaligned::<$ty>; - | ^^^^^^^^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` - | - ::: tests/ui/derive_transparent.rs:35:1 - | -35 | assert_is_unaligned!(TransparentStruct); - | --------------------------------------------- in this macro invocation +33 | assert_is_unaligned!(TransparentStruct); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Unaligned` is not implemented for `NotZerocopy` | = help: the following other types implement trait `Unaligned`: () @@ -102,21 +78,21 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop and $N others -note: required for `TransparentStruct` to implement `Unaligned` - --> tests/ui/derive_transparent.rs:20:30 +note: required for `TransparentStruct` to implement `Unaligned` + --> tests/ui/derive_transparent.rs:21:30 | -20 | #[derive(AsBytes, FromBytes, Unaligned)] +21 | #[derive(AsBytes, FromBytes, Unaligned)] | ^^^^^^^^^ -note: required by a bound in `IsUnaligned` +note: required by a bound in `is_unaligned` --> tests/ui/../util.rs | - | struct IsUnaligned(T); - | ^^^^^^^^^^^^^^^^^^^ required by this bound in `IsUnaligned` + | const fn is_unaligned() {} + | ^^^^^^^^^^^^^^^^^^^ required by this bound in `is_unaligned` | - ::: tests/ui/derive_transparent.rs:35:1 + ::: tests/ui/derive_transparent.rs:33:1 | -35 | assert_is_unaligned!(TransparentStruct); - | --------------------------------------------- in this macro invocation - = note: this error originates in the macro `assert_is_unaligned` (in Nightly builds, run with -Z macro-backtrace for more info) +33 | assert_is_unaligned!(TransparentStruct); + | ---------------------------------------------------- in this macro invocation + = note: this error originates in the derive macro `Unaligned` which comes from the expansion of the macro `assert_is_unaligned` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/tests/ui/late_compile_pass.rs b/zerocopy-derive/tests/ui/late_compile_pass.rs index bdd91e0a88..b3ed6b9745 100644 --- a/zerocopy-derive/tests/ui/late_compile_pass.rs +++ b/zerocopy-derive/tests/ui/late_compile_pass.rs @@ -8,7 +8,7 @@ extern crate zerocopy; #[path = "../util.rs"] mod util; -use self::util::AU16; +use self::util::{NotZerocopy, AU16}; fn main() {} @@ -29,13 +29,10 @@ struct FromBytes1 { // AsBytes errors // -#[repr(C)] -struct NotAsBytes(u32); - #[derive(AsBytes)] #[repr(C)] struct AsBytes1 { - value: NotAsBytes, + value: NotZerocopy, } // diff --git a/zerocopy-derive/tests/ui/late_compile_pass.stderr b/zerocopy-derive/tests/ui/late_compile_pass.stderr index 252b0bb8fc..e2bb59691d 100644 --- a/zerocopy-derive/tests/ui/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui/late_compile_pass.stderr @@ -6,23 +6,23 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied | = help: the following other types implement trait `FromBytes`: () + AU16 F32 F64 FromBytes1 I128 I16 I32 - I64 and $N others = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable = 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/late_compile_pass.rs:35:10 +error[E0277]: the trait bound `NotZerocopy: AsBytes` is not satisfied + --> tests/ui/late_compile_pass.rs:32:10 | -35 | #[derive(AsBytes)] - | ^^^^^^^ the trait `AsBytes` is not implemented for `NotAsBytes` +32 | #[derive(AsBytes)] + | ^^^^^^^ the trait `AsBytes` is not implemented for `NotZerocopy` | = help: the following other types implement trait `AsBytes`: () @@ -39,9 +39,9 @@ error[E0277]: the trait bound `NotAsBytes: AsBytes` is not satisfied = 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 `AU16: Unaligned` is not satisfied - --> tests/ui/late_compile_pass.rs:45:10 + --> tests/ui/late_compile_pass.rs:42:10 | -45 | #[derive(Unaligned)] +42 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: the following other types implement trait `Unaligned`: @@ -52,16 +52,16 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop and $N others = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable = 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 `AU16: Unaligned` is not satisfied - --> tests/ui/late_compile_pass.rs:53:10 + --> tests/ui/late_compile_pass.rs:50:10 | -53 | #[derive(Unaligned)] +50 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: the following other types implement trait `Unaligned`: @@ -72,16 +72,16 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop and $N others = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable = 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 `AU16: Unaligned` is not satisfied - --> tests/ui/late_compile_pass.rs:60:10 + --> tests/ui/late_compile_pass.rs:57:10 | -60 | #[derive(Unaligned)] +57 | #[derive(Unaligned)] | ^^^^^^^^^ the trait `Unaligned` is not implemented for `AU16` | = help: the following other types implement trait `Unaligned`: @@ -92,7 +92,7 @@ error[E0277]: the trait bound `AU16: Unaligned` is not satisfied I16 I32 I64 - PhantomData + ManuallyDrop and $N others = help: see issue #48214 = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable diff --git a/zerocopy-derive/tests/util.rs b/zerocopy-derive/tests/util.rs index ce91d24b05..883e8aa639 100644 --- a/zerocopy-derive/tests/util.rs +++ b/zerocopy-derive/tests/util.rs @@ -2,13 +2,16 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -use zerocopy::AsBytes; +use zerocopy::{AsBytes, FromBytes}; -// A `u16` with alignment 2. -// -// Though `u16` has alignment 2 on some platforms, it's not guaranteed. -// By contrast, `AU16` is guaranteed to have alignment 2. -#[derive(AsBytes, Copy, Clone)] +/// A type that doesn't implement any zerocopy traits. +pub struct NotZerocopy(()); + +/// A `u16` with alignment 2. +/// +/// Though `u16` has alignment 2 on some platforms, it's not guaranteed. By +/// contrast, `AU16` is guaranteed to have alignment 2. +#[derive(FromBytes, AsBytes, Copy, Clone)] #[repr(C, align(2))] pub struct AU16(u16); @@ -16,8 +19,8 @@ pub struct AU16(u16); macro_rules! assert_is_as_bytes { ($ty:ty) => { const _: () = { - struct IsAsBytes(T); - const _: fn($ty) -> IsAsBytes<$ty> = IsAsBytes::<$ty>; + const fn is_as_bytes() {} + const _: () = is_as_bytes::<$ty>(); }; }; } @@ -26,8 +29,8 @@ macro_rules! assert_is_as_bytes { macro_rules! assert_is_from_bytes { ($ty:ty) => { const _: () = { - struct IsFromBytes(T); - const _: fn($ty) -> IsFromBytes<$ty> = IsFromBytes::<$ty>; + const fn is_from_bytes() {} + const _: () = is_from_bytes::<$ty>(); }; }; } @@ -36,8 +39,8 @@ macro_rules! assert_is_from_bytes { macro_rules! assert_is_unaligned { ($ty:ty) => { const _: () = { - struct IsUnaligned(T); - const _: fn($ty) -> IsUnaligned<$ty> = IsUnaligned::<$ty>; + const fn is_unaligned() {} + const _: () = is_unaligned::<$ty>(); }; }; }