diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index b60ca81b04609..dfc56fc9f6d2c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -209,6 +209,8 @@ Improvements to Clang's diagnostics `WG14 N3029 `_. Fixes `#69352 `_. +- Clang now diagnoses extraneous template parameter lists as a language extension. + Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 6e3cebc311eeb..47e892e34260c 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -5325,7 +5325,7 @@ def err_alias_template_extra_headers : Error< def err_template_spec_extra_headers : Error< "extraneous template parameter list in template specialization or " "out-of-line template definition">; -def warn_template_spec_extra_headers : Warning< +def ext_template_spec_extra_headers : ExtWarn< "extraneous template parameter list in template specialization">; def note_explicit_template_spec_does_not_need_header : Note< "'template<>' header not required for explicitly-specialized class %0 " diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 9e516da2aa27a..1a975a8d0a0df 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -3667,7 +3667,7 @@ TemplateParameterList *Sema::MatchTemplateParametersToScopeSpecifier( if (!SuppressDiagnostic) Diag(ParamLists[ParamIdx]->getTemplateLoc(), - AllExplicitSpecHeaders ? diag::warn_template_spec_extra_headers + AllExplicitSpecHeaders ? diag::ext_template_spec_extra_headers : diag::err_template_spec_extra_headers) << SourceRange(ParamLists[ParamIdx]->getTemplateLoc(), ParamLists[ParamLists.size() - 2]->getRAngleLoc()); diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp index 469c4e091953c..683392c589153 100644 --- a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp +++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p23.cpp @@ -1,7 +1,6 @@ // RUN: %clang_cc1 -std=c++20 -pedantic-errors -verify %s -// FIXME: This should be an error with -pedantic-errors. -template<> // expected-warning {{extraneous template parameter list in template specialization}} +template<> // expected-error {{extraneous template parameter list in template specialization}} void f(auto); template @@ -18,7 +17,6 @@ void A::g(auto) { } template<> void A::g(auto) { } -// FIXME: This should be an error with -pedantic-errors. template<> -template<> // expected-warning {{extraneous template parameter list in template specialization}} +template<> // expected-error {{extraneous template parameter list in template specialization}} void A::g(auto) { } diff --git a/clang/test/CXX/drs/dr5xx.cpp b/clang/test/CXX/drs/dr5xx.cpp index 21a6646d4abcf..0e1de342f6706 100644 --- a/clang/test/CXX/drs/dr5xx.cpp +++ b/clang/test/CXX/drs/dr5xx.cpp @@ -354,17 +354,15 @@ namespace dr531 { // dr531: partial template<> void A::f(char) {} // expected-error@-1 {{no function template matches function template specialization 'f'}} - // FIXME: This is ill-formed; -pedantic-errors should reject. template<> template void A::g(char, U) {} - // expected-warning@-1 {{extraneous template parameter list in template specialization}} + // expected-error@-1 {{extraneous template parameter list in template specialization}} // expected-note@#dr531-A-char {{'template<>' header not required for explicitly-specialized class 'dr531::bad::A' declared here}} template<> struct A::B {}; // expected-error@-1 {{extraneous 'template<>' in declaration of struct 'B'}} // expected-error@-2 {{specialization of member 'dr531::bad::A::B' does not specialize an instantiated member}} // expected-note@#dr531-B {{attempt to specialize declaration here}} - // FIXME: This is ill-formed; -pedantic-errors should reject. template<> template struct A::C {}; - // expected-warning@-1 {{extraneous template parameter list in template specialization}} + // expected-error@-1 {{extraneous template parameter list in template specialization}} // expected-note@#dr531-A-char {{'template<>' header not required for explicitly-specialized class 'dr531::bad::A' declared here}} template<> char A::n = 0; // expected-error@-1 {{extraneous 'template<>' in declaration of variable 'n'}} @@ -808,7 +806,7 @@ namespace dr571 { // dr571 unknown int n; // FIXME: Test if this has internal linkage. const ir r = n; - // expected-warning@-1 {{'const' qualifier on reference type 'ir' (aka 'int &') has no effect}} + // expected-warning@-1 {{'const' qualifier on reference type 'ir' (aka 'int &') has no effect}} } namespace dr572 { // dr572: yes @@ -990,7 +988,7 @@ namespace dr580 { // dr580: partial // FIXME: We incorrectly accept this // because we think C2::Y::A<...> might // instantiate to C2::X::A - template struct A {}; + template struct A {}; }; }; diff --git a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p16.cpp b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p16.cpp index c7597e9f817ca..b7c8d3fdfe264 100644 --- a/clang/test/CXX/temp/temp.spec/temp.expl.spec/p16.cpp +++ b/clang/test/CXX/temp/temp.spec/temp.expl.spec/p16.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsyntax-only -pedantic-errors -verify %s template struct A { void f(T); template void g1(T, X1); @@ -36,3 +36,97 @@ namespace PR10024 { template <> void Test::get(double i) {} // expected-error{{cannot specialize (with 'template<>') a member of an unspecialized template}} } + +namespace extraneous { + template struct A; + + template int x; + + template void f(); + + template<> // expected-error{{extraneous template parameter list in template specialization}} + template<> + struct A; + + template<> // expected-error{{extraneous template parameter list in template specialization}} + template<> + int x; + + template<> // expected-error{{extraneous template parameter list in template specialization}} + template<> + void f(); + + template + struct B { + struct C; + + template + struct D; + + static int y; + + template + static int z; + + void g(); + + template + void h(); + + enum class E; + + enum F : int; + }; + + template<> + template<> // expected-error{{extraneous 'template<>' in declaration of struct 'C'}} + struct B::C; + + template<> + template<> // expected-error{{extraneous template parameter list in template specialization}} + template<> + struct B::D; + + template<> + template<> // expected-error{{extraneous template parameter list in template specialization}} + template + struct B::D; + + template<> + template<> // expected-error{{extraneous 'template<>' in declaration of variable 'y'}} + int B::y; + + template<> + template<> // expected-error{{extraneous template parameter list in template specialization}} + template<> + int B::z; + + template<> + template<> // expected-error{{extraneous template parameter list in template specialization}} + template + int B::z; + + template<> + template<> + void B::g(); // expected-error{{no function template matches function template specialization 'g'}} + + template<> + template<> // expected-error{{extraneous template parameter list in template specialization}} + template<> + void B::h(); + + template<> + template<> // expected-error{{extraneous template parameter list in template specialization}} + template + void B::h(); // expected-error{{function template partial specialization is not allowed}} + + // FIXME: We should diagnose this as having an extraneous 'template<>' + template<> + template<> + enum class B::E; // expected-error{{enumeration cannot be a template}} + + // FIXME: We should diagnose this as having an extraneous 'template<>' + template<> + template<> + enum B::F : int; // expected-error{{enumeration cannot be a template}} +} diff --git a/clang/test/Misc/warning-flags.c b/clang/test/Misc/warning-flags.c index 07a75046d4e01..9d4cac9e39b42 100644 --- a/clang/test/Misc/warning-flags.c +++ b/clang/test/Misc/warning-flags.c @@ -26,6 +26,7 @@ CHECK-NEXT: ext_missing_whitespace_after_macro_name CHECK-NEXT: ext_new_paren_array_nonconst CHECK-NEXT: ext_plain_complex CHECK-NEXT: ext_template_arg_extra_parens +CHECK-NEXT: ext_template_spec_extra_headers CHECK-NEXT: ext_typecheck_cond_incompatible_operands CHECK-NEXT: ext_typecheck_ordered_comparison_of_pointer_integer CHECK-NEXT: ext_using_undefined_std @@ -78,7 +79,6 @@ CHECK-NEXT: warn_register_objc_catch_parm CHECK-NEXT: warn_related_result_type_compatibility_class CHECK-NEXT: warn_related_result_type_compatibility_protocol CHECK-NEXT: warn_template_export_unsupported -CHECK-NEXT: warn_template_spec_extra_headers CHECK-NEXT: warn_tentative_incomplete_array CHECK-NEXT: warn_typecheck_function_qualifiers CHECK-NEXT: warn_undef_interface