llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Krystian Stasiowski (sdkrystian) <details> <summary>Changes</summary> According to [[dcl.fct] p23](http://eel.is/c++draft/dcl.fct#<!-- -->23): > An abbreviated function template can have a _template-head_. The invented _template-parameters_ are appended to the _template-parameter-list_ after the explicitly declared _template-parameters_. `template<>` is not a _template-head_ -- a _template-head_ must have at least one _template-parameter_. This patch corrects our current behavior of appending the invented template parameters to the innermost template parameter list, regardless of whether it is empty. Example: ```cpp template<typename T> struct A { void f(auto); }; template<> void A<int>::f(auto); // ok template<> template<> // warning: extraneous template parameter list in template specialization void A<int>::f(auto); ``` --- Full diff: https://github.com/llvm/llvm-project/pull/80864.diff 4 Files Affected: - (modified) clang/docs/ReleaseNotes.rst (+2) - (modified) clang/lib/Sema/SemaDecl.cpp (+1-1) - (modified) clang/lib/Sema/SemaDeclCXX.cpp (+10-1) - (added) clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp (+24) ``````````diff diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4d57ea4fd55b8..d0ba986601e92 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -204,6 +204,8 @@ Bug Fixes to C++ Support parameter where we did an incorrect specialization of the initialization of the default parameter. Fixes (`#68490 <https://github.com/llvm/llvm-project/issues/68490>`_) +- Fixed a bug where abbreviated function templates would append their invented template parameters to + an empty template parameter lists. Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 481d952d2389b..d9704e5bfd67c 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -9759,7 +9759,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, SmallVector<TemplateParameterList *, 4> TemplateParamLists; llvm::append_range(TemplateParamLists, TemplateParamListsRef); if (TemplateParameterList *Invented = D.getInventedTemplateParameterList()) { - if (!TemplateParamLists.empty() && + if (!TemplateParamLists.empty() && TemplateParamLists.back()->size() && Invented->getDepth() == TemplateParamLists.back()->getDepth()) TemplateParamLists.back() = Invented; else diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index ab8a967b06a45..f34893d88a3b3 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -19294,7 +19294,16 @@ void Sema::ActOnStartFunctionDeclarationDeclarator( ExplicitLists, /*IsFriend=*/false, IsMemberSpecialization, IsInvalid, /*SuppressDiagnostic=*/true); } - if (ExplicitParams) { + // C++23 [dcl.fct]p23: + // An abbreviated function template can have a template-head. The invented + // template-parameters are appended to the template-parameter-list after + // the explicitly declared template-parameters. + // + // A template-head must have one or more template-parameters (read: + // 'template<>' is *not* a template-head). Only append the invented + // template parameters if we matched the nested-name-specifier to a non-empty + // TemplateParameterList. + if (ExplicitParams && ExplicitParams->size()) { Info.AutoTemplateParameterDepth = ExplicitParams->getDepth(); llvm::append_range(Info.TemplateParams, *ExplicitParams); Info.NumExplicitTemplateParams = ExplicitParams->size(); diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp new file mode 100644 index 0000000000000..469c4e091953c --- /dev/null +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp @@ -0,0 +1,24 @@ +// RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify %s + +// FIXME: This should be an error with -pedantic-errors. +template<> // expected-warning {{extraneous template parameter list in template specialization}} +void f(auto); + +template<typename> +void f(auto); + +template<typename T> +struct A { + void g(auto); +}; + +template<typename T> +void A<T>::g(auto) { } + +template<> +void A<int>::g(auto) { } + +// FIXME: This should be an error with -pedantic-errors. +template<> +template<> // expected-warning {{extraneous template parameter list in template specialization}} +void A<long>::g(auto) { } `````````` </details> https://github.com/llvm/llvm-project/pull/80864 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits