]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Reject anonymous struct with bases
authorJason Merrill <jason@redhat.com>
Fri, 30 Jul 2021 12:45:01 +0000 (08:45 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 30 Jul 2021 16:21:32 +0000 (12:21 -0400)
In discussion of jakub's patch for C++20 pointer-interconvertibility, it
came up that we allow anonymous structs to have bases, but don't do anything
usable with them.  Let's reject it.

The comment change is something I noticed while looking for the right place
to diagnose this: finish_struct_anon does not actually check for anything
invalid, so it shouldn't claim to.

gcc/cp/ChangeLog:

* class.c (finish_struct_anon): Improve comment.
* decl.c (fixup_anonymous_aggr): Reject anonymous struct
with bases.

gcc/testsuite/ChangeLog:

* g++.dg/ext/anon-struct8.C: New test.

gcc/cp/class.c
gcc/cp/decl.c
gcc/testsuite/g++.dg/ext/anon-struct8.C [new file with mode: 0644]

index 14db06692dc90c68b06a5870b4afdc5e2420e596..6f31700c06c46b8e07c8e638e2b5f3cbeeb3562e 100644 (file)
@@ -3072,8 +3072,7 @@ finish_struct_anon_r (tree field)
     }
 }
 
-/* Check for things that are invalid.  There are probably plenty of other
-   things we should check for also.  */
+/* Fix up any anonymous union/struct members of T.  */
 
 static void
 finish_struct_anon (tree t)
index 01d64a16125ec0bb8e3798136472c30f475f96a2..71308a06c639c2931adb6fae066c7b641539754e 100644 (file)
@@ -5084,6 +5084,9 @@ fixup_anonymous_aggr (tree t)
     {
       tree field, type;
 
+      if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)))
+       error_at (location_of (t), "anonymous struct with base classes");
+
       for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
        if (TREE_CODE (field) == FIELD_DECL)
          {
diff --git a/gcc/testsuite/g++.dg/ext/anon-struct8.C b/gcc/testsuite/g++.dg/ext/anon-struct8.C
new file mode 100644 (file)
index 0000000..f4e3f11
--- /dev/null
@@ -0,0 +1,9 @@
+// { dg-options "" }
+
+struct A { };
+struct B {
+  struct: A { int i; };                // { dg-error "anonymous struct with base" }
+};
+union U {
+  struct: A { int i; };                // { dg-error "anonymous struct with base" }
+};