diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index da3cb85dce8e7..a98ad496d2249 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -107,6 +107,8 @@ Bug Fixes in This Version instantiated in one module and whose definition is instantiated in another module may end up with members associated with the wrong declaration of the class, which can result in miscompiles in some cases. +- Fix crash on use of a variadic overloaded operator. + (`#42535 _`) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 40a94e13c7295..ad47b81b9dfad 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -9850,7 +9850,7 @@ getImplicitObjectParamType(ASTContext &Context, const FunctionDecl *F) { } static bool haveSameParameterTypes(ASTContext &Context, const FunctionDecl *F1, - const FunctionDecl *F2, unsigned NumParams) { + const FunctionDecl *F2) { if (declaresSameEntity(F1, F2)) return true; @@ -9863,8 +9863,14 @@ static bool haveSameParameterTypes(ASTContext &Context, const FunctionDecl *F1, return F->getParamDecl(I++)->getType(); }; + unsigned F1NumParams = F1->getNumParams() + isa(F1); + unsigned F2NumParams = F2->getNumParams() + isa(F2); + + if (F1NumParams != F2NumParams) + return false; + unsigned I1 = 0, I2 = 0; - for (unsigned I = 0; I != NumParams; ++I) { + for (unsigned I = 0; I != F1NumParams; ++I) { QualType T1 = NextParam(F1, I1, I == 0); QualType T2 = NextParam(F2, I2, I == 0); assert(!T1.isNull() && !T2.isNull() && "Unexpected null param types"); @@ -10032,8 +10038,7 @@ bool clang::isBetterOverloadCandidate( case ImplicitConversionSequence::Worse: if (Cand1.Function && Cand2.Function && Cand1.isReversed() != Cand2.isReversed() && - haveSameParameterTypes(S.Context, Cand1.Function, Cand2.Function, - NumArgs)) { + haveSameParameterTypes(S.Context, Cand1.Function, Cand2.Function)) { // Work around large-scale breakage caused by considering reversed // forms of operator== in C++20: // @@ -13997,6 +14002,10 @@ ExprResult Sema::CreateOverloadedBinOp(SourceLocation OpLoc, std::swap(Args[0], Args[1]); if (FnDecl) { + + if (FnDecl->isInvalidDecl()) + return ExprError(); + Expr *Base = nullptr; // We matched an overloaded operator. Build a call to that // operator. @@ -14029,7 +14038,7 @@ ExprResult Sema::CreateOverloadedBinOp(SourceLocation OpLoc, llvm::SmallVector AmbiguousWith; for (OverloadCandidate &Cand : CandidateSet) { if (Cand.Viable && Cand.Function && Cand.isReversed() && - haveSameParameterTypes(Context, Cand.Function, FnDecl, 2)) { + haveSameParameterTypes(Context, Cand.Function, FnDecl)) { for (unsigned ArgIdx = 0; ArgIdx < 2; ++ArgIdx) { if (CompareImplicitConversionSequences( *this, OpLoc, Cand.Conversions[ArgIdx], diff --git a/clang/test/SemaCXX/overloaded-operator-decl.cpp b/clang/test/SemaCXX/overloaded-operator-decl.cpp index 777be6c5a0487..8a76c9251e419 100644 --- a/clang/test/SemaCXX/overloaded-operator-decl.cpp +++ b/clang/test/SemaCXX/overloaded-operator-decl.cpp @@ -1,4 +1,5 @@ // RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s struct X { X(); X(int); @@ -58,3 +59,67 @@ namespace PR14120 { A()(i); } } + +namespace GH42535 { +class E { + E& operator=(const E& rhs, ...); // expected-error{{overloaded 'operator=' cannot be variadic}} + E& operator+=(const E& rhs, ...); // expected-error{{overloaded 'operator+=' cannot be variadic}} + +}; +void operator+(E, ...) {} // expected-error{{overloaded 'operator+' cannot be variadic}} +void operator-(E, ...) {} // expected-error{{overloaded 'operator-' cannot be variadic}} +void operator*(E, ...) {} // expected-error{{overloaded 'operator*' cannot be variadic}} +void operator/(E, ...) {} // expected-error{{overloaded 'operator/' must be a binary operator}} +void operator/(E, E, ...) {} // expected-error{{overloaded 'operator/' cannot be variadic}} +void operator%(E, ...) {} // expected-error{{overloaded 'operator%' must be a binary operator}} +void operator%(E, E, ...) {} // expected-error{{overloaded 'operator%' cannot be variadic}} +E& operator++(E&, ...); // expected-error{{overloaded 'operator++' cannot be variadic}} +E& operator--(E&, ...); // expected-error{{overloaded 'operator--' cannot be variadic}} +bool operator<(const E& lhs, ...); // expected-error{{overloaded 'operator<' must be a binary operator}} +bool operator<(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator>(const E& lhs, ...); // expected-error{{overloaded 'operator>' must be a binary operator}} +bool operator>(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator>=(const E& lhs, ...); // expected-error{{overloaded 'operator>=' must be a binary operator}} +bool operator>=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator<=(const E& lhs, ...); // expected-error{{overloaded 'operator<=' must be a binary operator}} +bool operator<=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator==(const E& lhs, ...); // expected-error{{overloaded 'operator==' must be a binary operator}} +bool operator==(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator!=(const E& lhs, ...); // expected-error{{overloaded 'operator!=' must be a binary operator}} +bool operator!=(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator&&(const E& lhs, ...); // expected-error{{overloaded 'operator&&' must be a binary operator}} +bool operator&&(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator||(const E& lhs, ...); // expected-error{{overloaded 'operator||' must be a binary operator}} +bool operator||(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator>>(const E& lhs, ...); // expected-error{{overloaded 'operator>>' must be a binary operator}} +bool operator>>(const E& lhs, const E& rhs, ...); // expected-error{{cannot be variadic}} +bool operator&(const E& lhs, ...); // expected-error{{cannot be variadic}} +#if __cplusplus >= 202002L +auto operator<=>(const E& lhs, ...); // expected-error{{overloaded 'operator<=>' must be a binary operator}} +#endif +void d() { + E() + E(); + E() - E(); + E() * E(); + E() / E(); + E() % E(); + ++E(); // expected-error{{cannot increment value of type 'E'}} + --E(); // expected-error{{cannot decrement value of type 'E'}} + E() < E(); + E() > E(); + E() <= E(); + E() >= E(); + E() == E(); + E() != E(); +#if __cplusplus >= 202002L + E() <=> E(); +#endif + E e; + E e1 = e; + e += e1; + E() && E(); + E() || E(); + E() & E(); + E() >> E(); +} +}