Skip to content

Commit c2a0ef6

Browse files
committed
Do not add leading asterisk in the PartialEq
Adding leading asterisk can cause compilation failure for the _types_ that don't implement the `Copy`.
1 parent 5a6e995 commit c2a0ef6

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

Diff for: compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn expand_deriving_partial_eq(
3131
};
3232

3333
// We received arguments of type `&T`. Convert them to type `T` by stripping
34-
// any leading `&` or adding `*`. This isn't necessary for type checking, but
34+
// any leading `&`. This isn't necessary for type checking, but
3535
// it results in better error messages if something goes wrong.
3636
//
3737
// Note: for arguments that look like `&{ x }`, which occur with packed
@@ -53,8 +53,7 @@ pub fn expand_deriving_partial_eq(
5353
inner.clone()
5454
}
5555
} else {
56-
// No leading `&`: add a leading `*`.
57-
cx.expr_deref(field.span, expr.clone())
56+
expr.clone()
5857
}
5958
};
6059
cx.expr_binary(

Diff for: tests/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0369]: binary operation `==` cannot be applied to type `Error`
1+
error[E0369]: binary operation `==` cannot be applied to type `&Error`
22
--> $DIR/derives-span-PartialEq-enum-struct-variant.rs:9:6
33
|
44
LL | #[derive(PartialEq)]

Diff for: tests/ui/derives/derives-span-PartialEq-enum.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0369]: binary operation `==` cannot be applied to type `Error`
1+
error[E0369]: binary operation `==` cannot be applied to type `&Error`
22
--> $DIR/derives-span-PartialEq-enum.rs:9:6
33
|
44
LL | #[derive(PartialEq)]

Diff for: tests/ui/deriving/deriving-all-codegen.rs

+14
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ enum EnumGeneric<T, U> {
156156
Two(U),
157157
}
158158

159+
// An enum that has variant, which does't implement `Copy`.
160+
#[derive(PartialEq)]
161+
enum NonCopyEnum {
162+
// The `dyn NonCopyTrait` implements `PartialEq`, but it doesn't require `Copy`.
163+
// So we cannot generate `PartialEq` with dereference.
164+
NonCopyField(Box<dyn NonCopyTrait>),
165+
}
166+
trait NonCopyTrait {}
167+
impl PartialEq for dyn NonCopyTrait {
168+
fn eq(&self, _other: &Self) -> bool {
169+
true
170+
}
171+
}
172+
159173
// A union. Most builtin traits are not derivable for unions.
160174
#[derive(Clone, Copy)]
161175
pub union Union {

Diff for: tests/ui/deriving/deriving-all-codegen.stdout

+32-8
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ impl ::core::cmp::PartialEq for Enum1 {
876876
fn eq(&self, other: &Enum1) -> bool {
877877
match (self, other) {
878878
(Enum1::Single { x: __self_0 }, Enum1::Single { x: __arg1_0 }) =>
879-
*__self_0 == *__arg1_0,
879+
__self_0 == __arg1_0,
880880
}
881881
}
882882
}
@@ -1119,10 +1119,10 @@ impl ::core::cmp::PartialEq for Mixed {
11191119
__self_discr == __arg1_discr &&
11201120
match (self, other) {
11211121
(Mixed::R(__self_0), Mixed::R(__arg1_0)) =>
1122-
*__self_0 == *__arg1_0,
1122+
__self_0 == __arg1_0,
11231123
(Mixed::S { d1: __self_0, d2: __self_1 }, Mixed::S {
11241124
d1: __arg1_0, d2: __arg1_1 }) =>
1125-
*__self_0 == *__arg1_0 && *__self_1 == *__arg1_1,
1125+
__self_0 == __arg1_0 && __self_1 == __arg1_1,
11261126
_ => true,
11271127
}
11281128
}
@@ -1245,11 +1245,11 @@ impl ::core::cmp::PartialEq for Fielded {
12451245
__self_discr == __arg1_discr &&
12461246
match (self, other) {
12471247
(Fielded::X(__self_0), Fielded::X(__arg1_0)) =>
1248-
*__self_0 == *__arg1_0,
1248+
__self_0 == __arg1_0,
12491249
(Fielded::Y(__self_0), Fielded::Y(__arg1_0)) =>
1250-
*__self_0 == *__arg1_0,
1250+
__self_0 == __arg1_0,
12511251
(Fielded::Z(__self_0), Fielded::Z(__arg1_0)) =>
1252-
*__self_0 == *__arg1_0,
1252+
__self_0 == __arg1_0,
12531253
_ => unsafe { ::core::intrinsics::unreachable() }
12541254
}
12551255
}
@@ -1368,9 +1368,9 @@ impl<T: ::core::cmp::PartialEq, U: ::core::cmp::PartialEq>
13681368
__self_discr == __arg1_discr &&
13691369
match (self, other) {
13701370
(EnumGeneric::One(__self_0), EnumGeneric::One(__arg1_0)) =>
1371-
*__self_0 == *__arg1_0,
1371+
__self_0 == __arg1_0,
13721372
(EnumGeneric::Two(__self_0), EnumGeneric::Two(__arg1_0)) =>
1373-
*__self_0 == *__arg1_0,
1373+
__self_0 == __arg1_0,
13741374
_ => unsafe { ::core::intrinsics::unreachable() }
13751375
}
13761376
}
@@ -1426,6 +1426,30 @@ impl<T: ::core::cmp::Ord, U: ::core::cmp::Ord> ::core::cmp::Ord for
14261426
}
14271427
}
14281428

1429+
// An enum that has variant, which does't implement `Copy`.
1430+
enum NonCopyEnum {
1431+
1432+
// The `dyn NonCopyTrait` implements `PartialEq`, but it doesn't require `Copy`.
1433+
// So we cannot generate `PartialEq` with dereference.
1434+
NonCopyField(Box<dyn NonCopyTrait>),
1435+
}
1436+
#[automatically_derived]
1437+
impl ::core::marker::StructuralPartialEq for NonCopyEnum { }
1438+
#[automatically_derived]
1439+
impl ::core::cmp::PartialEq for NonCopyEnum {
1440+
#[inline]
1441+
fn eq(&self, other: &NonCopyEnum) -> bool {
1442+
match (self, other) {
1443+
(NonCopyEnum::NonCopyField(__self_0),
1444+
NonCopyEnum::NonCopyField(__arg1_0)) => __self_0 == __arg1_0,
1445+
}
1446+
}
1447+
}
1448+
trait NonCopyTrait {}
1449+
impl PartialEq for dyn NonCopyTrait {
1450+
fn eq(&self, _other: &Self) -> bool { true }
1451+
}
1452+
14291453
// A union. Most builtin traits are not derivable for unions.
14301454
pub union Union {
14311455
pub b: bool,

0 commit comments

Comments
 (0)