diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 119cdf2a3d3c0f..01f0079198c74d 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -8531,8 +8531,6 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD, QualType PlainTy = Context.getRecordType(RD); QualType RefTy = Context.getLValueReferenceType(PlainTy.withConst()); - if (IsMethod) - PlainTy = QualType(); Diag(FD->getLocation(), diag::err_defaulted_comparison_param) << int(DCK) << ParmTy << RefTy << int(!IsMethod) << PlainTy << Param->getSourceRange(); @@ -17266,10 +17264,13 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc) { // that we've marked it as defaulted. FD->setWillHaveBody(false); - // If this is a comparison's defaulted definition within the record, do - // the checking when the record is complete. - if (DefKind.isComparison() && isa(FD->getLexicalDeclContext())) - return; + if (DefKind.isComparison()) { + // If this comparison's defaulting occurs within the definition of its + // lexical class context, we have to do the checking when complete. + if (auto const *RD = dyn_cast(FD->getLexicalDeclContext())) + if (!RD->isCompleteDefinition()) + return; + } // If this member fn was defaulted on its first declaration, we will have // already performed the checking in CheckCompletedCXXClass. Such a 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 136afd8996432d..4bfd27461ed5e2 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 @@ -115,6 +115,28 @@ namespace LookupContext { } } +namespace evil1 { +template struct Bad { + // expected-error@+1{{found 'const float &'}} + bool operator==(T const &) const = default; + Bad(int = 0); +}; + +template struct Weird { + // expected-error@+1{{'float' cannot be used prior to '::'}} + bool operator==(typename T::Weird_ const &) const = default; + Weird(int = 0); +}; + +struct evil { + using Weird_ = Weird; +}; +template struct Bad; // expected-note{{evil1::Bad' requested}} +template struct Weird; // expected-note{{evil1::Weird' requested}} +template struct Weird; + +} // namespace evil1 + namespace P1946 { struct A { friend bool operator==(A &, A &); // expected-note {{would lose const qualifier}} @@ -161,5 +183,40 @@ enum e {}; bool operator==(e, int) = default; // expected-error{{expected class or reference to a constant class}} bool operator==(e *, int *) = default; // expected-error{{must have at least one}} - } // namespace p2085 + +namespace p2085_2 { +template struct S6 { + // expected-error@+2{{found 'const int &'}} + // expected-error@+1{{found 'const float &'}} + bool operator==(T const &) const; +}; +template bool S6::operator==(T const &) const = default; + +template struct S6; // expected-note{{S6::operator==' requested}} + +void f1() { + S6 a; + (void)(a == 0); // expected-note{{S6::operator==' requested}} +} + +template struct S7 { + // expected-error@+2{{'float' cannot be used}} + // expected-error@+1{{'int' cannot be used}} + bool operator==(typename T::S7_ const &) const; + S7(int = 0); +}; +template bool S7::operator==(typename T::S7_ const &) const = default; + +struct evil { + using S7_ = S7; +}; +template struct S7; // expected-note{{S7' requested}} + +void f2() { + S7 a; // expected-note{{S7' requested}} + S7 b; + (void)(a == 0); // expected-error{{invalid operands}} + (void)(b == 0); +} +} // namespace p2085_2