[C++ PATCH] Fix implicit assignment operators with anonymous aggregates (take 2)

Jakub Jelinek jakub@redhat.com
Thu Mar 22 03:33:00 GMT 2001


On Thu, Mar 22, 2001 at 09:17:57AM +0000, Jason Merrill wrote:
> >>>>> "Jakub" == Jakub Jelinek <jakub@redhat.com> writes:
> 
> > Should I keep the change in build_modify_expr or use the previous patch
> > plus the decl.c bits to disallow ctors, dtors and assign_refs?
> 
> The latter.  Sorry about the confusion.

Ok, can I commit this (bootstrapped, tested) into head and branch (it is
regression against 2.95.x)?

2001-03-22  Jakub Jelinek  <jakub@redhat.com>

	* method.c (do_build_assign_ref): Don't use build_modify_expr for
	anonymous aggregates, since they don't have assignment operator
	method.
	* decl.c (fixup_anonymous_aggr): Disallow ctors, dtors and copy
	assignment operators for anonymous structure fields.

	* g++.old-deja/g++.other/anon8.C: New test.

--- gcc/cp/method.c.jj	Tue Mar 20 13:45:06 2001
+++ gcc/cp/method.c	Thu Mar 22 12:36:44 2001
@@ -703,7 +703,11 @@ do_build_assign_ref (fndecl)
 	                build_qualified_type (TREE_TYPE (field), cvquals),
 	                init, field);
 
-	  finish_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
+	  if (DECL_NAME (field))
+	    finish_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
+	  else
+	    finish_expr_stmt (build (MODIFY_EXPR, TREE_TYPE (comp), comp,
+				     init));
 	}
     }
   finish_return_stmt (current_class_ref);
--- gcc/cp/decl.c.jj	Thu Mar 22 12:11:00 2001
+++ gcc/cp/decl.c	Thu Mar 22 12:47:18 2001
@@ -6815,6 +6815,33 @@ fixup_anonymous_aggr (t)
   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
   if (TYPE_METHODS (t))
     cp_error_at ("an anonymous union cannot have function members", t);
+
+  /* Anonymous aggregates cannot have fields with ctors, dtors or complex
+     assignment operators (because they cannot have these methods themselves).
+     For anonymous unions this is already checked because they are not allowed
+     in any union, otherwise we have to check it.  */
+  if (TREE_CODE (t) != UNION_TYPE)
+    {
+      tree field, type;
+
+      for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
+	if (TREE_CODE (field) == FIELD_DECL)
+	  {
+	    type = TREE_TYPE (field);
+	    if (CLASS_TYPE_P (type))
+	      {
+	        if (TYPE_NEEDS_CONSTRUCTING (type))
+		  cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
+			       field);
+		if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
+		  cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
+			       field);
+		if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
+		  cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
+			       field);
+	      }
+	  }
+    }
 }
 
 /* Make sure that a declaration with no declarator is well-formed, i.e.
--- gcc/testsuite/g++.old-deja/g++.other/anon8.C.jj	Mon Mar 19 17:13:30 2001
+++ gcc/testsuite/g++.old-deja/g++.other/anon8.C	Tue Mar 20 17:01:57 2001
@@ -0,0 +1,22 @@
+// Build don't link:
+
+struct B
+{
+  int a;
+  B & operator= (const B &);
+};
+
+struct A
+{
+  union {
+    int a;
+  };
+  B b;
+};
+
+A x;
+
+void foo (const A &y)
+{
+  x = y;
+}

	Jakub



More information about the Gcc-patches mailing list