public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r9-10177] c++: NRV in lambda in template [PR91217]
Date: Fri, 13 May 2022 17:42:09 +0000 (GMT)	[thread overview]
Message-ID: <20220513174209.8B5A9395BC3E@sourceware.org> (raw)

https://gcc.gnu.org/g:da999d938e516ff12b39a0aa7e8f6d97fbee60df

commit r9-10177-gda999d938e516ff12b39a0aa7e8f6d97fbee60df
Author: Jason Merrill <jason@redhat.com>
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 eeee23181fc..4bae467fa71 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -18472,8 +18472,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 "<retval> = a" "gimple" } }
+
+struct A
+{
+  int ar[42];
+};
+
+template <class T>
+A f()
+{
+  return [] { A a; return a; }();
+}
+
+int main()
+{
+  f<int>();
+}


                 reply	other threads:[~2022-05-13 17:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220513174209.8B5A9395BC3E@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).