]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: another constexpr nested empty object [PR125336]
authorMarek Polacek <polacek@redhat.com>
Fri, 15 May 2026 18:51:29 +0000 (14:51 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 19 May 2026 12:57:45 +0000 (08:57 -0400)
When looking into 125315 I came up with another test that crashes
due to an empty object.  It still crashes even after Jason's patch.

Here we have a subobject nested in an empty object:

  {.w = {.v = TARGET_EXPR <f(s)>}}

w's type is W, an empty union due to [[no_unique_address]], so
init_subob_ctx clears the ctor, but then we recurse to

  {.v = TARGET_EXPR <f(s)>}

with a null ctx->ctor so the call to get_or_insert_ctor_field in
cxx_eval_bare_aggregate crashes.  This fixes the crash similarly
to c++/125315.

no_unique_address18.C worked fine even before this patch because
there we don't have an empty object.  But let's test it also.

PR c++/125336

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_bare_aggregate): Don't call
get_or_insert_ctor_field when there is no CONSTRUCTOR.  Assert
is_empty_class.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/no_unique_address17.C: New test.
* g++.dg/cpp2a/no_unique_address18.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/no_unique_address17.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/no_unique_address18.C [new file with mode: 0644]

index 701269cd3f60c60e125b23ebc5a2381831371bb4..6c6d2f3fa9943edb3fd5c2a594087c83df1eb0b0 100644 (file)
@@ -6841,7 +6841,11 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
       /* Like in cxx_eval_store_expression, omit entries for empty fields.  */
       bool no_slot = new_ctx.ctor == NULL_TREE;
       int pos_hint = -1;
-      if (new_ctx.ctor != ctx->ctor && !no_slot)
+      if (!ctx->ctor)
+       /* The enclosing object could be an empty subobject so we have no
+          CONSTRUCTOR (c++/125336).  */
+       gcc_checking_assert (is_empty_class (type));
+      else if (new_ctx.ctor != ctx->ctor && !no_slot)
        {
          /* If we built a new CONSTRUCTOR, attach it now so that other
             initializers can refer to it.  */
@@ -6883,7 +6887,7 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
        /* This is an initializer for an empty field; now that we've
           checked that it's constant, we can ignore it.  */
        changed = true;
-      else
+      else if (ctx->ctor)
        {
          if (TREE_CODE (type) == UNION_TYPE
              && (*p)->last().index != index)
diff --git a/gcc/testsuite/g++.dg/cpp2a/no_unique_address17.C b/gcc/testsuite/g++.dg/cpp2a/no_unique_address17.C
new file mode 100644 (file)
index 0000000..1109969
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/125336
+// { dg-do compile { target c++20 } }
+// { dg-prune-output "used but never defined" }
+
+struct S{ };
+constexpr S& f (S&);
+union W {
+  [[no_unique_address]] S v;
+};
+struct R { W w; };
+S s;
+auto x = R{{ f(s) }};
diff --git a/gcc/testsuite/g++.dg/cpp2a/no_unique_address18.C b/gcc/testsuite/g++.dg/cpp2a/no_unique_address18.C
new file mode 100644 (file)
index 0000000..bea8a65
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/125336
+// { dg-do compile { target c++20 } }
+// { dg-prune-output "used but never defined" }
+
+struct S { int i; };
+constexpr S& f (S&);
+union W {
+  [[no_unique_address]] S v;
+};
+struct R { W w; };
+S s;
+auto x = R{{ f(s) }};