Skip to content

[Clang] fix cast failures by adjusting the resolution of record declaration contexts to handle semantic and lexical distinctions #96228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 15, 2024

Conversation

a-tarasyuk
Copy link
Member

Fixes #96043

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 20, 2024

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

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 --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;

@a-tarasyuk a-tarasyuk requested a review from zyn0217 June 21, 2024 18:46
@a-tarasyuk a-tarasyuk changed the title [Clang] resolve record declaration of defaulted comparison method by using the first argument [Clang] use parent declaration context to avoid asserting cast failure in defaulted comparison method Jun 21, 2024
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix.

I am curious what specifically about this code triggers the crash. We do have a test w/ a defaulted outside the class. So there is another condition needed to trigger this. It looks like removing constexpr from operator==(d h, g i) prevents the crash.

Once we understand the specific condition we should add that to the summary.

@shafik
Copy link
Collaborator

shafik commented Jun 21, 2024

There are several test failures that look related to this change, please check them out.

…ration contexts to handle semantic and lexical distinctions
@a-tarasyuk a-tarasyuk requested a review from shafik June 24, 2024 16:08
@a-tarasyuk a-tarasyuk changed the title [Clang] use parent declaration context to avoid asserting cast failure in defaulted comparison method [Clang] fix cast failures by adjusting the resolution of record declaration contexts to handle semantic and lexical distinctions Jun 24, 2024
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@a-tarasyuk
Copy link
Member Author

@shafik Thank you for your review. If the changes look good to @zyn0217, I would appreciate it if someone could merge them, as I don't have access. Thanks.

@zyn0217
Copy link
Contributor

zyn0217 commented Jul 15, 2024

Thanks

@zyn0217 zyn0217 merged commit 4ca024c into llvm:main Jul 15, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Clang-tidy crashes when defining default operator== outside of struct
4 participants