PR c++/78897 - constexpr union

* constexpr.c (cxx_eval_store_expression): A store to a union member
	erases a previous store to another member.

From-SVN: r245341
This commit is contained in:
Jason Merrill 2017-02-10 13:50:30 -05:00 committed by Jason Merrill
parent f494ac0ebd
commit 5c97093ba7
3 changed files with 20 additions and 0 deletions

View File

@ -1,5 +1,9 @@
2017-02-10 Jason Merrill <jason@redhat.com>
PR c++/78897 - constexpr union
* constexpr.c (cxx_eval_store_expression): A store to a union member
erases a previous store to another member.
PR c++/71285 - member of fold-expression
* semantics.c (finish_unary_fold_expr)
(finish_binary_fold_expr): Use null type for fold-expressions.

View File

@ -3466,6 +3466,11 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
unsigned HOST_WIDE_INT idx;
if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
&& CONSTRUCTOR_ELT (*valp, 0)->index != index)
/* Changing active member. */
vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
for (idx = 0;
vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
idx++, fields = DECL_CHAIN (fields))

View File

@ -0,0 +1,11 @@
// PR c++/78897
// { dg-do compile { target c++14 } }
struct Optional {
constexpr Optional() : _dummy{} { _value = 1; }
union {
int _dummy;
int _value;
};
};
Optional opt{};