diff options
author | Jason Merrill <jason@redhat.com> | 2021-07-30 08:45:01 -0400 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2021-07-30 12:21:32 -0400 |
commit | 3ead06c1cff8fb42b4e278c3624917e6b5477f12 (patch) | |
tree | 59b79334540cc2c7c14d0dbf92e588e57796a85b | |
parent | 0ba2003cf306aa98b6ec91c9d849ab9bafcf17c2 (diff) | |
download | gcc-3ead06c1cff8fb42b4e278c3624917e6b5477f12.zip gcc-3ead06c1cff8fb42b4e278c3624917e6b5477f12.tar.gz gcc-3ead06c1cff8fb42b4e278c3624917e6b5477f12.tar.bz2 |
c++: Reject anonymous struct with bases
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.
-rw-r--r-- | gcc/cp/class.c | 3 | ||||
-rw-r--r-- | gcc/cp/decl.c | 3 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/ext/anon-struct8.C | 9 |
3 files changed, 13 insertions, 2 deletions
diff --git a/gcc/cp/class.c b/gcc/cp/class.c index 14db066..6f31700 100644 --- a/gcc/cp/class.c +++ b/gcc/cp/class.c @@ -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) diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 01d64a1..71308a0 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -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 index 0000000..f4e3f11 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/anon-struct8.C @@ -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" } +}; |