From a4b4b1e3ac9a2c97f7550024c3cc5aff52e0614c Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Tue, 28 Jan 2020 16:06:33 -0500 Subject: [PATCH] c++: Fix return deduction of lambda in discarded stmt. A return statement in a discarded statement is not used for return type deduction, but we still want to do deduction for a return statement in a lambda in a discarded statement. PR c++/93442 * parser.c (cp_parser_lambda_expression): Clear in_discarded_stmt. --- gcc/cp/ChangeLog | 3 +++ gcc/cp/parser.c | 6 ++++++ .../g++.dg/cpp1z/constexpr-if-lambda1.C | 16 ++++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda1.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 0a959bbd49d..9392c850750 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,8 @@ 2020-01-28 Jason Merrill + PR c++/93442 + * parser.c (cp_parser_lambda_expression): Clear in_discarded_stmt. + PR c++/93477 PR c++/91476 * decl2.c (copy_linkage): Do copy DECL_ONE_ONLY and DECL_WEAK. diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index 72037ee7b46..b8327823777 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -10530,6 +10530,10 @@ cp_parser_lambda_expression (cp_parser* parser) parser->implicit_template_scope = 0; parser->auto_is_implicit_function_template_parm_p = false; + /* The body of a lambda in a discarded statement is not discarded. */ + bool discarded = in_discarded_stmt; + in_discarded_stmt = 0; + /* By virtue of defining a local class, a lambda expression has access to the private variables of enclosing classes. */ @@ -10560,6 +10564,8 @@ cp_parser_lambda_expression (cp_parser* parser) finish_struct (type, /*attributes=*/NULL_TREE); + in_discarded_stmt = discarded; + parser->num_template_parameter_lists = saved_num_template_parameter_lists; parser->in_statement = in_statement; parser->in_switch_statement_p = in_switch_statement_p; diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda1.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda1.C new file mode 100644 index 00000000000..64c4cd27fe6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda1.C @@ -0,0 +1,16 @@ +// PR c++/93442 +// { dg-do compile { target c++17 } } + +struct bar { + int foo(){return 0;} +}; + +int foobar() { + if constexpr(true) { + return 0; + } else { + return [](){ + return bar{}; + }().foo(); + } +} -- 2.30.2