[C++] PR c++/96437: ICE-on-invalid-code error recovery.

Message ID 004b01d8321f$80e50fa0$82af2ee0$@nextmovesoftware.com
State New
Headers
Series [C++] PR c++/96437: ICE-on-invalid-code error recovery. |

Commit Message

Roger Sayle March 7, 2022, 12:33 p.m. UTC
  This patch fixes PR c++/96437 which is an ICE-on-invalid-code regression
affecting mainline.

This patch has been tested on x86_64-pc-linux-gnu, enabling languages
c and c++, with make bootstrap and make -k check with no new failures.
Ok for mainline?

2022-03-07  Roger Sayle  <roger@nextmovesoftware.com>

gcc/cp/ChangeLog
	PR c++/96437
	* parser.cc (synthesize_implicit_template_parm): Check that
	TREE_VALUE (new_parm) isn't error_mark_node before setting its
	DECL_VIRTUAL_P.

gcc/testsuite/ChangeLog
	PR c++/96437
	* g++.dg/pr96437.C: New test case.


Thanks in advance,
Roger
--
  

Comments

Jason Merrill March 7, 2022, 11:43 p.m. UTC | #1
On 3/7/22 08:33, Roger Sayle wrote:
> 
> This patch fixes PR c++/96437 which is an ICE-on-invalid-code regression
> affecting mainline.
> 
> This patch has been tested on x86_64-pc-linux-gnu, enabling languages
> c and c++, with make bootstrap and make -k check with no new failures.
> Ok for mainline?
> 
> 2022-03-07  Roger Sayle  <roger@nextmovesoftware.com>
> 
> gcc/cp/ChangeLog
> 	PR c++/96437
> 	* parser.cc (synthesize_implicit_template_parm): Check that
> 	TREE_VALUE (new_parm) isn't error_mark_node before setting its
> 	DECL_VIRTUAL_P.
> 
> gcc/testsuite/ChangeLog
> 	PR c++/96437
> 	* g++.dg/pr96437.C: New test case.

> +  if (TREE_VALUE (new_parm) != error_mark_node)
> +    DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;

Hmm, I wonder about returning early if there was an error, but this is 
fine as is.

> +/* { dg-options "-O2" } */

This also seems unneeded for this test.

Also, please put tests in a subdirectory of g++.dg, not the top 
directory.  This one might go in cpp2a/ since it's a C++20 feature.

OK with those adjustments.

Jason
  
Roger Sayle March 9, 2022, 6:35 p.m. UTC | #2
Hi Jason,

Very many thanks for your reviews/approvals of these ICE-on-invalid-code fixes.

> > +  if (TREE_VALUE (new_parm) != error_mark_node)
> > +    DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
> 
> Hmm, I wonder about returning early if there was an error, but this is fine as is.

The challenge here is the need to perform "current_binding_level = entry_scope;"
required to restore the parser state before returning.  Now that the parser is
written in C++, we could (in theory) make more use of destructors to automatically
restore/pop state, making it safe(r) to just "return error_mark_node" immediately
in more places.

> > +/* { dg-options "-O2" } */
>
> This also seems unneeded for this test.

Doh! Force of habit (from working in the middle-end).  Consider this removed
from all the test cases below.

Is there any chance I could ask you to also look at these (that probably slipped
through the net), though PR 84964 might require a nod from a middle-end maintainer.

PR c++/39751: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590951.html
PR c++/84964: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590961.html
PR c++/95999: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590653.html
PR c++/96442: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590716.html

Very many thanks again for your help/reviews.  Much appreciated.
Roger
--
  
Jason Merrill March 10, 2022, 4:18 a.m. UTC | #3
On 3/9/22 14:35, Roger Sayle wrote:
> 
> Hi Jason,
> 
> Very many thanks for your reviews/approvals of these ICE-on-invalid-code fixes.
> 
>>> +  if (TREE_VALUE (new_parm) != error_mark_node)
>>> +    DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
>>
>> Hmm, I wonder about returning early if there was an error, but this is fine as is.
> 
> The challenge here is the need to perform "current_binding_level = entry_scope;"
> required to restore the parser state before returning.  Now that the parser is
> written in C++, we could (in theory) make more use of destructors to automatically
> restore/pop state, making it safe(r) to just "return error_mark_node" immediately
> in more places.

Yes, e.g.

auto ov = make_temp_override (current_binding_level);

>>> +/* { dg-options "-O2" } */
>>
>> This also seems unneeded for this test.
> 
> Doh! Force of habit (from working in the middle-end).  Consider this removed
> from all the test cases below.
> 
> Is there any chance I could ask you to also look at these (that probably slipped
> through the net), though PR 84964 might require a nod from a middle-end maintainer.
> 
> PR c++/39751: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590951.html
> PR c++/84964: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590961.html
> PR c++/95999: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590653.html
> PR c++/96442: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590716.html

Will do.

Jason
  

Patch

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 03d99ab..992f839 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -48217,7 +48217,8 @@  synthesize_implicit_template_parm  (cp_parser *parser, tree constr)
      function template is equivalent to an explicit template.
 
      Note that DECL_ARTIFICIAL is used elsewhere for template parameters.  */
-  DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
+  if (TREE_VALUE (new_parm) != error_mark_node)
+    DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
 
   // Chain the new parameter to the list of implicit parameters.
   if (parser->implicit_template_parms)
diff --git a/gcc/testsuite/g++.dg/pr96437.C b/gcc/testsuite/g++.dg/pr96437.C
new file mode 100644
index 0000000..00c4141
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr96437.C
@@ -0,0 +1,5 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* { dg-excess-errors "" } */
+
+template <()> void A(auto){}