public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Roger Sayle <sayle@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-7574] PR c++/95999: Improved error recovery in enumeration lists.
Date: Thu, 10 Mar 2022 07:43:22 +0000 (GMT)	[thread overview]
Message-ID: <20220310074322.CC4A53857C6E@sourceware.org> (raw)

https://gcc.gnu.org/g:bae10419f6e07dbde8e66ff4ff70d050f82ad451

commit r12-7574-gbae10419f6e07dbde8e66ff4ff70d050f82ad451
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Thu Mar 10 07:41:12 2022 +0000

    PR c++/95999: Improved error recovery in enumeration lists.
    
    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 the testcase reported in the
    PR, it 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.
    
    2022-03-10  Roger Sayle  <roger@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/parse/pr95999.C: New test case.

Diff:
---
 gcc/cp/decl.cc                       | 4 ++--
 gcc/testsuite/g++.dg/parse/pr95999.C | 8 ++++++++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 992e38385c2..58210905ee9 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -16263,7 +16263,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.  */
@@ -16552,7 +16552,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/parse/pr95999.C b/gcc/testsuite/g++.dg/parse/pr95999.C
new file mode 100644
index 00000000000..cf8437ef262
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/pr95999.C
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+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	other threads:[~2022-03-10  7:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220310074322.CC4A53857C6E@sourceware.org \
    --to=sayle@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).