]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: NRV and goto [PR92407]
authorJason Merrill <jason@redhat.com>
Sun, 4 Jun 2023 16:00:55 +0000 (12:00 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 24 Jan 2024 10:18:29 +0000 (05:18 -0500)
commit193321d2649a818c3b322c8db79a2805cfdd7095
treefa4ce674cc2decd4f9ce07b7f9db99bbe8c3e74c
parentde3cc9af64b19ad64bce00c1a20b0989db29bf3e
c++: NRV and goto [PR92407]

Here our named return value optimization was breaking the required
destructor when the goto takes 'a' out of scope.  A simple fix for the
release branches is to disable the optimization in the presence of backward
goto.

We could do better by disabling the optimization only if there is a backward
goto across the variable declaration, but we don't track that, and in GCC 14
we instead make the goto work with NRV.

PR c++/92407

gcc/cp/ChangeLog:

* cp-tree.h (struct language_function): Add backward_goto.
* decl.cc (check_goto): Set it.
* typeck.cc (check_return_expr): Prevent NRV if set.

gcc/testsuite/ChangeLog:

* g++.dg/opt/nrv22.C: New test.

(cherry picked from commit a645347c19b07cc7abd7bf276c6769fc41afc932)
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/opt/nrv22.C [new file with mode: 0644]