Hi,

this accepts-invalid is about an invalid loop of the form:

    for (int i = 5: arr)

thus it starts with an initialized declaration, which would be legal in a normal for loop, but then the colon means that it can only be an invalid range-based for loop. Ideally, it would be nice to say something more detailed about the invalid declaration, but that doesn't seem trivial... Would something like the below be ok for now? Tested x86_64-linux.

Thanks,
Paolo.

/////////////////////////
/cp
2014-12-04  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/63985
        * parser.c (cp_parser_for_init_statement): Reject invalid declarations
        in range-based for loops.

/testsuite
2014-12-04  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/63985
        * g++.dg/cpp0x/range-for29.C: New.
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 218348)
+++ cp/parser.c (working copy)
@@ -10841,6 +10841,7 @@ cp_parser_for_init_statement (cp_parser* parser, t
     {
       bool is_range_for = false;
       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
+      location_t loc = cp_lexer_peek_token (parser->lexer)->location;
 
       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
          && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
@@ -10881,6 +10882,8 @@ cp_parser_for_init_statement (cp_parser* parser, t
                       "-std=c++11 or -std=gnu++11");
              *decl = error_mark_node;
            }
+         if (*decl == error_mark_node)
+           error_at (loc, "invalid declaration in range-based %<for%> loop");  
    
        }
       else
          /* The ';' is not consumed yet because we told
Index: testsuite/g++.dg/cpp0x/range-for29.C
===================================================================
--- testsuite/g++.dg/cpp0x/range-for29.C        (revision 0)
+++ testsuite/g++.dg/cpp0x/range-for29.C        (working copy)
@@ -0,0 +1,10 @@
+// PR c++/63985
+// { dg-require-effective-target c++11 }
+
+int main()
+{
+  int arr;
+  for (int i = 5: arr)  // { dg-error "invalid declaration" }
+    return 1;
+  return 0;
+}

Reply via email to