public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Reject anonymous struct with bases
@ 2021-07-30 16:23 Jason Merrill
  2021-07-30 19:35 ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Jason Merrill @ 2021-07-30 16:23 UTC (permalink / raw)
  To: gcc-patches; +Cc: jakub

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.

Tested x86_64-pc-linux-gnu, applying to trunk.

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                          | 3 +--
 gcc/cp/decl.c                           | 3 +++
 gcc/testsuite/g++.dg/ext/anon-struct8.C | 9 +++++++++
 3 files changed, 13 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ext/anon-struct8.C

diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 14db06692dc..6f31700c06c 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 01d64a16125..71308a06c63 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 00000000000..f4e3f11b678
--- /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" }
+};

base-commit: 0ba2003cf306aa98b6ec91c9d849ab9bafcf17c2
-- 
2.27.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: Reject anonymous struct with bases
  2021-07-30 16:23 [PATCH] c++: Reject anonymous struct with bases Jason Merrill
@ 2021-07-30 19:35 ` Andrew Pinski
  2021-07-31 14:46   ` Jason Merrill
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2021-07-30 19:35 UTC (permalink / raw)
  To: Jason Merrill; +Cc: GCC Patches, Jakub Jelinek

On Fri, Jul 30, 2021 at 9:26 AM Jason Merrill via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> 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.

This should fix PR 96636 by rejecting the code.

Thanks,
Andrew Pinski

>
> Tested x86_64-pc-linux-gnu, applying to trunk.
>
> 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                          | 3 +--
>  gcc/cp/decl.c                           | 3 +++
>  gcc/testsuite/g++.dg/ext/anon-struct8.C | 9 +++++++++
>  3 files changed, 13 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/ext/anon-struct8.C
>
> diff --git a/gcc/cp/class.c b/gcc/cp/class.c
> index 14db06692dc..6f31700c06c 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 01d64a16125..71308a06c63 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 00000000000..f4e3f11b678
> --- /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" }
> +};
>
> base-commit: 0ba2003cf306aa98b6ec91c9d849ab9bafcf17c2
> --
> 2.27.0
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++: Reject anonymous struct with bases
  2021-07-30 19:35 ` Andrew Pinski
@ 2021-07-31 14:46   ` Jason Merrill
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Merrill @ 2021-07-31 14:46 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: GCC Patches, Jakub Jelinek

On Fri, Jul 30, 2021 at 3:35 PM Andrew Pinski <pinskia@gmail.com> wrote:

> On Fri, Jul 30, 2021 at 9:26 AM Jason Merrill via Gcc-patches
> <gcc-patches@gcc.gnu.org> wrote:
> >
> > 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.
>
> This should fix PR 96636 by rejecting the code.
>

Thanks.

Jason

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-07-31 14:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 16:23 [PATCH] c++: Reject anonymous struct with bases Jason Merrill
2021-07-30 19:35 ` Andrew Pinski
2021-07-31 14:46   ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).