Hi,

apparently we aren't implementing the bits of DR 1732 [C++14] about range-based for declarations. Tested x86_64-linux.

Thanks,
Paolo.

///////////////////////
/cp
2015-04-28  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/59729, DR 1732
        * parser.c (cp_parser_for_init_statement): Do not allow type
        definitions in range-based for loop declarations.

/testsuite
2015-04-28  Paolo Carlini  <paolo.carl...@oracle.com>

        PR c++/59729, DR 1732
        * g++.dg/cpp0x/range-for30.C: New.
        * g++.dg/cpp0x/range-for8.C: Update.
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 222548)
+++ cp/parser.c (working copy)
@@ -10897,6 +10897,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;
+      const char *saved_message = parser->type_definition_forbidden_message;
 
       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
          && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
@@ -10916,6 +10917,9 @@ cp_parser_for_init_statement (cp_parser* parser, t
 
       /* A colon is used in range-based for.  */
       parser->colon_corrects_to_scope_p = false;
+      parser->type_definition_forbidden_message
+       = G_("types may not be defined in a range-based %<for%> loop "
+            "declaration");
 
       /* We're going to speculatively look for a declaration, falling back
         to an expression, if necessary.  */
@@ -10924,6 +10928,8 @@ cp_parser_for_init_statement (cp_parser* parser, t
       cp_parser_simple_declaration (parser,
                                    /*function_definition_allowed_p=*/false,
                                    decl);
+
+      parser->type_definition_forbidden_message = saved_message;
       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
        {
Index: testsuite/g++.dg/cpp0x/range-for30.C
===================================================================
--- testsuite/g++.dg/cpp0x/range-for30.C        (revision 0)
+++ testsuite/g++.dg/cpp0x/range-for30.C        (working copy)
@@ -0,0 +1,12 @@
+// PR c++/59729, DR 1732
+// { dg-require-effective-target c++11 }
+
+struct S {
+  template <typename T>
+  operator T() { return T(); }
+};
+
+int main() {
+  S s[1];
+  for (struct T { } t : s) { }  // { dg-error "types may not be defined" }
+}
Index: testsuite/g++.dg/cpp0x/range-for8.C
===================================================================
--- testsuite/g++.dg/cpp0x/range-for8.C (revision 222548)
+++ testsuite/g++.dg/cpp0x/range-for8.C (working copy)
@@ -1,5 +1,5 @@
 // Test for range-based for loop when the declarator declares
-// a new type
+// a new type (rejected per DR 1732)
 
 // { dg-do compile { target c++11 } }
 
@@ -7,9 +7,9 @@
 
 void test()
 {
-    for (struct S { } *x : { (S*)0, (S*)0 } )
+    for (struct S { } *x : { (S*)0, (S*)0 } ) // { dg-error "types may not be 
defined" }
         ;
 
-    for (struct S { } x : { S(), S() } )
+    for (struct S { } x : { S(), S() } ) // { dg-error "types may not be 
defined" }
         ;
 }

Reply via email to