[clang] [Clang] Reject declaring an alias template with the same name as its template parameter. (PR #123533)
Valentyn Yukhymenko via cfe-commits
cfe-commits at lists.llvm.org
Sun Jan 19 15:58:01 PST 2025
https://github.com/BaLiKfromUA updated https://github.com/llvm/llvm-project/pull/123533
>From e451a8869420d9240f9006eb2adb599a3e6fd9f8 Mon Sep 17 00:00:00 2001
From: Valentyn Yukhymenko <valentin.yukhymenko at gmail.com>
Date: Sun, 19 Jan 2025 23:13:46 +0000
Subject: [PATCH] [Clang] Reject declaring an alias template with the same name
as its template parameter.
Fixes llvm#123423
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaDeclCXX.cpp | 8 ++++++++
.../CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp | 4 ++--
clang/test/SemaCXX/alias-template.cpp | 5 +++--
4 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index aa1c02d04f7caa..29e40b4ecab412 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -956,6 +956,7 @@ Bug Fixes to C++ Support
- Fixed a crash caused by the incorrect construction of template arguments for CTAD alias guides when type
constraints are applied. (#GH122134)
- Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
+- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
Bug Fixes to AST Handling
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index a867ed73bd4033..4e43a8397cec4e 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13464,6 +13464,14 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
}
TemplateParameterList *TemplateParams = TemplateParamLists[0];
+ // Check shadowing of a template parameter name
+ for (NamedDecl *TP : TemplateParams->asArray()) {
+ if (NameInfo.getName() == TP->getDeclName()) {
+ DiagnoseTemplateParameterShadow(Name.StartLocation, TP);
+ return nullptr;
+ }
+ }
+
// Check that we can declare a template here.
if (CheckTemplateDeclScope(S, TemplateParams))
return nullptr;
diff --git a/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp
index a990c82564aa40..ab4c663d24c7d5 100644
--- a/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp
+++ b/clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp
@@ -121,8 +121,8 @@ namespace PartialSpecialization {
namespace FixedAliasTemplate {
template<typename,typename,typename> struct S {};
- template<typename T, typename U> using U = S<T, int, U>; // expected-note 2{{template parameter is declared here}}
- template<typename...Ts> U<Ts...> &f(U<Ts...>, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}}
+ template<typename T, typename U> using Z = S<T, int, U>; // expected-note 2{{template parameter is declared here}}
+ template<typename...Ts> Z<Ts...> &f(Z<Ts...>, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}}
S<int, int, double> &s1 = f({}, 0, 0.0); // expected-error {{no matching function}}
}
diff --git a/clang/test/SemaCXX/alias-template.cpp b/clang/test/SemaCXX/alias-template.cpp
index 5189405e23db56..97134d2f3a96ad 100644
--- a/clang/test/SemaCXX/alias-template.cpp
+++ b/clang/test/SemaCXX/alias-template.cpp
@@ -65,7 +65,8 @@ namespace InFunctions {
template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
};
- template<typename Z> using Z = Z;
+ template<typename Z> // expected-note {{template parameter is declared here}}
+ using Z = Z; // expected-error {{declaration of 'Z' shadows template parameter}}
}
namespace ClassNameRedecl {
@@ -191,4 +192,4 @@ int g = sfinae_me<int>(); // expected-error{{no matching function for call to 's
namespace NullExceptionDecl {
template<int... I> auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}}
-}
+}
\ No newline at end of file
More information about the cfe-commits
mailing list