Skip to content

Commit 942c039

Browse files
committed
[Clang] Diagnose invalid member variable with template parameters
Fixes #54151 Reviewed By: erichkeane, aaron.ballman Differential Revision: https://reviews.llvm.org/D120881
1 parent 7d249df commit 942c039

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,6 +4811,7 @@ def warn_cxx11_compat_variable_template : Warning<
48114811
def err_template_variable_noparams : Error<
48124812
"extraneous 'template<>' in declaration of variable %0">;
48134813
def err_template_member : Error<"member %0 declared as a template">;
4814+
def err_member_with_template_arguments : Error<"member %0 cannot have template arguments">;
48144815
def err_template_member_noparams : Error<
48154816
"extraneous 'template<>' in declaration of member %0">;
48164817
def err_template_tag_noparams : Error<

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,6 +3395,14 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
33953395
return nullptr;
33963396
}
33973397

3398+
if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) {
3399+
Diag(D.getIdentifierLoc(), diag::err_member_with_template_arguments)
3400+
<< II
3401+
<< SourceRange(D.getName().TemplateId->LAngleLoc,
3402+
D.getName().TemplateId->RAngleLoc)
3403+
<< D.getName().TemplateId->LAngleLoc;
3404+
}
3405+
33983406
if (SS.isSet() && !SS.isInvalid()) {
33993407
// The user provided a superfluous scope specifier inside a class
34003408
// definition:

clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,23 @@ namespace test1 {
3232
template <> int AB::foo = 0; // expected-error{{extraneous 'template<>'}}
3333
int AB::bar = 1;
3434
}
35+
36+
namespace GH54151 {
37+
38+
struct S {
39+
int i<0>; // expected-error {{member 'i' cannot have template arguments}}
40+
int j<int>; // expected-error {{member 'j' cannot have template arguments}}
41+
42+
static int k<12>; // expected-error {{template specialization requires 'template<>'}} \
43+
expected-error{{no variable template matches specialization}}
44+
void f<12>(); // expected-error {{template specialization requires 'template<>'}} \
45+
// expected-error {{no function template matches function template specialization 'f'}}
46+
};
47+
48+
template <typename T, int N>
49+
struct U {
50+
int i<N>; // expected-error {{member 'i' cannot have template arguments}}
51+
int j<T>; // expected-error {{member 'j' cannot have template arguments}}
52+
};
53+
54+
} // namespace GH54151

0 commit comments

Comments
 (0)