From 5a6e995980e8e8945677e59ff590460bf5de4d55 Mon Sep 17 00:00:00 2001 From: Daniel Sedlak Date: Fri, 19 Apr 2024 12:19:45 +0200 Subject: [PATCH 1/2] Fix typos --- compiler/rustc_builtin_macros/src/deriving/mod.rs | 2 +- compiler/rustc_builtin_macros/src/format.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/mod.rs b/compiler/rustc_builtin_macros/src/deriving/mod.rs index 9f786d22c932f..8401e38823096 100644 --- a/compiler/rustc_builtin_macros/src/deriving/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/mod.rs @@ -123,7 +123,7 @@ fn assert_ty_bounds( span: Span, assert_path: &[Symbol], ) { - // Deny anonymous structs or unions to avoid wierd errors. + // Deny anonymous structs or unions to avoid weird errors. assert!(!ty.kind.is_anon_adt(), "Anonymous structs or unions cannot be type parameters"); // Generate statement `let _: assert_path;`. let span = cx.with_def_site_ctxt(span); diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs index 51d6058a744fc..40b5595f91969 100644 --- a/compiler/rustc_builtin_macros/src/format.rs +++ b/compiler/rustc_builtin_macros/src/format.rs @@ -316,7 +316,7 @@ fn make_format_args( let mut used = vec![false; args.explicit_args().len()]; let mut invalid_refs = Vec::new(); - let mut numeric_refences_to_named_arg = Vec::new(); + let mut numeric_references_to_named_arg = Vec::new(); enum ArgRef<'a> { Index(usize), @@ -337,7 +337,7 @@ fn make_format_args( used[index] = true; if arg.kind.ident().is_some() { // This was a named argument, but it was used as a positional argument. - numeric_refences_to_named_arg.push((index, span, used_as)); + numeric_references_to_named_arg.push((index, span, used_as)); } Ok(index) } else { @@ -545,7 +545,7 @@ fn make_format_args( // Only check for unused named argument names if there are no other errors to avoid causing // too much noise in output errors, such as when a named argument is entirely unused. if invalid_refs.is_empty() && !has_unused && !unnamed_arg_after_named_arg { - for &(index, span, used_as) in &numeric_refences_to_named_arg { + for &(index, span, used_as) in &numeric_references_to_named_arg { let (position_sp_to_replace, position_sp_for_msg) = match used_as { Placeholder(pspan) => (span, pspan), Precision => { From c2a0ef65da366c7e5235b8f9781278d62ff2f8e1 Mon Sep 17 00:00:00 2001 From: Daniel Sedlak Date: Fri, 19 Apr 2024 12:13:59 +0200 Subject: [PATCH 2/2] Do not add leading asterisk in the `PartialEq` Adding leading asterisk can cause compilation failure for the _types_ that don't implement the `Copy`. --- .../src/deriving/cmp/partial_eq.rs | 5 +-- ...-span-PartialEq-enum-struct-variant.stderr | 2 +- .../derives-span-PartialEq-enum.stderr | 2 +- tests/ui/deriving/deriving-all-codegen.rs | 14 +++++++ tests/ui/deriving/deriving-all-codegen.stdout | 40 +++++++++++++++---- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs index 234918ae42961..2fb63fd8739b2 100644 --- a/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs +++ b/compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs @@ -31,7 +31,7 @@ pub fn expand_deriving_partial_eq( }; // 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. // // Note: for arguments that look like `&{ x }`, which occur with packed @@ -53,8 +53,7 @@ pub fn expand_deriving_partial_eq( inner.clone() } } else { - // No leading `&`: add a leading `*`. - cx.expr_deref(field.span, expr.clone()) + expr.clone() } }; cx.expr_binary( diff --git a/tests/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr b/tests/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr index d0b14ef94c17b..e88a523ef4f17 100644 --- a/tests/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr +++ b/tests/ui/derives/derives-span-PartialEq-enum-struct-variant.stderr @@ -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 | LL | #[derive(PartialEq)] diff --git a/tests/ui/derives/derives-span-PartialEq-enum.stderr b/tests/ui/derives/derives-span-PartialEq-enum.stderr index f69451ac793ee..80b225446b47d 100644 --- a/tests/ui/derives/derives-span-PartialEq-enum.stderr +++ b/tests/ui/derives/derives-span-PartialEq-enum.stderr @@ -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 | LL | #[derive(PartialEq)] diff --git a/tests/ui/deriving/deriving-all-codegen.rs b/tests/ui/deriving/deriving-all-codegen.rs index 498930fc0c664..6fa4f74f2a508 100644 --- a/tests/ui/deriving/deriving-all-codegen.rs +++ b/tests/ui/deriving/deriving-all-codegen.rs @@ -156,6 +156,20 @@ enum EnumGeneric { 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), +} +trait NonCopyTrait {} +impl PartialEq for dyn NonCopyTrait { + fn eq(&self, _other: &Self) -> bool { + true + } +} + // A union. Most builtin traits are not derivable for unions. #[derive(Clone, Copy)] pub union Union { diff --git a/tests/ui/deriving/deriving-all-codegen.stdout b/tests/ui/deriving/deriving-all-codegen.stdout index 9f8a9f30ff682..6b69b57c51619 100644 --- a/tests/ui/deriving/deriving-all-codegen.stdout +++ b/tests/ui/deriving/deriving-all-codegen.stdout @@ -876,7 +876,7 @@ impl ::core::cmp::PartialEq for Enum1 { fn eq(&self, other: &Enum1) -> bool { match (self, other) { (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 && match (self, other) { (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 { d1: __arg1_0, d2: __arg1_1 }) => - *__self_0 == *__arg1_0 && *__self_1 == *__arg1_1, + __self_0 == __arg1_0 && __self_1 == __arg1_1, _ => true, } } @@ -1245,11 +1245,11 @@ impl ::core::cmp::PartialEq for Fielded { __self_discr == __arg1_discr && match (self, other) { (Fielded::X(__self_0), Fielded::X(__arg1_0)) => - *__self_0 == *__arg1_0, + __self_0 == __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)) => - *__self_0 == *__arg1_0, + __self_0 == __arg1_0, _ => unsafe { ::core::intrinsics::unreachable() } } } @@ -1368,9 +1368,9 @@ impl __self_discr == __arg1_discr && match (self, other) { (EnumGeneric::One(__self_0), EnumGeneric::One(__arg1_0)) => - *__self_0 == *__arg1_0, + __self_0 == __arg1_0, (EnumGeneric::Two(__self_0), EnumGeneric::Two(__arg1_0)) => - *__self_0 == *__arg1_0, + __self_0 == __arg1_0, _ => unsafe { ::core::intrinsics::unreachable() } } } @@ -1426,6 +1426,30 @@ impl ::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), +} +#[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. pub union Union { pub b: bool,