Skip to content

Commit 5b0bcec

Browse files
authored
[Clang][Parser] Fix name lookup of template parameters for out-of-line specializations (#101020)
Since the implementation of DR458 (d144601), 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 #64082
1 parent 8c5d53f commit 5b0bcec

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ Bug Fixes to C++ Support
336336
- Mangle placeholders for deduced types as a template-prefix, such that mangling
337337
of template template parameters uses the correct production. (#GH106182)
338338
- Fixed an assertion failure when converting vectors to int/float with invalid expressions. (#GH105486)
339+
- Template parameter names are considered in the name lookup of out-of-line class template
340+
specialization right before its declaration context. (#GH64082)
339341

340342
Bug Fixes to AST Handling
341343
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaTemplate.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,27 @@ DeclResult Sema::CheckClassTemplate(
18731873
if (Previous.isAmbiguous())
18741874
return true;
18751875

1876+
// Let the template parameter scope enter the lookup chain of the current
1877+
// class template. For example, given
1878+
//
1879+
// namespace ns {
1880+
// template <class> bool Param = false;
1881+
// template <class T> struct N;
1882+
// }
1883+
//
1884+
// template <class Param> struct ns::N { void foo(Param); };
1885+
//
1886+
// When we reference Param inside the function parameter list, our name lookup
1887+
// chain for it should be like:
1888+
// FunctionScope foo
1889+
// -> RecordScope N
1890+
// -> TemplateParamScope (where we will find Param)
1891+
// -> NamespaceScope ns
1892+
//
1893+
// See also CppLookupName().
1894+
if (S->isTemplateParamScope())
1895+
EnterTemplatedContext(S, SemanticContext);
1896+
18761897
NamedDecl *PrevDecl = nullptr;
18771898
if (Previous.begin() != Previous.end())
18781899
PrevDecl = (*Previous.begin())->getUnderlyingDecl();
@@ -8089,6 +8110,9 @@ DeclResult Sema::ActOnClassTemplateSpecialization(
80898110
return true;
80908111
}
80918112

8113+
if (S->isTemplateParamScope())
8114+
EnterTemplatedContext(S, ClassTemplate->getTemplatedDecl());
8115+
80928116
DeclContext *DC = ClassTemplate->getDeclContext();
80938117

80948118
bool isMemberSpecialization = false;

clang/test/CXX/temp/temp.res/temp.local/p8.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
23

34
namespace N {
45
enum { C };
@@ -151,4 +152,38 @@ namespace SearchClassBetweenTemplateParameterLists {
151152
void A<T>::B<BB>::k(V) { // expected-error {{does not match}}
152153
BB bb; // expected-error {{incomplete type}}
153154
}
155+
156+
int CC;
157+
template <typename> struct C;
158+
template <template<typename> typename> struct D;
159+
#if __cplusplus >= 202002L
160+
template <bool CC> requires (CC) struct E;
161+
template <typename> struct F;
162+
template <typename> concept True = true;
163+
#endif
154164
}
165+
166+
template <typename CC>
167+
struct SearchClassBetweenTemplateParameterLists::C {
168+
void foo(CC); // This should find the template type parameter.
169+
};
170+
171+
template <template<typename> typename CC>
172+
struct SearchClassBetweenTemplateParameterLists::D {
173+
template <typename AA>
174+
CC<AA> foo(CC<AA>);
175+
};
176+
177+
#if __cplusplus >= 202002L
178+
179+
template <bool CC> requires (CC)
180+
struct SearchClassBetweenTemplateParameterLists::E {
181+
void foo() requires (CC);
182+
};
183+
184+
template <SearchClassBetweenTemplateParameterLists::True CC>
185+
struct SearchClassBetweenTemplateParameterLists::F<CC> {
186+
void foo(CC);
187+
};
188+
189+
#endif

0 commit comments

Comments
 (0)