]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constexpr aggr init of empty class [PR101040]
authorJason Merrill <jason@redhat.com>
Thu, 24 Jun 2021 21:32:02 +0000 (17:32 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 7 Jul 2021 21:35:50 +0000 (17:35 -0400)
This is basically the aggregate initializer version of PR97566; as in that
bug, we are trying to initialize empty field 'obj' in 'single' when there's
no CONSTRUCTOR entry for the 'single' base class subobject of 'derived'.  As
with that bug, the fix is to stop trying to add entries for empty fields,
this time in cxx_eval_bare_aggregate.

The change to the other function isn't necessary for this version of
the patch, but seems worthwhile for robustness anyway.

PR c++/101040
PR c++/97566

gcc/cp/ChangeLog:

* class.c (is_empty_field): Handle null argument.
* constexpr.c (cxx_eval_bare_aggregate): Discard initializer
for empty field.

gcc/testsuite/ChangeLog:

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

gcc/cp/class.c
gcc/cp/constexpr.c
gcc/testsuite/g++.dg/cpp2a/no_unique_address13.C [new file with mode: 0644]

index 25413097ba35b2b7aa67c44a85bfae15a40625a1..3251aaa1a46cf3a8cf6d7001e32fae8ffc1349d1 100644 (file)
@@ -4249,7 +4249,7 @@ field_poverlapping_p (tree decl)
 bool
 is_empty_field (tree decl)
 {
-  if (TREE_CODE (decl) != FIELD_DECL)
+  if (!decl || TREE_CODE (decl) != FIELD_DECL)
     return false;
 
   bool r = (is_empty_class (TREE_TYPE (decl))
index 0fb0ab44b39e3f0cf6bf6a3e01cdf3c80228f120..c4bcc561228bb09d381c7ddf104e83edc612cd5b 100644 (file)
@@ -4438,7 +4438,12 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
   FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
     {
       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 = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
+      if (no_slot)
+       new_ctx = *ctx;
+      else
+       init_subob_ctx (ctx, new_ctx, index, value);
       int pos_hint = -1;
       if (new_ctx.ctor != ctx->ctor)
        {
@@ -4484,6 +4489,8 @@ cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
          gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
          changed = true;
        }
+      else if (no_slot)
+       changed = true;
       else
        {
          if (TREE_CODE (type) == UNION_TYPE
diff --git a/gcc/testsuite/g++.dg/cpp2a/no_unique_address13.C b/gcc/testsuite/g++.dg/cpp2a/no_unique_address13.C
new file mode 100644 (file)
index 0000000..66b83d6
--- /dev/null
@@ -0,0 +1,24 @@
+// PR c++/101040
+// { dg-do compile { target c++11 } }
+
+// This class has to be empty.
+struct empty
+{};
+
+// This class has to be empty.
+struct single
+{
+    // This member has to be no_unique_address.
+    [[no_unique_address]] empty obj;
+};
+
+// This class has to be empty and derived from single.
+struct derived : single
+{
+    // This constructor has to be constexpr and take a forwarding reference.
+    template <typename Arg>
+    constexpr derived(Arg&& arg) : single{arg}
+    {}
+};
+
+auto obj = derived{empty{}};