Hi,
the below implements quite literally the requirements. It does that
after the cp_parser_new_initializer call, I think that makes in general
for better error recovery. The wording definitely needs a review, though
(more concise?). Tested x86_64-linux.
Thanks,
Paolo.
///////////////////////////
/cp
2015-06-24 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/51911
* parser.c (cp_parser_new_expression): Enforce 5.3.4/2.
/testsuite
2015-06-24 Paolo Carlini <paolo.carl...@oracle.com>
PR c++/51911
* g++.dg/cpp0x/new-auto1.C: New.
Index: cp/parser.c
===================================================================
--- cp/parser.c (revision 224918)
+++ cp/parser.c (working copy)
@@ -7457,6 +7457,7 @@ cp_parser_new_expression (cp_parser* parser)
vec<tree, va_gc> *initializer;
tree nelts = NULL_TREE;
tree ret;
+ cp_token *token;
/* Look for the optional `::' operator. */
global_scope_p
@@ -7482,7 +7483,6 @@ cp_parser_new_expression (cp_parser* parser)
type-id. */
if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
{
- cp_token *token;
const char *saved_message = parser->type_definition_forbidden_message;
/* Consume the `('. */
@@ -7513,9 +7513,11 @@ cp_parser_new_expression (cp_parser* parser)
else
type = cp_parser_new_type_id (parser, &nelts);
+ token = cp_lexer_peek_token (parser->lexer);
+
/* 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))
+ if (token->type == CPP_OPEN_PAREN
+ || token->type == CPP_OPEN_BRACE)
initializer = cp_parser_new_initializer (parser);
else
initializer = NULL;
@@ -7524,6 +7526,19 @@ 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
+ || BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])))
+ {
+ 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,9 @@
+// PR c++/51911
+// { dg-do compile { target c++11 } }
+
+#include <initializer_list>
+
+int main()
+{
+ auto foo = new auto {3, 4, 5}; // { dg-error "initialization" }
+}