[clang] [libc] [libcxx] [flang] [compiler-rt] [llvm] [lldb] [clang] Fix assertion failure with deleted overloaded unary operators (PR #78316)

Mariya Podchishchaeva via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 17 06:06:27 PST 2024


https://github.com/Fznamznon updated https://github.com/llvm/llvm-project/pull/78316

>From cf33d7ce01aafe0fa29b8a38a9824a0b03d24f05 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Tue, 16 Jan 2024 09:16:10 -0800
Subject: [PATCH 1/3] [clang] Fix assertion failure with deleted overloaded
 unary operators

When emitting notes related to wrong number of arguments do not consider
implicit object argument.

Fixes https://github.com/llvm/llvm-project/issues/78314
---
 clang/docs/ReleaseNotes.rst                |  2 ++
 clang/lib/Sema/SemaOverload.cpp            |  4 ++--
 clang/test/SemaCXX/overloaded-operator.cpp | 27 ++++++++++++++++++++++
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6e31849ce16dd4..8382e5d55f6c6e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -750,6 +750,8 @@ Bug Fixes in This Version
   Fixes (`#77583 <https://github.com/llvm/llvm-project/issues/77583>`_)
 - Fix an issue where CTAD fails for function-type/array-type arguments.
   Fixes (`#51710 <https://github.com/llvm/llvm-project/issues/51710>`_)
+- Fixed assertion failure with deleted overloaded unary operators.
+  Fixes (`#78314 <https://github.com/llvm/llvm-project/issues/78314>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 37c62b306b3cd3..83ab7cb0f3411b 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -14310,8 +14310,8 @@ Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc,
         PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_deleted_oper)
                                        << UnaryOperator::getOpcodeStr(Opc)
                                        << Input->getSourceRange()),
-        *this, OCD_AllCandidates, ArgsArray, UnaryOperator::getOpcodeStr(Opc),
-        OpLoc);
+        *this, OCD_AllCandidates, ArgsArray.slice(1),
+        UnaryOperator::getOpcodeStr(Opc), OpLoc);
     return ExprError();
   }
 
diff --git a/clang/test/SemaCXX/overloaded-operator.cpp b/clang/test/SemaCXX/overloaded-operator.cpp
index 83a7e65b43dd01..60332019f516cf 100644
--- a/clang/test/SemaCXX/overloaded-operator.cpp
+++ b/clang/test/SemaCXX/overloaded-operator.cpp
@@ -598,3 +598,30 @@ namespace B {
 }
 void g(B::X x) { A::f(x); }
 }
+
+namespace GH78314 {
+
+class a {
+public:
+  void operator--() = delete; // expected-note {{candidate function has been explicitly deleted}} \
+                              // expected-note {{candidate function not viable: requires 0 arguments, but 1 was provided}}
+  void operator--(int) = delete; // expected-note {{candidate function has been explicitly deleted}} \
+                                 // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
+};
+
+void foo() {
+  a aa;
+  --aa; // expected-error {{overload resolution selected deleted operator '--'}}
+  aa--; // expected-error {{overload resolution selected deleted operator '--'}}
+}
+
+class b {
+  void operator++() = delete; // expected-note {{candidate function has been explicitly deleted}}
+  template <class> void operator++(int) { // expected-note {{function template not viable: requires 1 argument, but 0 were provided}}
+    b bb;
+    ++bb; // expected-error {{overload resolution selected deleted operator '++'}}
+  }
+};
+
+
+}

>From 03daf97e74c05c1fa0c0c4b1637cbc76d3184404 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Wed, 17 Jan 2024 02:30:04 -0800
Subject: [PATCH 2/3] Add a test with explicit object parameter

---
 clang/test/SemaCXX/overloaded-operator.cpp | 30 ++++++++++++++++++----
 1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/clang/test/SemaCXX/overloaded-operator.cpp b/clang/test/SemaCXX/overloaded-operator.cpp
index 60332019f516cf..887848c29b83c5 100644
--- a/clang/test/SemaCXX/overloaded-operator.cpp
+++ b/clang/test/SemaCXX/overloaded-operator.cpp
@@ -1,4 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx23 -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx23 -std=c++23 %s
+
 class X { };
 
 X operator+(X, X);
@@ -33,7 +35,9 @@ struct A {
 
 A make_A();
 
-bool operator==(A&, Z&); // expected-note 3{{candidate function}}
+bool operator==(A&, Z&); // expected-note 3{{candidate function}} \
+                         // cxx23-note 2{{candidate function}}
+
 
 void h(A a, const A ac, Z z) {
   make_A() == z; // expected-warning{{equality comparison result unused}}
@@ -68,7 +72,9 @@ struct E2 {
 };
 
 // C++ [over.match.oper]p3 - enum restriction.
-float& operator==(E1, E2);  // expected-note{{candidate function}}
+float& operator==(E1, E2);  // expected-note{{candidate function}} \
+                            // cxx23-note{{candidate function}}
+
 
 void enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2, Enum1 next_enum1) {
   float &f1 = (e1 == e2);
@@ -86,7 +92,8 @@ class pr5244_foo
 };
 
 bool operator==(const pr5244_foo& s1, const pr5244_foo& s2); // expected-note{{candidate function}}
-bool operator==(char c, const pr5244_foo& s); // expected-note{{candidate function}}
+bool operator==(char c, const pr5244_foo& s); // expected-note{{candidate function}} \
+                                              // cxx23-note{{candidate function}}
 
 enum pr5244_bar
 {
@@ -130,7 +137,7 @@ struct SmartPtr {
 };
 
 void test_smartptr(SmartPtr ptr, const SmartPtr cptr, 
-                   const volatile SmartPtr cvptr) {
+                   const volatile SmartPtr cvptr) { // cxx23-warning {{volatile-qualified parameter type 'const volatile SmartPtr' is deprecated}}
   int &ir = *ptr;
   long &lr = *cptr;
   long &lr2 = *cvptr;
@@ -609,10 +616,23 @@ class a {
                                  // expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
 };
 
+class c {
+  void operator--(this c) = delete; //precxx23-error {{explicit object parameters are incompatible with C++ standards before C++2b}} \
+                                    // expected-note {{candidate function has been explicitly deleted}} \
+                                    // expected-note {{candidate function not viable: requires 0 non-object arguments, but 1 was provided}}
+  void operator--(this c, int) = delete; //precxx23-error {{explicit object parameters are incompatible with C++ standards before C++2b}} \
+                                         // expected-note {{candidate function has been explicitly deleted}} \
+                                         // expected-note {{candidate function not viable: requires 1 non-object argument, but 0 were provided}}
+};
+
 void foo() {
   a aa;
   --aa; // expected-error {{overload resolution selected deleted operator '--'}}
   aa--; // expected-error {{overload resolution selected deleted operator '--'}}
+
+  c cc; 
+  --cc; // expected-error {{overload resolution selected deleted operator '--'}}
+  cc--; // expected-error {{overload resolution selected deleted operator '--'}}
 }
 
 class b {

>From 64bdccceb3d6bb2fa16e0ef2230582643a7f16c6 Mon Sep 17 00:00:00 2001
From: "Podchishchaeva, Mariya" <mariya.podchishchaeva at intel.com>
Date: Wed, 17 Jan 2024 06:02:30 -0800
Subject: [PATCH 3/3] Add a comment.

---
 clang/lib/Sema/SemaOverload.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 83ab7cb0f3411b..6cf3fd6bf64845 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -14306,6 +14306,11 @@ Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc,
     return ExprError();
 
   case OR_Deleted:
+    // CreateOverloadedUnaryOp fills first element of ArgsArray with the object
+    // whose method was called. Later in NoteCandidates size of ArgsArray is
+    // passed further and it eventually ends up compared to number of function
+    // candidate parameters which never includes implicit object parameter, so
+    // do a slice to ArgsArray to make sure apples are compared to apples.
     CandidateSet.NoteCandidates(
         PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_deleted_oper)
                                        << UnaryOperator::getOpcodeStr(Opc)



More information about the llvm-commits mailing list