public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-42] c++: Don't allow defining types in enum-base [PR96380]
@ 2021-04-21 15:12 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2021-04-21 15:12 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:001c63d15e31bc0a1545426d889a0b9f671b4961

commit r12-42-g001c63d15e31bc0a1545426d889a0b9f671b4961
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Apr 20 12:16:04 2021 -0400

    c++: Don't allow defining types in enum-base [PR96380]
    
    In r11-2064 I made cp_parser_enum_specifier commit to tentative parse
    when seeing a '{'.  That still looks like the correct thing to do, but
    it caused an ICE-on-invalid as well as accepts-invalid.
    
    When we have something sneaky like this, which is broken in multiple
    ways:
    
      template <class>
      enum struct c : union enum struct c { e = b, f = a };
    
    we parse the "enum struct c" part (that's OK) and then we see that
    we have an enum-base, so we consume ':' and then parse the type-specifier
    that follows the :.  "union enum" is clearly invalid, but we're still
    parsing tentatively and we parse everything up to the ;, and then
    throw away the underlying type.  We parsed everything because we were
    tricked into parsing an enum-specifier in an enum-base of another
    enum-specifier!  Not good.
    
    Since the grammar for enum-base doesn't allow a defining-type-specifier,
    only a type-specifier, we should set type_definition_forbidden_message
    which fixes all the problems in this PR.
    
    gcc/cp/ChangeLog:
    
            PR c++/96380
            * parser.c (cp_parser_enum_specifier): Don't allow defining
            types in enum-base.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/96380
            * g++.dg/cpp0x/enum_base4.C: New test.
            * g++.dg/cpp0x/enum_base5.C: New test.

Diff:
---
 gcc/cp/parser.c                         | 4 ++++
 gcc/testsuite/g++.dg/cpp0x/enum_base4.C | 8 ++++++++
 gcc/testsuite/g++.dg/cpp0x/enum_base5.C | 7 +++++++
 3 files changed, 19 insertions(+)

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 99eccf0c5e4..fba516efa23 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -19942,6 +19942,10 @@ cp_parser_enum_specifier (cp_parser* parser)
       /* Consume the `:'.  */
       cp_lexer_consume_token (parser->lexer);
 
+      auto tdf
+	= make_temp_override (parser->type_definition_forbidden_message,
+			      G_("types may not be defined in enum-base"));
+
       /* Parse the type-specifier-seq.  */
       cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
 				    /*is_declaration=*/false,
diff --git a/gcc/testsuite/g++.dg/cpp0x/enum_base4.C b/gcc/testsuite/g++.dg/cpp0x/enum_base4.C
new file mode 100644
index 00000000000..b3015256386
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/enum_base4.C
@@ -0,0 +1,8 @@
+// PR c++/96380
+// { dg-do compile { target c++11 } }
+
+extern const int a, b;
+enum struct c;
+template <class>
+enum struct c : union enum struct c { e = b, f = a };  // { dg-error "types may not be defined|expected|elaborated-type-specifier" }
+enum class c {};
diff --git a/gcc/testsuite/g++.dg/cpp0x/enum_base5.C b/gcc/testsuite/g++.dg/cpp0x/enum_base5.C
new file mode 100644
index 00000000000..c01e857e612
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/enum_base5.C
@@ -0,0 +1,7 @@
+// PR c++/96380
+// { dg-do compile { target c++11 } }
+
+extern const int a, b;
+enum struct c;
+template <class>
+enum struct c : union enum struct c { e = b, f = a }; // { dg-error "types may not be defined|expected|elaborated-type-specifier" }


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-21 15:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-21 15:12 [gcc r12-42] c++: Don't allow defining types in enum-base [PR96380] Marek Polacek

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).