From patchwork Wed Jun 7 01:31:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 70691 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4C113385770F for ; Wed, 7 Jun 2023 01:36:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C113385770F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686101809; bh=scPpf3fNHY6gn/162aCl3yCW9gso1XE3pV7l+C7edYM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=P1Aq4MilIyrYDzjksQY9wvJkW784wH1tGSU9/Sm8sglWhJaHpDuQJwQi08iRUHRow a81aNqF2NR113fA7rdEGRd4gQjyGXgFakagekmfE2pJ5bUtAFCq9v5/uRXyzmCyx4b F23YKlIZRDnUhk9DQWNBhqM0qdx5WE1uvyh7Hqh4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 995563858025 for ; Wed, 7 Jun 2023 01:36:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 995563858025 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-61-Hn5s9GRvPtGPEta0h39fhg-1; Tue, 06 Jun 2023 21:36:19 -0400 X-MC-Unique: Hn5s9GRvPtGPEta0h39fhg-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-62b3d4accd8so46615656d6.1 for ; Tue, 06 Jun 2023 18:36:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686101479; x=1688693479; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=scPpf3fNHY6gn/162aCl3yCW9gso1XE3pV7l+C7edYM=; b=X41WPqIcKO0Qedww7knMMNQwXjMGh1O+3h4QgCkhgF6ja28k5roBIQsfinNFhBLqpJ /mkRJukKUUNap0a8waVfaXZ4yFMhYx/f62ZAznsRp+tofaIOdmjK2iFof3SRix7Kr2Fl L15eaw3jsCivc85rBr0NrZxVG4Yll2e7fCwYSyU643646TU0QjIrwuunt8HPxBGTq9Hr ND3eUs0xcnhHb4nYX2tmOxL1023OqBSGHLCHf719GRoswFaXI19MOSChc1TJUUnRhz2m K+iE2YpumPDfKOZLaNwtjXfWgO8tHrBo0SWD+/pvUodHgaW6AUvzXzc4+ilbSOZhjGZd /Yfw== X-Gm-Message-State: AC+VfDxNqK9BfT0WhDmdM8mRokOIC8v2bJf+P1GgvjE6cFvkoM4elp4n FyYpBRQPz/UR/uzn2lSg8knowiYOoQ/zVwcMlYzq2YK/f1977lJNK0Om2cNKhBlDk0voxSDo4wD JznUi2E26pDHX2OsWoDic/iinjfjAItVePCYiJozrW3eKzrNODLQRKDQCIVxa0wyJEcnj449snw == X-Received: by 2002:a05:6214:1d0a:b0:625:aa49:9aba with SMTP id e10-20020a0562141d0a00b00625aa499abamr1838893qvd.62.1686101479105; Tue, 06 Jun 2023 18:31:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ42H7QvHVGczAQuJlNiEpAvKecjAeS91Xp9J8WGsrdcC58be0+QAZNFzsVBe2qVftn+EhLMEQ== X-Received: by 2002:a05:6214:1d0a:b0:625:aa49:9aba with SMTP id e10-20020a0562141d0a00b00625aa499abamr1838874qvd.62.1686101478724; Tue, 06 Jun 2023 18:31:18 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id c12-20020a0cd60c000000b006238888dbffsm5801591qvj.139.2023.06.06.18.31.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Jun 2023 18:31:17 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: NRV and goto [PR92407] Date: Tue, 6 Jun 2023 21:31:16 -0400 Message-Id: <20230607013116.2770869-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- 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. --- gcc/cp/typeck.cc | 3 +++ gcc/testsuite/g++.dg/opt/nrv22.C | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 gcc/testsuite/g++.dg/opt/nrv22.C base-commit: 29c82c6ca929e0f5eccfe038dea71177d814c6b7 prerequisite-patch-id: aed53cdac161144c31cb1433282e1ad1d49d3770 prerequisite-patch-id: 1098cb4457a5eff90fa8176f9b0d8d2e9477596e prerequisite-patch-id: 823f2ce422455c6c7ccbaa9938b670a600b376df 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 (); +}