Skip to content

Commit 0852c8c

Browse files
committed
[Clang] Reject declaring an alias template with the same name as its template parameter.
Fixes #123423
1 parent bb59eb8 commit 0852c8c

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ Bug Fixes to C++ Support
956956
- Fixed a crash caused by the incorrect construction of template arguments for CTAD alias guides when type
957957
constraints are applied. (#GH122134)
958958
- Fixed canonicalization of pack indexing types - Clang did not always recognized identical pack indexing. (#GH123033)
959+
- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
959960

960961

961962
Bug Fixes to AST Handling

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13464,6 +13464,14 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
1346413464
}
1346513465
TemplateParameterList *TemplateParams = TemplateParamLists[0];
1346613466

13467+
// Check shadowing of a template parameter name
13468+
for (NamedDecl *TP : TemplateParams->asArray()) {
13469+
if (NameInfo.getName() == TP->getDeclName()) {
13470+
DiagnoseTemplateParameterShadow(Name.StartLocation, TP);
13471+
return nullptr;
13472+
}
13473+
}
13474+
1346713475
// Check that we can declare a template here.
1346813476
if (CheckTemplateDeclScope(S, TemplateParams))
1346913477
return nullptr;

clang/test/SemaCXX/alias-template.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ namespace InFunctions {
6565
template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
6666
template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
6767
};
68-
template<typename Z> using Z = Z;
68+
template<typename Z> // expected-note {{template parameter is declared here}}
69+
using Z = Z; // expected-error {{declaration of 'Z' shadows template parameter}}
6970
}
7071

7172
namespace ClassNameRedecl {
@@ -191,4 +192,4 @@ int g = sfinae_me<int>(); // expected-error{{no matching function for call to 's
191192

192193
namespace NullExceptionDecl {
193194
template<int... I> auto get = []() { try { } catch(...) {}; return I; }; // expected-error{{initializer contains unexpanded parameter pack 'I'}}
194-
}
195+
}

0 commit comments

Comments
 (0)