]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: NRV in lambda in template [PR91217]
authorJason Merrill <jason@redhat.com>
Sat, 3 Apr 2021 05:07:36 +0000 (01:07 -0400)
committerJason Merrill <jason@redhat.com>
Fri, 13 May 2022 17:39:31 +0000 (13:39 -0400)
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.

gcc/cp/pt.c
gcc/testsuite/g++.dg/opt/nrv20.C [new file with mode: 0644]

index eeee23181fc6377ba13de3dcb82dbd0362489577..4bae467fa716eea80b6f528a3e1b0aef032f3a1e 100644 (file)
@@ -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 (file)
index 0000000..ade0c28
--- /dev/null
@@ -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>();
+}