From 4ca024c877e3eb20c0af518d3335883fb7b4b910 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Mon, 15 Jul 2024 05:00:19 +0300 Subject: [PATCH] [Clang] Fix cast failures by adjusting the resolution of record declaration contexts to handle semantic and lexical distinctions (#96228) Fixes #96043 --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/SemaDeclCXX.cpp | 5 ++++- .../class.compare/class.compare.default/p1.cpp | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 35cc15142089b2..83a1353f3c4008 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1041,6 +1041,7 @@ Bug Fixes to C++ Support (#GH48937) - Fix a crash when parsing an invalid type-requirement in a requires expression. (#GH51868) - Fix parsing of built-in type-traits such as ``__is_pointer`` in libstdc++ headers. (#GH95598) +- 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 a7da5285ad0e40..76db539b5726bc 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -9070,7 +9070,10 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc, EnterExpressionEvaluationContext Context( S, Sema::ExpressionEvaluationContext::Unevaluated); - CXXRecordDecl *RD = cast(FD->getLexicalParent()); + CXXRecordDecl *RD = + cast(FD->getFriendObjectKind() == Decl::FOK_None + ? FD->getDeclContext() + : FD->getLexicalDeclContext()); SourceLocation BodyLoc = FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation(); StmtResult Body = 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 252860bfc4de07..ddf82f432c2eab 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 @@ -265,3 +265,21 @@ void f2() { // access info for unnamed bit-field } } + +namespace GH96043 { +template class a {}; +template b c(a); +template class e { +public: + typedef a f; + f begin(); +}; +template constexpr bool operator==(d h, g i) { + return *c(h.begin()) == *c(i.begin()); +} +struct j { + e bar; + bool operator==(const j &) const; +}; +bool j::operator==(const j &) const = default; +}