Skip to content

Commit

Permalink
[Clang] Diagnose invalid member variable with template parameters
Browse files Browse the repository at this point in the history
Fixes #54151

Reviewed By: erichkeane, aaron.ballman

Differential Revision: https://reviews.llvm.org/D120881
  • Loading branch information
cor3ntin committed Mar 3, 2022
1 parent 7d249df commit 942c039
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -4811,6 +4811,7 @@ def warn_cxx11_compat_variable_template : Warning<
def err_template_variable_noparams : Error<
"extraneous 'template<>' in declaration of variable %0">;
def err_template_member : Error<"member %0 declared as a template">;
def err_member_with_template_arguments : Error<"member %0 cannot have template arguments">;
def err_template_member_noparams : Error<
"extraneous 'template<>' in declaration of member %0">;
def err_template_tag_noparams : Error<
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaDeclCXX.cpp
Expand Up @@ -3395,6 +3395,14 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
return nullptr;
}

if (D.getName().getKind() == UnqualifiedIdKind::IK_TemplateId) {
Diag(D.getIdentifierLoc(), diag::err_member_with_template_arguments)
<< II
<< SourceRange(D.getName().TemplateId->LAngleLoc,
D.getName().TemplateId->RAngleLoc)
<< D.getName().TemplateId->LAngleLoc;
}

if (SS.isSet() && !SS.isInvalid()) {
// The user provided a superfluous scope specifier inside a class
// definition:
Expand Down
20 changes: 20 additions & 0 deletions clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp
Expand Up @@ -32,3 +32,23 @@ namespace test1 {
template <> int AB::foo = 0; // expected-error{{extraneous 'template<>'}}
int AB::bar = 1;
}

namespace GH54151 {

struct S {
int i<0>; // expected-error {{member 'i' cannot have template arguments}}
int j<int>; // expected-error {{member 'j' cannot have template arguments}}

static int k<12>; // expected-error {{template specialization requires 'template<>'}} \
expected-error{{no variable template matches specialization}}
void f<12>(); // expected-error {{template specialization requires 'template<>'}} \
// expected-error {{no function template matches function template specialization 'f'}}
};

template <typename T, int N>
struct U {
int i<N>; // expected-error {{member 'i' cannot have template arguments}}
int j<T>; // expected-error {{member 'j' cannot have template arguments}}
};

} // namespace GH54151

0 comments on commit 942c039

Please sign in to comment.