Skip to content

Commit

Permalink
[Clang][Sema] Fix template name lookup for operator= (#90999)
Browse files Browse the repository at this point in the history
This fixes a bug in #90152 where `operator=` was never looked up in the
current instantiation, resulting in `<` never being interpreted as the
start of a template argument list.

Since function templates are not copy/move assignment operators, the fix
is accomplished by allowing lookup in the current instantiation for
`operator=` when looking up a template name.
  • Loading branch information
sdkrystian committed May 3, 2024
1 parent 0397226 commit 3191e0b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 4 additions & 7 deletions clang/lib/Sema/SemaLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,8 @@ bool Sema::CppLookupName(LookupResult &R, Scope *S) {
for (Scope *PreS = S; PreS; PreS = PreS->getParent())
if (DeclContext *DC = PreS->getEntity()) {
if (DC->isDependentContext() && isa<CXXRecordDecl>(DC) &&
Name.getCXXOverloadedOperator() == OO_Equal) {
Name.getCXXOverloadedOperator() == OO_Equal &&
!R.isTemplateNameLookup()) {
R.setNotFoundInCurrentInstantiation();
return false;
}
Expand Down Expand Up @@ -2471,10 +2472,8 @@ bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
}
} QL(LookupCtx);

bool TemplateNameLookup = R.isTemplateNameLookup();
CXXRecordDecl *LookupRec = dyn_cast<CXXRecordDecl>(LookupCtx);
// FIXME: Per [temp.dep.general]p2, an unqualified name is also dependent
// if it's a dependent conversion-function-id or operator= where the current
// class is a templated entity. This should be handled in LookupName.
if (!InUnqualifiedLookup && !R.isForRedeclaration()) {
// C++23 [temp.dep.type]p5:
// A qualified name is dependent if
Expand All @@ -2488,7 +2487,7 @@ bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
(Name.getNameKind() == DeclarationName::CXXConversionFunctionName &&
Name.getCXXNameType()->isDependentType()) ||
(Name.getCXXOverloadedOperator() == OO_Equal && LookupRec &&
LookupRec->isDependentContext())) {
LookupRec->isDependentContext() && !TemplateNameLookup)) {
R.setNotFoundInCurrentInstantiation();
return false;
}
Expand Down Expand Up @@ -2584,8 +2583,6 @@ bool Sema::LookupQualifiedName(LookupResult &R, DeclContext *LookupCtx,
return true;
};

bool TemplateNameLookup = R.isTemplateNameLookup();

// Determine whether two sets of members contain the same members, as
// required by C++ [class.member.lookup]p6.
auto HasSameDeclarations = [&](DeclContext::lookup_iterator A,
Expand Down
18 changes: 18 additions & 0 deletions clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ namespace N3 {
this->A::operator=(*this);
}
};

template<typename T>
struct C {
template<typename U>
void operator=(int);

void not_instantiated() {
operator=<int>(0);
C::operator=<int>(0);
this->operator=<int>(0);
this->C::operator=<int>(0);

operator=(*this);
C::operator=(*this);
this->operator=(*this);
this->C::operator=(*this);
}
};
} // namespace N3

namespace N4 {
Expand Down

0 comments on commit 3191e0b

Please sign in to comment.