C++ PATCH for c++/68377 (parens in fold-expression)

Jason Merrill jason@redhat.com
Thu Nov 17 21:40:00 GMT 2016


finish_parenthesized_expr uses TREE_NO_WARNING to mark parenthesized
expressions, so I think it's reasonable to use that to recognize them
here.

Tested x86_64-pc-linux-gnu, applying to trunk.
-------------- next part --------------
commit 043b2830c4e296951a8209ade25b8d01394c7386
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Nov 17 15:25:00 2016 -0500

            PR c++/68377 - parenthesized expr in fold-expression
    
            * parser.c (cp_parser_fold_expression): Check TREE_NO_WARNING.

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 9a5039f..3ab0b68 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -4679,7 +4679,9 @@ cp_parser_fold_expression (cp_parser *parser, tree expr1)
   /* The operands of a fold-expression are cast-expressions, so binary or
      conditional expressions are not allowed.  We check this here to avoid
      tentative parsing.  */
-  if (is_binary_op (TREE_CODE (expr1)))
+  if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
+    /* OK, the expression was parenthesized.  */;
+  else if (is_binary_op (TREE_CODE (expr1)))
     error_at (location_of (expr1),
 	      "binary expression in operand of fold-expression");
   else if (TREE_CODE (expr1) == COND_EXPR)
diff --git a/gcc/testsuite/g++.dg/cpp1z/fold8.C b/gcc/testsuite/g++.dg/cpp1z/fold8.C
new file mode 100644
index 0000000..e27db7a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/fold8.C
@@ -0,0 +1,15 @@
+// PR c++/68377
+// { dg-options -std=c++1z }
+
+struct Sink { } s;
+template <class T> Sink& operator<<(Sink&, const T&);
+
+template<class... Tx> 
+int f(Tx... xs) {
+  return ((xs+1) + ...);
+}
+
+int main() {
+  s << f(3,4,5) << "\n";
+  return 0;
+}


More information about the Gcc-patches mailing list