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] c++/71147 - [6 Regression] Flexible array member wrongly rejected in template


The handling of flexible array members whose element type was
dependent tried to deal with the case when the element type
was not yet completed but it did it wrong.  The attached patch
corrects the handling by trying to complete the element type
first.

Thanks
Martin
PR c++/71147 - [6 Regression] Flexible array member wrongly rejected in template

gcc/testsuite/ChangeLog:
2016-05-18  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* g++.dg/ext/flexary16.C: New test.

gcc/cp/ChangeLog:
2016-05-18  Martin Sebor  <msebor@redhat.com>

	PR c++/71147
	* pt.c (instantiate_class_template_1): Try to complete the element
	type of a flexible array member.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 65bfd42..73291e0 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -10119,16 +10119,25 @@ instantiate_class_template_1 (tree type)
 			  if (can_complete_type_without_circularity (rtype))
 			    complete_type (rtype);
 
+			  bool complete;
+
                           if (TREE_CODE (r) == FIELD_DECL
                               && TREE_CODE (rtype) == ARRAY_TYPE
-                              && COMPLETE_TYPE_P (TREE_TYPE (rtype))
                               && !COMPLETE_TYPE_P (rtype))
                             {
-                              /* Flexible array mmembers of elements
-                                 of complete type have an incomplete type
-                                 and that's okay.  */
+                              /* Flexible array mmembers have an incomplete
+				 type and that's okay as long as their element
+				 type is complete.  */
+			      tree eltype = TREE_TYPE (rtype);
+			      if (can_complete_type_without_circularity (eltype))
+				complete_type (eltype);
+
+			      complete = COMPLETE_TYPE_P (eltype);
                             }
-                          else if (!COMPLETE_TYPE_P (rtype))
+			  else
+			    complete = COMPLETE_TYPE_P (rtype);
+
+			  if (!complete)
 			    {
 			      cxx_incomplete_type_error (r, rtype);
 			      TREE_TYPE (r) = error_mark_node;
diff --git a/gcc/testsuite/g++.dg/ext/flexary16.C b/gcc/testsuite/g++.dg/ext/flexary16.C
new file mode 100644
index 0000000..a3e040d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/flexary16.C
@@ -0,0 +1,37 @@
+// PR c++/71147 - [6 Regression] Flexible array member wrongly rejected
+//   in template
+// { dg-do compile }
+
+template <typename>
+struct container
+{
+  struct elem {
+    unsigned u;
+  };
+
+  struct incomplete {
+    int x;
+    elem array[];
+  };
+};
+
+unsigned f (container<void>::incomplete* i)
+{
+  return i->array [0].u;
+}
+
+
+template <typename T>
+struct D: container<T>
+{
+  struct S {
+    int x;
+    typename container<T>::elem array[];
+  };
+};
+
+
+unsigned g (D<void>::S *s)
+{
+  return s->array [0].u;
+}

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