This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix PR c++/29731: ICE with statement expression as template parameter


Hi all.

The following invalid snippet causes an ICE on mainline:

=== cut here ===
template<int> struct A {};
A<({})> a;
=== cut here ===

The fact that the statement-expression is ill-placed (i.e. not in a function 
body) is reported in 'cp_parser_primary_expression', which does not return 
error_mark_node in that case but the TREE for the statement-expression. This 
causes an assertion to fail in 'uses_template_parms' later on. The attached 
patch fixes this by returning error_mark_node in 
'cp_parser_primary_expression' instead.

I have successfully regtested it on i686-pc-linux-gnu. Is it OK?

Thanks in advance.

Best regards,
Simon

:ADDPATCH c++:
2006-12-07  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/29731
	* parser.c (cp_parser_primary_expression): Return error_mark_node when
	a statement-expression is found outside of a function body.
Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	(revision 119633)
+++ gcc/cp/parser.c	(working copy)
@@ -2974,6 +2974,7 @@ cp_parser_primary_expression (cp_parser 
       {
 	tree expr;
 	bool saved_greater_than_is_operator_p;
+	bool has_error = false;
 
 	/* Consume the `('.  */
 	cp_lexer_consume_token (parser->lexer);
@@ -2998,7 +2999,10 @@ cp_parser_primary_expression (cp_parser 
 
 	       at class or namespace scope.  */
 	    if (!parser->in_function_body)
-	      error ("statement-expressions are allowed only inside functions");
+	      {
+		error ("statement-expressions are allowed only inside functions");
+		has_error = true;
+	      }
 	    /* Start the statement-expression.  */
 	    expr = begin_stmt_expr ();
 	    /* Parse the compound-statement.  */
@@ -3025,7 +3029,7 @@ cp_parser_primary_expression (cp_parser 
 	if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
 	  cp_parser_skip_to_end_of_statement (parser);
 
-	return expr;
+	return has_error ? error_mark_node : expr;
       }
 
     case CPP_KEYWORD:
2006-12-07  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/29731
	* g++.dg/parse/template22.C: New test.
/* PR c++/29731. This used to ICE in uses_template_parms. */

template<int> struct A {};

A<({})> a; /* { dg-error "forbids braced-groups within expressions|statement-expressions|template argument 1 is invalid|invalid type" } */

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]