[clang] [SemaCXX] Fixed defaulted equality operator deletion issue (#97087)[SemaCXX] Fix defaulted equality operator being incorrectly deleted when using namespace (llvm#97087) (PR #166996)
Muvvala Sairam Suhas via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 7 11:09:42 PST 2025
https://github.com/suhas-1012 updated https://github.com/llvm/llvm-project/pull/166996
>From b6a08dc9042cdbb9a2ae08ab1c19a1a8b648c413 Mon Sep 17 00:00:00 2001
From: Muvvala Sairam Suhas <cs23btech11038 at iith.ac.in>
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 at -1 {{defaulting this equality comparison operator would delete it after its first declaration}}
+// expected-note at 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
More information about the cfe-commits
mailing list