From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id D37E6397283E; Thu, 20 May 2021 21:34:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D37E6397283E MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jason Merrill To: gcc-cvs@gcc.gnu.org Subject: [gcc r10-9841] c++: NRV in lambda in template [PR91217] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 5e7801808d2e9e99dc2931462c69a80c6f0e89cc X-Git-Newrev: a12ae580e13f5856983142a0234651d8e8aad599 Message-Id: <20210520213443.D37E6397283E@sourceware.org> Date: Thu, 20 May 2021 21:34:43 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 20 May 2021 21:34:43 -0000 https://gcc.gnu.org/g:a12ae580e13f5856983142a0234651d8e8aad599 commit r10-9841-ga12ae580e13f5856983142a0234651d8e8aad599 Author: Jason Merrill Date: Sat Apr 3 01:07:36 2021 -0400 c++: NRV in lambda in template [PR91217] tsubst_lambda_expr was producing a function with two blocks that claimed to be the outermost block in the function body, one from the call to start_lambda_function in tsubst_lambda_expr, and one from tsubsting the block added by start_lambda_function when we first parsed the lambda. This messed with the named return value optimization, which only works for variables in the outermost block. gcc/cp/ChangeLog: PR c++/91217 * pt.c (tsubst_lambda_expr): Skip the body block from DECL_SAVED_TREE. gcc/testsuite/ChangeLog: PR c++/91217 * g++.dg/opt/nrv20.C: New test. Diff: --- gcc/cp/pt.c | 9 +++++++-- gcc/testsuite/g++.dg/opt/nrv20.C | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index e41f15659bc..8616118228c 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -19265,8 +19265,13 @@ tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) the purposes of template argument deduction. */ complain = tf_warning_or_error; - tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r, - /*constexpr*/false); + tree saved = DECL_SAVED_TREE (oldfn); + if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved)) + /* We already have a body block from start_lambda_function, we don't + need another to confuse NRV (91217). */ + saved = BIND_EXPR_BODY (saved); + + tsubst_expr (saved, args, complain, r, /*constexpr*/false); finish_lambda_function (body); diff --git a/gcc/testsuite/g++.dg/opt/nrv20.C b/gcc/testsuite/g++.dg/opt/nrv20.C new file mode 100644 index 00000000000..ade0c28824a --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/nrv20.C @@ -0,0 +1,20 @@ +// PR c++/91217 +// { dg-do compile { target c++11 } } +// { dg-additional-options -fdump-tree-gimple } +// { dg-final { scan-tree-dump-not " = a" "gimple" } } + +struct A +{ + int ar[42]; +}; + +template +A f() +{ + return [] { A a; return a; }(); +} + +int main() +{ + f(); +}