[C++ PATCH] Fix incomplete type error recovery (PR c++/71516)

Jakub Jelinek jakub@redhat.com
Mon Jun 13 18:44:00 GMT 2016


Hi!

On the following testcase we ICE during error recovery, because
a is first added to the incomplete vars vector, but then is attempted to
be initialized, which results in error and setting its type to
error_mark_node (as the type has been incomplete).
When we try to complete vars, we ICE because TYPE_MAIN_VARIANT expects to
see a type, rather than error_mark_node (with tree checking).

Ok for trunk?  Would this be reasonable to backport too (I mean, it
shouldn't break anything and accessing TYPE_MAIN_VARIANT (error_mark_node)
can crash miserably)?

Bootstrapped/regtested on x86_64-linux and i686-linux.

2016-06-13  Jakub Jelinek  <jakub@redhat.com>

	PR c++/71516
	* decl.c (complete_vars): Handle gracefully type == error_mark_node.

	* g++.dg/init/pr71516.C: New test.

--- gcc/cp/decl.c.jj	2016-06-09 22:45:57.000000000 +0200
+++ gcc/cp/decl.c	2016-06-13 17:05:37.742493834 +0200
@@ -15029,8 +15029,9 @@ complete_vars (tree type)
 	  tree var = iv->decl;
 	  tree type = TREE_TYPE (var);
 
-	  if (TYPE_MAIN_VARIANT (strip_array_types (type))
-	      == iv->incomplete_type)
+	  if (type != error_mark_node
+	      && (TYPE_MAIN_VARIANT (strip_array_types (type))
+		  == iv->incomplete_type))
 	    {
 	      /* Complete the type of the variable.  The VAR_DECL itself
 		 will be laid out in expand_expr.  */
--- gcc/testsuite/g++.dg/init/pr71516.C.jj	2016-06-13 17:08:07.734548282 +0200
+++ gcc/testsuite/g++.dg/init/pr71516.C	2016-06-13 17:07:20.000000000 +0200
@@ -0,0 +1,10 @@
+// PR c++/71516
+// { dg-do compile }
+
+struct A;	// { dg-message "forward declaration of" }
+struct B
+{ 
+  static A a;
+};
+A B::a = A();	// { dg-error "has initializer but incomplete type|invalid use of incomplete type" }
+struct A {};

	Jakub



More information about the Gcc-patches mailing list