1
Fork 0

Do not add leading asterisk in the PartialEq

Adding leading asterisk can cause compilation failure for
the _types_ that don't implement the `Copy`.
This commit is contained in:
Daniel Sedlak 2024-04-19 12:13:59 +02:00
parent 5a6e995980
commit c2a0ef65da
5 changed files with 50 additions and 13 deletions

View file

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

View file

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

View file

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

View file

@ -156,6 +156,20 @@ enum EnumGeneric<T, U> {
Two(U), Two(U),
} }
// An enum that has variant, which does't implement `Copy`.
#[derive(PartialEq)]
enum NonCopyEnum {
// The `dyn NonCopyTrait` implements `PartialEq`, but it doesn't require `Copy`.
// So we cannot generate `PartialEq` with dereference.
NonCopyField(Box<dyn NonCopyTrait>),
}
trait NonCopyTrait {}
impl PartialEq for dyn NonCopyTrait {
fn eq(&self, _other: &Self) -> bool {
true
}
}
// A union. Most builtin traits are not derivable for unions. // A union. Most builtin traits are not derivable for unions.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub union Union { pub union Union {

View file

@ -876,7 +876,7 @@ impl ::core::cmp::PartialEq for Enum1 {
fn eq(&self, other: &Enum1) -> bool { fn eq(&self, other: &Enum1) -> bool {
match (self, other) { match (self, other) {
(Enum1::Single { x: __self_0 }, Enum1::Single { x: __arg1_0 }) => (Enum1::Single { x: __self_0 }, Enum1::Single { x: __arg1_0 }) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
} }
} }
} }
@ -1119,10 +1119,10 @@ impl ::core::cmp::PartialEq for Mixed {
__self_discr == __arg1_discr && __self_discr == __arg1_discr &&
match (self, other) { match (self, other) {
(Mixed::R(__self_0), Mixed::R(__arg1_0)) => (Mixed::R(__self_0), Mixed::R(__arg1_0)) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
(Mixed::S { d1: __self_0, d2: __self_1 }, Mixed::S { (Mixed::S { d1: __self_0, d2: __self_1 }, Mixed::S {
d1: __arg1_0, d2: __arg1_1 }) => d1: __arg1_0, d2: __arg1_1 }) =>
*__self_0 == *__arg1_0 && *__self_1 == *__arg1_1, __self_0 == __arg1_0 && __self_1 == __arg1_1,
_ => true, _ => true,
} }
} }
@ -1245,11 +1245,11 @@ impl ::core::cmp::PartialEq for Fielded {
__self_discr == __arg1_discr && __self_discr == __arg1_discr &&
match (self, other) { match (self, other) {
(Fielded::X(__self_0), Fielded::X(__arg1_0)) => (Fielded::X(__self_0), Fielded::X(__arg1_0)) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
(Fielded::Y(__self_0), Fielded::Y(__arg1_0)) => (Fielded::Y(__self_0), Fielded::Y(__arg1_0)) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
(Fielded::Z(__self_0), Fielded::Z(__arg1_0)) => (Fielded::Z(__self_0), Fielded::Z(__arg1_0)) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() } _ => unsafe { ::core::intrinsics::unreachable() }
} }
} }
@ -1368,9 +1368,9 @@ impl<T: ::core::cmp::PartialEq, U: ::core::cmp::PartialEq>
__self_discr == __arg1_discr && __self_discr == __arg1_discr &&
match (self, other) { match (self, other) {
(EnumGeneric::One(__self_0), EnumGeneric::One(__arg1_0)) => (EnumGeneric::One(__self_0), EnumGeneric::One(__arg1_0)) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
(EnumGeneric::Two(__self_0), EnumGeneric::Two(__arg1_0)) => (EnumGeneric::Two(__self_0), EnumGeneric::Two(__arg1_0)) =>
*__self_0 == *__arg1_0, __self_0 == __arg1_0,
_ => unsafe { ::core::intrinsics::unreachable() } _ => unsafe { ::core::intrinsics::unreachable() }
} }
} }
@ -1426,6 +1426,30 @@ impl<T: ::core::cmp::Ord, U: ::core::cmp::Ord> ::core::cmp::Ord for
} }
} }
// An enum that has variant, which does't implement `Copy`.
enum NonCopyEnum {
// The `dyn NonCopyTrait` implements `PartialEq`, but it doesn't require `Copy`.
// So we cannot generate `PartialEq` with dereference.
NonCopyField(Box<dyn NonCopyTrait>),
}
#[automatically_derived]
impl ::core::marker::StructuralPartialEq for NonCopyEnum { }
#[automatically_derived]
impl ::core::cmp::PartialEq for NonCopyEnum {
#[inline]
fn eq(&self, other: &NonCopyEnum) -> bool {
match (self, other) {
(NonCopyEnum::NonCopyField(__self_0),
NonCopyEnum::NonCopyField(__arg1_0)) => __self_0 == __arg1_0,
}
}
}
trait NonCopyTrait {}
impl PartialEq for dyn NonCopyTrait {
fn eq(&self, _other: &Self) -> bool { true }
}
// A union. Most builtin traits are not derivable for unions. // A union. Most builtin traits are not derivable for unions.
pub union Union { pub union Union {
pub b: bool, pub b: bool,