On 1/1/26 10:41 AM, Egas Ribeiro wrote:
Bootstrapped and regtested on x86_64-pc-linux-gnu. Ok for trunk?

Before r14-2820 we had:
```
   bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
```
I can't tell if there is a better way to keep the intent of r14-2820
while fixing this issue, but it seemed okay to ignore the elision here
for unions (reverting back to the previous no_slot might also work?).

Maybe Patrick has a suggestion for this?

I think it would be better to change init_subob_ctx.

-- >8 --

r14-2820 changed cxx_eval_bare_aggregate to set no_slot based on whether
new_ctx.ctor is NULL_TREE, to handle empty subobject elision. However
this incorrectly omits entries for empty union members, which later need
the entry to exist.
This caused valid code to be rejected as non-constant after gcc 13.3,
and in trunk caused an ICE when the diagnostic code tries to print a
CONSTRUCTOR with a null value.

        PR c++/123346

gcc/cp/ChangeLog:

        * constexpr.cc (cxx_eval_bare_aggregate): Don't set no_slot for
        union types.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp2a/constexpr-union10.C: New test.

Signed-off-by: Egas Ribeiro <[email protected]>
---
  gcc/cp/constexpr.cc                            | 3 ++-
  gcc/testsuite/g++.dg/cpp2a/constexpr-union10.C | 7 +++++++
  2 files changed, 9 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-union10.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index e7194cd2894..061e6342cce 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -6508,7 +6508,8 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
        tree orig_value = value;
        init_subob_ctx (ctx, new_ctx, index, value);
        /* Like in cxx_eval_store_expression, omit entries for empty fields.  */
-      bool no_slot = new_ctx.ctor == NULL_TREE;
+      bool no_slot
+       = new_ctx.ctor == NULL_TREE && TREE_CODE (type) != UNION_TYPE;
        int pos_hint = -1;
        if (new_ctx.ctor != ctx->ctor && !no_slot)
        {
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-union10.C 
b/gcc/testsuite/g++.dg/cpp2a/constexpr-union10.C
new file mode 100644
index 00000000000..62f0e471235
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-union10.C
@@ -0,0 +1,7 @@
+// { dg-do compile { target c++20 } }
+struct Unit {};
+union Union { Unit unit; };
+constexpr Union make(Union&& other) {
+    return Union {.unit = other.unit };
+}
+constexpr Union u = make(Union { .unit = Unit{} });

Reply via email to