This patch addresses PR c++/124573 by making finish_enum_value_list more
robust to DECL_INITIAL being NULL, which I believe only occurs in the
event of an error, and treating it as integer_zero_node for the purposes
of error recovery.
This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check with no new failures. Ok for mainline?
2026-03-28 Roger Sayle <[email protected]>
gcc/cp/ChangeLog
PR c++/124753
* decl.cc (finish_enum_value_list): Defend against DECL_INITIAL
being NULL during error recovery.
gcc/testsuite/ChangeLog
PR c++/124573
* g++.dg/r124573.C: New test case.
Roger
--
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 6b210a30b6a..8716d6dd386 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -19015,7 +19015,7 @@ finish_enum_value_list (tree enumtype)
/* Update the minimum and maximum values, if appropriate. */
value = DECL_INITIAL (decl);
- if (TREE_CODE (value) != INTEGER_CST)
+ if (!value || TREE_CODE (value) != INTEGER_CST)
value = integer_zero_node;
/* Figure out what the minimum and maximum values of the
enumerators are. */
@@ -19168,6 +19168,8 @@ finish_enum_value_list (tree enumtype)
value = perform_implicit_conversion (underlying_type,
DECL_INITIAL (decl),
tf_warning_or_error);
+ if (!value)
+ value = integer_zero_node;
/* Do not clobber shared ints. But do share identical enumerators. */
value = fold_convert (enumtype, value);
diff --git a/gcc/testsuite/g++.dg/pr124573.C b/gcc/testsuite/g++.dg/pr124573.C
new file mode 100644
index 00000000000..c5136bd3308
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr124573.C
@@ -0,0 +1,19 @@
+/* PR c++/124573 */
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-require-effective-target c++11 } */
+
+template <typename T>
+struct A {
+ enum E : T;
+};
+
+using X = A<int>;
+
+template <typename T>
+enum X::E : /* { dg-error "cannot add an enumerator list" } */
+ T { e1 }; /* { dg-error "different underlying type" } */
+
+template <>
+enum A<int>::E : int {};
+