C++ PATCH for c++/86060, c++98 ICE with range for

Message ID CADzB+2=QbA=gknNfxzYzpmzyyWomEioLhNmVzVO1MwwhpywACQ@mail.gmail.com
State New
Headers show
Series
  • C++ PATCH for c++/86060, c++98 ICE with range for
Related show

Commit Message

Jason Merrill June 6, 2018, 5:47 p.m.
If we're going to allow it with a pedwarn, we shouldn't clobber the
decl with an error_mark_node.

Tested x86_64-pc-linux-gnu, applying to trunk and 8.

Comments

H.J. Lu June 7, 2018, 10:51 a.m. | #1
On Wed, Jun 6, 2018 at 10:47 AM, Jason Merrill <jason@redhat.com> wrote:
> If we're going to allow it with a pedwarn, we shouldn't clobber the

> decl with an error_mark_node.

>

> Tested x86_64-pc-linux-gnu, applying to trunk and 8.


I got

/export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:
error: range-based 'for' loops only available with -std=c++11 or
-std=gnu++11^M
/export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:
error: forming reference to reference type 'int (&)[3]'^M
compiler exited with status 1
PASS: g++.dg/cpp0x/range-for9.C  -std=c++98  (test for errors, line 8)
FAIL: g++.dg/cpp0x/range-for9.C  -std=c++98 (test for excess errors)
Excess errors:
/export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:
error: forming reference to reference type 'int (&)[3]'


-- 
H.J.
Marek Polacek June 7, 2018, 1:14 p.m. | #2
On Thu, Jun 07, 2018 at 03:51:24AM -0700, H.J. Lu wrote:
> On Wed, Jun 6, 2018 at 10:47 AM, Jason Merrill <jason@redhat.com> wrote:

> > If we're going to allow it with a pedwarn, we shouldn't clobber the

> > decl with an error_mark_node.

> >

> > Tested x86_64-pc-linux-gnu, applying to trunk and 8.

> 

> I got

> 

> /export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:

> error: range-based 'for' loops only available with -std=c++11 or

> -std=gnu++11^M

> /export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:

> error: forming reference to reference type 'int (&)[3]'^M

> compiler exited with status 1

> PASS: g++.dg/cpp0x/range-for9.C  -std=c++98  (test for errors, line 8)

> FAIL: g++.dg/cpp0x/range-for9.C  -std=c++98 (test for excess errors)

> Excess errors:

> /export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:

> error: forming reference to reference type 'int (&)[3]'


Jason, ok to fix with this one?

2018-06-07  Marek Polacek  <polacek@redhat.com>

	* g++.dg/cpp0x/range-for9.C: Adjust dg-error.

--- gcc/testsuite/g++.dg/cpp0x/range-for9.C
+++ gcc/testsuite/g++.dg/cpp0x/range-for9.C
@@ -5,6 +5,6 @@
 void test()
 {
     int a[] = {0,1,2};
-    for (int x : a)  // { dg-error "range-based 'for'" }
+    for (int x : a)  // { dg-error "range-based 'for'|forming reference" }
         ;
 }
Jason Merrill June 7, 2018, 2:16 p.m. | #3
Ok.

On Thu, Jun 7, 2018, 3:14 PM Marek Polacek <polacek@redhat.com> wrote:

> On Thu, Jun 07, 2018 at 03:51:24AM -0700, H.J. Lu wrote:

> > On Wed, Jun 6, 2018 at 10:47 AM, Jason Merrill <jason@redhat.com> wrote:

> > > If we're going to allow it with a pedwarn, we shouldn't clobber the

> > > decl with an error_mark_node.

> > >

> > > Tested x86_64-pc-linux-gnu, applying to trunk and 8.

> >

> > I got

> >

> >

> /export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:

> > error: range-based 'for' loops only available with -std=c++11 or

> > -std=gnu++11^M

> >

> /export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:

> > error: forming reference to reference type 'int (&)[3]'^M

> > compiler exited with status 1

> > PASS: g++.dg/cpp0x/range-for9.C  -std=c++98  (test for errors, line 8)

> > FAIL: g++.dg/cpp0x/range-for9.C  -std=c++98 (test for excess errors)

> > Excess errors:

> >

> /export/gnu/import/git/gcc-test/src-8/gcc/testsuite/g++.dg/cpp0x/range-for9.C:8:18:

> > error: forming reference to reference type 'int (&)[3]'

>

> Jason, ok to fix with this one?

>

> 2018-06-07  Marek Polacek  <polacek@redhat.com>

>

>         * g++.dg/cpp0x/range-for9.C: Adjust dg-error.

>

> --- gcc/testsuite/g++.dg/cpp0x/range-for9.C

> +++ gcc/testsuite/g++.dg/cpp0x/range-for9.C

> @@ -5,6 +5,6 @@

>  void test()

>  {

>      int a[] = {0,1,2};

> -    for (int x : a)  // { dg-error "range-based 'for'" }

> +    for (int x : a)  // { dg-error "range-based 'for'|forming reference" }

>          ;

>  }

>

Patch

commit ee298d715781a806980f93348ba1b0afd62187db
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 6 17:21:42 2018 +0200

            PR c++/86060 - ICE on range for with -std=c++98.
    
            * parser.c (cp_parser_init_statement): Don't clobber *decl after
            pedwarn.

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 03aea2f1150..edb0ef8025e 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -12384,12 +12384,9 @@  cp_parser_init_statement (cp_parser *parser, tree *decl)
 	  cp_lexer_consume_token (parser->lexer);
 	  is_range_for = true;
 	  if (cxx_dialect < cxx11)
-	    {
-	      pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
-		       "range-based %<for%> loops only available with "
-		       "-std=c++11 or -std=gnu++11");
-	      *decl = error_mark_node;
-	    }
+	    pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
+		     "range-based %<for%> loops only available with "
+		     "-std=c++11 or -std=gnu++11");
 	}
       else
 	/* The ';' is not consumed yet because we told
diff --git a/gcc/testsuite/g++.dg/cpp0x/range-for35.C b/gcc/testsuite/g++.dg/cpp0x/range-for35.C
new file mode 100644
index 00000000000..c77a5af5a44
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/range-for35.C
@@ -0,0 +1,8 @@ 
+// PR c++/86060
+// { dg-options -Wpedantic }
+
+template <typename T> void foo(T (&a)[8]) {
+  for (int i : a)		// { dg-warning "range-based" "" { target c++98_only } }
+    i;
+}
+void fn1() { foo<int>; }