Skip to content

Commit 2b04ca9

Browse files
committed
Add #[derive(Clone, Copy)] to anonymous adts
Fix the `AssertBoundIsClone` error for anonymous adts.
1 parent 822d6dc commit 2b04ca9

File tree

5 files changed

+193
-176
lines changed

5 files changed

+193
-176
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2161,6 +2161,10 @@ impl TyKind {
21612161
None
21622162
}
21632163
}
2164+
2165+
pub fn is_anon_adt(&self) -> bool {
2166+
matches!(self, TyKind::AnonStruct(..) | TyKind::AnonUnion(..))
2167+
}
21642168
}
21652169

21662170
/// Syntax used to declare a trait object.

Diff for: compiler/rustc_builtin_macros/src/deriving/clone.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ fn cs_clone_simple(
110110
&& !seen_type_names.insert(name)
111111
{
112112
// Already produced an assertion for this type.
113-
} else {
113+
// Anonymous structs or unions must be eliminated as they cannot be
114+
// type parameters.
115+
} else if !field.ty.kind.is_anon_adt() {
114116
// let _: AssertParamIsClone<FieldTy>;
115117
super::assert_ty_bounds(
116118
cx,

Diff for: compiler/rustc_builtin_macros/src/deriving/mod.rs

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

Diff for: tests/ui/union/unnamed-fields/field_uniqueness_check.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#![allow(incomplete_features)]
22
#![feature(unnamed_fields)]
33

4+
#[derive(Clone, Copy)]
45
#[repr(C)]
56
struct Foo {
67
a: u8,
78
}
89

10+
#[derive(Clone, Copy)]
911
#[repr(C)]
1012
struct Bar {
1113
_: union {
@@ -15,6 +17,7 @@ struct Bar {
1517

1618

1719
// duplicated with a normal field
20+
#[derive(Clone, Copy)]
1821
#[repr(C)]
1922
union A {
2023
// referent field
@@ -44,6 +47,7 @@ union A {
4447
}
4548

4649
// duplicated with a nested field
50+
#[derive(Clone, Copy)]
4751
#[repr(C)]
4852
struct B {
4953
_: union {
@@ -95,6 +99,7 @@ struct B {
9599
}
96100

97101
// duplicated with a more nested field
102+
#[derive(Clone, Copy)]
98103
#[repr(C)]
99104
union C {
100105
_: struct {
@@ -168,6 +173,7 @@ union C {
168173
}
169174

170175
// duplicated with a nested field in a named adt
176+
#[derive(Clone, Copy)]
171177
#[repr(C)]
172178
struct D {
173179
// referent field `a`
@@ -196,6 +202,7 @@ struct D {
196202
}
197203

198204
// duplicated with a nested field in a nested field of a named adt
205+
#[derive(Clone, Copy)]
199206
#[repr(C)]
200207
union D2 {
201208
// referent field `a`
@@ -224,6 +231,7 @@ union D2 {
224231
}
225232

226233
// duplicated with a nested field in a named adt in an anonymous adt
234+
#[derive(Clone, Copy)]
227235
#[repr(C)]
228236
struct E {
229237
_: struct {
@@ -276,6 +284,7 @@ struct E {
276284

277285
// duplicated with a nested field in a named adt in an anonymous adt
278286
#[repr(C)]
287+
#[derive(Clone, Copy)]
279288
union E2 {
280289
_: struct {
281290
// referent field `a`

0 commit comments

Comments
 (0)