diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index d41ab126c426f..2331774804c89 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -8262,6 +8262,8 @@ class DefaultedComparisonAnalyzer /// resolution [...] CandidateSet.exclude(FD); + auto &Fns = this->Fns; + if (Args[0]->getType()->isOverloadableType()) S.LookupOverloadedBinOp(CandidateSet, OO, Fns, Args); else @@ -8913,6 +8915,9 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD, // Perform any unqualified lookups we're going to need to default this // function. + if (!S) + S = getScopeForContext(FD->getLexicalDeclContext()); + if (S) { UnresolvedSet<32> Operators; lookupOperatorsForDefaultedComparison(*this, S, Operators, diff --git a/clang/test/SemaCXX/default-op-notusing-namespace.cpp b/clang/test/SemaCXX/default-op-notusing-namespace.cpp new file mode 100644 index 0000000000000..48515fb28366f --- /dev/null +++ b/clang/test/SemaCXX/default-op-notusing-namespace.cpp @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s + +struct X {}; +namespace NS { + bool operator==(X, X); +} + +struct Y { + X x; + friend bool operator==(Y, Y); +}; + +// There is no `using namespace NS;` here, so this operator== +// will be implicitly deleted due to missing viable operator== for X. +bool operator==(Y, Y) = default; +// expected-error@-1 {{defaulting this equality comparison operator would delete it after its first declaration}} +// expected-note@9 {{defaulted 'operator==' is implicitly deleted because there is no viable 'operator==' for member 'x'}} diff --git a/clang/test/SemaCXX/default-op-using-namespace.cpp b/clang/test/SemaCXX/default-op-using-namespace.cpp new file mode 100644 index 0000000000000..89003ae0a42ca --- /dev/null +++ b/clang/test/SemaCXX/default-op-using-namespace.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s +// expected-no-diagnostics + +struct X {}; +namespace NS { + bool operator==(X, X); +} +using namespace NS; +struct Y { + X x; + friend bool operator==(Y, Y); +}; +bool operator==(Y, Y) = default; \ No newline at end of file