Skip to content

Commit

Permalink
Add #[derive(Clone, Copy)] to anonymous adts
Browse files Browse the repository at this point in the history
Fix the `AssertBoundIsClone` error for anonymous adts.
  • Loading branch information
frank-king committed Feb 12, 2024
1 parent 822d6dc commit 2b04ca9
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 176 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,10 @@ impl TyKind {
None
}
}

pub fn is_anon_adt(&self) -> bool {
matches!(self, TyKind::AnonStruct(..) | TyKind::AnonUnion(..))
}
}

/// Syntax used to declare a trait object.
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ fn cs_clone_simple(
&& !seen_type_names.insert(name)
{
// 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>;
super::assert_ty_bounds(
cx,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_builtin_macros/src/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ fn assert_ty_bounds(
span: Span,
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>;`.
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)]);
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/union/unnamed-fields/field_uniqueness_check.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#![allow(incomplete_features)]
#![feature(unnamed_fields)]

#[derive(Clone, Copy)]
#[repr(C)]
struct Foo {
a: u8,
}

#[derive(Clone, Copy)]
#[repr(C)]
struct Bar {
_: union {
Expand All @@ -15,6 +17,7 @@ struct Bar {


// duplicated with a normal field
#[derive(Clone, Copy)]
#[repr(C)]
union A {
// referent field
Expand Down Expand Up @@ -44,6 +47,7 @@ union A {
}

// duplicated with a nested field
#[derive(Clone, Copy)]
#[repr(C)]
struct B {
_: union {
Expand Down Expand Up @@ -95,6 +99,7 @@ struct B {
}

// duplicated with a more nested field
#[derive(Clone, Copy)]
#[repr(C)]
union C {
_: struct {
Expand Down Expand Up @@ -168,6 +173,7 @@ union C {
}

// duplicated with a nested field in a named adt
#[derive(Clone, Copy)]
#[repr(C)]
struct D {
// referent field `a`
Expand Down Expand Up @@ -196,6 +202,7 @@ struct D {
}

// duplicated with a nested field in a nested field of a named adt
#[derive(Clone, Copy)]
#[repr(C)]
union D2 {
// referent field `a`
Expand Down Expand Up @@ -224,6 +231,7 @@ union D2 {
}

// duplicated with a nested field in a named adt in an anonymous adt
#[derive(Clone, Copy)]
#[repr(C)]
struct E {
_: struct {
Expand Down Expand Up @@ -276,6 +284,7 @@ struct E {

// duplicated with a nested field in a named adt in an anonymous adt
#[repr(C)]
#[derive(Clone, Copy)]
union E2 {
_: struct {
// referent field `a`
Expand Down
Loading

0 comments on commit 2b04ca9

Please sign in to comment.