Skip to content

Commit 11d1310

Browse files
authored
[clang] Fix assertion failure with deleted overloaded unary operators (#78316)
When emitting notes related to wrong number of arguments do not consider object argument. Fixes #78314
1 parent 6c47419 commit 11d1310

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,8 @@ Bug Fixes in This Version
837837
- Fix an issue with missing symbol definitions when the first coroutine
838838
statement appears in a discarded ``if constexpr`` branch.
839839
Fixes (`#78290 <https://github.com/llvm/llvm-project/issues/78290>`_)
840+
- Fixed assertion failure with deleted overloaded unary operators.
841+
Fixes (`#78314 <https://github.com/llvm/llvm-project/issues/78314>`_)
840842

841843
Bug Fixes to Compiler Builtins
842844
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaOverload.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14332,12 +14332,17 @@ Sema::CreateOverloadedUnaryOp(SourceLocation OpLoc, UnaryOperatorKind Opc,
1433214332
return ExprError();
1433314333

1433414334
case OR_Deleted:
14335+
// CreateOverloadedUnaryOp fills the first element of ArgsArray with the
14336+
// object whose method was called. Later in NoteCandidates size of ArgsArray
14337+
// is passed further and it eventually ends up compared to number of
14338+
// function candidate parameters which never includes the object parameter,
14339+
// so slice ArgsArray to make sure apples are compared to apples.
1433514340
CandidateSet.NoteCandidates(
1433614341
PartialDiagnosticAt(OpLoc, PDiag(diag::err_ovl_deleted_oper)
1433714342
<< UnaryOperator::getOpcodeStr(Opc)
1433814343
<< Input->getSourceRange()),
14339-
*this, OCD_AllCandidates, ArgsArray, UnaryOperator::getOpcodeStr(Opc),
14340-
OpLoc);
14344+
*this, OCD_AllCandidates, ArgsArray.drop_front(),
14345+
UnaryOperator::getOpcodeStr(Opc), OpLoc);
1434114346
return ExprError();
1434214347
}
1434314348

clang/test/SemaCXX/overloaded-operator.cpp

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
1+
// RUN: %clang_cc1 -fsyntax-only -verify=expected,precxx23 -std=c++11 %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx23 -std=c++23 %s
3+
24
class X { };
35

46
X operator+(X, X);
@@ -33,7 +35,9 @@ struct A {
3335

3436
A make_A();
3537

36-
bool operator==(A&, Z&); // expected-note 3{{candidate function}}
38+
bool operator==(A&, Z&); // expected-note 3{{candidate function}} \
39+
// cxx23-note 2{{candidate function}}
40+
3741

3842
void h(A a, const A ac, Z z) {
3943
make_A() == z; // expected-warning{{equality comparison result unused}}
@@ -68,7 +72,9 @@ struct E2 {
6872
};
6973

7074
// C++ [over.match.oper]p3 - enum restriction.
71-
float& operator==(E1, E2); // expected-note{{candidate function}}
75+
float& operator==(E1, E2); // expected-note{{candidate function}} \
76+
// cxx23-note{{candidate function}}
77+
7278

7379
void enum_test(Enum1 enum1, Enum2 enum2, E1 e1, E2 e2, Enum1 next_enum1) {
7480
float &f1 = (e1 == e2);
@@ -86,7 +92,8 @@ class pr5244_foo
8692
};
8793

8894
bool operator==(const pr5244_foo& s1, const pr5244_foo& s2); // expected-note{{candidate function}}
89-
bool operator==(char c, const pr5244_foo& s); // expected-note{{candidate function}}
95+
bool operator==(char c, const pr5244_foo& s); // expected-note{{candidate function}} \
96+
// cxx23-note{{candidate function}}
9097

9198
enum pr5244_bar
9299
{
@@ -130,7 +137,7 @@ struct SmartPtr {
130137
};
131138

132139
void test_smartptr(SmartPtr ptr, const SmartPtr cptr,
133-
const volatile SmartPtr cvptr) {
140+
const volatile SmartPtr cvptr) { // cxx23-warning {{volatile-qualified parameter type 'const volatile SmartPtr' is deprecated}}
134141
int &ir = *ptr;
135142
long &lr = *cptr;
136143
long &lr2 = *cvptr;
@@ -598,3 +605,43 @@ namespace B {
598605
}
599606
void g(B::X x) { A::f(x); }
600607
}
608+
609+
namespace GH78314 {
610+
611+
class a {
612+
public:
613+
void operator--() = delete; // expected-note {{candidate function has been explicitly deleted}} \
614+
// expected-note {{candidate function not viable: requires 0 arguments, but 1 was provided}}
615+
void operator--(int) = delete; // expected-note {{candidate function has been explicitly deleted}} \
616+
// expected-note {{candidate function not viable: requires 1 argument, but 0 were provided}}
617+
};
618+
619+
class c {
620+
void operator--(this c) = delete; //precxx23-error {{explicit object parameters are incompatible with C++ standards before C++2b}} \
621+
// expected-note {{candidate function has been explicitly deleted}} \
622+
// expected-note {{candidate function not viable: requires 0 non-object arguments, but 1 was provided}}
623+
void operator--(this c, int) = delete; //precxx23-error {{explicit object parameters are incompatible with C++ standards before C++2b}} \
624+
// expected-note {{candidate function has been explicitly deleted}} \
625+
// expected-note {{candidate function not viable: requires 1 non-object argument, but 0 were provided}}
626+
};
627+
628+
void foo() {
629+
a aa;
630+
--aa; // expected-error {{overload resolution selected deleted operator '--'}}
631+
aa--; // expected-error {{overload resolution selected deleted operator '--'}}
632+
633+
c cc;
634+
--cc; // expected-error {{overload resolution selected deleted operator '--'}}
635+
cc--; // expected-error {{overload resolution selected deleted operator '--'}}
636+
}
637+
638+
class b {
639+
void operator++() = delete; // expected-note {{candidate function has been explicitly deleted}}
640+
template <class> void operator++(int) { // expected-note {{function template not viable: requires 1 argument, but 0 were provided}}
641+
b bb;
642+
++bb; // expected-error {{overload resolution selected deleted operator '++'}}
643+
}
644+
};
645+
646+
647+
}

0 commit comments

Comments
 (0)