diff mbox

Fix PR c++/77639 (ICE during error recovery)

Message ID alpine.DEB.2.20.15.1609191105120.23045@idea
State New
Headers show

Commit Message

Patrick Palka Sept. 19, 2016, 3:17 p.m. UTC
On Mon, 19 Sep 2016, Jason Merrill wrote:

> >        if (tree t = maybe_new_partial_specialization (type))
> >         {
> > +         if (processing_template_parmlist)
> > +           {
> > +            /* Some syntactically invalid code can confuse the compiler into
> > +               defining a specialization from inside a template parameter
> > +               list.  Bail out now so that we won't ICE later.  */
> > +             gcc_assert (seen_error ());
> > +             return error_mark_node;
> > +           }
> 
> Rather than decide it's a specialization and then fail, let's avoid
> treating it as a specialization in the first place.  I think we can
> add a processing_template_parmlist check at the top where we avoid
> trying to specialize lambdas.
> 
> Jason
> 

This works too.  However I was playing around with the parser a bit and
noticed that the following also fixes the ICE.  It makes no sense to try
to recover gracefully from a presumed missing "template <>" prefix when
processing_template_parmlist.  This is what seems to be responsible for
creating the bogus specialization.  Does this look OK or do you prefer
adjusting maybe_process_partial_specialization()?

gcc/cp/ChangeLog:

	PR c++/77639
	* parser.c (cp_parser_class_head): When
	processing_template_parmlist, don't assume that a
	class-head starts an explicit specialization.

gcc/testsuite/ChangeLog:

	PR c++/77639
	* g++.dg/template/error-recovery4.C: New test.
---
 gcc/cp/parser.c                                 | 1 +
 gcc/testsuite/g++.dg/template/error-recovery4.C | 5 +++++
 2 files changed, 6 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/template/error-recovery4.C

Comments

Jason Merrill Sept. 19, 2016, 5:24 p.m. UTC | #1
That looks good, OK.

On Mon, Sep 19, 2016 at 11:17 AM, Patrick Palka <patrick@parcs.ath.cx> wrote:
> On Mon, 19 Sep 2016, Jason Merrill wrote:
>
>> >        if (tree t = maybe_new_partial_specialization (type))
>> >         {
>> > +         if (processing_template_parmlist)
>> > +           {
>> > +            /* Some syntactically invalid code can confuse the compiler into
>> > +               defining a specialization from inside a template parameter
>> > +               list.  Bail out now so that we won't ICE later.  */
>> > +             gcc_assert (seen_error ());
>> > +             return error_mark_node;
>> > +           }
>>
>> Rather than decide it's a specialization and then fail, let's avoid
>> treating it as a specialization in the first place.  I think we can
>> add a processing_template_parmlist check at the top where we avoid
>> trying to specialize lambdas.
>>
>> Jason
>>
>
> This works too.  However I was playing around with the parser a bit and
> noticed that the following also fixes the ICE.  It makes no sense to try
> to recover gracefully from a presumed missing "template <>" prefix when
> processing_template_parmlist.  This is what seems to be responsible for
> creating the bogus specialization.  Does this look OK or do you prefer
> adjusting maybe_process_partial_specialization()?
>
> gcc/cp/ChangeLog:
>
>         PR c++/77639
>         * parser.c (cp_parser_class_head): When
>         processing_template_parmlist, don't assume that a
>         class-head starts an explicit specialization.
>
> gcc/testsuite/ChangeLog:
>
>         PR c++/77639
>         * g++.dg/template/error-recovery4.C: New test.
> ---
>  gcc/cp/parser.c                                 | 1 +
>  gcc/testsuite/g++.dg/template/error-recovery4.C | 5 +++++
>  2 files changed, 6 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/template/error-recovery4.C
>
> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
> index fb88021..c03b9c2 100644
> --- a/gcc/cp/parser.c
> +++ b/gcc/cp/parser.c
> @@ -22025,6 +22025,7 @@ cp_parser_class_head (cp_parser* parser,
>       it is not, try to recover gracefully.  */
>    if (at_namespace_scope_p ()
>        && parser->num_template_parameter_lists == 0
> +      && !processing_template_parmlist
>        && template_id_p)
>      {
>        /* Build a location of this form:
> diff --git a/gcc/testsuite/g++.dg/template/error-recovery4.C b/gcc/testsuite/g++.dg/template/error-recovery4.C
> new file mode 100644
> index 0000000..1e52c6a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/error-recovery4.C
> @@ -0,0 +1,5 @@
> +// PR c++/77639
> +
> +template <class, int, class, int> struct B {};
> +template <class T, int a, class U struct B<T, a, U, 1> {}; // { dg-error "" }
> +B<int, 2, char, 1> i;
> --
> 2.10.0.87.g1cd26dd
>
diff mbox

Patch

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index fb88021..c03b9c2 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -22025,6 +22025,7 @@  cp_parser_class_head (cp_parser* parser,
      it is not, try to recover gracefully.  */
   if (at_namespace_scope_p ()
       && parser->num_template_parameter_lists == 0
+      && !processing_template_parmlist
       && template_id_p)
     {
       /* Build a location of this form:
diff --git a/gcc/testsuite/g++.dg/template/error-recovery4.C b/gcc/testsuite/g++.dg/template/error-recovery4.C
new file mode 100644
index 0000000..1e52c6a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/error-recovery4.C
@@ -0,0 +1,5 @@ 
+// PR c++/77639
+
+template <class, int, class, int> struct B {};
+template <class T, int a, class U struct B<T, a, U, 1> {}; // { dg-error "" }
+B<int, 2, char, 1> i;