]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constexpr nested empty objects [PR125315]
authorJason Merrill <jason@redhat.com>
Fri, 15 May 2026 02:24:09 +0000 (22:24 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 15 May 2026 05:13:55 +0000 (01:13 -0400)
Here we have one empty subobject inside another; we didn't create a
CONSTRUCTOR for the outer one, so we don't have it as context for the inner.

PR c++/125315

gcc/cp/ChangeLog:

* constexpr.cc (init_subob_ctx): Allow both ctor and object
to be null for an empty subobject.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/no_unique_address16.C: New test.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/no_unique_address16.C [new file with mode: 0644]

index 9916b63d428708420b86bf8a33051167de97e166..1fc8a56503c622b425555add3f5256a5893284d1 100644 (file)
@@ -6718,7 +6718,12 @@ init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
   else if (ctx->object)
     ctxtype = TREE_TYPE (ctx->object);
   else
-    gcc_unreachable ();
+    {
+      /* This can happen if the enclosing object is also an empty subobject
+        (c++/125315).  */
+      gcc_checking_assert (is_empty_class (type));
+      return;
+    }
 
   if (VECTOR_TYPE_P (type)
       && VECTOR_TYPE_P (ctxtype)
diff --git a/gcc/testsuite/g++.dg/cpp2a/no_unique_address16.C b/gcc/testsuite/g++.dg/cpp2a/no_unique_address16.C
new file mode 100644 (file)
index 0000000..22c0b69
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/125315
+// { dg-do compile { target c++20 } }
+// { dg-prune-output "used but never defined" }
+
+struct S{~S(){}};
+constexpr S& f(S& t);
+struct W{[[no_unique_address]]S v;};
+struct R:W{};
+S s;
+auto x=R{{f(s)}};