]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c: Fix ICE for nested enum redefinitions with/without fixed underlying type [PR112571]
authorJoseph Myers <josmyers@redhat.com>
Wed, 31 Jan 2024 21:39:53 +0000 (21:39 +0000)
committerJoseph Myers <josmyers@redhat.com>
Mon, 25 Mar 2024 22:49:21 +0000 (22:49 +0000)
Bug 112571 reports an ICE-on-invalid for cases where an enum is
defined, without a fixed underlying type, inside the enum type
specifier for a definition of that same enum with a fixed underlying
type.

The ultimate cause is attempting to access ENUM_UNDERLYING_TYPE in a
case where it is NULL.  Avoid this by clearing
ENUM_FIXED_UNDERLYING_TYPE_P in thie case of inconsistent definitions.

Bootstrapped wth no regressions for x86_64-pc-linux-gnu.

(Note: for this GCC 13 branch backport, the tests were changed to use
-std=c2x not -std=c23, and c23-enum-9.c was changed to expect
different diagnostics because GCC 13 branch doesn't have the C23 tag
compatibility support for redefinitions of tagged types and
enumerators.)

PR c/112571

gcc/c/
* c-decl.cc (start_enum): Clear ENUM_FIXED_UNDERLYING_TYPE_P when
defining without a fixed underlying type an enumeration previously
declared with a fixed underlying type.

gcc/testsuite/
* gcc.dg/c23-enum-9.c, gcc.dg/c23-enum-10.c: New tests.

(cherry picked from commit d22d1a9346f27db41459738c6eb404f8f0956e6f)

gcc/c/c-decl.cc
gcc/testsuite/gcc.dg/c23-enum-10.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/c23-enum-9.c [new file with mode: 0644]

index 1b53f2d0785af8d169f8606d1fefdd5a76a5d543..7dcb1141bf7997383eb77302dd877e5e88d136ca 100644 (file)
@@ -9587,8 +9587,11 @@ start_enum (location_t loc, struct c_enum_contents *the_enum, tree name,
 
   if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype)
       && fixed_underlying_type == NULL_TREE)
-    error_at (loc, "%<enum%> declared with but defined without "
-             "fixed underlying type");
+    {
+      error_at (loc, "%<enum%> declared with but defined without "
+               "fixed underlying type");
+      ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = false;
+    }
 
   the_enum->enum_next_value = integer_zero_node;
   the_enum->enum_type = enumtype;
diff --git a/gcc/testsuite/gcc.dg/c23-enum-10.c b/gcc/testsuite/gcc.dg/c23-enum-10.c
new file mode 100644 (file)
index 0000000..8948931
--- /dev/null
@@ -0,0 +1,6 @@
+/* PR c/112571.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x" } */
+
+enum X : typeof (enum X { A }); /* { dg-error "declared with but defined without fixed underlying type" } */
+/* { dg-error "invalid 'enum' underlying type" "invalid" { target *-*-* } .-1 } */
diff --git a/gcc/testsuite/gcc.dg/c23-enum-9.c b/gcc/testsuite/gcc.dg/c23-enum-9.c
new file mode 100644 (file)
index 0000000..b9a6afe
--- /dev/null
@@ -0,0 +1,8 @@
+/* PR c/112571.  */
+/* { dg-do compile } */
+/* { dg-options "-std=c2x" } */
+
+enum h : typeof (enum h { D }) { D }; /* { dg-error "declared with but defined without fixed underlying type" } */
+/* { dg-error "invalid 'enum' underlying type" "invalid" { target *-*-* } .-1 } */
+/* { dg-error "redeclaration of 'enum h'" "enumeration" { target *-*-* } .-2 } */
+/* { dg-error "redeclaration of enumerator" "enumerator" { target *-*-* } .-3 } */