]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: no_unique_address and constexpr [PR112439]
authorJason Merrill <jason@redhat.com>
Thu, 1 Feb 2024 22:23:53 +0000 (17:23 -0500)
committerJason Merrill <jason@redhat.com>
Fri, 2 Feb 2024 03:22:06 +0000 (22:22 -0500)
Here, because we don't build a CONSTRUCTOR for an empty base, we were
wrongly marking the Foo CONSTRUCTOR as complete after initializing the Empty
member.  Fixed by checking empty_base here as well.

PR c++/112439

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_store_expression): Check empty_base
before marking a CONSTRUCTOR readonly.

gcc/testsuite/ChangeLog:

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

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

index 6350fe1540856ade686cec72bfa7cf1a61165d08..2ebb1470dd5e644e72a1c3303898f0b8f268d00b 100644 (file)
@@ -6694,6 +6694,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
      object.  Make a note of this fact by marking the CONSTRUCTOR
      TREE_READONLY.  */
   if (TREE_CODE (t) == INIT_EXPR
+      && !empty_base
       && TREE_CODE (*valp) == CONSTRUCTOR
       && TYPE_READONLY (type))
     {
diff --git a/gcc/testsuite/g++.dg/cpp2a/no_unique_address15.C b/gcc/testsuite/g++.dg/cpp2a/no_unique_address15.C
new file mode 100644 (file)
index 0000000..3e7cf08
--- /dev/null
@@ -0,0 +1,19 @@
+// PR c++/112439
+// { dg-do compile { target c++14 } }
+
+struct Empty {};
+
+class Foo {
+public:
+    constexpr Foo(int x, Empty y, int z) : a(x), b(y)
+    {
+        c = z;
+    }
+
+private:
+    int a{};
+    [[no_unique_address]] Empty b{};
+    [[no_unique_address]] int c{};
+};
+
+constexpr Foo r{1, {}, 3};