Add #[derive(Clone, Copy)] to anonymous adts

Fix the `AssertBoundIsClone` error for anonymous adts.
This commit is contained in:
Frank King 2024-01-20 12:55:21 +08:00
parent 822d6dc5fd
commit 2b04ca94bb
5 changed files with 193 additions and 176 deletions

View file

@ -2161,6 +2161,10 @@ impl TyKind {
None None
} }
} }
pub fn is_anon_adt(&self) -> bool {
matches!(self, TyKind::AnonStruct(..) | TyKind::AnonUnion(..))
}
} }
/// Syntax used to declare a trait object. /// Syntax used to declare a trait object.

View file

@ -110,7 +110,9 @@ fn cs_clone_simple(
&& !seen_type_names.insert(name) && !seen_type_names.insert(name)
{ {
// Already produced an assertion for this type. // Already produced an assertion for this type.
} else { // Anonymous structs or unions must be eliminated as they cannot be
// type parameters.
} else if !field.ty.kind.is_anon_adt() {
// let _: AssertParamIsClone<FieldTy>; // let _: AssertParamIsClone<FieldTy>;
super::assert_ty_bounds( super::assert_ty_bounds(
cx, cx,

View file

@ -123,6 +123,8 @@ fn assert_ty_bounds(
span: Span, span: Span,
assert_path: &[Symbol], assert_path: &[Symbol],
) { ) {
// Deny anonymous structs or unions to avoid wierd errors.
assert!(!ty.kind.is_anon_adt(), "Anonymous structs or unions cannot be type parameters");
// Generate statement `let _: assert_path<ty>;`. // Generate statement `let _: assert_path<ty>;`.
let span = cx.with_def_site_ctxt(span); let span = cx.with_def_site_ctxt(span);
let assert_path = cx.path_all(span, true, cx.std_path(assert_path), vec![GenericArg::Type(ty)]); let assert_path = cx.path_all(span, true, cx.std_path(assert_path), vec![GenericArg::Type(ty)]);

View file

@ -1,11 +1,13 @@
#![allow(incomplete_features)] #![allow(incomplete_features)]
#![feature(unnamed_fields)] #![feature(unnamed_fields)]
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
struct Foo { struct Foo {
a: u8, a: u8,
} }
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
struct Bar { struct Bar {
_: union { _: union {
@ -15,6 +17,7 @@ struct Bar {
// duplicated with a normal field // duplicated with a normal field
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
union A { union A {
// referent field // referent field
@ -44,6 +47,7 @@ union A {
} }
// duplicated with a nested field // duplicated with a nested field
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
struct B { struct B {
_: union { _: union {
@ -95,6 +99,7 @@ struct B {
} }
// duplicated with a more nested field // duplicated with a more nested field
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
union C { union C {
_: struct { _: struct {
@ -168,6 +173,7 @@ union C {
} }
// duplicated with a nested field in a named adt // duplicated with a nested field in a named adt
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
struct D { struct D {
// referent field `a` // referent field `a`
@ -196,6 +202,7 @@ struct D {
} }
// duplicated with a nested field in a nested field of a named adt // duplicated with a nested field in a nested field of a named adt
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
union D2 { union D2 {
// referent field `a` // referent field `a`
@ -224,6 +231,7 @@ union D2 {
} }
// duplicated with a nested field in a named adt in an anonymous adt // duplicated with a nested field in a named adt in an anonymous adt
#[derive(Clone, Copy)]
#[repr(C)] #[repr(C)]
struct E { struct E {
_: struct { _: struct {
@ -276,6 +284,7 @@ struct E {
// duplicated with a nested field in a named adt in an anonymous adt // duplicated with a nested field in a named adt in an anonymous adt
#[repr(C)] #[repr(C)]
#[derive(Clone, Copy)]
union E2 { union E2 {
_: struct { _: struct {
// referent field `a` // referent field `a`

File diff suppressed because it is too large Load diff