This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] Fix PR c++/36460: >> in C++0x template argument misinterpreted


PR c++/36460 is a case where we are parsing a template argument, and
we see a type-id followed by a '>>'. In C++98, we need to reparse the
template argument, because the type-id could be something like X()
that is also an expression that could then be used as the left-hand
side of an >> expression (this is what we do now). In C++0x, however,
the '>>' should be considered two '>' tokens. This patch does that,
and the tests verify both that we get appropriate warnings from
-Wc++0x-compat (since this is another area of incompatibility) and
that we handle the test case properly.

Tested i686-pc-linux-gnu; okay for mainline and 4.3?

  - Doug

2008-08-06  Douglas Gregor  <doug.gregor@gmail.com>

	PR c++/36460
	* parser.c (cp_parser_template_argument): Don't assume that '>>'
	following a type-id is an error when in C++0x mode.

2008-08-06  Douglas Gregor  <doug.gregor@gmail.com>

	PR c++/36460
	* g++.dg/cpp0x/bracket3.C: Add another test case for the >>
	warning under -Wc++0x-compat.
	* g++.dg/cpp0x/bracket4.C: Add testcase for PR c++/36460.
Index: cp/parser.c
===================================================================
--- cp/parser.c	(revision 137601)
+++ cp/parser.c	(working copy)
@@ -10307,9 +10307,10 @@ cp_parser_template_argument (cp_parser* 
      Therefore, we try a type-id first.  */
   cp_parser_parse_tentatively (parser);
   argument = cp_parser_type_id (parser);
-  /* If there was no error parsing the type-id but the next token is a '>>',
-     we probably found a typo for '> >'. But there are type-id which are
-     also valid expressions. For instance:
+  /* If there was no error parsing the type-id but the next token is a
+     '>>', our behavior depends on which dialect of C++ we're
+     parsing. In C++98, we probably found a typo for '> >'. But there
+     are type-id which are also valid expressions. For instance:
 
      struct X { int operator >> (int); };
      template <int V> struct Foo {};
@@ -10318,8 +10319,12 @@ cp_parser_template_argument (cp_parser* 
      Here 'X()' is a valid type-id of a function type, but the user just
      wanted to write the expression "X() >> 5". Thus, we remember that we
      found a valid type-id, but we still try to parse the argument as an
-     expression to see what happens.  */
+     expression to see what happens. 
+
+     In C++0x, the '>>' will be considered two separate '>'
+     tokens.  */
   if (!cp_parser_error_occurred (parser)
+      && cxx_dialect == cxx98
       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
     {
       maybe_type_id = true;
Index: testsuite/g++.dg/cpp0x/bracket3.C
===================================================================
--- testsuite/g++.dg/cpp0x/bracket3.C	(revision 137601)
+++ testsuite/g++.dg/cpp0x/bracket3.C	(working copy)
@@ -3,3 +3,8 @@
 template<int N> struct X {};
 
 X<1 >> 2> x; // { dg-warning "will be treated as|suggest parentheses" }
+
+// From cp/parser.c
+typedef int Y;
+template <int V> struct Foo {};
+Foo<Y () >> 5> r; // { dg-warning "will be treated as|suggest parentheses" }
Index: testsuite/g++.dg/cpp0x/bracket4.C
===================================================================
--- testsuite/g++.dg/cpp0x/bracket4.C	(revision 137601)
+++ testsuite/g++.dg/cpp0x/bracket4.C	(working copy)
@@ -1,6 +1,5 @@
 // { dg-do "compile" }
 // { dg-options "-std=c++0x" }
-
 template<typename T>
 struct vector { 
 };
@@ -25,3 +24,12 @@ void f()
 {
   vector<vector<int>>() + 2;
 }
+
+// PR c++/36460
+template <class a>
+class A {};
+template <class b>
+class B {};
+
+A<B<void()>> x;
+

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]