https://gcc.gnu.org/g:bdd5d55644db0f26d658c3245369982a8aa2ec74
commit r17-6033-gbdd5d55644db0f26d658c3245369982a8aa2ec74 Author: Martin Uecker <[email protected]> Date: Wed Nov 4 00:01:57 2026 +0100 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/c/ChangeLog: * c-decl.cc (build_enumerator): Fix logic. gcc/testsuite/ChangeLog: * gcc.dg/pr128335.c: New test. Diff: --- gcc/c/c-decl.cc | 7 +++++-- gcc/testsuite/gcc.dg/pr128335.c | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 6529969aace3..0c52a807a0ef 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 000000000000..acb7739b68b9 --- /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)); +
