[clang] [Clang] resolve record declaration of defaulted comparison method by using the first argument (PR #96228)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 20 11:59:12 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

<details>
<summary>Changes</summary>

Fixes #<!-- -->96043

---
Full diff: https://github.com/llvm/llvm-project/pull/96228.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+1-1) 
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+7-5) 
- (added) clang/test/SemaCXX/defaulted-comparison-struct.cpp (+18) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d0e5e67651364..510804efc6023 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -889,7 +889,7 @@ Bug Fixes to C++ Support
   between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (#GH95366).
 - Fix immediate escalation bugs in the presence of dependent call arguments. (#GH94935)
 - Clang now diagnoses explicit specializations with storage class specifiers in all contexts.
-
+- Fixed failed assertion when resolving context of defaulted comparison method outside of struct. (#GH96043).
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index d38700d56e4ff..066a89edaf6e7 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -9127,6 +9127,11 @@ void Sema::DeclareImplicitEqualityComparison(CXXRecordDecl *RD,
   popCodeSynthesisContext();
 }
 
+static inline CXXRecordDecl *getRecordDeclFromFirstParameter(FunctionDecl *FD) {
+  auto PT = FD->getParamDecl(0)->getType();
+  return PT.getNonReferenceType()->getAsCXXRecordDecl();
+}
+
 void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
                                      DefaultedComparisonKind DCK) {
   assert(FD->isDefaulted() && !FD->isDeleted() &&
@@ -9141,10 +9146,7 @@ void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
 
   {
     // Build and set up the function body.
-    // The first parameter has type maybe-ref-to maybe-const T, use that to get
-    // the type of the class being compared.
-    auto PT = FD->getParamDecl(0)->getType();
-    CXXRecordDecl *RD = PT.getNonReferenceType()->getAsCXXRecordDecl();
+    auto RD = getRecordDeclFromFirstParameter(FD);
     SourceLocation BodyLoc =
         FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
     StmtResult Body =
@@ -9192,7 +9194,7 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
     EnterExpressionEvaluationContext Context(
         S, Sema::ExpressionEvaluationContext::Unevaluated);
 
-    CXXRecordDecl *RD = cast<CXXRecordDecl>(FD->getLexicalParent());
+    auto RD = getRecordDeclFromFirstParameter(FD);
     SourceLocation BodyLoc =
         FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
     StmtResult Body =
diff --git a/clang/test/SemaCXX/defaulted-comparison-struct.cpp b/clang/test/SemaCXX/defaulted-comparison-struct.cpp
new file mode 100644
index 0000000000000..de4ec2852a137
--- /dev/null
+++ b/clang/test/SemaCXX/defaulted-comparison-struct.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+template <typename> class a {};
+template <typename b> b c(a<b>);
+template <typename d> class e {
+public:
+  typedef a<d *> f;
+  f begin();
+};
+template <typename d, typename g> constexpr bool operator==(d h, g i) {
+  return *c(h.begin()) == *c(i.begin());
+}
+struct j {
+  e<j> bar;
+  bool operator==(const j &) const;
+};
+bool j::operator==(const j &) const = default;

``````````

</details>


https://github.com/llvm/llvm-project/pull/96228


More information about the cfe-commits mailing list