https://github.com/suhas-1012 created https://github.com/llvm/llvm-project/pull/166996
#### Summary Fixed an issue where defaulted equality comparison operators (`operator==`) were incorrectly treated as *implicitly deleted* when the required `operator==` for a member subobject (such as `X` inside `Y`) was only made visible through a `using namespace` directive. --- #### Changes Made - Implemented a fix in **`clang/lib/Sema/SemaDeclCXX.cpp`** to ensure that unqualified lookup correctly accounts for `using namespace` directives when generating defaulted comparison operators. - Added two new test cases in **`clang/test/SemaCXX/`**: - `default-op-using-namespace.cpp` – verifies that the code now compiles successfully and no longer treats the operator as implicitly deleted when `using namespace` makes it visible. - `default-op-notusing-namespace.cpp` – verifies that the operator remains correctly deleted when no `using namespace` directive is present. --- #### Explanation Previously, Clang sometimes performed lookup for defaulted comparison operators without a valid scope, which caused it to miss operators introduced via `using namespace` directives. The fix ensures that when the lookup `Scope*` is `nullptr`, Clang now explicitly retrieves the appropriate lexical scope (via `getScopeForContext(FD->getLexicalDeclContext())`) instead of ignoring the case. This change allows unqualified lookup to correctly include `using namespace` directives and ensures consistent behavior during operator synthesis. --- Fixes: [llvm/llvm-project#97087](https://github.com/llvm/llvm-project/issues/97087) >From b6a08dc9042cdbb9a2ae08ab1c19a1a8b648c413 Mon Sep 17 00:00:00 2001 From: Muvvala Sairam Suhas <[email protected]> Date: Sat, 8 Nov 2025 00:07:26 +0530 Subject: [PATCH] [SemaCXX] Fixed defaulted equality operator deletion issue (#97087) --- clang/lib/Sema/SemaDeclCXX.cpp | 5 +++++ .../SemaCXX/default-op-notusing-namespace.cpp | 17 +++++++++++++++++ .../test/SemaCXX/default-op-using-namespace.cpp | 13 +++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 clang/test/SemaCXX/default-op-notusing-namespace.cpp create mode 100644 clang/test/SemaCXX/default-op-using-namespace.cpp 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
