From 0c382da0943dc7d14455ba2ada2f620a25bd1366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= Date: Thu, 25 Jul 2024 01:00:02 +0200 Subject: c++: diagnose usage of co_await and co_yield in default args [PR115906] This is a partial fix for PR115906. Per [expr.await] 2s3, "An await-expression shall not appear in a default argument ([dcl.fct.default])". This patch introduces the diagnostic in that case, and in the case of a co_yield (as co_yield is defined in terms of co_await, so prerequisites of co_await hold). PR c++/115906 - [coroutines] missing diagnostic and ICE when co_await used as default argument in function declaration gcc/cp/ChangeLog: PR c++/115906 * parser.cc (cp_parser_unary_expression): Reject await expressions if use of local variables is currently forbidden. (cp_parser_yield_expression): Reject yield expressions if use of local variables is currently forbidden. gcc/testsuite/ChangeLog: PR c++/115906 * g++.dg/coroutines/pr115906-yield.C: New test. * g++.dg/coroutines/pr115906.C: New test. * g++.dg/coroutines/co-await-syntax-02-outside-fn.C: Don't rely on default arguments. * g++.dg/coroutines/co-yield-syntax-01-outside-fn.C: Ditto. --- gcc/cp/parser.cc | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'gcc/cp/parser.cc') diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index e46cdfd..eb102de 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -9242,6 +9242,14 @@ cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk, if (expr == error_mark_node) return error_mark_node; + /* ... but, we cannot use co_await in default arguments. */ + if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN) + { + error_at (kw_loc, + "% cannot be used in default arguments"); + return error_mark_node; + } + /* Handle [expr.await]. */ return cp_expr (finish_co_await_expr (kw_loc, expr)); } @@ -29651,6 +29659,15 @@ cp_parser_yield_expression (cp_parser* parser) else expr = cp_parser_assignment_expression (parser); + /* Similar to co_await, we cannot use co_yield in default arguments (as + co_awaits underlie co_yield). */ + if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN) + { + error_at (kw_loc, + "% cannot be used in default arguments"); + return error_mark_node; + } + if (expr == error_mark_node) return expr; -- cgit v1.1