[clang] 02bb2be - [clang] Fix diagnostics for defaulted, implicitly deleted 'operator=='.

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 23 01:58:23 PDT 2023


Author: Amirreza Ashouri
Date: 2023-07-23T10:58:17+02:00
New Revision: 02bb2beeef3d93360694de29573430f584caafe9

URL: https://github.com/llvm/llvm-project/commit/02bb2beeef3d93360694de29573430f584caafe9
DIFF: https://github.com/llvm/llvm-project/commit/02bb2beeef3d93360694de29573430f584caafe9.diff

LOG: [clang] Fix diagnostics for defaulted, implicitly deleted 'operator=='.

https://godbolt.org/z/cMKE3o1aG

According to the issue https://github.com/llvm/llvm-project/issues/63960 , compiler falsely complains that no viable `operator<=>` was found while we are actually looking for a `operator==`.
This bug has been fixed through adding a check of the `OverloadedOperatorKind` type's object, `OO`, to see if it is `OO_EqualEqual` in addition to `OO_ExclaimEqual`.

Reviewed By: #clang-language-wg, cor3ntin

Differential Revision: https://reviews.llvm.org/D155714

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaDeclCXX.cpp
    clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
    clang/test/CXX/class/class.compare/class.compare.default/p4.cpp
    clang/test/CXX/class/class.compare/class.compare.secondary/p2.cpp
    clang/test/CXX/class/class.compare/class.eq/p2.cpp
    clang/test/CXX/class/class.compare/class.spaceship/p1.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1944605f09a25f..9298f24d62970c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -407,8 +407,10 @@ Improvements to Clang's diagnostics
   by making use of the syntactical structure of function calls. This avoids display
   of syntactically invalid codes in diagnostics.
   (`#57081: <https://github.com/llvm/llvm-project/issues/57081>`_)
-- Clang no longer emits inappropriate notes about the loss of ``__unaligned`` qualifier
-  on overload resolution, when the actual reason for the failure is loss of other qualifiers.
+- The note emitted when an ``operator==`` was defaulted as deleted used to refer to
+  the lack of a data member's "three-way comparison operator". It now refers correctly
+  to the data member's ``operator==``.
+  (`#63960: <https://github.com/llvm/llvm-project/issues/63960>`_)
 - Clang's notes about unconvertible types in overload resolution failure now covers
   the source range of parameter declaration of the candidate function declaration.
 

diff  --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 30e8e380e6596b..40fbaa043c1bfb 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -8209,7 +8209,8 @@ class DefaultedComparisonAnalyzer
 
       if (Diagnose == ExplainDeleted) {
         S.Diag(Subobj.Loc, diag::note_defaulted_comparison_no_viable_function)
-            << FD << (OO == OO_ExclaimEqual) << Subobj.Kind << Subobj.Decl;
+            << FD << (OO == OO_EqualEqual || OO == OO_ExclaimEqual)
+            << Subobj.Kind << Subobj.Decl;
 
         // For a three-way comparison, list both the candidates for the
         // original operator and the candidates for the synthesized operator.

diff  --git a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
index 9094d400fd5467..b595825e1750ad 100644
--- a/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.default/p1.cpp
@@ -153,7 +153,7 @@ namespace P1946 {
     friend bool operator==(A &, A &); // expected-note {{would lose const qualifier}}
   };
   struct B {
-    A a; // expected-note {{no viable three-way comparison}}
+    A a; // expected-note {{no viable 'operator=='}}
     friend bool operator==(B, B) = default; // ok
     friend bool operator==(const B&, const B&) = default; // expected-warning {{deleted}} expected-note{{replace 'default'}}
   };

diff  --git a/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp
index fab2abb7dee004..02cdd7f85aebfa 100644
--- a/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp
@@ -100,7 +100,7 @@ namespace DeleteAfterFirstDecl {
   struct Q {
     struct X {
       friend std::strong_ordering operator<=>(const X&, const X&);
-    } x; // expected-note {{no viable three-way comparison}}
+    } x; // expected-note {{no viable 'operator=='}}
     // expected-error at +1 {{defaulting the corresponding implicit 'operator==' for this defaulted 'operator<=>' would delete it after its first declaration}}
     friend std::strong_ordering operator<=>(const Q&, const Q&) = default;
   };

diff  --git a/clang/test/CXX/class/class.compare/class.compare.secondary/p2.cpp b/clang/test/CXX/class/class.compare/class.compare.secondary/p2.cpp
index 07d43f15aac14c..e53738632e805e 100644
--- a/clang/test/CXX/class/class.compare/class.compare.secondary/p2.cpp
+++ b/clang/test/CXX/class/class.compare/class.compare.secondary/p2.cpp
@@ -5,6 +5,14 @@ struct A {
   // expected-note at -1 {{defaulted 'operator!=' is implicitly deleted because there is no viable 'operator==' for 'A'}}
 };
 
+struct A2 {
+  struct E {};
+  E e;
+  bool operator==(const A2&) const = default; // expected-warning {{explicitly defaulted equality comparison operator is implicitly deleted}} expected-note{{replace 'default'}}
+  // expected-note at -2 {{defaulted 'operator==' is implicitly deleted because there is no viable 'operator==' for member 'e'}}
+};
+
+
 struct Q {};
 bool operator!=(Q, Q); // expected-note {{defaulted 'operator!=' is implicitly deleted because this non-rewritten comparison function would be the best match for the comparison}}
 struct B {

diff  --git a/clang/test/CXX/class/class.compare/class.eq/p2.cpp b/clang/test/CXX/class/class.compare/class.eq/p2.cpp
index 7cb07b784bc28b..7ff827e208aa7b 100644
--- a/clang/test/CXX/class/class.compare/class.eq/p2.cpp
+++ b/clang/test/CXX/class/class.compare/class.eq/p2.cpp
@@ -37,7 +37,7 @@ struct H3 {
 template<typename T> struct X {
   X();
   bool operator==(const X&) const = default; // #x expected-note 4{{deleted here}}
-  T t; // expected-note 3{{because there is no viable three-way comparison function for member 't'}}
+  T t; // expected-note 3{{because there is no viable 'operator==' for member 't'}}
        // expected-note at -1 {{because it would invoke a deleted comparison function for member 't'}}
 };
 

diff  --git a/clang/test/CXX/class/class.compare/class.spaceship/p1.cpp b/clang/test/CXX/class/class.compare/class.spaceship/p1.cpp
index cd5c8b155a9fa6..7e801f459ab2bd 100644
--- a/clang/test/CXX/class/class.compare/class.spaceship/p1.cpp
+++ b/clang/test/CXX/class/class.compare/class.spaceship/p1.cpp
@@ -80,7 +80,7 @@ namespace Deletedness {
   // expected-note@#base {{deleted comparison function for base class 'C'}}
   // expected-note@#base {{no viable three-way comparison function for base class 'D1'}}
   // expected-note@#base {{three-way comparison cannot be synthesized because there is no viable function for '<' comparison}}
-  // expected-note@#base {{no viable three-way comparison function for base class 'D2'}}
+  // expected-note@#base {{no viable 'operator==' for base class 'D2'}}
   // expected-note@#base {{three-way comparison cannot be synthesized because there is no viable function for '==' comparison}}
   // expected-note@#base {{deleted comparison function for base class 'E'}}
   // expected-note@#base {{implied comparison for base class 'F' is ambiguous}}
@@ -112,7 +112,7 @@ namespace Deletedness {
   // expected-note@#arr {{deleted comparison function for member 'arr'}}
   // expected-note@#arr {{no viable three-way comparison function for member 'arr'}}
   // expected-note@#arr {{three-way comparison cannot be synthesized because there is no viable function for '<' comparison}}
-  // expected-note@#arr {{no viable three-way comparison function for member 'arr'}}
+  // expected-note@#arr {{no viable 'operator==' for member 'arr'}}
   // expected-note@#arr {{three-way comparison cannot be synthesized because there is no viable function for '==' comparison}}
   // expected-note@#arr {{deleted comparison function for member 'arr'}}
   // expected-note@#arr {{implied comparison for member 'arr' is ambiguous}}


        


More information about the cfe-commits mailing list