This patch resolves PR c++/95999 which is an ICE-after-error regression
in the g++ front-end.  When parsing an enumerator list, the C++ parser
assumes that cp_parser_constant_expression always returns either an
INTEGER_CST or error_mark_node, but in testcase reported in the PR
actually returns a VAR_DECL.

The usual (but perhaps controversial) design philosophy is that the
routine that reports the error normally has a duty to indicate this to
the rest of the compiler (via error_mark_node), but here the return
value from calling require_rvalue_constant_expression (parser.cc:10666)
is ignored.  I initially experimented with setting EXPRESSION to
error_mark_node here in cp_parser_constant_expression but (perhaps
conveniently) that's insufficient to resolve the problem.  The simple
fix in this patch is to tweak the two places that require INTEGER_CST
to treat all other tree types as though they are error_mark_node.

This patch has been tested on x86_64-pc-linunx-gnu with make bootstrap
and make -k check with no new (unexpected) failures.  Ok for mainline?


2022-02-21  Roger Sayle  <ro...@nextmovesoftware.com>

gcc/cp/ChangeLog
        PR c++/95999
        * decl.cc (finish_enum_value_list): If VALUE isn't an INTEGER_CST
        consider it to be zero (i.e. treat it like error_mark_node).
        (build_enumerator): Likewise, if PREV_VALUE isn't an INTEGER_CST,
        set VALUE to error_mark_node.

gcc/testsuite/ChangeLog
        PR c++/95999
        * g++.dg/pr95999.c: New test case.


Thanks in advance,
Roger
--

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 7b48b56..40b8f0c 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -16202,7 +16202,7 @@ finish_enum_value_list (tree enumtype)
 
          /* Update the minimum and maximum values, if appropriate.  */
          value = DECL_INITIAL (decl);
-         if (value == error_mark_node)
+         if (TREE_CODE (value) != INTEGER_CST)
            value = integer_zero_node;
          /* Figure out what the minimum and maximum values of the
             enumerators are.  */
@@ -16491,7 +16491,7 @@ build_enumerator (tree name, tree value, tree enumtype, 
tree attributes,
                 which case the type is an unspecified integral type
                 sufficient to contain the incremented value.  */
              prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
-             if (error_operand_p (prev_value))
+             if (TREE_CODE (prev_value) != INTEGER_CST)
                value = error_mark_node;
              else
                {
diff --git a/gcc/testsuite/g++.dg/pr95999.C b/gcc/testsuite/g++.dg/pr95999.C
new file mode 100644
index 0000000..f399f6d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr95999.C
@@ -0,0 +1,9 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+int a;
+enum struct b;
+template <typename = enum struct b { c = a d } 
+template <> enum struct b { e };  // { dg-error "explicit specialization" }
+// { dg-excess-errors "note" }
+// { dg-excess-errors "5:" }
+

Reply via email to