From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2122) id 7F2D3385735D; Wed, 7 Jun 2023 01:33:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7F2D3385735D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686101615; bh=+mYOU9exd87nxDtQxMhTpioFzWjjkZT4g8ftlRh3zS4=; h=From:To:Subject:Date:From; b=DSDhc602t8f9RJaxBS1jUsVepnJMLT1BFmhrRbiQN/JdOcv2qt5Rdn6KIEty2BMRJ 5A1PeXSmoS2B70pa8qkZGnWue4ggUig69RIGdEY0r4EjS9r9CKtMY/f1ib2b07i0mL eAoOeZvqcvDZ+aghqngYR6wuDL6MYi8dISqjrxAI= 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 r14-1592] c++: NRV and goto [PR92407] X-Act-Checkin: gcc X-Git-Author: Jason Merrill X-Git-Refname: refs/heads/trunk X-Git-Oldrev: 08cea4e56a094ff9cc7c65fdc9ce8c4d7aff64be X-Git-Newrev: b192e2007e1c98b548f4aa878523b485968d24a4 Message-Id: <20230607013335.7F2D3385735D@sourceware.org> Date: Wed, 7 Jun 2023 01:33:35 +0000 (GMT) List-Id: https://gcc.gnu.org/g:b192e2007e1c98b548f4aa878523b485968d24a4 commit r14-1592-gb192e2007e1c98b548f4aa878523b485968d24a4 Author: Jason Merrill Date: Sun Jun 4 12:00:55 2023 -0400 c++: NRV and goto [PR92407] Here our named return value optimization was breaking the required destructor when the goto takes 'a' out of scope. The simplest fix is to disable the optimization in the presence of user labels. We could do better by disabling the optimization only if there is a backward goto across the variable declaration, but we don't currently track that. PR c++/92407 gcc/cp/ChangeLog: * typeck.cc (check_return_expr): Prevent NRV in the presence of named labels. gcc/testsuite/ChangeLog: * g++.dg/opt/nrv22.C: New test. Diff: --- gcc/cp/typeck.cc | 3 +++ gcc/testsuite/g++.dg/opt/nrv22.C | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc index 11fcc7fcd3b..6618c6a2021 100644 --- a/gcc/cp/typeck.cc +++ b/gcc/cp/typeck.cc @@ -11155,6 +11155,9 @@ check_return_expr (tree retval, bool *no_warning) if (fn_returns_value_p && flag_elide_constructors) { if (named_return_value_okay_p + /* The current NRV implementation breaks if a backward goto needs to + destroy the object (PR92407). */ + && !cp_function_chain->x_named_labels && (current_function_return_value == NULL_TREE || current_function_return_value == bare_retval)) current_function_return_value = bare_retval; diff --git a/gcc/testsuite/g++.dg/opt/nrv22.C b/gcc/testsuite/g++.dg/opt/nrv22.C new file mode 100644 index 00000000000..eb889fa615b --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/nrv22.C @@ -0,0 +1,30 @@ +// PR c++/92407 +// { dg-do run } + +struct A +{ + A () { a++; } + A (const A &) { a++; } + ~A () { a--; } + static int a; +}; +int A::a = 0; + +A +foo () +{ + int cnt = 10; +lab: + A a; + if (cnt--) + goto lab; + return a; +} + +int +main () +{ + foo (); + if (A::a) + __builtin_abort (); +}