From patchwork Thu Aug 25 21:03:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 772 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp421800wro; Thu, 25 Aug 2022 14:04:31 -0700 (PDT) X-Google-Smtp-Source: AA6agR4yV5RQC/A8xD2TzzU+OsDNtAjxzTf7yJNWo9mtI/LIgsMCh32xfluGt0S2W2vVWLkl+5bZ X-Received: by 2002:a05:6402:241e:b0:443:be9:83c0 with SMTP id t30-20020a056402241e00b004430be983c0mr4655972eda.24.1661461470958; Thu, 25 Aug 2022 14:04:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661461470; cv=none; d=google.com; s=arc-20160816; b=obpcMl01R2fBz8u6IPqTHyHDtJb99SlSeg7IXBdOFrITdJ7tH6Sr9sCyUKGVcfqI3H e1iZACz543snBqGck2gakeHpAS8rBKwEUD06eCeoIlud2BRfyLRoBkMdKCMkTt25FCPJ ArHTJLwWMRp68YH6onfaoxjCdzFV6cOEPmjDLo8BykkQTP3akwdUgoWbXphK9UXxIxDC qanBcwaveTMVAW8eXUs2356Ef96CqURVc4HtAEp9vIj2umQ1IBufsUuH/G56O1Xm/9Dd zIiyF7KdIaAbeZ0Rm/NezgtHtqbcayQxGIQg4EeWJUCFKr3PfOVcblCP0tf8XLsSsR0N Od6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=iLJGDGykUoJp6ozPcgOpG37V6tp4MU+h29rT7eFSAmc=; b=S1j5GPPS7MO11SmZTtrBbP/UcgsjgCPzwhQBqgRAgzqvppGOptvvQdNbmav/pCIi74 zTQKG65XQbA9zlErq8Ch1/8HBBYN4ym7P9L7uSFHxT/HyaB5MSLvpnfDXDZ/ETG3aO9L fYL/GuGzHuhF1DvWUmyJUq2VfvDt2sgPruNbv4BAMQwLLKD2BZC0M1Xc2eZHI53/SCNQ 8E2UJYz3DVs7Fw2/GRJcANSjxpcVo7kHinshOrFAVLbhhDhSjG+V1Qx6Mx5yT06i5JHS tZMTti84BYeQzUbczZ5sUBDIvp7xmNnL4Zsl3vvJ/Pj14D4xa5S3OBo4NPh6UhMYj8GQ LpZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Q8nHXQbH; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id g17-20020a1709065d1100b00730fdb36019si148214ejt.21.2022.08.25.14.04.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 14:04:30 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Q8nHXQbH; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BA4233851A8A for ; Thu, 25 Aug 2022 21:04:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BA4233851A8A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661461469; bh=iLJGDGykUoJp6ozPcgOpG37V6tp4MU+h29rT7eFSAmc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Q8nHXQbHcGAysS8pMid/DLSiNCJdMLf7hoWyNchCjWIpglaWusLeP5qL4Tu0133dD XKaHoeyDkS7DBZ2+3CRIzVXkE0cuXo7lYfP5u10DN67mc0AiDedIlC7nh847oprl83 PtcbSqwBRjNFPgvh9ZMO+JY20UFfp/Rb4t+p1g1A= 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 7995538515C4 for ; Thu, 25 Aug 2022 21:03:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7995538515C4 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-70-nbV0TjhKO9aTvCw0uivZEg-1; Thu, 25 Aug 2022 17:03:45 -0400 X-MC-Unique: nbV0TjhKO9aTvCw0uivZEg-1 Received: by mail-qv1-f69.google.com with SMTP id o16-20020a0cecd0000000b0049656c32564so12560060qvq.19 for ; Thu, 25 Aug 2022 14:03:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc; bh=iLJGDGykUoJp6ozPcgOpG37V6tp4MU+h29rT7eFSAmc=; b=79Phh2G+PsYrazaYX6sxnyU3H7tMfzmM9Pe6Uoh/Aq8qhH0DC4EWMPwsBLXzcdg5fs sgNVhGVCyBGZ5TRoXHnDXzD1UNUYfRFK+GJ2bRDqhT7X0U9oulseR8zjLtPF73K9BlKq 9VgUCLyC8XCQdC6hO8hTutitee4eN+3qlQNZ6X/i8saRe8KzesNPb6D69houPEPppGQT LPMDC8umY8VfMmUfixAUqrimN1N3RXRfPCEhuxUuXpivFAX6r9y0nAOL/LjGQpPODpEX kUeMpsJFgEyHXObzfYfOLgn9gVXM8NNNwgi4pxmQzOl8o3LWdkUFcBqgy2rIqtc5KdW3 PxTQ== X-Gm-Message-State: ACgBeo1rvzWHdP/wKRSW5REKpVVfhygsfqDnPOw2R1Wrp+5h+ha5ASMe q+Nng1QdbBTrCp4dFislQUGZNgywoKfpMjSe//EeWTxpUDKJ/FTvEPCpQYPvgRQF/XayQajGse0 5bA+JZyR3dfM9OY9zcX7sFX1y1Xq4mUF8yM2OUkBjVApNKHxQd4LA7Rjj5gMycZ4vEQ== X-Received: by 2002:ac8:578c:0:b0:344:9333:7dee with SMTP id v12-20020ac8578c000000b0034493337deemr5336929qta.177.1661461424242; Thu, 25 Aug 2022 14:03:44 -0700 (PDT) X-Received: by 2002:ac8:578c:0:b0:344:9333:7dee with SMTP id v12-20020ac8578c000000b0034493337deemr5336881qta.177.1661461423827; Thu, 25 Aug 2022 14:03:43 -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 11-20020ac8574b000000b003435bb7fe9csm16107737qtx.78.2022.08.25.14.03.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Aug 2022 14:03:43 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: block copy elision in delegating ctor Date: Thu, 25 Aug 2022 17:03:41 -0400 Message-Id: <20220825210341.2217843-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.9 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742168623339202075?= X-GMAIL-MSGID: =?utf-8?q?1742168623339202075?= CWG2403 deals with the issue that copy elision is not possible when the initialized object is a potentially-overlapping subobject and the initializer is a function that returns by value. Jonathan pointed out that this also affects delegating constructors, which might be used to construct a base subobject. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * call.cc (unsafe_return_slot_p): Return 2 for *this in a constructor. gcc/testsuite/ChangeLog: * g++.dg/init/elide8.C: New test. --- gcc/cp/call.cc | 7 +++++++ gcc/testsuite/g++.dg/init/elide8.C | 11 +++++++++++ 2 files changed, 18 insertions(+) create mode 100644 gcc/testsuite/g++.dg/init/elide8.C base-commit: 30e160475489867a09ed89532cae135b5849cf98 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index 370137ebd6d..d107a2814dc 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9022,6 +9022,13 @@ unsafe_return_slot_p (tree t) if (is_empty_base_ref (t)) return 2; + /* A delegating constructor might be used to initialize a base. */ + if (current_function_decl + && DECL_CONSTRUCTOR_P (current_function_decl) + && (t == current_class_ref + || tree_strip_nop_conversions (t) == current_class_ptr)) + return 2; + STRIP_NOPS (t); if (TREE_CODE (t) == ADDR_EXPR) t = TREE_OPERAND (t, 0); diff --git a/gcc/testsuite/g++.dg/init/elide8.C b/gcc/testsuite/g++.dg/init/elide8.C new file mode 100644 index 00000000000..31f899b6623 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/elide8.C @@ -0,0 +1,11 @@ +// CWG 2403 case 3: we can't elide this copy because the delegating constructor +// might be used to initialize a base. +// { dg-do compile { target c++11 } } + +struct Noncopyable { + Noncopyable() = default; + Noncopyable(const Noncopyable &) = delete; + Noncopyable(int) : Noncopyable(make()) {} // { dg-error "deleted" } + + static Noncopyable make(); +};