[clang] [Clang][Sema] 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
Fri Jan 24 12:08:03 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 1/4] [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
>From 5d2d7dca93adac1ef2d2348713f626d2f2d95f6a Mon Sep 17 00:00:00 2001
From: Valentyn Yukhymenko <valentin.yukhymenko at gmail.com>
Date: Tue, 21 Jan 2025 22:38:43 +0000
Subject: [PATCH 2/4] code review remark and removal of the loop
---
clang/lib/Sema/SemaDeclCXX.cpp | 10 ----------
clang/test/SemaCXX/alias-template.cpp | 2 +-
2 files changed, 1 insertion(+), 11 deletions(-)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index e2e6326c5094dc..068442e66c3049 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13406,8 +13406,6 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
SourceLocation UsingLoc, UnqualifiedId &Name,
const ParsedAttributesView &AttrList,
TypeResult Type, Decl *DeclFromDeclSpec) {
- // Get the innermost enclosing declaration scope.
- S = S->getDeclParent();
if (Type.isInvalid())
return nullptr;
@@ -13473,14 +13471,6 @@ 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/SemaCXX/alias-template.cpp b/clang/test/SemaCXX/alias-template.cpp
index 97134d2f3a96ad..8ceaeb9452b017 100644
--- a/clang/test/SemaCXX/alias-template.cpp
+++ b/clang/test/SemaCXX/alias-template.cpp
@@ -192,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
+}
>From 59dddfc86608488fe3a97f151ace25a22330d6b1 Mon Sep 17 00:00:00 2001
From: Valentyn Yukhymenko <valentin.yukhymenko at gmail.com>
Date: Wed, 22 Jan 2025 22:34:54 +0000
Subject: [PATCH 3/4] Update ReleaseNotes.rst
---
clang/docs/ReleaseNotes.rst | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f65d1b7462224e..7898ca2a4907f6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -974,7 +974,6 @@ Bug Fixes to C++ Support
- Fix immediate escalation not propagating through inherited constructors. (#GH112677)
- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
-
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
>From b17ae6e55ccc0b213494d8e484e181382c8affb9 Mon Sep 17 00:00:00 2001
From: Valentyn Yukhymenko <valentin.yukhymenko at gmail.com>
Date: Fri, 24 Jan 2025 20:07:39 +0000
Subject: [PATCH 4/4] move changing of context to be after lookup but before
chaining
---
clang/lib/Sema/SemaDeclCXX.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 068442e66c3049..08065e3cad2bb3 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -13456,6 +13456,9 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
CheckTypedefForVariablyModifiedType(S, NewTD);
Invalid |= NewTD->isInvalidDecl();
+ // Get the innermost enclosing declaration scope.
+ S = S->getDeclParent();
+
bool Redeclaration = false;
NamedDecl *NewND;
More information about the cfe-commits
mailing list