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 7995538515C4 for ; Thu, 25 Aug 2022 21:03:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7995538515C4 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=1661461426; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=iLJGDGykUoJp6ozPcgOpG37V6tp4MU+h29rT7eFSAmc=; b=gutsJHqXmqbDoKPKcFhCoinbRZ9KrZiVLu5lGIoQ974+zwg8jp+mLWgBx8dRxujghh9z/c 5BpSWZStzzA5MPsJCijRDX/tGmnw+LiyJbSt9M7UOjtoGGlSils0ifkkDGaGuPfFX0H8r0 Xc7lhXPmouAp6Zq1YDPePFyxxTGEovQ= 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-Google-Smtp-Source: AA6agR5tiKZsAZc41HJ7yu0uFB1k4xl7GmYAIHkkFClScl6zo17wp/oaMMO5klPqAQT/bKSkran+mQ== 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) From: Jason Merrill 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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(); +}; base-commit: 30e160475489867a09ed89532cae135b5849cf98 -- 2.31.1