Patchwork C++ PATCH for core 1612 (lambda and anonymous union)

login
register
mail settings
Submitter Jason Merrill
Date April 22, 2013, 6:52 p.m.
Message ID <517586E3.809@redhat.com>
Download mbox | patch
Permalink /patch/238630/
State New
Headers show

Comments

Jason Merrill - April 22, 2013, 6:52 p.m.
When a lambda names a member of an anonymous union, it's not clear 
whether it's intended to capture the union as a whole or the member.  So 
we decided it should be ill-formed.

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

Patch

commit 37272f53baa15670141fcbf02a3f8219edb3827a
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Apr 16 13:07:00 2013 +0100

    	Core 1612
    	* semantics.c (finish_id_expression): Reject capture of anonymous
    	union member.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 2784d79..391dc1e 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -3105,6 +3105,12 @@  finish_id_expression (tree id_expression,
 		= decl_function_context (containing_function);
 	    }
 
+	  if (lambda_expr && TREE_CODE (decl) == VAR_DECL
+	      && DECL_ANON_UNION_VAR_P (decl))
+	    {
+	      error ("cannot capture member %qD of anonymous union", decl);
+	      return error_mark_node;
+	    }
 	  if (context == containing_function)
 	    {
 	      decl = add_default_capture (lambda_stack,
diff --git a/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-anon1.C b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-anon1.C
new file mode 100644
index 0000000..482193e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/lambda/lambda-anon1.C
@@ -0,0 +1,18 @@ 
+// DR 1612
+// { dg-require-effective-target c++11 }
+
+int main() {
+  static int result;
+  struct A { int x; };
+  struct B { int y; };
+  union {
+    A a; B b;
+  };
+  a.x = 1;
+  [=]() mutable {
+    a.x = 2;			// { dg-error "anonymous union" }
+    result = b.y;		// { dg-error "anonymous union" }
+  }();
+  if (result == 1) return 0;
+  throw 0;
+}