Hi,
this is a slightly reworked (simplified) version of a patch I sent a
while ago. The issue is that we are not enforcing at all 5.3.4/2 in the
parser, thus we end up rejecting the first test below with a misleading
error message talking about list-initialization (and a wrong location),
because we diagnose it too late like 'auto foo{3, 4, 5};', and simply
accepting the second. Tested x86_64-linux.
Thanks,
Paolo.
////////////////////////
/cp
2015-09-11 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/51911
* parser.c (cp_parser_new_expression): Enforce 5.3.4/2.
/testsuite
2015-09-11 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/51911
* g++.dg/cpp0x/new-auto1.C: New.
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 227690)
+++ cp/parser.c (working copy)
@@ -7591,8 +7591,9 @@ cp_parser_new_expression (cp_parser* parser)
type = cp_parser_new_type_id (parser, &nelts);
/* If the next token is a `(' or '{', then we have a new-initializer. */
- if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
- || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
+ cp_token *token = cp_lexer_peek_token (parser->lexer);
+ if (token->type == CPP_OPEN_PAREN
+ || token->type == CPP_OPEN_BRACE)
initializer = cp_parser_new_initializer (parser);
else
initializer = NULL;
@@ -7601,6 +7602,18 @@ cp_parser_new_expression (cp_parser* parser)
expression. */
if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
ret = error_mark_node;
+ /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
+ of a new-type-id or type-id of a new-expression, the new-expression shall
+ contain a new-initializer of the form ( assignment-expression )". */
+ else if (type_uses_auto (type)
+ && (token->type != CPP_OPEN_PAREN
+ || vec_safe_length (initializer) != 1))
+ {
+ error_at (token->location,
+ "initialization of new-expression for type %<auto%> "
+ "requires exactly one parenthesized expression");
+ ret = error_mark_node;
+ }
else
{
/* Create a representation of the new-expression. */
Index: testsuite/g++.dg/cpp0x/new-auto1.C
===================================================================
--- testsuite/g++.dg/cpp0x/new-auto1.C (revision 0)
+++ testsuite/g++.dg/cpp0x/new-auto1.C (working copy)
@@ -0,0 +1,7 @@
+// PR c++/51911
+// { dg-do compile { target c++11 } }
+
+#include <initializer_list>
+
+auto foo = new auto { 3, 4, 5 }; // { dg-error "21:initialization of
new-expression for type 'auto'" }
+auto bar = new auto { 3 }; // { dg-error "21:initialization of new-expression
for type 'auto'" }