Commit 426b9428 by Patrick Palka

Fix PR c++/70347 (default member initializer not picked up by union)

gcc/cp/ChangeLog:

	PR c++/70347
	* typeck.c (process_init_constructor_union): If the initializer
	is empty, use the union's NSDMI if it has one.

gcc/testsuite/ChangeLog:

	PR c++/70347
	* g++.dg/cpp1y/nsdmi-union1.C: New test.

From-SVN: r234443
parent cd1588c4
2016-03-23 Patrick Palka <ppalka@gcc.gnu.org> 2016-03-23 Patrick Palka <ppalka@gcc.gnu.org>
PR c++/70347
* typeck.c (process_init_constructor_union): If the initializer
is empty, use the union's NSDMI if it has one.
2016-03-23 Patrick Palka <ppalka@gcc.gnu.org>
PR c++/70332 PR c++/70332
* pt.c (tsubst_copy) [PARM_DECL]: Handle the use of 'this' in an * pt.c (tsubst_copy) [PARM_DECL]: Handle the use of 'this' in an
NSDMI that's part of an aggregrate initialization. NSDMI that's part of an aggregrate initialization.
......
...@@ -1499,9 +1499,24 @@ process_init_constructor_union (tree type, tree init, ...@@ -1499,9 +1499,24 @@ process_init_constructor_union (tree type, tree init,
constructor_elt *ce; constructor_elt *ce;
int len; int len;
/* If the initializer was empty, use default zero initialization. */ /* If the initializer was empty, use the union's NSDMI if it has one.
Otherwise use default zero initialization. */
if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
return 0; {
for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
{
if (DECL_INITIAL (field))
{
CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
field,
get_nsdmi (field, /*in_ctor=*/false));
break;
}
}
if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
return 0;
}
len = CONSTRUCTOR_ELTS (init)->length (); len = CONSTRUCTOR_ELTS (init)->length ();
if (len > 1) if (len > 1)
......
2016-03-23 Patrick Palka <ppalka@gcc.gnu.org> 2016-03-23 Patrick Palka <ppalka@gcc.gnu.org>
PR c++/70347
* g++.dg/cpp1y/nsdmi-union1.C: New test.
2016-03-23 Patrick Palka <ppalka@gcc.gnu.org>
PR c++/70332 PR c++/70332
* g++.dg/cpp1y/nsdmi-aggr5.C: New test. * g++.dg/cpp1y/nsdmi-aggr5.C: New test.
......
// PR c++/70347
// { dg-do run { target c++14 } }
union A {
char a;
long b = -42;
};
struct B {
union {
char a = 10;
long b;
};
};
A c1{};
A c2{4};
B c3{};
B c4{{9}};
int main() {
if (c1.b != -42)
__builtin_abort ();
if (c2.a != 4)
__builtin_abort ();
if (c3.a != 10)
__builtin_abort ();
if (c4.a != 9)
__builtin_abort ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment