From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 491793858CDB for ; Thu, 13 Jul 2023 18:49:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 491793858CDB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689274186; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=pA3sJyt3B3xJpOH1HTeFP3IOzz1eHzaQMtwvj2pYznU=; b=VfTm4yB6sf2RMtuMERh7wH3xuw4/jjLFEArCJRYyOHEbsrFHipfoRdw1rbOTBs3lDbXcHj gYakTxzv7xCML2LJK2J/1pJfbFC0xiCjuqYuRc+HjyMUHrGuiC3RVGI5ZuKw3pNlwwFyVa L+BxdZg/MDmsN9PSUoATuZYwKY25pD8= 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-378-FTS3zliYPSW3dq39UQkJ1g-1; Thu, 13 Jul 2023 14:49:45 -0400 X-MC-Unique: FTS3zliYPSW3dq39UQkJ1g-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-63788123d11so10011956d6.0 for ; Thu, 13 Jul 2023 11:49:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689274184; x=1691866184; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pA3sJyt3B3xJpOH1HTeFP3IOzz1eHzaQMtwvj2pYznU=; b=N/G+e1OEQvThGEjbOAY49G6zJRXurdKT/gO3NX2IeUUK/s1zxBNH6xXCrTJwIs2apE ZCtdaphzPVP2M+NDhuKdoFNdy33GHuq7PV+WNUcoU/v5Dyw74F62I+EffsN7B1aNVtS+ TXaUkK9aLOHiq7wsq+PjIbLxeKfwqMTfw7tvfAlp3eBr2v0eLDLuBuW6GX77BEcUzwXL bUYWomBWecHPzZLMTRU+1IgoIEcKYF/e+9J8DtLkE9pEoqF4MJtYKtx1Out7+mcyiI0+ VeecmUJyCJI6q1mu1nFjtFX/xDBleQgsSEuyM85Ipg7nTE0xJ337qb1v9nFBnZhxtlfp sA0Q== X-Gm-Message-State: ABy/qLb3zyHcEcB+qxdyuS7rvHP4zdG6n3GjL47c5mVUAhQH53FWAnWs YwCNtDgmqT0Bx047dqcJvn++VGCSsFf0u+WJ0K5FBd/y8/aksayk6ZwzlTVQfZoUU/Y/p19II2c hgBK12NEG3nEc7x6PZ8eHIYULcYTzMU98eFy8a2e0bjFiXtEmRW/F2++95rfw0VSXU3e8O/Ik2+ c= X-Received: by 2002:a0c:b2d2:0:b0:630:7a8:f602 with SMTP id d18-20020a0cb2d2000000b0063007a8f602mr2021896qvf.7.1689274184339; Thu, 13 Jul 2023 11:49:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlFivULRJ9ogMMRJFss69qyv+F+MyMi2m/wEK5MmuD04AgY/bjgUsAx+ari6QQ+vNNJBZvjLoA== X-Received: by 2002:a0c:b2d2:0:b0:630:7a8:f602 with SMTP id d18-20020a0cb2d2000000b0063007a8f602mr2021882qvf.7.1689274184009; Thu, 13 Jul 2023 11:49:44 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id c13-20020a0cf2cd000000b0060530c942f4sm3313537qvm.46.2023.07.13.11.49.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 11:49:43 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: copy elision of object arg in static memfn call [PR110441] Date: Thu, 13 Jul 2023 14:49:23 -0400 Message-ID: <20230713184923.3699777-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.327.gaa9166bcc0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.7 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,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 List-Id: Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- Here the call A().f() is represented as a COMPOUND_EXPR whose first operand is the otherwise unused object argument A() and second operand is the call result (both are TARGET_EXPRs). Within the return statement, this outermost COMPOUND_EXPR ends up foiling the copy elision check in build_special_member_call, resulting in us introducing a bogus call to the deleted move constructor. (Within the variable initialization, which goes through ocp_convert instead of convert_for_initialization, we've already been eliding the copy despite the outermost COMPOUND_EXPR ever since r10-7410-g72809d6fe8e085 made ocp_convert look through COMPOUND_EXPR). In contrast, I noticed '(A(), A::f())' (which should be equivalent to the above call) is represented with the COMPOUND_EXPR inside the RHS's TARGET_EXPR initializer thanks to a special case in cp_build_compound_expr thus avoiding the issue. So this patch fixes this by making keep_unused_object_arg use cp_build_compound_expr as well. PR c++/110441 gcc/cp/ChangeLog: * call.cc (keep_unused_object_arg): Use cp_build_compound_expr instead of building a COMPOUND_EXPR directly. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/elide8.C: New test. --- gcc/cp/call.cc | 2 +- gcc/testsuite/g++.dg/cpp1z/elide8.C | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/elide8.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 119063979fa..b0a69cb46d4 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -5218,7 +5218,7 @@ keep_unused_object_arg (tree result, tree obj, tree fn) if (TREE_THIS_VOLATILE (a)) a = build_this (a); if (TREE_SIDE_EFFECTS (a)) - return build2 (COMPOUND_EXPR, TREE_TYPE (result), a, result); + return cp_build_compound_expr (a, result, tf_warning_or_error); return result; } diff --git a/gcc/testsuite/g++.dg/cpp1z/elide8.C b/gcc/testsuite/g++.dg/cpp1z/elide8.C new file mode 100644 index 00000000000..7d471be8a2a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/elide8.C @@ -0,0 +1,25 @@ +// PR c++/110441 +// { dg-do compile { target c++11 } } + +struct immovable { + immovable(immovable &&) = delete; +}; + +struct A { + static immovable f(); +}; + +immovable f() { + immovable m = A().f(); // { dg-error "deleted" "" { target c++14_down } } + return A().f(); // { dg-error "deleted" "" { target c++14_down } } +} + +struct B { + A* operator->(); +}; + +immovable g() { + B b; + immovable m = b->f(); // { dg-error "deleted" "" { target c++14_down } } + return b->f(); // { dg-error "deleted" "" { target c++14_down } } +} -- 2.41.0.327.gaa9166bcc0