===================================================================
@@ -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))
{
===================================================================
@@ -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" }
+}
===================================================================
@@ -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" }
;
}