[clang] 5b0bcec - [Clang][Parser] Fix name lookup of template parameters for out-of-line specializations (#101020)

via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 31 02:35:55 PDT 2024


Author: Younan Zhang
Date: 2024-08-31T17:35:51+08:00
New Revision: 5b0bcec93dbc2e5bec049c452b157548334c5e28

URL: https://github.com/llvm/llvm-project/commit/5b0bcec93dbc2e5bec049c452b157548334c5e28
DIFF: https://github.com/llvm/llvm-project/commit/5b0bcec93dbc2e5bec049c452b157548334c5e28.diff

LOG: [Clang][Parser] Fix name lookup of template parameters for out-of-line specializations (#101020)

Since the implementation of DR458 (d1446017), we have had an algorithm
that template parameters would take precedence over its parent scopes at
the name lookup. However, we failed to handle the following case where
the member function declaration is not yet deferral parsed (This is
where the patch of DR458 applies):

```cpp
namespace NS {
  int CC;
  template <typename> struct C;
}

template <typename CC>
struct NS::C {
  void foo(CC);
};
```

When parsing the parameter of the function declaration `void foo(CC)`,
we used to perform a name lookup following such a Scope chain:
```
FunctionScope foo (failed)
RecordScope C (failed)
NamespaceScope NS (found `int CC`)
(If failed)
TemplateParameterScope of C
```
This doesn't seem right because according to `[temp.local]`, a template
parameter scope should be searched before its parent scope to which the
parameter appertains. This patch corrects the search scopes by setting a
lookup Entity for template parameter Scopes so that we can bail out in
CppNameLookup() when reaching the RecordScope. Afterward, the search
chain would be like:
```
FunctionScope foo (failed)
RecordScope C (failed)
TemplateParameterScope of C (found CC)
(If failed)
NamespaceScope NS
```

Fixes https://github.com/llvm/llvm-project/issues/64082

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaTemplate.cpp
    clang/test/CXX/temp/temp.res/temp.local/p8.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d0ac3b65820b05..98fb0174d4a37e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -336,6 +336,8 @@ Bug Fixes to C++ Support
 - Mangle placeholders for deduced types as a template-prefix, such that mangling
   of template template parameters uses the correct production. (#GH106182)
 - Fixed an assertion failure when converting vectors to int/float with invalid expressions. (#GH105486)
+- Template parameter names are considered in the name lookup of out-of-line class template
+  specialization right before its declaration context. (#GH64082)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index f8f41d0bafffc3..bf6b53700d90eb 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -1873,6 +1873,27 @@ DeclResult Sema::CheckClassTemplate(
   if (Previous.isAmbiguous())
     return true;
 
+  // Let the template parameter scope enter the lookup chain of the current
+  // class template. For example, given
+  //
+  //  namespace ns {
+  //    template <class> bool Param = false;
+  //    template <class T> struct N;
+  //  }
+  //
+  //  template <class Param> struct ns::N { void foo(Param); };
+  //
+  // When we reference Param inside the function parameter list, our name lookup
+  // chain for it should be like:
+  //  FunctionScope foo
+  //  -> RecordScope N
+  //  -> TemplateParamScope (where we will find Param)
+  //  -> NamespaceScope ns
+  //
+  // See also CppLookupName().
+  if (S->isTemplateParamScope())
+    EnterTemplatedContext(S, SemanticContext);
+
   NamedDecl *PrevDecl = nullptr;
   if (Previous.begin() != Previous.end())
     PrevDecl = (*Previous.begin())->getUnderlyingDecl();
@@ -8089,6 +8110,9 @@ DeclResult Sema::ActOnClassTemplateSpecialization(
     return true;
   }
 
+  if (S->isTemplateParamScope())
+    EnterTemplatedContext(S, ClassTemplate->getTemplatedDecl());
+
   DeclContext *DC = ClassTemplate->getDeclContext();
 
   bool isMemberSpecialization = false;

diff  --git a/clang/test/CXX/temp/temp.res/temp.local/p8.cpp b/clang/test/CXX/temp/temp.res/temp.local/p8.cpp
index 6b2071eb12ce0f..56599985da06e9 100644
--- a/clang/test/CXX/temp/temp.res/temp.local/p8.cpp
+++ b/clang/test/CXX/temp/temp.res/temp.local/p8.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
 
 namespace N { 
   enum { C };
@@ -151,4 +152,38 @@ namespace SearchClassBetweenTemplateParameterLists {
   void A<T>::B<BB>::k(V) { // expected-error {{does not match}}
     BB bb; // expected-error {{incomplete type}}
   }
+
+  int CC;
+  template <typename> struct C;
+  template <template<typename> typename> struct D;
+#if __cplusplus >= 202002L
+  template <bool CC> requires (CC) struct E;
+  template <typename> struct F;
+  template <typename> concept True = true;
+#endif
 }
+
+template <typename CC>
+struct SearchClassBetweenTemplateParameterLists::C {
+  void foo(CC); // This should find the template type parameter.
+};
+
+template <template<typename> typename CC>
+struct SearchClassBetweenTemplateParameterLists::D {
+  template <typename AA>
+  CC<AA> foo(CC<AA>);
+};
+
+#if __cplusplus >= 202002L
+
+template <bool CC> requires (CC)
+struct SearchClassBetweenTemplateParameterLists::E {
+  void foo() requires (CC);
+};
+
+template <SearchClassBetweenTemplateParameterLists::True CC>
+struct SearchClassBetweenTemplateParameterLists::F<CC> {
+  void foo(CC);
+};
+
+#endif


        


More information about the cfe-commits mailing list