diff --git a/src/lib.rs b/src/lib.rs index 59ab3cae0b..8053e0c51d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2474,9 +2474,23 @@ mod tests { } } - // Converts a `u64` to bytes using this platform's endianness. - fn u64_to_bytes(u: u64) -> [u8; 8] { - U64::::new(u).as_bytes().try_into().unwrap() + // A `u64` with alignment 8. + // + // Though `u64` has alignment 8 on some platforms, it's not guaranteed. + // By contrast, `AU64` is guaranteed to have alignment 8. + #[derive(FromBytes, AsBytes, Eq, PartialEq, Ord, PartialOrd, Default, Debug, Copy, Clone)] + #[repr(C, align(8))] + struct AU64(u64); + + impl Display for AU64 { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + Display::fmt(&self.0, f) + } + } + + // Converts an `AU64` to bytes using this platform's endianness. + fn au64_to_bytes(u: AU64) -> [u8; 8] { + U64::::new(u.0).as_bytes().try_into().unwrap() } #[test] @@ -2581,34 +2595,34 @@ mod tests { // between the typed and untyped representations, that reads via `deref` and // `read` behave the same, and that writes via `deref_mut` and `write` // behave the same. - fn test_new_helper(mut lv: LayoutVerified<&mut [u8], u64>) { + fn test_new_helper(mut lv: LayoutVerified<&mut [u8], AU64>) { // assert that the value starts at 0 - assert_eq!(*lv, 0); - assert_eq!(lv.read(), 0); + assert_eq!(*lv, AU64(0)); + assert_eq!(lv.read(), AU64(0)); // Assert that values written to the typed value are reflected in the // byte slice. - const VAL1: u64 = 0xFF00FF00FF00FF00; + const VAL1: AU64 = AU64(0xFF00FF00FF00FF00); *lv = VAL1; - assert_eq!(lv.bytes(), &u64_to_bytes(VAL1)); - *lv = 0; + assert_eq!(lv.bytes(), &au64_to_bytes(VAL1)); + *lv = AU64(0); lv.write(VAL1); - assert_eq!(lv.bytes(), &u64_to_bytes(VAL1)); + assert_eq!(lv.bytes(), &au64_to_bytes(VAL1)); // Assert that values written to the byte slice are reflected in the // typed value. - const VAL2: u64 = !VAL1; // different from `VAL1` - lv.bytes_mut().copy_from_slice(&u64_to_bytes(VAL2)[..]); + const VAL2: AU64 = AU64(!VAL1.0); // different from `VAL1` + lv.bytes_mut().copy_from_slice(&au64_to_bytes(VAL2)[..]); assert_eq!(*lv, VAL2); assert_eq!(lv.read(), VAL2); } // Verify that values written to a `LayoutVerified` are properly shared // between the typed and untyped representations; pass a value with - // `typed_len` `u64`s backed by an array of `typed_len * 8` bytes. - fn test_new_helper_slice(mut lv: LayoutVerified<&mut [u8], [u64]>, typed_len: usize) { + // `typed_len` `AU64`s backed by an array of `typed_len * 8` bytes. + fn test_new_helper_slice(mut lv: LayoutVerified<&mut [u8], [AU64]>, typed_len: usize) { // Assert that the value starts out zeroed. - assert_eq!(&*lv, vec![0; typed_len].as_slice()); + assert_eq!(&*lv, vec![AU64(0); typed_len].as_slice()); // Check the backing storage is the exact same slice. let untyped_len = typed_len * 8; @@ -2617,16 +2631,16 @@ mod tests { // Assert that values written to the typed value are reflected in the // byte slice. - const VAL1: u64 = 0xFF00FF00FF00FF00; + const VAL1: AU64 = AU64(0xFF00FF00FF00FF00); for typed in &mut *lv { *typed = VAL1; } - assert_eq!(lv.bytes(), VAL1.to_ne_bytes().repeat(typed_len).as_slice()); + assert_eq!(lv.bytes(), VAL1.0.to_ne_bytes().repeat(typed_len).as_slice()); // Assert that values written to the byte slice are reflected in the // typed value. - const VAL2: u64 = !VAL1; // different from VAL1 - lv.bytes_mut().copy_from_slice(&VAL2.to_ne_bytes().repeat(typed_len)); + const VAL2: AU64 = AU64(!VAL1.0); // different from VAL1 + lv.bytes_mut().copy_from_slice(&VAL2.0.to_ne_bytes().repeat(typed_len)); assert!(lv.iter().copied().all(|x| x == VAL2)); } @@ -2692,61 +2706,61 @@ mod tests { // memory. // A buffer with an alignment of 8. - let mut buf = Align::<[u8; 8], u64>::default(); + let mut buf = Align::<[u8; 8], AU64>::default(); // `buf.t` should be aligned to 8, so this should always succeed. - test_new_helper(LayoutVerified::<_, u64>::new(&mut buf.t[..]).unwrap()); + test_new_helper(LayoutVerified::<_, AU64>::new(&mut buf.t[..]).unwrap()); buf.t = [0xFFu8; 8]; - test_new_helper(LayoutVerified::<_, u64>::new_zeroed(&mut buf.t[..]).unwrap()); + test_new_helper(LayoutVerified::<_, AU64>::new_zeroed(&mut buf.t[..]).unwrap()); { // In a block so that `lv` and `suffix` don't live too long. buf.set_default(); - let (lv, suffix) = LayoutVerified::<_, u64>::new_from_prefix(&mut buf.t[..]).unwrap(); + let (lv, suffix) = LayoutVerified::<_, AU64>::new_from_prefix(&mut buf.t[..]).unwrap(); assert!(suffix.is_empty()); test_new_helper(lv); } { buf.t = [0xFFu8; 8]; let (lv, suffix) = - LayoutVerified::<_, u64>::new_from_prefix_zeroed(&mut buf.t[..]).unwrap(); + LayoutVerified::<_, AU64>::new_from_prefix_zeroed(&mut buf.t[..]).unwrap(); assert!(suffix.is_empty()); test_new_helper(lv); } { buf.set_default(); - let (prefix, lv) = LayoutVerified::<_, u64>::new_from_suffix(&mut buf.t[..]).unwrap(); + let (prefix, lv) = LayoutVerified::<_, AU64>::new_from_suffix(&mut buf.t[..]).unwrap(); assert!(prefix.is_empty()); test_new_helper(lv); } { buf.t = [0xFFu8; 8]; let (prefix, lv) = - LayoutVerified::<_, u64>::new_from_suffix_zeroed(&mut buf.t[..]).unwrap(); + LayoutVerified::<_, AU64>::new_from_suffix_zeroed(&mut buf.t[..]).unwrap(); assert!(prefix.is_empty()); test_new_helper(lv); } // A buffer with alignment 8 and length 16. - let mut buf = Align::<[u8; 16], u64>::default(); + let mut buf = Align::<[u8; 16], AU64>::default(); // `buf.t` should be aligned to 8 and have a length which is a multiple - // of `size_of::()`, so this should always succeed. - test_new_helper_slice(LayoutVerified::<_, [u64]>::new_slice(&mut buf.t[..]).unwrap(), 2); + // of `size_of::()`, so this should always succeed. + test_new_helper_slice(LayoutVerified::<_, [AU64]>::new_slice(&mut buf.t[..]).unwrap(), 2); buf.t = [0xFFu8; 16]; test_new_helper_slice( - LayoutVerified::<_, [u64]>::new_slice_zeroed(&mut buf.t[..]).unwrap(), + LayoutVerified::<_, [AU64]>::new_slice_zeroed(&mut buf.t[..]).unwrap(), 2, ); { buf.set_default(); let (lv, suffix) = - LayoutVerified::<_, [u64]>::new_slice_from_prefix(&mut buf.t[..], 1).unwrap(); + LayoutVerified::<_, [AU64]>::new_slice_from_prefix(&mut buf.t[..], 1).unwrap(); assert_eq!(suffix, [0; 8]); test_new_helper_slice(lv, 1); } { buf.t = [0xFFu8; 16]; let (lv, suffix) = - LayoutVerified::<_, [u64]>::new_slice_from_prefix_zeroed(&mut buf.t[..], 1) + LayoutVerified::<_, [AU64]>::new_slice_from_prefix_zeroed(&mut buf.t[..], 1) .unwrap(); assert_eq!(suffix, [0xFF; 8]); test_new_helper_slice(lv, 1); @@ -2754,14 +2768,14 @@ mod tests { { buf.set_default(); let (prefix, lv) = - LayoutVerified::<_, [u64]>::new_slice_from_suffix(&mut buf.t[..], 1).unwrap(); + LayoutVerified::<_, [AU64]>::new_slice_from_suffix(&mut buf.t[..], 1).unwrap(); assert_eq!(prefix, [0; 8]); test_new_helper_slice(lv, 1); } { buf.t = [0xFFu8; 16]; let (prefix, lv) = - LayoutVerified::<_, [u64]>::new_slice_from_suffix_zeroed(&mut buf.t[..], 1) + LayoutVerified::<_, [AU64]>::new_slice_from_suffix_zeroed(&mut buf.t[..], 1) .unwrap(); assert_eq!(prefix, [0xFF; 8]); test_new_helper_slice(lv, 1); @@ -2820,7 +2834,7 @@ mod tests { let mut buf = [0u8; 16]; // `buf.t` should be aligned to 8 and have a length which is a multiple - // of `size_of::()`, so this should always succeed. + // of `size_of::AU64>()`, so this should always succeed. test_new_helper_slice_unaligned( LayoutVerified::<_, [u8]>::new_slice_unaligned(&mut buf[..]).unwrap(), 16, @@ -2872,11 +2886,11 @@ mod tests { // remainder and prefix of the slice respectively. Test that // `xxx_zeroed` behaves the same, and zeroes the memory. - let mut buf = Align::<[u8; 16], u64>::default(); + let mut buf = Align::<[u8; 16], AU64>::default(); { // In a block so that `lv` and `suffix` don't live too long. // `buf.t` should be aligned to 8, so this should always succeed. - let (lv, suffix) = LayoutVerified::<_, u64>::new_from_prefix(&mut buf.t[..]).unwrap(); + let (lv, suffix) = LayoutVerified::<_, AU64>::new_from_prefix(&mut buf.t[..]).unwrap(); assert_eq!(suffix.len(), 8); test_new_helper(lv); } @@ -2884,7 +2898,7 @@ mod tests { buf.t = [0xFFu8; 16]; // `buf.t` should be aligned to 8, so this should always succeed. let (lv, suffix) = - LayoutVerified::<_, u64>::new_from_prefix_zeroed(&mut buf.t[..]).unwrap(); + LayoutVerified::<_, AU64>::new_from_prefix_zeroed(&mut buf.t[..]).unwrap(); // Assert that the suffix wasn't zeroed. assert_eq!(suffix, &[0xFFu8; 8]); test_new_helper(lv); @@ -2892,7 +2906,7 @@ mod tests { { buf.set_default(); // `buf.t` should be aligned to 8, so this should always succeed. - let (prefix, lv) = LayoutVerified::<_, u64>::new_from_suffix(&mut buf.t[..]).unwrap(); + let (prefix, lv) = LayoutVerified::<_, AU64>::new_from_suffix(&mut buf.t[..]).unwrap(); assert_eq!(prefix.len(), 8); test_new_helper(lv); } @@ -2900,7 +2914,7 @@ mod tests { buf.t = [0xFFu8; 16]; // `buf.t` should be aligned to 8, so this should always succeed. let (prefix, lv) = - LayoutVerified::<_, u64>::new_from_suffix_zeroed(&mut buf.t[..]).unwrap(); + LayoutVerified::<_, AU64>::new_from_suffix_zeroed(&mut buf.t[..]).unwrap(); // Assert that the prefix wasn't zeroed. assert_eq!(prefix, &[0xFFu8; 8]); test_new_helper(lv); @@ -2955,26 +2969,26 @@ mod tests { // Fail because the buffer is too large. // A buffer with an alignment of 8. - let mut buf = Align::<[u8; 16], u64>::default(); + let mut buf = Align::<[u8; 16], AU64>::default(); // `buf.t` should be aligned to 8, so only the length check should fail. - assert!(LayoutVerified::<_, u64>::new(&buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_zeroed(&mut buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new(&buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_zeroed(&mut buf.t[..]).is_none()); assert!(LayoutVerified::<_, [u8; 8]>::new_unaligned(&buf.t[..]).is_none()); assert!(LayoutVerified::<_, [u8; 8]>::new_unaligned_zeroed(&mut buf.t[..]).is_none()); // Fail because the buffer is too small. // A buffer with an alignment of 8. - let mut buf = Align::<[u8; 4], u64>::default(); + let mut buf = Align::<[u8; 4], AU64>::default(); // `buf.t` should be aligned to 8, so only the length check should fail. - assert!(LayoutVerified::<_, u64>::new(&buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_zeroed(&mut buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new(&buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_zeroed(&mut buf.t[..]).is_none()); assert!(LayoutVerified::<_, [u8; 8]>::new_unaligned(&buf.t[..]).is_none()); assert!(LayoutVerified::<_, [u8; 8]>::new_unaligned_zeroed(&mut buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_prefix(&buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_prefix_zeroed(&mut buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_suffix(&buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_suffix_zeroed(&mut buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_prefix(&buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_prefix_zeroed(&mut buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_suffix(&buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_suffix_zeroed(&mut buf.t[..]).is_none()); assert!(LayoutVerified::<_, [u8; 8]>::new_unaligned_from_prefix(&buf.t[..]).is_none()); assert!(LayoutVerified::<_, [u8; 8]>::new_unaligned_from_prefix_zeroed(&mut buf.t[..]) .is_none()); @@ -2984,26 +2998,26 @@ mod tests { // Fail because the length is not a multiple of the element size. - let mut buf = Align::<[u8; 12], u64>::default(); + let mut buf = Align::<[u8; 12], AU64>::default(); // `buf.t` has length 12, but element size is 8. - assert!(LayoutVerified::<_, [u64]>::new_slice(&buf.t[..]).is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice_zeroed(&mut buf.t[..]).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice(&buf.t[..]).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice_zeroed(&mut buf.t[..]).is_none()); assert!(LayoutVerified::<_, [[u8; 8]]>::new_slice_unaligned(&buf.t[..]).is_none()); assert!( LayoutVerified::<_, [[u8; 8]]>::new_slice_unaligned_zeroed(&mut buf.t[..]).is_none() ); // Fail because the buffer is too short. - let mut buf = Align::<[u8; 12], u64>::default(); + let mut buf = Align::<[u8; 12], AU64>::default(); // `buf.t` has length 12, but the element size is 8 (and we're expecting // two of them). - assert!(LayoutVerified::<_, [u64]>::new_slice_from_prefix(&buf.t[..], 2).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_prefix(&buf.t[..], 2).is_none()); assert!( - LayoutVerified::<_, [u64]>::new_slice_from_prefix_zeroed(&mut buf.t[..], 2).is_none() + LayoutVerified::<_, [AU64]>::new_slice_from_prefix_zeroed(&mut buf.t[..], 2).is_none() ); - assert!(LayoutVerified::<_, [u64]>::new_slice_from_suffix(&buf.t[..], 2).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_suffix(&buf.t[..], 2).is_none()); assert!( - LayoutVerified::<_, [u64]>::new_slice_from_suffix_zeroed(&mut buf.t[..], 2).is_none() + LayoutVerified::<_, [AU64]>::new_slice_from_suffix_zeroed(&mut buf.t[..], 2).is_none() ); assert!(LayoutVerified::<_, [[u8; 8]]>::new_slice_unaligned_from_prefix(&buf.t[..], 2) .is_none()); @@ -3024,42 +3038,42 @@ mod tests { // A buffer with an alignment of 8. An odd buffer size is chosen so that // the last byte of the buffer has odd alignment. - let mut buf = Align::<[u8; 13], u64>::default(); + let mut buf = Align::<[u8; 13], AU64>::default(); // Slicing from 1, we get a buffer with size 12 (so the length check // should succeed) but an alignment of only 1, which is insufficient. - assert!(LayoutVerified::<_, u64>::new(&buf.t[1..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_zeroed(&mut buf.t[1..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_prefix(&buf.t[1..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_prefix_zeroed(&mut buf.t[1..]).is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice(&buf.t[1..]).is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice_zeroed(&mut buf.t[1..]).is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice_from_prefix(&buf.t[1..], 1).is_none()); + assert!(LayoutVerified::<_, AU64>::new(&buf.t[1..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_zeroed(&mut buf.t[1..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_prefix(&buf.t[1..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_prefix_zeroed(&mut buf.t[1..]).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice(&buf.t[1..]).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice_zeroed(&mut buf.t[1..]).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_prefix(&buf.t[1..], 1).is_none()); assert!( - LayoutVerified::<_, [u64]>::new_slice_from_prefix_zeroed(&mut buf.t[1..], 1).is_none() + LayoutVerified::<_, [AU64]>::new_slice_from_prefix_zeroed(&mut buf.t[1..], 1).is_none() ); - assert!(LayoutVerified::<_, [u64]>::new_slice_from_suffix(&buf.t[1..], 1).is_none()); + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_suffix(&buf.t[1..], 1).is_none()); assert!( - LayoutVerified::<_, [u64]>::new_slice_from_suffix_zeroed(&mut buf.t[1..], 1).is_none() + LayoutVerified::<_, [AU64]>::new_slice_from_suffix_zeroed(&mut buf.t[1..], 1).is_none() ); // Slicing is unnecessary here because `new_from_suffix[_zeroed]` use // the suffix of the slice, which has odd alignment. - assert!(LayoutVerified::<_, u64>::new_from_suffix(&buf.t[..]).is_none()); - assert!(LayoutVerified::<_, u64>::new_from_suffix_zeroed(&mut buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_suffix(&buf.t[..]).is_none()); + assert!(LayoutVerified::<_, AU64>::new_from_suffix_zeroed(&mut buf.t[..]).is_none()); // Fail due to arithmetic overflow. - let mut buf = Align::<[u8; 16], u64>::default(); - let unreasonable_len = usize::MAX / mem::size_of::() + 1; - assert!(LayoutVerified::<_, [u64]>::new_slice_from_prefix(&buf.t[..], unreasonable_len) + let mut buf = Align::<[u8; 16], AU64>::default(); + let unreasonable_len = usize::MAX / mem::size_of::() + 1; + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_prefix(&buf.t[..], unreasonable_len) .is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice_from_prefix_zeroed( + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_prefix_zeroed( &mut buf.t[..], unreasonable_len ) .is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice_from_suffix(&buf.t[..], unreasonable_len) + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_suffix(&buf.t[..], unreasonable_len) .is_none()); - assert!(LayoutVerified::<_, [u64]>::new_slice_from_suffix_zeroed( + assert!(LayoutVerified::<_, [AU64]>::new_slice_from_suffix_zeroed( &mut buf.t[..], unreasonable_len ) diff --git a/zerocopy-derive/tests/struct_as_bytes.rs b/zerocopy-derive/tests/struct_as_bytes.rs index fd464ced1a..d9dba8912f 100644 --- a/zerocopy-derive/tests/struct_as_bytes.rs +++ b/zerocopy-derive/tests/struct_as_bytes.rs @@ -4,10 +4,14 @@ #![allow(warnings)] +mod util; + use std::{marker::PhantomData, option::IntoIter}; use zerocopy::AsBytes; +use crate::util::AU16; + struct IsAsBytes(T); // Fail compilation if `$ty: !AsBytes`. @@ -36,7 +40,7 @@ is_as_bytes!(CZst); struct C { a: u8, b: u8, - c: u16, + c: AU16, } is_as_bytes!(C); @@ -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, } diff --git a/zerocopy-derive/tests/struct_unaligned.rs b/zerocopy-derive/tests/struct_unaligned.rs index 355c208ec0..e1fc0e4b1b 100644 --- a/zerocopy-derive/tests/struct_unaligned.rs +++ b/zerocopy-derive/tests/struct_unaligned.rs @@ -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, } diff --git a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr index 29f18ae05b..cee8b03d3d 100644 --- a/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-msrv/late_compile_pass.stderr @@ -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: - - = 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: - - = 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: - - = 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-msrv/struct.stderr b/zerocopy-derive/tests/ui-msrv/struct.stderr index 5d590a3d1d..871e6ce1af 100644 --- a/zerocopy-derive/tests/ui-msrv/struct.stderr +++ b/zerocopy-derive/tests/ui-msrv/struct.stderr @@ -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: ShouldBe` 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` is not implemented for `HasPadding` | = help: the following implementations were found: diff --git a/zerocopy-derive/tests/ui-msrv/union.stderr b/zerocopy-derive/tests/ui-msrv/union.stderr index cf2a421a8e..48bc71dc5b 100644 --- a/zerocopy-derive/tests/ui-msrv/union.stderr +++ b/zerocopy-derive/tests/ui-msrv/union.stderr @@ -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: ShouldBe` 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` is not implemented for `HasPadding` | = help: the following implementations were found: diff --git a/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr b/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr index 76d752e3e6..d2f850680c 100644 --- a/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui-stable/late_compile_pass.stderr @@ -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`: @@ -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 F64 I128 I16 I32 - I64 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 + F64 + I128 + I16 + I32 + I64 + PhantomData + 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 + F64 + I128 + I16 + I32 + I64 + PhantomData + 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 + F64 + I128 + I16 + I32 + I64 + PhantomData + 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-stable/struct.stderr b/zerocopy-derive/tests/ui-stable/struct.stderr index d84f4a4021..16fa32807e 100644 --- a/zerocopy-derive/tests/ui-stable/struct.stderr +++ b/zerocopy-derive/tests/ui-stable/struct.stderr @@ -1,51 +1,51 @@ error: unsupported on types with type parameters - --> tests/ui-stable/struct.rs:14:10 + --> tests/ui-stable/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-stable/struct.rs:34:11 + --> tests/ui-stable/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-stable/struct.rs:38:21 + --> tests/ui-stable/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-stable/struct.rs:44:16 + --> tests/ui-stable/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-stable/struct.rs:48:18 + --> tests/ui-stable/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-stable/struct.rs:52:8 + --> tests/ui-stable/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-stable/struct.rs:38:8 + --> tests/ui-stable/struct.rs:39:8 | -38 | #[repr(transparent, align(2))] +39 | #[repr(transparent, align(2))] | ^^^^^^^^^^^ ^^^^^^^^ error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-stable/struct.rs:22:10 + --> tests/ui-stable/struct.rs:23:10 | -22 | #[derive(AsBytes)] +23 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` diff --git a/zerocopy-derive/tests/ui-stable/union.stderr b/zerocopy-derive/tests/ui-stable/union.stderr index e8e1a98e39..544d93b0a0 100644 --- a/zerocopy-derive/tests/ui-stable/union.stderr +++ b/zerocopy-derive/tests/ui-stable/union.stderr @@ -1,39 +1,39 @@ error: unsupported on types with type parameters - --> tests/ui-stable/union.rs:16:10 + --> tests/ui-stable/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-stable/union.rs:34:11 + --> tests/ui-stable/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-stable/union.rs:50:16 + --> tests/ui-stable/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-stable/union.rs:56:18 + --> tests/ui-stable/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-stable/union.rs:62:8 + --> tests/ui-stable/union.rs:67:8 | -62 | #[repr(align(2), align(4))] +67 | #[repr(align(2), align(4))] | ^^^^^^^^ error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-stable/union.rs:22:10 + --> tests/ui-stable/union.rs:27:10 | -22 | #[derive(AsBytes)] +27 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` diff --git a/zerocopy-derive/tests/ui/late_compile_pass.rs b/zerocopy-derive/tests/ui/late_compile_pass.rs index c1cd307039..344c8c4080 100644 --- a/zerocopy-derive/tests/ui/late_compile_pass.rs +++ b/zerocopy-derive/tests/ui/late_compile_pass.rs @@ -5,6 +5,11 @@ #[macro_use] extern crate zerocopy; +#[path = "../util.rs"] +mod util; + +use crate::util::AU16; + fn main() {} // These tests cause errors which are generated by a later compilation pass than @@ -40,7 +45,7 @@ struct AsBytes1 { #[derive(Unaligned)] #[repr(C)] struct Unaligned1 { - aligned: u16, + aligned: AU16, } // This specifically tests a bug we had in an old version of the code in which @@ -49,11 +54,11 @@ struct Unaligned1 { #[repr(C)] struct Unaligned2 { unaligned: u8, - aligned: u16, + aligned: AU16, } #[derive(Unaligned)] #[repr(transparent)] struct Unaligned3 { - aligned: u16, + aligned: AU16, } diff --git a/zerocopy-derive/tests/ui/late_compile_pass.stderr b/zerocopy-derive/tests/ui/late_compile_pass.stderr index 0425f3e90a..252b0bb8fc 100644 --- a/zerocopy-derive/tests/ui/late_compile_pass.stderr +++ b/zerocopy-derive/tests/ui/late_compile_pass.stderr @@ -1,7 +1,7 @@ error[E0277]: the trait bound `&'static str: FromBytes` is not satisfied - --> tests/ui/late_compile_pass.rs:18:10 + --> tests/ui/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`: @@ -19,60 +19,81 @@ 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/late_compile_pass.rs:30:10 + --> tests/ui/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 F64 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 `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0277]: the trait bound `u16: Unaligned` is not satisfied - --> tests/ui/late_compile_pass.rs:40:10 +error[E0277]: the trait bound `AU16: Unaligned` is not satisfied + --> tests/ui/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 + F64 + I128 + I16 + I32 + I64 + PhantomData + 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 `u16: Unaligned` is not satisfied - --> tests/ui/late_compile_pass.rs:48:10 +error[E0277]: the trait bound `AU16: Unaligned` is not satisfied + --> tests/ui/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 + F64 + I128 + I16 + I32 + I64 + PhantomData + 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 `u16: Unaligned` is not satisfied - --> tests/ui/late_compile_pass.rs:55:10 +error[E0277]: the trait bound `AU16: Unaligned` is not satisfied + --> tests/ui/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 + F64 + I128 + I16 + I32 + I64 + PhantomData + 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) diff --git a/zerocopy-derive/tests/ui/struct.rs b/zerocopy-derive/tests/ui/struct.rs index 6b71e5203d..0fcc15601a 100644 --- a/zerocopy-derive/tests/ui/struct.rs +++ b/zerocopy-derive/tests/ui/struct.rs @@ -5,6 +5,11 @@ #[macro_use] extern crate zerocopy; +#[path = "../util.rs"] +mod util; + +use crate::util::AU16; + fn main() {} // @@ -15,15 +20,11 @@ fn main() {} #[repr(C)] struct AsBytes1(T); -#[derive(AsBytes)] -#[repr(C, align(4))] -struct AlignedU32(u32); - #[derive(AsBytes)] #[repr(C)] struct AsBytes2 { foo: u8, - bar: AlignedU32, + bar: AU16, } // diff --git a/zerocopy-derive/tests/ui/struct.stderr b/zerocopy-derive/tests/ui/struct.stderr index eaab93324e..e93a94540e 100644 --- a/zerocopy-derive/tests/ui/struct.stderr +++ b/zerocopy-derive/tests/ui/struct.stderr @@ -1,51 +1,51 @@ error: unsupported on types with type parameters - --> tests/ui/struct.rs:14:10 + --> tests/ui/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/struct.rs:34:11 + --> tests/ui/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/struct.rs:38:21 + --> tests/ui/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/struct.rs:44:16 + --> tests/ui/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/struct.rs:48:18 + --> tests/ui/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/struct.rs:52:8 + --> tests/ui/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/struct.rs:38:8 + --> tests/ui/struct.rs:39:8 | -38 | #[repr(transparent, align(2))] +39 | #[repr(transparent, align(2))] | ^^^^^^^^^^^ ^^^^^^^^ error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui/struct.rs:22:10 + --> tests/ui/struct.rs:23:10 | -22 | #[derive(AsBytes)] +23 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` diff --git a/zerocopy-derive/tests/ui/union.rs b/zerocopy-derive/tests/ui/union.rs index 24e9af4876..d92ae50e2b 100644 --- a/zerocopy-derive/tests/ui/union.rs +++ b/zerocopy-derive/tests/ui/union.rs @@ -5,8 +5,13 @@ #[macro_use] extern crate zerocopy; +#[path = "../util.rs"] +mod util; + use std::mem::ManuallyDrop; +use crate::util::AU16; + fn main() {} // @@ -34,7 +39,7 @@ union AsBytes2 { #[repr(C, align(2))] union Unaligned1 { foo: i16, - bar: u16, + bar: AU16, } // Transparent unions are unstable; see issue #60405 diff --git a/zerocopy-derive/tests/ui/union.stderr b/zerocopy-derive/tests/ui/union.stderr index 17241ebbc9..2f7de1b12e 100644 --- a/zerocopy-derive/tests/ui/union.stderr +++ b/zerocopy-derive/tests/ui/union.stderr @@ -1,39 +1,39 @@ error: unsupported on types with type parameters - --> tests/ui/union.rs:16:10 + --> tests/ui/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/union.rs:34:11 + --> tests/ui/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/union.rs:50:16 + --> tests/ui/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/union.rs:56:18 + --> tests/ui/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/union.rs:62:8 + --> tests/ui/union.rs:67:8 | -62 | #[repr(align(2), align(4))] +67 | #[repr(align(2), align(4))] | ^^^^^^^^ error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui/union.rs:22:10 + --> tests/ui/union.rs:27:10 | -22 | #[derive(AsBytes)] +27 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` diff --git a/zerocopy-derive/tests/union_unaligned.rs b/zerocopy-derive/tests/union_unaligned.rs index 076595c5dd..d3474d22f3 100644 --- a/zerocopy-derive/tests/union_unaligned.rs +++ b/zerocopy-derive/tests/union_unaligned.rs @@ -47,6 +47,13 @@ is_unaligned!(Foo); #[derive(Unaligned)] #[repr(packed)] union 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, } diff --git a/zerocopy-derive/tests/util.rs b/zerocopy-derive/tests/util.rs new file mode 100644 index 0000000000..6c588ea5d3 --- /dev/null +++ b/zerocopy-derive/tests/util.rs @@ -0,0 +1,13 @@ +// Copyright 2022 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use zerocopy::AsBytes; + +// 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)] +#[repr(C, align(2))] +pub struct AU16(u16);