This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG942c03910aef: [Clang] Diagnose invalid member variable with template parameters (authored by cor3ntin).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D120881/new/ https://reviews.llvm.org/D120881 Files: clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaDeclCXX.cpp clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp Index: clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp =================================================================== --- clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp +++ clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp @@ -32,3 +32,23 @@ 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 Index: clang/lib/Sema/SemaDeclCXX.cpp =================================================================== --- clang/lib/Sema/SemaDeclCXX.cpp +++ clang/lib/Sema/SemaDeclCXX.cpp @@ -3395,6 +3395,14 @@ 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: Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4811,6 +4811,7 @@ 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<
Index: clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp =================================================================== --- clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp +++ clang/test/CXX/temp/temp.spec/temp.expl.spec/p17.cpp @@ -32,3 +32,23 @@ 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 Index: clang/lib/Sema/SemaDeclCXX.cpp =================================================================== --- clang/lib/Sema/SemaDeclCXX.cpp +++ clang/lib/Sema/SemaDeclCXX.cpp @@ -3395,6 +3395,14 @@ 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: Index: clang/include/clang/Basic/DiagnosticSemaKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticSemaKinds.td +++ clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4811,6 +4811,7 @@ 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<
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits