On 11/29/25 10:16 PM, Jakub Jelinek wrote:
On Sat, Nov 29, 2025 at 09:54:50PM +0530, Jason Merrill wrote:
+ error_out:
+ if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
+ *maybe_range_for_decl = error_mark_node;
goto done;
Instead of putting error_out: here...
@@ -17347,6 +17347,8 @@ cp_parser_simple_declaration (cp_parser*
if (comma_loc != UNKNOWN_LOCATION)
error_at (comma_loc,
"multiple declarations in range-based %<for%> loop");
...how about putting it here?
Like this?
Will test tonight.
OK.
2025-11-29 Jakub Jelinek <[email protected]>
PR c++/122465
* parser.cc (cp_parser_simple_declaration): Adjust function comment.
Set *maybe_range_for_decl to error_mark_node instead of keeping it
NULL_TREE in error cases or when followed by CPP_COLON.
* g++.dg/cpp0x/pr122465.C: New test.
--- gcc/cp/parser.cc.jj 2025-11-24 09:02:57.276720338 +0100
+++ gcc/cp/parser.cc 2025-11-29 17:41:35.368721400 +0100
@@ -17100,8 +17100,9 @@ cp_parser_block_declaration (cp_parser *
If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
parsed declaration if it is an uninitialized single declarator not followed
- by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
- if present, will not be consumed. */
+ by a `;', or to NULL_TREE when not followed by `:' or to error_mark_node
+ otherwise. Either way, the trailing `;', if present, will not be
+ consumed. */
static void
cp_parser_simple_declaration (cp_parser* parser,
@@ -17153,7 +17154,7 @@ cp_parser_simple_declaration (cp_parser*
&& !decl_specifiers.any_specifiers_p)
{
cp_parser_error (parser, "expected declaration");
- goto done;
+ goto error_out;
}
/* If the next two tokens are both identifiers, the code is
@@ -17169,7 +17170,7 @@ cp_parser_simple_declaration (cp_parser*
looking at a declaration. */
cp_parser_commit_to_tentative_parse (parser);
/* Give up. */
- goto done;
+ goto error_out;
}
cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
@@ -17194,11 +17195,7 @@ cp_parser_simple_declaration (cp_parser*
if (token->type == CPP_SEMICOLON)
goto finish;
else if (maybe_range_for_decl)
- {
- if (*maybe_range_for_decl == NULL_TREE)
- *maybe_range_for_decl = error_mark_node;
- goto finish;
- }
+ goto finish;
/* Anything else is an error. */
else
{
@@ -17277,7 +17274,7 @@ cp_parser_simple_declaration (cp_parser*
statement is treated as a declaration-statement until proven
otherwise.) */
if (cp_parser_error_occurred (parser))
- goto done;
+ goto error_out;
if (auto_specifier_p && cxx_dialect >= cxx14)
{
@@ -17415,6 +17412,9 @@ cp_parser_simple_declaration (cp_parser*
if (comma_loc != UNKNOWN_LOCATION)
error_at (comma_loc,
"multiple declarations in range-based %<for%> loop");
+ error_out:
+ if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
+ *maybe_range_for_decl = error_mark_node;
}
done:
--- gcc/testsuite/g++.dg/cpp0x/pr122465.C.jj 2025-11-29 17:39:37.187641552
+0100
+++ gcc/testsuite/g++.dg/cpp0x/pr122465.C 2025-11-29 17:39:37.187641552
+0100
@@ -0,0 +1,10 @@
+// PR c++/122465
+// { dg-do compile { target c++11 } }
+
+void
+foo ()
+{
+ int x = 0;
+ for (const T i = { i } : x) // { dg-error "'T' does not name a type" }
+ ;
+}
Jakub