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 <zyn7...@gmail.com> Date: Mon, 29 Jul 2024 22:10:19 +0800 Subject: [PATCH 1/3] [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 >From 91e0c068b35358a7d64d8bd87ebadbf92ece74d1 Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Sat, 31 Aug 2024 16:39:22 +0800 Subject: [PATCH 2/3] Add more tests & a release note --- .../test/CXX/temp/temp.res/temp.local/p8.cpp | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) 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 7404c14118c76d..56599985da06e9 100644 --- a/clang/test/CXX/temp/temp.res/temp.local/p8.cpp +++ b/clang/test/CXX/temp/temp.res/temp.local/p8.cpp @@ -155,21 +155,34 @@ namespace SearchClassBetweenTemplateParameterLists { int CC; template <typename> struct C; -#if __cplusplus >= 202003L - template <typename> struct D; - template <class> concept True = true; + 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. + void foo(CC); // This should find the template type parameter. }; -#if __cplusplus >= 202003L +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 <True CC> -struct SearchClassBetweenTemplateParameterLists::D<CC> { +template <SearchClassBetweenTemplateParameterLists::True CC> +struct SearchClassBetweenTemplateParameterLists::F<CC> { void foo(CC); }; >From e55443f0f3c26d1eb41913c1026a7786718d5203 Mon Sep 17 00:00:00 2001 From: Younan Zhang <zyn7...@gmail.com> Date: Sat, 31 Aug 2024 16:46:21 +0800 Subject: [PATCH 3/3] Release note --- clang/docs/ReleaseNotes.rst | 2 ++ clang/lib/Sema/SemaTemplate.cpp | 1 + 2 files changed, 3 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index d3aebdb0b06477..1d64d3b07609c2 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -329,6 +329,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 5465834f68c06e..bf6b53700d90eb 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -8112,6 +8112,7 @@ DeclResult Sema::ActOnClassTemplateSpecialization( if (S->isTemplateParamScope()) EnterTemplatedContext(S, ClassTemplate->getTemplatedDecl()); + DeclContext *DC = ClassTemplate->getDeclContext(); bool isMemberSpecialization = false; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits