Skip to content

Commit f607e3f

Browse files
authored
[Clang][Sema] Reject declaring an alias template with the same name as its template parameter. (#123533)
The issue occurred because the template parameter scope was skipped too early, before diagnosing the alias name shadowing. To fix this, the patch moves it to after LookupName, such that the behavior remains consistent with the typedef implementation. Fixes #123423
1 parent 1f26ac1 commit f607e3f

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ Bug Fixes to C++ Support
990990
- Fix immediate escalation not propagating through inherited constructors. (#GH112677)
991991
- Fixed assertions or false compiler diagnostics in the case of C++ modules for
992992
lambda functions or inline friend functions defined inside templates (#GH122493).
993+
- Clang now rejects declaring an alias template with the same name as its template parameter. (#GH123423)
993994

994995
Bug Fixes to AST Handling
995996
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13406,8 +13406,6 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
1340613406
SourceLocation UsingLoc, UnqualifiedId &Name,
1340713407
const ParsedAttributesView &AttrList,
1340813408
TypeResult Type, Decl *DeclFromDeclSpec) {
13409-
// Get the innermost enclosing declaration scope.
13410-
S = S->getDeclParent();
1341113409

1341213410
if (Type.isInvalid())
1341313411
return nullptr;
@@ -13458,6 +13456,9 @@ Decl *Sema::ActOnAliasDeclaration(Scope *S, AccessSpecifier AS,
1345813456
CheckTypedefForVariablyModifiedType(S, NewTD);
1345913457
Invalid |= NewTD->isInvalidDecl();
1346013458

13459+
// Get the innermost enclosing declaration scope.
13460+
S = S->getDeclParent();
13461+
1346113462
bool Redeclaration = false;
1346213463

1346313464
NamedDecl *NewND;

clang/test/CXX/temp/temp.decls/temp.variadic/fixed-expansion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ namespace PartialSpecialization {
121121

122122
namespace FixedAliasTemplate {
123123
template<typename,typename,typename> struct S {};
124-
template<typename T, typename U> using U = S<T, int, U>; // expected-note 2{{template parameter is declared here}}
125-
template<typename...Ts> U<Ts...> &f(U<Ts...>, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}}
124+
template<typename T, typename U> using Z = S<T, int, U>; // expected-note 2{{template parameter is declared here}}
125+
template<typename...Ts> Z<Ts...> &f(Z<Ts...>, Ts...); // expected-error 2{{pack expansion used as argument for non-pack parameter of alias template}}
126126
S<int, int, double> &s1 = f({}, 0, 0.0); // expected-error {{no matching function}}
127127
}
128128

clang/test/SemaCXX/alias-template.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,24 @@ namespace LookupFilter {
5454
template<typename U> using S = S<U>*; // ok
5555
}
5656

57-
namespace InFunctions {
57+
namespace UnexpandedPack {
5858
template<typename...T> struct S0 {
5959
template<typename Z> using U = T*; // expected-error {{declaration type contains unexpanded parameter pack 'T'}}
6060
U<char> u;
6161
};
62+
}
6263

64+
namespace InvalidType {
6365
template<typename Z> using T1 = int;
6466
template<typename Z> using T2 = int[-1]; // expected-error {{array size is negative}}
67+
}
68+
69+
namespace ShadowTemplateParam {
6570
template<typename...T> struct S3 { // expected-note {{template parameter is declared here}}
6671
template<typename Z> using T = int; // expected-error {{declaration of 'T' shadows template parameter}}
6772
};
68-
template<typename Z> using Z = Z;
73+
template<typename Z> // expected-note {{template parameter is declared here}}
74+
using Z = Z; // expected-error {{declaration of 'Z' shadows template parameter}}
6975
}
7076

7177
namespace ClassNameRedecl {

0 commit comments

Comments
 (0)