Skip to content

Commit 3345a48

Browse files
committed
[Clang] resolve record declaration of defaulted comparison method by using the first argument
1 parent ace069d commit 3345a48

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ Bug Fixes to C++ Support
889889
between the addresses of two labels (a GNU extension) to a pointer within a constant expression. (#GH95366).
890890
- Fix immediate escalation bugs in the presence of dependent call arguments. (#GH94935)
891891
- Clang now diagnoses explicit specializations with storage class specifiers in all contexts.
892-
892+
- Fixed failed assertion when resolving context of defaulted comparison method outside of struct. (#GH96043).
893893

894894
Bug Fixes to AST Handling
895895
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9127,6 +9127,11 @@ void Sema::DeclareImplicitEqualityComparison(CXXRecordDecl *RD,
91279127
popCodeSynthesisContext();
91289128
}
91299129

9130+
static inline CXXRecordDecl *getRecordDeclFromFirstParameter(FunctionDecl *FD) {
9131+
auto PT = FD->getParamDecl(0)->getType();
9132+
return PT.getNonReferenceType()->getAsCXXRecordDecl();
9133+
}
9134+
91309135
void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
91319136
DefaultedComparisonKind DCK) {
91329137
assert(FD->isDefaulted() && !FD->isDeleted() &&
@@ -9141,10 +9146,7 @@ void Sema::DefineDefaultedComparison(SourceLocation UseLoc, FunctionDecl *FD,
91419146

91429147
{
91439148
// Build and set up the function body.
9144-
// The first parameter has type maybe-ref-to maybe-const T, use that to get
9145-
// the type of the class being compared.
9146-
auto PT = FD->getParamDecl(0)->getType();
9147-
CXXRecordDecl *RD = PT.getNonReferenceType()->getAsCXXRecordDecl();
9149+
auto RD = getRecordDeclFromFirstParameter(FD);
91489150
SourceLocation BodyLoc =
91499151
FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
91509152
StmtResult Body =
@@ -9192,7 +9194,7 @@ ComputeDefaultedComparisonExceptionSpec(Sema &S, SourceLocation Loc,
91929194
EnterExpressionEvaluationContext Context(
91939195
S, Sema::ExpressionEvaluationContext::Unevaluated);
91949196

9195-
CXXRecordDecl *RD = cast<CXXRecordDecl>(FD->getLexicalParent());
9197+
auto RD = getRecordDeclFromFirstParameter(FD);
91969198
SourceLocation BodyLoc =
91979199
FD->getEndLoc().isValid() ? FD->getEndLoc() : FD->getLocation();
91989200
StmtResult Body =
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2+
// expected-no-diagnostics
3+
4+
template <typename> class a {};
5+
template <typename b> b c(a<b>);
6+
template <typename d> class e {
7+
public:
8+
typedef a<d *> f;
9+
f begin();
10+
};
11+
template <typename d, typename g> constexpr bool operator==(d h, g i) {
12+
return *c(h.begin()) == *c(i.begin());
13+
}
14+
struct j {
15+
e<j> bar;
16+
bool operator==(const j &) const;
17+
};
18+
bool j::operator==(const j &) const = default;

0 commit comments

Comments
 (0)