]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Skip unnamed bit-fields more
authorMarek Polacek <polacek@redhat.com>
Mon, 8 Nov 2021 17:55:52 +0000 (12:55 -0500)
committerMarek Polacek <polacek@redhat.com>
Tue, 9 Nov 2021 16:36:19 +0000 (11:36 -0500)
As Jason noticed in
<https://gcc.gnu.org/pipermail/gcc-patches/2021-November/583592.html>,
we shouldn't require an initializer for an unnamed bit-field, because,
as [class.bit] says, they cannot be initialized.

gcc/cp/ChangeLog:

* class.c (default_init_uninitialized_part): Use
next_initializable_field.
* method.c (walk_field_subobs): Skip unnamed bit-fields.

gcc/testsuite/ChangeLog:

* g++.dg/init/bitfield6.C: New test.

gcc/cp/class.c
gcc/cp/method.c
gcc/testsuite/g++.dg/init/bitfield6.C [new file with mode: 0644]

index f16e50b9de9d7dba8ebfffbdaf69365cbb09ff2c..bf92300a1789c8e9187e0a855d31ba4f9358f23c 100644 (file)
@@ -5455,10 +5455,9 @@ default_init_uninitialized_part (tree type)
       if (r)
        return r;
     }
-  for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t))
-    if (TREE_CODE (t) == FIELD_DECL
-       && !DECL_ARTIFICIAL (t)
-       && !DECL_INITIAL (t))
+  for (t = next_initializable_field (TYPE_FIELDS (type)); t;
+       t = next_initializable_field (DECL_CHAIN (t)))
+    if (!DECL_INITIAL (t) && !DECL_ARTIFICIAL (t))
       {
        r = default_init_uninitialized_part (TREE_TYPE (t));
        if (r)
index 1023aefc575b3124989c188867bda75cac010839..935946f5eefb25a1353e9fc63fe868565aa471d5 100644 (file)
@@ -2295,7 +2295,9 @@ walk_field_subobs (tree fields, special_function_kind sfk, tree fnname,
     {
       tree mem_type, argtype, rval;
 
-      if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
+      if (TREE_CODE (field) != FIELD_DECL
+         || DECL_ARTIFICIAL (field)
+         || DECL_UNNAMED_BIT_FIELD (field))
        continue;
 
       /* Variant members only affect deletedness.  In particular, they don't
diff --git a/gcc/testsuite/g++.dg/init/bitfield6.C b/gcc/testsuite/g++.dg/init/bitfield6.C
new file mode 100644 (file)
index 0000000..70854ee
--- /dev/null
@@ -0,0 +1,20 @@
+// { dg-do compile { target c++11 } }
+
+struct A
+{
+  int : 8;
+  int i = 42;
+};
+
+constexpr A a;
+
+struct B : A {
+};
+
+constexpr B b;
+
+struct C {
+  int : 0;
+};
+
+constexpr C c;