[clang] fixed clang frontend crash with friend class declaration and overload == (PR #133878)

Ankur Ahir via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 2 00:57:12 PDT 2025


https://github.com/Ankur-0429 updated https://github.com/llvm/llvm-project/pull/133878

>From 289f8630dccf916b8cf7cc43758bae60df036c5d Mon Sep 17 00:00:00 2001
From: Ankur Ahir <ankurahir at google.com>
Date: Wed, 2 Apr 2025 00:53:33 -0700
Subject: [PATCH] clang frontend crash with friend class declaration and
 overloaded operator ==

---
 clang/docs/ReleaseNotes.rst                          |  1 +
 clang/lib/Sema/SemaDeclCXX.cpp                       |  3 +--
 .../class/class.compare/class.compare.default/p1.cpp | 12 ++++++++++++
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index daad01919ecd4..431ae0863d9e4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -342,6 +342,7 @@ Bug Fixes to Attribute Support
 Bug Fixes to C++ Support
 ^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Clang Now Supports Implicitly-Defined Comparison Operators for Friend-Declarations
 - Clang now diagnoses copy constructors taking the class by value in template instantiations. (#GH130866)
 - Clang is now better at keeping track of friend function template instance contexts. (#GH55509)
 - Clang now prints the correct instantiation context for diagnostics suppressed
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 676d53a1f4b45..d1516860dfe71 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9037,8 +9037,7 @@ bool Sema::CheckExplicitlyDefaultedComparison(Scope *S, FunctionDecl *FD,
       return true;
 
     if (llvm::none_of(RD->friends(), [&](const FriendDecl *F) {
-          return FD->getCanonicalDecl() ==
-                 F->getFriendDecl()->getCanonicalDecl();
+          return declaresSameEntity(F->getFriendDecl(), FD);
         })) {
       Diag(FD->getLocation(), diag::err_defaulted_comparison_not_friend)
           << int(DCK) << int(0) << RD;
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 a195e0548152d..5db4f05da552c 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
@@ -285,3 +285,15 @@ struct j {
 };
 bool j::operator==(const j &) const = default;
 }
+
+namespace evil2 {
+  struct k {
+  };
+  
+  struct l {
+      friend bool operator==(const l& a, const l& b);
+      friend class k;
+  };
+  
+  bool operator==(const l& a, const l& b) = default;
+}
\ No newline at end of file



More information about the cfe-commits mailing list