Patchwork C++ PATCH for c++/52915 (accepts-invalid anonymous union in C++11 mode)

login
register
mail settings
Submitter Jason Merrill
Date April 13, 2012, 7:11 p.m.
Message ID <4F887A5B.70408@redhat.com>
Download mbox | patch
Permalink /patch/152385/
State New
Headers show

Comments

Jason Merrill - April 13, 2012, 7:11 p.m.
C++11 extends unions so that a member can have a non-trivial default 
constructor, but the union then has a deleted constructor unless the 
user defines one.  As a result, we can't assume that an anonymous union 
has a trivial default constructor anymore.

Tested x86_64-pc-linux-gnu, applying to trunk.

Patch

commit 761b558950409d024fd509228b1a3e04fcefb38a
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Apr 12 18:10:48 2012 -0400

    	PR c++/52915
    	* decl2.c (finish_anon_union): Use cp_finish_decl.
    	* error.c (dump_function_name): Avoid showing anonymous "name".

diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index b048ac7..212feea 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -1456,12 +1456,7 @@  finish_anon_union (tree anon_union_decl)
     }
 
   pushdecl (anon_union_decl);
-  if (building_stmt_list_p ()
-      && at_function_scope_p ())
-    add_decl_expr (anon_union_decl);
-  else if (!processing_template_decl)
-    rest_of_decl_compilation (anon_union_decl,
-			      toplevel_bindings_p (), at_eof);
+  cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0);
 }
 
 /* Auxiliary functions to make type signatures for
diff --git a/gcc/cp/error.c b/gcc/cp/error.c
index ee8f0e0..77eb306 100644
--- a/gcc/cp/error.c
+++ b/gcc/cp/error.c
@@ -1556,6 +1556,8 @@  dump_function_name (tree t, int flags)
     {
       if (LAMBDA_TYPE_P (DECL_CONTEXT (t)))
 	name = get_identifier ("<lambda>");
+      else if (TYPE_ANONYMOUS_P (DECL_CONTEXT (t)))
+	name = get_identifier ("<constructor>");
       else
 	name = constructor_name (DECL_CONTEXT (t));
     }
diff --git a/gcc/testsuite/g++.dg/other/anon-union2.C b/gcc/testsuite/g++.dg/other/anon-union2.C
new file mode 100644
index 0000000..31bb74f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/anon-union2.C
@@ -0,0 +1,10 @@ 
+// PR c++/52915
+
+struct S {
+  int val;
+  S(int v) : val(v) {}
+};
+
+void f() {
+  union { S a; };		// { dg-error "constructor|no match" }
+}