From: Marek Polacek Date: Mon, 26 Oct 2020 19:04:58 +0000 (-0400) Subject: c++: GCC accepts junk before fold-expression [PR86773] X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=43cb72263fb3b7b97a74fb38d71364a1d5cf0448;p=gcc.git c++: GCC accepts junk before fold-expression [PR86773] Here we accept a bogus expression before a left fold: Recall that a fold expression looks like: fold-expression: ( cast-expression fold-operator ... ) ( ... fold-operator cast-expression ) ( cast-expression fold-operator ... fold-operator cast-expression ) but here we have ( cast-expression ... fold-operator cast-expression ) The best fix seems to just return error_mark_node when we know this code is invalid, and let the subsequent code report that a ) was expected. gcc/cp/ChangeLog: PR c++/86773 * parser.c (cp_parser_fold_expression): Return error_mark_node if a left fold is preceded by an expression. gcc/testsuite/ChangeLog: PR c++/86773 * g++.dg/cpp1z/fold12.C: New test. --- diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 52637b1d2af..80d903cca2f 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -5138,6 +5138,8 @@ cp_parser_fold_expression (cp_parser *parser, tree expr1) // Left fold. if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) { + if (expr1) + return error_mark_node; cp_lexer_consume_token (parser->lexer); int op = cp_parser_fold_operator (parser); if (op == ERROR_MARK) diff --git a/gcc/testsuite/g++.dg/cpp1z/fold12.C b/gcc/testsuite/g++.dg/cpp1z/fold12.C new file mode 100644 index 00000000000..90d74cc5947 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/fold12.C @@ -0,0 +1,13 @@ +// PR c++/86773 +// { dg-do compile { target c++17 } } + +template +auto work(Param && ...param) +{ + return ("asda" ... / param); // { dg-error "expected" } +} + +int main() +{ + work(1.0, 2.0, 5, 4.0); +}