[clang] 3191e0b - [Clang][Sema] Fix template name lookup for operator= (#90999)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 3 14:07:56 PDT 2024


Author: Krystian Stasiowski
Date: 2024-05-03T17:07:52-04:00
New Revision: 3191e0b52725aa17651e38d26284386f3ea64eb6

URL: https://github.com/llvm/llvm-project/commit/3191e0b52725aa17651e38d26284386f3ea64eb6
DIFF: https://github.com/llvm/llvm-project/commit/3191e0b52725aa17651e38d26284386f3ea64eb6.diff

LOG: [Clang][Sema] Fix template name lookup for operator= (#90999)

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.

Added: 
    

Modified: 
    clang/lib/Sema/SemaLookup.cpp
    clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 2f6ad49fc08b61..e63da5875d2c91 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -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;
         }
@@ -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
@@ -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;
     }
@@ -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,

diff  --git a/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp b/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
index 0f24d716a7b749..46dd52f8c4c133 100644
--- a/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
+++ b/clang/test/CXX/temp/temp.res/temp.dep/temp.dep.type/p4.cpp
@@ -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 {


        


More information about the cfe-commits mailing list