Skip to content

Commit 5286b07

Browse files
committed
c++: Diagnose this specifier on template parameters [PR113929]
For template parameters, the optional this specifier is in the grammar template-parameter-list -> template-parameter -> parameter-declaration, just [dcl.fct/6] says that it is only valid in parameter-list of certain functions. So, unlike the case of decl-specifier-seq used in non-terminals other than parameter-declaration, I think it is better not to fix this by cp_parser_decl_specifier_seq (parser, - flags | CP_PARSER_FLAGS_PARAMETER, + flags | (template_parameter_p ? 0 + : CP_PARSER_FLAGS_PARAMETER), &decl_specifiers, &declares_class_or_enum); which would be pretending it isn't in the grammar, but by diagnosing it separately, which is what the following patch does. 2024-02-16 Jakub Jelinek <[email protected]> PR c++/113929 * parser.cc (cp_parser_parameter_declaration): Diagnose this specifier on template parameter declaration. * g++.dg/parse/pr113929.C: New test.
1 parent 945cb84 commit 5286b07

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

gcc/cp/parser.cc

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25724,8 +25724,15 @@ cp_parser_parameter_declaration (cp_parser *parser,
2572425724
for a C-style variadic function. */
2572525725
token = cp_lexer_peek_token (parser->lexer);
2572625726

25727-
bool const xobj_param_p
25727+
bool xobj_param_p
2572825728
= decl_spec_seq_has_spec_p (&decl_specifiers, ds_this);
25729+
if (xobj_param_p && template_parm_p)
25730+
{
25731+
error_at (decl_specifiers.locations[ds_this],
25732+
"%<this%> specifier in template parameter declaration");
25733+
xobj_param_p = false;
25734+
decl_specifiers.locations[ds_this] = 0;
25735+
}
2572925736

2573025737
if (xobj_param_p
2573125738
&& ((declarator && declarator->parameter_pack_p)

gcc/testsuite/g++.dg/parse/pr113929.C

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// PR c++/113929
2+
// { dg-do compile }
3+
4+
template <this int C> // { dg-error "'this' specifier in template parameter declaration" }
5+
struct S {};
6+
template <int N, this int C> // { dg-error "'this' specifier in template parameter declaration" }
7+
struct T {};

0 commit comments

Comments
 (0)