From patchwork Thu Apr 14 00:23:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 52890 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 2D7C43857C6F for ; Thu, 14 Apr 2022 00:24:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D7C43857C6F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1649895862; bh=0BrQFtg7YLmyrzjC6LB4ZK6RoGsX+5hDp12tYL+1bFQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=RRRUuRcuqRm8ZBbHX2GBi9fg9HDH4RzfBOpW28ieNyr0/JsLVSUwiRko41udvd7Wx lvKqLOjpB30ULlC8gaac+qqforekfyQroetXsDyMes5jMAf2xZhb2Oyw2N0SnmJQ5r 2WRa3bAu5sahzflvVgYzDcrBdrk48KKU5GYTmxt8= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 019783858D3C for ; Thu, 14 Apr 2022 00:23:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 019783858D3C Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-654-GKxcKkJOPAyegN7kkAg4cw-1; Wed, 13 Apr 2022 20:23:51 -0400 X-MC-Unique: GKxcKkJOPAyegN7kkAg4cw-1 Received: by mail-qt1-f199.google.com with SMTP id d18-20020ac81192000000b002ebdd6ef307so2278907qtj.20 for ; Wed, 13 Apr 2022 17:23:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=0BrQFtg7YLmyrzjC6LB4ZK6RoGsX+5hDp12tYL+1bFQ=; b=lJLaedFQddQSd+gOzu14jurBgfcaWGEJ6ZBtOSyUBUCjjfpbOjnICKG6PswcDg5jra k9brxIzea9H9Ocv5hidvcukJ2DC8dyjUyKdcm5f/aIBU39df/9OBfZOimxbj6ehNg4Kh fLrpoPCZ/g9ttrpANU3Ni0L0a7A3sw6IsPHcPg1FL7PuBEHF7zJiB8N11JSgYIPm7WNY 5XQDcvc9+Llny5a+6i8f7v4Nfm0dVFZd9dwQrgEqFeslaQ64Ei5omJIrt60qyY/ebXjI LtLT8jvhpV7IRT8lpVzQ+Qt4Sw6RiH+n8TQo5tauC3V7If5ti1g046JattXJY+SOK3Br IZyA== X-Gm-Message-State: AOAM533DoOlt5RBlWBJ0oSCd0oIMOHcDBGAHyuq3YFy4FjMbdSRfGkof ok8BtLdDOd9EIvW00dtTgpMdkfPrsaFonFiu3covHOz9Ut19YLtw/8f0gfPA+n1llhkTcMhCkuy 1lLGbMxdwnfrw8lpI2Du3DjuKameatMNcZ3lFkRP8fNmo/S1HyYGs+6BeFAYK5nBQ3w== X-Received: by 2002:ac8:7090:0:b0:2ed:ca4:f08c with SMTP id y16-20020ac87090000000b002ed0ca4f08cmr67707qto.57.1649895830637; Wed, 13 Apr 2022 17:23:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz9M06cRSOT+jD6Vw7wQBQAwAhn6Yo2Z2ALF7D7uXLWK4Vzeay8aB5EheSa2uYK2mIVO+xHhw== X-Received: by 2002:ac8:7090:0:b0:2ed:ca4:f08c with SMTP id y16-20020ac87090000000b002ed0ca4f08cmr67688qto.57.1649895830232; Wed, 13 Apr 2022 17:23:50 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id v67-20020a379346000000b0069bdb3bb132sm209531qkd.37.2022.04.13.17.23.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 17:23:49 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: NRV and ref-extended temps [PR101442] Date: Wed, 13 Apr 2022 20:23:46 -0400 Message-Id: <20220414002346.3271123-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 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_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" This issue goes back to r83221, where the cleanup for extended ref temps changed from being unconditional to being tied to the declaration they formed part of the initializer for. The named return value optimization changes the cleanup for the NRV variable to only run on the EH path; we don't want that change to affect temporary cleanups. The perform_member_init change isn't necessary (there 'decl' is a COMPONENT_REF), it's just for consistency. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/101442 gcc/cp/ChangeLog: * decl.cc (cp_finish_decl): Don't pass decl to push_cleanup. * init.cc (perform_member_init): Likewise. * semantics.cc (push_cleanup): Adjust comment. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/initlist-nrv1.C: New test. --- gcc/cp/decl.cc | 2 +- gcc/cp/init.cc | 2 +- gcc/cp/semantics.cc | 3 +- gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C | 34 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C base-commit: 019d6d4149ee97d55ce9efe4e5e470d38130cdeb diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 5f59612fb00..d51fd75b003 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8554,7 +8554,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, { for (tree t : *cleanups) { - push_cleanup (decl, t, false); + push_cleanup (NULL_TREE, t, false); /* As in initialize_local_var. */ wrap_temporary_cleanups (init, t); } diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index 7ce8d3a46e5..75ab965a218 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -1066,7 +1066,7 @@ perform_member_init (tree member, tree init, hash_set &uninitialized) init = build2 (INIT_EXPR, type, decl, init); finish_expr_stmt (init); FOR_EACH_VEC_ELT (*cleanups, i, t) - push_cleanup (decl, t, false); + push_cleanup (NULL_TREE, t, false); } else if (type_build_ctor_call (type) || (init && CLASS_TYPE_P (strip_array_types (type)))) diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 43627ed30af..f5ec808b1bc 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -666,7 +666,8 @@ do_pushlevel (scope_kind sk) /* Queue a cleanup. CLEANUP is an expression/statement to be executed when the current scope is exited. EH_ONLY is true when this is not - meant to apply to normal control flow transfer. */ + meant to apply to normal control flow transfer. DECL is the VAR_DECL + being cleaned up, if any, or null for temporaries or subobjects. */ void push_cleanup (tree decl, tree cleanup, bool eh_only) diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C new file mode 100644 index 00000000000..e44dbecfece --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-nrv1.C @@ -0,0 +1,34 @@ +// PR c++/101442 +// { dg-do run { target c++11 } } + +bool destroyed = false; + +struct A +{ + A() {} + A(const A &) = delete; + A &operator=(const A &) = delete; + ~A() {destroyed = true;} +}; + +struct B +{ + const A &a; + struct string { + string(const char*) { } + ~string() { } + } s; +}; + +B foo() +{ + B ret{ A{}, "" }; + return ret; +} + +int main() +{ + B b = foo(); + if (!destroyed) + __builtin_abort(); +}