[clang] [Clang][Parser] Fix name lookup of template parameters for out-of-line specializations (PR #101020)
Younan Zhang via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 29 19:12:59 PDT 2024
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/101020
>From 420705203a72c82446bd491b5766dde5fef116ff Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Mon, 29 Jul 2024 22:10:19 +0800
Subject: [PATCH] [Clang][Parser] Fix name lookup of template parameters for
out-of-line specializations
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:
```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`)
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)
---
clang/lib/Sema/SemaTemplate.cpp | 24 +++++++++++++++++++
.../test/CXX/temp/temp.res/temp.local/p8.cpp | 22 +++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 87b1f98bbe5ac9..a4ab9f99269927 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -1876,6 +1876,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();
@@ -8085,6 +8106,9 @@ DeclResult Sema::ActOnClassTemplateSpecialization(
return true;
}
+ if (S->isTemplateParamScope())
+ EnterTemplatedContext(S, ClassTemplate->getTemplatedDecl());
+
bool isMemberSpecialization = false;
bool isPartialSpecialization = 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..7404c14118c76d 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,25 @@ 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;
+#if __cplusplus >= 202003L
+ template <typename> struct D;
+ template <class> concept True = true;
+#endif
}
+
+template <typename CC>
+struct SearchClassBetweenTemplateParameterLists::C {
+ void foo(CC) {} // This should find the template type parameter.
+};
+
+#if __cplusplus >= 202003L
+
+template <True CC>
+struct SearchClassBetweenTemplateParameterLists::D<CC> {
+ void foo(CC);
+};
+
+#endif
More information about the cfe-commits
mailing list