diff mbox series

[C] : fix temporary enumerator type in redefinition [PR128335]

Message ID 284a05cbf1eb2097243873321826529ed1db855c.camel@tugraz.at
State New
Headers show
Series [C] : fix temporary enumerator type in redefinition [PR128335] | expand

Commit Message

Martin Uecker Nov. 4, 2026, 2:08 p.m. UTC
As discussed in the bugzilla, the submitter (Jay) thought the type 
might start again with "int" during the redefinition.  My 
interpretation is that it should be the visible type of the
previous declaration and it is then fixed in this way during
the redefinition. This implies that

enum E { A = 1, B = _Generic(A, int: -1U) };
enum E { A = 1, B = _Generic(A, int: -1U) };

will continue to be not allowed as the type for A is "int" in
the first _Generic but then "unsigned int" in the second.

This is annoying but I think the language-design issue is
that the type of A is observable already at a time where it 
is not finalized.  Hopefully, nobody writes such code anyway.

Martin



Bootstrapped and regression tested on x86_64.


    c: fix temporary enumerator type in redefinition [PR128335]
    
    During redefining an enumerated type the visible type for an
    enumerator should be the one of the previous definition, i.e. and
    "int" for enumerated types without fixed underlying type where all
    enumerators fit into "int" or otherwise the enumerated type itself.
    We used the underlying type for internal computation of the
    next value which then was visible during the redefinition, but
    the visible type should remain the enumerated type.
    
            PR c/128335
    
    gcc/ChangeLog:
            * c/c-decl.cc: Fix logic.
    
    gcc/testsuite/ChangeLog:
            * gcc.dg/pr128335.c: New test.

Comments

Joseph Myers Nov. 4, 2026, 7:33 p.m. UTC | #1
On Wed, 4 Nov 2026, Martin Uecker wrote:

>     c: fix temporary enumerator type in redefinition [PR128335]
>     
>     During redefining an enumerated type the visible type for an
>     enumerator should be the one of the previous definition, i.e. and
>     "int" for enumerated types without fixed underlying type where all
>     enumerators fit into "int" or otherwise the enumerated type itself.
>     We used the underlying type for internal computation of the
>     next value which then was visible during the redefinition, but
>     the visible type should remain the enumerated type.
>     
>             PR c/128335
>     
>     gcc/ChangeLog:
>             * c/c-decl.cc: Fix logic.
>     
>     gcc/testsuite/ChangeLog:
>             * gcc.dg/pr128335.c: New test.

OK.
diff mbox series

Patch

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index ada5a5043af..a76cd9b1d54 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -10635,12 +10635,15 @@  build_enumerator (location_t decl_loc, location_t loc,
     }
   else
     {
+      tree tmp = value;
+
       /* In a redeclaration the type can already be the enumeral type.  */
       if (TREE_CODE (TREE_TYPE (value)) == ENUMERAL_TYPE)
-	value = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (value)), value);
+	tmp = convert (ENUM_UNDERLYING_TYPE (TREE_TYPE (tmp)), tmp);
+
       the_enum->enum_next_value
 	= build_binary_op (EXPR_LOC_OR_LOC (value, input_location),
-			   PLUS_EXPR, value, integer_one_node, false);
+			   PLUS_EXPR, tmp, integer_one_node, false);
     }
   the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
   if (the_enum->enum_overflow
diff --git a/gcc/testsuite/gcc.dg/pr128335.c b/gcc/testsuite/gcc.dg/pr128335.c
new file mode 100644
index 00000000000..c7f987ce21e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128335.c
@@ -0,0 +1,24 @@ 
+/* { dg-do "compile" } */
+/* { dg-options "-std=c23" } */
+
+// check that after a definition and inside a redefintion the type of
+// the enumerator is the enumerated type (and not a compatible integer)
+enum F { C = -1U };
+static_assert(_Generic(C, enum F: true));
+enum E { A = 1, B = _Generic(A, int: -1U) };
+static_assert(_Generic(A, enum E: true, enum F: false));
+static_assert(_Generic(B, enum E: true, enum F: false));
+enum E { A = 1, B = _Generic(A, enum E: -1U, enum F: 1U) };
+static_assert(_Generic(A, enum E: true, enum F: false));
+static_assert(_Generic(B, enum E: true, enum F: false));
+
+// check that after a definition and inside a redefintion the type of
+// the enumerator is "int" (and not another compatible enumerated type)
+enum I : int { II = 1 };
+enum G { D = 1, E = _Generic(D, enum I: 2) };
+static_assert(_Generic(D, enum I: true));
+static_assert(_Generic(E, enum I: true));
+enum G { D = 1, E = _Generic(D, enum I: 2) };
+static_assert(_Generic(D, enum I: true));
+static_assert(_Generic(E, enum I: true));
+