diff --git a/src/byteorder.rs b/src/byteorder.rs index 2769410451..cb50f2f55e 100644 --- a/src/byteorder.rs +++ b/src/byteorder.rs @@ -276,7 +276,7 @@ example of how it can be used for parsing UDP packets. [`AsBytes`]: crate::AsBytes [`Unaligned`]: crate::Unaligned"), #[derive(Copy, Clone, Eq, PartialEq, Hash)] - #[cfg_attr(any(feature = "derive", test), derive(KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned))] + #[cfg_attr(any(feature = "derive", test), derive(KnownLayout, NoCell, FromZeroes, FromBytes, AsBytes, Unaligned))] #[repr(transparent)] pub struct $name([u8; $bytes], PhantomData); } @@ -288,7 +288,9 @@ example of how it can be used for parsing UDP packets. /// SAFETY: /// `$name` is `repr(transparent)`, and so it has the same layout /// as its only non-zero field, which is a `u8` array. `u8` arrays - /// are `FromZeroes`, `FromBytes`, `AsBytes`, and `Unaligned`. + /// are `NoCell`, `FromZeroes`, `FromBytes`, `AsBytes`, and + /// `Unaligned`. + impl_or_verify!(O => NoCell for $name); impl_or_verify!(O => FromZeroes for $name); impl_or_verify!(O => FromBytes for $name); impl_or_verify!(O => AsBytes for $name); diff --git a/src/lib.rs b/src/lib.rs index 0f154cab0f..cb154024c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -264,6 +264,14 @@ pub use zerocopy_derive::Unaligned; #[doc(hidden)] pub use zerocopy_derive::KnownLayout; +// `pub use` separately here so that we can mark it `#[doc(hidden)]`. +// +// TODO(#251): Remove this or add a doc comment. +#[cfg(any(feature = "derive", test))] +#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))] +#[doc(hidden)] +pub use zerocopy_derive::NoCell; + use core::{ cell::{self, RefMut}, cmp::Ordering, @@ -7966,7 +7974,7 @@ mod tests { assert_impls!(Wrapping: KnownLayout, !NoCell, !TryFromBytes, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); assert_impls!(Wrapping>: KnownLayout, !NoCell, !TryFromBytes, !FromZeroes, !FromBytes, !AsBytes, !Unaligned); - assert_impls!(Unalign: KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned, !NoCell, !TryFromBytes); + assert_impls!(Unalign: KnownLayout, NoCell, FromZeroes, FromBytes, AsBytes, Unaligned, !TryFromBytes); assert_impls!(Unalign: Unaligned, !NoCell, !KnownLayout, !TryFromBytes, !FromZeroes, !FromBytes, !AsBytes); assert_impls!([u8]: KnownLayout, NoCell, TryFromBytes, FromZeroes, FromBytes, AsBytes, Unaligned); diff --git a/src/util.rs b/src/util.rs index b35cc079c1..23520f3e85 100644 --- a/src/util.rs +++ b/src/util.rs @@ -697,6 +697,7 @@ pub(crate) mod testutil { // By contrast, `AU64` is guaranteed to have alignment 8. #[derive( KnownLayout, + NoCell, FromZeroes, FromBytes, AsBytes, @@ -726,7 +727,7 @@ pub(crate) mod testutil { } #[derive( - FromZeroes, FromBytes, Eq, PartialEq, Ord, PartialOrd, Default, Debug, Copy, Clone, + NoCell, FromZeroes, FromBytes, Eq, PartialEq, Ord, PartialOrd, Default, Debug, Copy, Clone, )] #[repr(C)] pub(crate) struct Nested { diff --git a/src/wrappers.rs b/src/wrappers.rs index 532d872978..ed00a98e96 100644 --- a/src/wrappers.rs +++ b/src/wrappers.rs @@ -60,7 +60,7 @@ use super::*; #[derive(Default, Copy)] #[cfg_attr( any(feature = "derive", test), - derive(KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned) + derive(NoCell, KnownLayout, FromZeroes, FromBytes, AsBytes, Unaligned) )] #[repr(C, packed)] pub struct Unalign(T); @@ -74,7 +74,10 @@ safety_comment! { /// alignment of `T`, and so we don't require that `T: Unaligned` /// - `Unalign` has the same bit validity as `T`, and so it is /// `FromZeroes`, `FromBytes`, or `AsBytes` exactly when `T` is as well. + /// - `NoCell`: `Unalign` has the same fields as `T`, so it contains + /// `UnsafeCell`s exactly when `T` does. impl_or_verify!(T => Unaligned for Unalign); + impl_or_verify!(T: NoCell => NoCell for Unalign); impl_or_verify!(T: FromZeroes => FromZeroes for Unalign); impl_or_verify!(T: FromBytes => FromBytes for Unalign); impl_or_verify!(T: AsBytes => AsBytes for Unalign); diff --git a/zerocopy-derive/src/lib.rs b/zerocopy-derive/src/lib.rs index 9af8a28a06..a54cc11268 100644 --- a/zerocopy-derive/src/lib.rs +++ b/zerocopy-derive/src/lib.rs @@ -243,6 +243,23 @@ pub fn derive_known_layout(ts: proc_macro::TokenStream) -> proc_macro::TokenStre .into() } +#[proc_macro_derive(NoCell)] +pub fn derive_no_cell(ts: proc_macro::TokenStream) -> proc_macro::TokenStream { + let ast = syn::parse_macro_input!(ts as DeriveInput); + match &ast.data { + Data::Struct(strct) => { + impl_block(&ast, strct, Trait::NoCell, RequireBoundedFields::Yes, false, None, None) + } + Data::Enum(enm) => { + impl_block(&ast, enm, Trait::NoCell, RequireBoundedFields::Yes, false, None, None) + } + Data::Union(unn) => { + impl_block(&ast, unn, Trait::NoCell, RequireBoundedFields::Yes, false, None, None) + } + } + .into() +} + #[proc_macro_derive(FromZeroes)] pub fn derive_from_zeroes(ts: proc_macro::TokenStream) -> proc_macro::TokenStream { let ast = syn::parse_macro_input!(ts as DeriveInput); @@ -637,6 +654,7 @@ impl PaddingCheck { #[derive(Debug, Eq, PartialEq)] enum Trait { KnownLayout, + NoCell, FromZeroes, FromBytes, AsBytes, diff --git a/zerocopy-derive/tests/enum_no_cell.rs b/zerocopy-derive/tests/enum_no_cell.rs new file mode 100644 index 0000000000..4324a5ed21 --- /dev/null +++ b/zerocopy-derive/tests/enum_no_cell.rs @@ -0,0 +1,50 @@ +// 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. + +#![allow(warnings)] + +mod util; + +use { + core::cell::UnsafeCell, core::marker::PhantomData, static_assertions::assert_impl_all, + zerocopy::NoCell, +}; + +#[derive(NoCell)] +enum Foo { + A, +} + +assert_impl_all!(Foo: NoCell); + +#[derive(NoCell)] +enum Bar { + A = 0, +} + +assert_impl_all!(Bar: NoCell); + +#[derive(NoCell)] +enum Baz { + A = 1, + B = 0, +} + +assert_impl_all!(Baz: NoCell); + +// Deriving `NoCell` should work if the enum has bounded parameters. + +#[derive(NoCell)] +#[repr(C)] +enum WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + NoCell> +where + 'a: 'b, + 'b: 'a, + T: 'a + 'b + NoCell, +{ + Variant([T; N], PhantomData<&'a &'b ()>), + UnsafeCell(PhantomData>, &'a UnsafeCell<()>), +} + +assert_impl_all!(WithParams<'static, 'static, 42, u8>: NoCell); diff --git a/zerocopy-derive/tests/struct_no_cell.rs b/zerocopy-derive/tests/struct_no_cell.rs new file mode 100644 index 0000000000..2205cc61a8 --- /dev/null +++ b/zerocopy-derive/tests/struct_no_cell.rs @@ -0,0 +1,101 @@ +// 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. + +#![allow(warnings)] + +#[macro_use] +mod util; + +use std::{cell::UnsafeCell, marker::PhantomData, option::IntoIter}; + +use { + static_assertions::{assert_impl_all, assert_not_impl_any}, + zerocopy::NoCell, +}; + +use crate::util::AU16; + +#[derive(NoCell)] +struct Zst; + +assert_impl_all!(Zst: NoCell); + +#[derive(NoCell)] +struct One { + a: bool, +} + +assert_impl_all!(One: NoCell); + +#[derive(NoCell)] +struct Two { + a: bool, + b: Zst, +} + +assert_impl_all!(Two: NoCell); + +#[derive(NoCell)] +struct Three { + a: [u8], +} + +assert_impl_all!(Three: NoCell); + +#[derive(NoCell)] +struct Four<'a> { + field: &'a UnsafeCell, +} + +assert_impl_all!(Four<'static>: NoCell); + +#[derive(NoCell)] +struct TypeParams<'a, T, U, I: Iterator> { + a: I::Item, + b: u8, + c: PhantomData<&'a [u8]>, + d: PhantomData<&'static str>, + e: PhantomData, + f: PhantomData, + g: T, +} + +assert_impl_all!(TypeParams<'static, (), (), IntoIter<()>>: NoCell); +assert_impl_all!(TypeParams<'static, AU16, AU16, IntoIter<()>>: NoCell); +assert_impl_all!(TypeParams<'static, AU16, UnsafeCell, IntoIter<()>>: NoCell); +assert_not_impl_any!(TypeParams<'static, UnsafeCell<()>, (), IntoIter<()>>: NoCell); +assert_not_impl_any!(TypeParams<'static, [UnsafeCell; 0], (), IntoIter<()>>: NoCell); +assert_not_impl_any!(TypeParams<'static, (), (), IntoIter>>: NoCell); + +trait Trait { + type Assoc; +} + +impl Trait for UnsafeCell { + type Assoc = T; +} + +#[derive(NoCell)] +struct WithAssocType { + field: ::Assoc, +} + +assert_impl_all!(WithAssocType>: NoCell); + +// Deriving `NoCell` should work if the struct has bounded parameters. + +#[derive(NoCell)] +#[repr(C)] +struct WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + NoCell>( + [T; N], + PhantomData<&'a &'b ()>, + PhantomData>, + &'a UnsafeCell<()>, +) +where + 'a: 'b, + 'b: 'a, + T: 'a + 'b + NoCell; + +assert_impl_all!(WithParams<'static, 'static, 42, u8>: NoCell); diff --git a/zerocopy-derive/tests/ui-msrv/enum.stderr b/zerocopy-derive/tests/ui-msrv/enum.stderr index 39bde3f9e3..fd2935baa8 100644 --- a/zerocopy-derive/tests/ui-msrv/enum.stderr +++ b/zerocopy-derive/tests/ui-msrv/enum.stderr @@ -31,149 +31,149 @@ error: must have a non-align #[repr(...)] attribute in order to guarantee this t = note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info) error: only C-like enums can implement FromZeroes - --> tests/ui-msrv/enum.rs:52:1 + --> tests/ui-msrv/enum.rs:70:1 | -52 | / enum FromZeroes1 { -53 | | A(u8), -54 | | } +70 | / enum FromZeroes1 { +71 | | A(u8), +72 | | } | |_^ error: only C-like enums can implement FromZeroes - --> tests/ui-msrv/enum.rs:57:1 + --> tests/ui-msrv/enum.rs:75:1 | -57 | / enum FromZeroes2 { -58 | | A, -59 | | B(u8), -60 | | } +75 | / enum FromZeroes2 { +76 | | A, +77 | | B(u8), +78 | | } | |_^ error: FromZeroes only supported on enums with a variant that has a discriminant of `0` - --> tests/ui-msrv/enum.rs:63:1 + --> tests/ui-msrv/enum.rs:81:1 | -63 | / enum FromZeroes3 { -64 | | A = 1, -65 | | B, -66 | | } +81 | / enum FromZeroes3 { +82 | | A = 1, +83 | | B, +84 | | } | |_^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:73:8 + --> tests/ui-msrv/enum.rs:91:8 | -73 | #[repr(C)] +91 | #[repr(C)] | ^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:79:8 + --> tests/ui-msrv/enum.rs:97:8 | -79 | #[repr(usize)] +97 | #[repr(usize)] | ^^^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:85:8 - | -85 | #[repr(isize)] - | ^^^^^ + --> tests/ui-msrv/enum.rs:103:8 + | +103 | #[repr(isize)] + | ^^^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:91:8 - | -91 | #[repr(u32)] - | ^^^ + --> tests/ui-msrv/enum.rs:109:8 + | +109 | #[repr(u32)] + | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:97:8 - | -97 | #[repr(i32)] - | ^^^ + --> tests/ui-msrv/enum.rs:115:8 + | +115 | #[repr(i32)] + | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:103:8 + --> tests/ui-msrv/enum.rs:121:8 | -103 | #[repr(u64)] +121 | #[repr(u64)] | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-msrv/enum.rs:109:8 + --> tests/ui-msrv/enum.rs:127:8 | -109 | #[repr(i64)] +127 | #[repr(i64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:119:8 + --> tests/ui-msrv/enum.rs:137:8 | -119 | #[repr(C)] +137 | #[repr(C)] | ^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:125:8 + --> tests/ui-msrv/enum.rs:143:8 | -125 | #[repr(u16)] +143 | #[repr(u16)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:131:8 + --> tests/ui-msrv/enum.rs:149:8 | -131 | #[repr(i16)] +149 | #[repr(i16)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:137:8 + --> tests/ui-msrv/enum.rs:155:8 | -137 | #[repr(u32)] +155 | #[repr(u32)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:143:8 + --> tests/ui-msrv/enum.rs:161:8 | -143 | #[repr(i32)] +161 | #[repr(i32)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:149:8 + --> tests/ui-msrv/enum.rs:167:8 | -149 | #[repr(u64)] +167 | #[repr(u64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:155:8 + --> tests/ui-msrv/enum.rs:173:8 | -155 | #[repr(i64)] +173 | #[repr(i64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:161:8 + --> tests/ui-msrv/enum.rs:179:8 | -161 | #[repr(usize)] +179 | #[repr(usize)] | ^^^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-msrv/enum.rs:167:8 + --> tests/ui-msrv/enum.rs:185:8 | -167 | #[repr(isize)] +185 | #[repr(isize)] | ^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/enum.rs:173:12 + --> tests/ui-msrv/enum.rs:191:12 | -173 | #[repr(u8, align(2))] +191 | #[repr(u8, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/enum.rs:179:12 + --> tests/ui-msrv/enum.rs:197:12 | -179 | #[repr(i8, align(2))] +197 | #[repr(i8, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/enum.rs:185:18 + --> tests/ui-msrv/enum.rs:203:18 | -185 | #[repr(align(1), align(2))] +203 | #[repr(align(1), align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/enum.rs:191:8 + --> tests/ui-msrv/enum.rs:209:8 | -191 | #[repr(align(2), align(4))] +209 | #[repr(align(2), align(4))] | ^^^^^^^^ error[E0565]: meta item in `repr` must be an identifier @@ -197,3 +197,21 @@ error[E0566]: conflicting representation hints = note: `#[deny(conflicting_repr_hints)]` on by default = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #68585 + +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-msrv/enum.rs:51:10 + | +51 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `UnsafeCell: NoCell` is not satisfied + --> tests/ui-msrv/enum.rs:59:10 + | +59 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell` + | + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (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 f4a435d5f5..3201f11ae4 100644 --- a/zerocopy-derive/tests/ui-msrv/struct.stderr +++ b/zerocopy-derive/tests/ui-msrv/struct.stderr @@ -1,45 +1,45 @@ error: unsupported on generic structs that are not repr(transparent) or repr(packed) - --> tests/ui-msrv/struct.rs:55:10 + --> tests/ui-msrv/struct.rs:69:10 | -55 | #[derive(AsBytes)] +69 | #[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:80:11 + --> tests/ui-msrv/struct.rs:94:11 | -80 | #[repr(C, align(2))] +94 | #[repr(C, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/struct.rs:84:21 + --> tests/ui-msrv/struct.rs:98:21 | -84 | #[repr(transparent, align(2))] +98 | #[repr(transparent, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/struct.rs:90:16 - | -90 | #[repr(packed, align(2))] - | ^^^^^^^^ + --> tests/ui-msrv/struct.rs:104:16 + | +104 | #[repr(packed, align(2))] + | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/struct.rs:94:18 - | -94 | #[repr(align(1), align(2))] - | ^^^^^^^^ + --> tests/ui-msrv/struct.rs:108:18 + | +108 | #[repr(align(1), align(2))] + | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/struct.rs:98:8 - | -98 | #[repr(align(2), align(4))] - | ^^^^^^^^ + --> tests/ui-msrv/struct.rs:112:8 + | +112 | #[repr(align(2), align(4))] + | ^^^^^^^^ error[E0692]: transparent struct cannot have other repr hints - --> tests/ui-msrv/struct.rs:84:8 + --> tests/ui-msrv/struct.rs:98:8 | -84 | #[repr(transparent, align(2))] +98 | #[repr(transparent, align(2))] | ^^^^^^^^^^^ ^^^^^^^^ error[E0277]: the size for values of type `[u8]` cannot be known at compilation time @@ -90,10 +90,29 @@ error[E0277]: the trait bound `NotKnownLayout: KnownLayout` is not satisfied = help: see issue #48214 = note: this error originates in the derive macro `KnownLayout` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-msrv/struct.rs:55:10 + | +55 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `UnsafeCell: NoCell` is not satisfied + --> tests/ui-msrv/struct.rs:60:10 + | +60 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell` + | + = note: required because of the requirements on the impl of `NoCell` for `[UnsafeCell; 0]` + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-msrv/struct.rs:59:10 + --> tests/ui-msrv/struct.rs:73:10 | -59 | #[derive(AsBytes)] +73 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the following implementations were found: @@ -102,9 +121,9 @@ error[E0277]: the trait bound `HasPadding: ShouldBe` is n = 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 `HasPadding: ShouldBe` is not satisfied - --> tests/ui-msrv/struct.rs:66:10 + --> tests/ui-msrv/struct.rs:80:10 | -66 | #[derive(AsBytes)] +80 | #[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 3e13059211..9ce5db19ee 100644 --- a/zerocopy-derive/tests/ui-msrv/union.stderr +++ b/zerocopy-derive/tests/ui-msrv/union.stderr @@ -1,39 +1,49 @@ error: unsupported on types with type parameters - --> tests/ui-msrv/union.rs:24:10 + --> tests/ui-msrv/union.rs:33:10 | -24 | #[derive(AsBytes)] +33 | #[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:42:11 + --> tests/ui-msrv/union.rs:51:11 | -42 | #[repr(C, align(2))] +51 | #[repr(C, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/union.rs:58:16 + --> tests/ui-msrv/union.rs:67:16 | -58 | #[repr(packed, align(2))] +67 | #[repr(packed, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/union.rs:64:18 + --> tests/ui-msrv/union.rs:73:18 | -64 | #[repr(align(1), align(2))] +73 | #[repr(align(1), align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-msrv/union.rs:70:8 + --> tests/ui-msrv/union.rs:79:8 | -70 | #[repr(align(2), align(4))] +79 | #[repr(align(2), align(4))] | ^^^^^^^^ +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-msrv/union.rs:24:10 + | +24 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = note: required because of the requirements on the impl of `NoCell` for `ManuallyDrop>` + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-msrv/union.rs:30:10 + --> tests/ui-msrv/union.rs:39:10 | -30 | #[derive(AsBytes)] +39 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the following implementations were found: diff --git a/zerocopy-derive/tests/ui-nightly/enum.rs b/zerocopy-derive/tests/ui-nightly/enum.rs index 31d5679d19..f9f9149e08 100644 --- a/zerocopy-derive/tests/ui-nightly/enum.rs +++ b/zerocopy-derive/tests/ui-nightly/enum.rs @@ -44,6 +44,24 @@ enum Generic5 { A, } +// +// NoCell errors +// + +#[derive(NoCell)] +enum NoCell1 { + A(core::cell::UnsafeCell<()>), +} + +#[derive(NoCell)] +enum Never {} + +#[derive(NoCell)] +enum NoCell2 { + Uninhabited(Never, core::cell::UnsafeCell), + Inhabited(u8), +} + // // FromZeroes errors // diff --git a/zerocopy-derive/tests/ui-nightly/enum.stderr b/zerocopy-derive/tests/ui-nightly/enum.stderr index a4d5edf35f..3fe3c7b278 100644 --- a/zerocopy-derive/tests/ui-nightly/enum.stderr +++ b/zerocopy-derive/tests/ui-nightly/enum.stderr @@ -31,149 +31,149 @@ error: must have a non-align #[repr(...)] attribute in order to guarantee this t = note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info) error: only C-like enums can implement FromZeroes - --> tests/ui-nightly/enum.rs:52:1 + --> tests/ui-nightly/enum.rs:70:1 | -52 | / enum FromZeroes1 { -53 | | A(u8), -54 | | } +70 | / enum FromZeroes1 { +71 | | A(u8), +72 | | } | |_^ error: only C-like enums can implement FromZeroes - --> tests/ui-nightly/enum.rs:57:1 + --> tests/ui-nightly/enum.rs:75:1 | -57 | / enum FromZeroes2 { -58 | | A, -59 | | B(u8), -60 | | } +75 | / enum FromZeroes2 { +76 | | A, +77 | | B(u8), +78 | | } | |_^ error: FromZeroes only supported on enums with a variant that has a discriminant of `0` - --> tests/ui-nightly/enum.rs:63:1 + --> tests/ui-nightly/enum.rs:81:1 | -63 | / enum FromZeroes3 { -64 | | A = 1, -65 | | B, -66 | | } +81 | / enum FromZeroes3 { +82 | | A = 1, +83 | | B, +84 | | } | |_^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:73:8 + --> tests/ui-nightly/enum.rs:91:8 | -73 | #[repr(C)] +91 | #[repr(C)] | ^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:79:8 + --> tests/ui-nightly/enum.rs:97:8 | -79 | #[repr(usize)] +97 | #[repr(usize)] | ^^^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:85:8 - | -85 | #[repr(isize)] - | ^^^^^ + --> tests/ui-nightly/enum.rs:103:8 + | +103 | #[repr(isize)] + | ^^^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:91:8 - | -91 | #[repr(u32)] - | ^^^ + --> tests/ui-nightly/enum.rs:109:8 + | +109 | #[repr(u32)] + | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:97:8 - | -97 | #[repr(i32)] - | ^^^ + --> tests/ui-nightly/enum.rs:115:8 + | +115 | #[repr(i32)] + | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:103:8 + --> tests/ui-nightly/enum.rs:121:8 | -103 | #[repr(u64)] +121 | #[repr(u64)] | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-nightly/enum.rs:109:8 + --> tests/ui-nightly/enum.rs:127:8 | -109 | #[repr(i64)] +127 | #[repr(i64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:119:8 + --> tests/ui-nightly/enum.rs:137:8 | -119 | #[repr(C)] +137 | #[repr(C)] | ^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:125:8 + --> tests/ui-nightly/enum.rs:143:8 | -125 | #[repr(u16)] +143 | #[repr(u16)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:131:8 + --> tests/ui-nightly/enum.rs:149:8 | -131 | #[repr(i16)] +149 | #[repr(i16)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:137:8 + --> tests/ui-nightly/enum.rs:155:8 | -137 | #[repr(u32)] +155 | #[repr(u32)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:143:8 + --> tests/ui-nightly/enum.rs:161:8 | -143 | #[repr(i32)] +161 | #[repr(i32)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:149:8 + --> tests/ui-nightly/enum.rs:167:8 | -149 | #[repr(u64)] +167 | #[repr(u64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:155:8 + --> tests/ui-nightly/enum.rs:173:8 | -155 | #[repr(i64)] +173 | #[repr(i64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:161:8 + --> tests/ui-nightly/enum.rs:179:8 | -161 | #[repr(usize)] +179 | #[repr(usize)] | ^^^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-nightly/enum.rs:167:8 + --> tests/ui-nightly/enum.rs:185:8 | -167 | #[repr(isize)] +185 | #[repr(isize)] | ^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/enum.rs:173:12 + --> tests/ui-nightly/enum.rs:191:12 | -173 | #[repr(u8, align(2))] +191 | #[repr(u8, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/enum.rs:179:12 + --> tests/ui-nightly/enum.rs:197:12 | -179 | #[repr(i8, align(2))] +197 | #[repr(i8, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/enum.rs:185:18 + --> tests/ui-nightly/enum.rs:203:18 | -185 | #[repr(align(1), align(2))] +203 | #[repr(align(1), align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/enum.rs:191:8 + --> tests/ui-nightly/enum.rs:209:8 | -191 | #[repr(align(2), align(4))] +209 | #[repr(align(2), align(4))] | ^^^^^^^^ error[E0565]: meta item in `repr` must be an identifier @@ -199,3 +199,43 @@ error[E0566]: conflicting representation hints = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #68585 = note: `#[deny(conflicting_repr_hints)]` on by default + +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-nightly/enum.rs:51:10 + | +51 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + 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 `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `UnsafeCell: NoCell` is not satisfied + --> tests/ui-nightly/enum.rs:59:10 + | +59 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + 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 `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/zerocopy-derive/tests/ui-nightly/struct.rs b/zerocopy-derive/tests/ui-nightly/struct.rs index c76dc7f952..3e31d51c77 100644 --- a/zerocopy-derive/tests/ui-nightly/struct.rs +++ b/zerocopy-derive/tests/ui-nightly/struct.rs @@ -48,6 +48,20 @@ struct KL08(u8, NotKnownLayoutDst); #[repr(C)] struct KL09(NotKnownLayout, NotKnownLayout); +// +// NoCell errors +// + +#[derive(NoCell)] +struct NoCell1 { + a: core::cell::UnsafeCell<()>, +} + +#[derive(NoCell)] +struct NoCell2 { + a: [core::cell::UnsafeCell; 0], +} + // // AsBytes errors // diff --git a/zerocopy-derive/tests/ui-nightly/struct.stderr b/zerocopy-derive/tests/ui-nightly/struct.stderr index c3abcbd182..f2602e9dd4 100644 --- a/zerocopy-derive/tests/ui-nightly/struct.stderr +++ b/zerocopy-derive/tests/ui-nightly/struct.stderr @@ -1,45 +1,45 @@ error: unsupported on generic structs that are not repr(transparent) or repr(packed) - --> tests/ui-nightly/struct.rs:55:10 + --> tests/ui-nightly/struct.rs:69:10 | -55 | #[derive(AsBytes)] +69 | #[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-nightly/struct.rs:80:11 + --> tests/ui-nightly/struct.rs:94:11 | -80 | #[repr(C, align(2))] +94 | #[repr(C, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/struct.rs:84:21 + --> tests/ui-nightly/struct.rs:98:21 | -84 | #[repr(transparent, align(2))] +98 | #[repr(transparent, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/struct.rs:90:16 - | -90 | #[repr(packed, align(2))] - | ^^^^^^^^ + --> tests/ui-nightly/struct.rs:104:16 + | +104 | #[repr(packed, align(2))] + | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/struct.rs:94:18 - | -94 | #[repr(align(1), align(2))] - | ^^^^^^^^ + --> tests/ui-nightly/struct.rs:108:18 + | +108 | #[repr(align(1), align(2))] + | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/struct.rs:98:8 - | -98 | #[repr(align(2), align(4))] - | ^^^^^^^^ + --> tests/ui-nightly/struct.rs:112:8 + | +112 | #[repr(align(2), align(4))] + | ^^^^^^^^ error[E0692]: transparent struct cannot have other repr hints - --> tests/ui-nightly/struct.rs:84:8 + --> tests/ui-nightly/struct.rs:98:8 | -84 | #[repr(transparent, align(2))] +98 | #[repr(transparent, align(2))] | ^^^^^^^^^^^ ^^^^^^^^ error[E0277]: the size for values of type `[u8]` cannot be known at compilation time @@ -114,10 +114,51 @@ error[E0277]: the trait bound `NotKnownLayout: KnownLayout` is not satisfied = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable = note: this error originates in the derive macro `KnownLayout` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-nightly/struct.rs:55:10 + | +55 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + 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 `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `UnsafeCell: NoCell` is not satisfied + --> tests/ui-nightly/struct.rs:60:10 + | +60 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = note: required for `[UnsafeCell; 0]` to implement `NoCell` + = help: see issue #48214 + = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-nightly/struct.rs:59:10 + --> tests/ui-nightly/struct.rs:73:10 | -59 | #[derive(AsBytes)] +73 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` @@ -126,9 +167,9 @@ error[E0277]: the trait bound `HasPadding: ShouldBe` is n = 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 `HasPadding: ShouldBe` is not satisfied - --> tests/ui-nightly/struct.rs:66:10 + --> tests/ui-nightly/struct.rs:80:10 | -66 | #[derive(AsBytes)] +80 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` @@ -137,7 +178,7 @@ error[E0277]: the trait bound `HasPadding: ShouldBe` is n = note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0587]: type has conflicting packed and align representation hints - --> tests/ui-nightly/struct.rs:91:1 - | -91 | struct Unaligned3; - | ^^^^^^^^^^^^^^^^^ + --> tests/ui-nightly/struct.rs:105:1 + | +105 | struct Unaligned3; + | ^^^^^^^^^^^^^^^^^ diff --git a/zerocopy-derive/tests/ui-nightly/union.rs b/zerocopy-derive/tests/ui-nightly/union.rs index 8938e78478..80ad450b7d 100644 --- a/zerocopy-derive/tests/ui-nightly/union.rs +++ b/zerocopy-derive/tests/ui-nightly/union.rs @@ -17,6 +17,15 @@ use std::mem::ManuallyDrop; fn main() {} +// +// NoCell errors +// + +#[derive(NoCell)] +union NoCell1 { + a: ManuallyDrop>, +} + // // AsBytes errors // diff --git a/zerocopy-derive/tests/ui-nightly/union.stderr b/zerocopy-derive/tests/ui-nightly/union.stderr index afc4e8c18d..504693364f 100644 --- a/zerocopy-derive/tests/ui-nightly/union.stderr +++ b/zerocopy-derive/tests/ui-nightly/union.stderr @@ -1,39 +1,60 @@ error: unsupported on types with type parameters - --> tests/ui-nightly/union.rs:24:10 + --> tests/ui-nightly/union.rs:33:10 | -24 | #[derive(AsBytes)] +33 | #[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-nightly/union.rs:42:11 + --> tests/ui-nightly/union.rs:51:11 | -42 | #[repr(C, align(2))] +51 | #[repr(C, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/union.rs:58:16 + --> tests/ui-nightly/union.rs:67:16 | -58 | #[repr(packed, align(2))] +67 | #[repr(packed, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/union.rs:64:18 + --> tests/ui-nightly/union.rs:73:18 | -64 | #[repr(align(1), align(2))] +73 | #[repr(align(1), align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-nightly/union.rs:70:8 + --> tests/ui-nightly/union.rs:79:8 | -70 | #[repr(align(2), align(4))] +79 | #[repr(align(2), align(4))] | ^^^^^^^^ +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-nightly/union.rs:24:10 + | +24 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = note: required for `ManuallyDrop>` to implement `NoCell` + = help: see issue #48214 + = help: add `#![feature(trivial_bounds)]` to the crate attributes to enable + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-nightly/union.rs:30:10 + --> tests/ui-nightly/union.rs:39:10 | -30 | #[derive(AsBytes)] +39 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` @@ -42,7 +63,7 @@ error[E0277]: the trait bound `HasPadding: ShouldBe` is n = note: this error originates in the derive macro `AsBytes` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0587]: type has conflicting packed and align representation hints - --> tests/ui-nightly/union.rs:59:1 + --> tests/ui-nightly/union.rs:68:1 | -59 | union Unaligned3 { +68 | union Unaligned3 { | ^^^^^^^^^^^^^^^^ diff --git a/zerocopy-derive/tests/ui-stable/enum.stderr b/zerocopy-derive/tests/ui-stable/enum.stderr index a47ce9c4ba..5a4e2e28a9 100644 --- a/zerocopy-derive/tests/ui-stable/enum.stderr +++ b/zerocopy-derive/tests/ui-stable/enum.stderr @@ -31,149 +31,149 @@ error: must have a non-align #[repr(...)] attribute in order to guarantee this t = note: this error originates in the derive macro `FromBytes` (in Nightly builds, run with -Z macro-backtrace for more info) error: only C-like enums can implement FromZeroes - --> tests/ui-stable/enum.rs:52:1 + --> tests/ui-stable/enum.rs:70:1 | -52 | / enum FromZeroes1 { -53 | | A(u8), -54 | | } +70 | / enum FromZeroes1 { +71 | | A(u8), +72 | | } | |_^ error: only C-like enums can implement FromZeroes - --> tests/ui-stable/enum.rs:57:1 + --> tests/ui-stable/enum.rs:75:1 | -57 | / enum FromZeroes2 { -58 | | A, -59 | | B(u8), -60 | | } +75 | / enum FromZeroes2 { +76 | | A, +77 | | B(u8), +78 | | } | |_^ error: FromZeroes only supported on enums with a variant that has a discriminant of `0` - --> tests/ui-stable/enum.rs:63:1 + --> tests/ui-stable/enum.rs:81:1 | -63 | / enum FromZeroes3 { -64 | | A = 1, -65 | | B, -66 | | } +81 | / enum FromZeroes3 { +82 | | A = 1, +83 | | B, +84 | | } | |_^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:73:8 + --> tests/ui-stable/enum.rs:91:8 | -73 | #[repr(C)] +91 | #[repr(C)] | ^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:79:8 + --> tests/ui-stable/enum.rs:97:8 | -79 | #[repr(usize)] +97 | #[repr(usize)] | ^^^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:85:8 - | -85 | #[repr(isize)] - | ^^^^^ + --> tests/ui-stable/enum.rs:103:8 + | +103 | #[repr(isize)] + | ^^^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:91:8 - | -91 | #[repr(u32)] - | ^^^ + --> tests/ui-stable/enum.rs:109:8 + | +109 | #[repr(u32)] + | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:97:8 - | -97 | #[repr(i32)] - | ^^^ + --> tests/ui-stable/enum.rs:115:8 + | +115 | #[repr(i32)] + | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:103:8 + --> tests/ui-stable/enum.rs:121:8 | -103 | #[repr(u64)] +121 | #[repr(u64)] | ^^^ error: FromBytes requires repr of "u8", "u16", "i8", or "i16" - --> tests/ui-stable/enum.rs:109:8 + --> tests/ui-stable/enum.rs:127:8 | -109 | #[repr(i64)] +127 | #[repr(i64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:119:8 + --> tests/ui-stable/enum.rs:137:8 | -119 | #[repr(C)] +137 | #[repr(C)] | ^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:125:8 + --> tests/ui-stable/enum.rs:143:8 | -125 | #[repr(u16)] +143 | #[repr(u16)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:131:8 + --> tests/ui-stable/enum.rs:149:8 | -131 | #[repr(i16)] +149 | #[repr(i16)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:137:8 + --> tests/ui-stable/enum.rs:155:8 | -137 | #[repr(u32)] +155 | #[repr(u32)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:143:8 + --> tests/ui-stable/enum.rs:161:8 | -143 | #[repr(i32)] +161 | #[repr(i32)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:149:8 + --> tests/ui-stable/enum.rs:167:8 | -149 | #[repr(u64)] +167 | #[repr(u64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:155:8 + --> tests/ui-stable/enum.rs:173:8 | -155 | #[repr(i64)] +173 | #[repr(i64)] | ^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:161:8 + --> tests/ui-stable/enum.rs:179:8 | -161 | #[repr(usize)] +179 | #[repr(usize)] | ^^^^^ error: Unaligned requires repr of "u8" or "i8", and no alignment (i.e., repr(align(N > 1))) - --> tests/ui-stable/enum.rs:167:8 + --> tests/ui-stable/enum.rs:185:8 | -167 | #[repr(isize)] +185 | #[repr(isize)] | ^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/enum.rs:173:12 + --> tests/ui-stable/enum.rs:191:12 | -173 | #[repr(u8, align(2))] +191 | #[repr(u8, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/enum.rs:179:12 + --> tests/ui-stable/enum.rs:197:12 | -179 | #[repr(i8, align(2))] +197 | #[repr(i8, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/enum.rs:185:18 + --> tests/ui-stable/enum.rs:203:18 | -185 | #[repr(align(1), align(2))] +203 | #[repr(align(1), align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/enum.rs:191:8 + --> tests/ui-stable/enum.rs:209:8 | -191 | #[repr(align(2), align(4))] +209 | #[repr(align(2), align(4))] | ^^^^^^^^ error[E0565]: meta item in `repr` must be an identifier @@ -199,3 +199,41 @@ error[E0566]: conflicting representation hints = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! = note: for more information, see issue #68585 = note: `#[deny(conflicting_repr_hints)]` on by default + +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-stable/enum.rs:51:10 + | +51 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `UnsafeCell: NoCell` is not satisfied + --> tests/ui-stable/enum.rs:59:10 + | +59 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (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 a93d7c45b0..97ab4b097a 100644 --- a/zerocopy-derive/tests/ui-stable/struct.stderr +++ b/zerocopy-derive/tests/ui-stable/struct.stderr @@ -1,45 +1,45 @@ error: unsupported on generic structs that are not repr(transparent) or repr(packed) - --> tests/ui-stable/struct.rs:55:10 + --> tests/ui-stable/struct.rs:69:10 | -55 | #[derive(AsBytes)] +69 | #[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:80:11 + --> tests/ui-stable/struct.rs:94:11 | -80 | #[repr(C, align(2))] +94 | #[repr(C, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/struct.rs:84:21 + --> tests/ui-stable/struct.rs:98:21 | -84 | #[repr(transparent, align(2))] +98 | #[repr(transparent, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/struct.rs:90:16 - | -90 | #[repr(packed, align(2))] - | ^^^^^^^^ + --> tests/ui-stable/struct.rs:104:16 + | +104 | #[repr(packed, align(2))] + | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/struct.rs:94:18 - | -94 | #[repr(align(1), align(2))] - | ^^^^^^^^ + --> tests/ui-stable/struct.rs:108:18 + | +108 | #[repr(align(1), align(2))] + | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/struct.rs:98:8 - | -98 | #[repr(align(2), align(4))] - | ^^^^^^^^ + --> tests/ui-stable/struct.rs:112:8 + | +112 | #[repr(align(2), align(4))] + | ^^^^^^^^ error[E0692]: transparent struct cannot have other repr hints - --> tests/ui-stable/struct.rs:84:8 + --> tests/ui-stable/struct.rs:98:8 | -84 | #[repr(transparent, align(2))] +98 | #[repr(transparent, align(2))] | ^^^^^^^^^^^ ^^^^^^^^ error[E0277]: the size for values of type `[u8]` cannot be known at compilation time @@ -110,10 +110,49 @@ error[E0277]: the trait bound `NotKnownLayout: KnownLayout` is not satisfied = help: see issue #48214 = note: this error originates in the derive macro `KnownLayout` (in Nightly builds, run with -Z macro-backtrace for more info) +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-stable/struct.rs:55:10 + | +55 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0277]: the trait bound `UnsafeCell: NoCell` is not satisfied + --> tests/ui-stable/struct.rs:60:10 + | +60 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = note: required for `[UnsafeCell; 0]` to implement `NoCell` + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-stable/struct.rs:59:10 + --> tests/ui-stable/struct.rs:73:10 | -59 | #[derive(AsBytes)] +73 | #[derive(AsBytes)] | ^^^^^^^ the trait `ShouldBe` is not implemented for `HasPadding` | = help: the trait `ShouldBe` is implemented for `HasPadding` @@ -121,9 +160,9 @@ error[E0277]: the trait bound `HasPadding: ShouldBe` is n = 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 `HasPadding: ShouldBe` is not satisfied - --> tests/ui-stable/struct.rs:66:10 + --> tests/ui-stable/struct.rs:80:10 | -66 | #[derive(AsBytes)] +80 | #[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 8d5cbbbd66..3738cf8df7 100644 --- a/zerocopy-derive/tests/ui-stable/union.stderr +++ b/zerocopy-derive/tests/ui-stable/union.stderr @@ -1,39 +1,59 @@ error: unsupported on types with type parameters - --> tests/ui-stable/union.rs:24:10 + --> tests/ui-stable/union.rs:33:10 | -24 | #[derive(AsBytes)] +33 | #[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:42:11 + --> tests/ui-stable/union.rs:51:11 | -42 | #[repr(C, align(2))] +51 | #[repr(C, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/union.rs:58:16 + --> tests/ui-stable/union.rs:67:16 | -58 | #[repr(packed, align(2))] +67 | #[repr(packed, align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/union.rs:64:18 + --> tests/ui-stable/union.rs:73:18 | -64 | #[repr(align(1), align(2))] +73 | #[repr(align(1), align(2))] | ^^^^^^^^ error: cannot derive Unaligned with repr(align(N > 1)) - --> tests/ui-stable/union.rs:70:8 + --> tests/ui-stable/union.rs:79:8 | -70 | #[repr(align(2), align(4))] +79 | #[repr(align(2), align(4))] | ^^^^^^^^ +error[E0277]: the trait bound `UnsafeCell<()>: NoCell` is not satisfied + --> tests/ui-stable/union.rs:24:10 + | +24 | #[derive(NoCell)] + | ^^^^^^ the trait `NoCell` is not implemented for `UnsafeCell<()>` + | + = help: the following other types implement trait `NoCell`: + bool + char + isize + i8 + i16 + i32 + i64 + i128 + and $N others + = note: required for `ManuallyDrop>` to implement `NoCell` + = help: see issue #48214 + = note: this error originates in the derive macro `NoCell` (in Nightly builds, run with -Z macro-backtrace for more info) + error[E0277]: the trait bound `HasPadding: ShouldBe` is not satisfied - --> tests/ui-stable/union.rs:30:10 + --> tests/ui-stable/union.rs:39:10 | -30 | #[derive(AsBytes)] +39 | #[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_no_cell.rs b/zerocopy-derive/tests/union_no_cell.rs new file mode 100644 index 0000000000..dec2a83599 --- /dev/null +++ b/zerocopy-derive/tests/union_no_cell.rs @@ -0,0 +1,67 @@ +// 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. + +#![allow(warnings)] + +#[macro_use] +mod util; + +use std::{cell::UnsafeCell, marker::PhantomData, option::IntoIter}; + +use {static_assertions::assert_impl_all, zerocopy::NoCell}; + +#[derive(Clone, Copy, NoCell)] +union Zst { + a: (), +} + +assert_impl_all!(Zst: NoCell); + +#[derive(NoCell)] +union One { + a: bool, +} + +assert_impl_all!(One: NoCell); + +#[derive(NoCell)] +union Two { + a: bool, + b: Zst, +} + +assert_impl_all!(Two: NoCell); + +#[derive(NoCell)] +union TypeParams<'a, T: Copy, I: Iterator> +where + I::Item: Copy, +{ + a: T, + c: I::Item, + d: u8, + e: PhantomData<&'a [u8]>, + f: PhantomData<&'static str>, + g: PhantomData, +} + +assert_impl_all!(TypeParams<'static, (), IntoIter<()>>: NoCell); + +// Deriving `NoCell` should work if the union has bounded parameters. + +#[derive(NoCell)] +#[repr(C)] +union WithParams<'a: 'b, 'b: 'a, const N: usize, T: 'a + 'b + NoCell> +where + 'a: 'b, + 'b: 'a, + T: 'a + 'b + Copy + NoCell, +{ + a: [T; N], + b: PhantomData<&'a &'b ()>, + c: PhantomData>, + d: &'a UnsafeCell<()>, +} + +assert_impl_all!(WithParams<'static, 'static, 42, u8>: NoCell); diff --git a/zerocopy-derive/tests/util.rs b/zerocopy-derive/tests/util.rs index a8656fb20f..796d622432 100644 --- a/zerocopy-derive/tests/util.rs +++ b/zerocopy-derive/tests/util.rs @@ -6,7 +6,7 @@ // This file may not be copied, modified, or distributed except according to // those terms. -use zerocopy::{AsBytes, FromBytes, FromZeroes, KnownLayout}; +use zerocopy::{AsBytes, FromBytes, FromZeroes, KnownLayout, NoCell}; /// A type that doesn't implement any zerocopy traits. pub struct NotZerocopy(T); @@ -15,6 +15,6 @@ pub struct NotZerocopy(T); /// /// Though `u16` has alignment 2 on some platforms, it's not guaranteed. By /// contrast, `AU16` is guaranteed to have alignment 2. -#[derive(KnownLayout, FromZeroes, FromBytes, AsBytes, Copy, Clone)] +#[derive(KnownLayout, NoCell, FromZeroes, FromBytes, AsBytes, Copy, Clone)] #[repr(C, align(2))] pub struct AU16(u16);