From patchwork Wed Oct 5 21:27:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1748 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp18296wrs; Wed, 5 Oct 2022 14:28:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5NGwpl1SC0TQUCfpUjTyJT4eSHoR+iG+u6A3emKmAV2Zsqpra7VBy3Atu0UV5UKD3akaMc X-Received: by 2002:a17:907:1c8a:b0:782:1a0d:3373 with SMTP id nb10-20020a1709071c8a00b007821a0d3373mr1277632ejc.135.1665005315065; Wed, 05 Oct 2022 14:28:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665005315; cv=none; d=google.com; s=arc-20160816; b=ixY/LXoJhzg8QOd/Kmy4+Pv/tj6j90Y4LrqSbQ+Q0LvItd8i6V3w5Ia3EUqnSGx9nn 5RriRblBDBdT/P239szeoARSVuCvkr7Jgy7sN9BWeJ8csls01/SIJq8jKkCefGNARFEx yCr5/7ooI6yFSuWL2iMygkNXKxGMZah3GJPawlEBJCItpMrxAhkRiU6G0sEqC/u2Eoxg dtKM8rDuEAfV9OSbGIPegBo9P2/3ATnyV2L6iF43FMtnd55zf4SeXgZJUY5NBelfoIx3 /vJz0f3lTtbIXdsZU2k73xfV6c680hM1AoOQfvWfTzhKsh0nAp7OF1e3snR3sTXilq8I 6Yww== 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=44fyW6lbCxbulXXg3B+EPF5HJs//2kGDKlO5BbRDvUo=; b=EuH0IfSuGRYt9gb2UeNqd9CovcVIy3dQQPwEFbLTAagY6t9FMdNQYeXaVM5z5hA6ZI 81thYrJ1z7SOkuzFSoBjhkPvnJI6+VnpRFzWiB1/11f6IvRW6aKPpp3xsvxvj6mlRCfy GuOxQxCVDIAxrmA6tg5ggShNfEHZFfMMaCWhk8fTuvu3aXWkAZsV7ccrHcBKqxdaf4ZT TfT5o/kQvFUC4r3UQil6qj4czPvqu0lg+S5ILMW5fKcYbUU15s1WcYUKPJC/rkeyEbe/ Y6KAxYBC1Rux9sfrSwLv4UcyfZX9efYIXhOaD4bCKD6SFmkN/2mp8/7903JZCjfiEkBI Zg+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=wCc4VcSG; 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 hq36-20020a1709073f2400b0072afc15af40si18433333ejc.39.2022.10.05.14.28.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 14:28:35 -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=wCc4VcSG; 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 CE8543857415 for ; Wed, 5 Oct 2022 21:28:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CE8543857415 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665005313; bh=44fyW6lbCxbulXXg3B+EPF5HJs//2kGDKlO5BbRDvUo=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wCc4VcSG4Zh/v39P0gQvzYuuA0pieMPCaaD1M/qajwl0woddvT/z1fyHPLDe9abXH kUpVdI8+dsSYhYrxmeElXRiiU1N3n60oqKDuCFW98MR7PSOTtQb8IMY3i47CL6gNm0 96wF+MOO9XGvpvCo+w+rgv0kts6hAVfkfBU7hrf4= 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 C9FC53857C7C for ; Wed, 5 Oct 2022 21:27:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C9FC53857C7C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-342-KBPcLiGYMlWzDTFLvTO69A-1; Wed, 05 Oct 2022 17:27:49 -0400 X-MC-Unique: KBPcLiGYMlWzDTFLvTO69A-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F27B985A5A6 for ; Wed, 5 Oct 2022 21:27:48 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.10.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id CF0F1140EBF3; Wed, 5 Oct 2022 21:27:48 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: fixes for derived-to-base reference binding [PR107085] Date: Wed, 5 Oct 2022 17:27:44 -0400 Message-Id: <20221005212744.640285-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 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 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek 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?1745884612911159379?= X-GMAIL-MSGID: =?utf-8?q?1745884612911159379?= This PR reports that struct Base {}; struct Derived : Base {}; static_assert(__reference_constructs_from_temporary(Base const&, Derived)); doesn't pass, which it should: it's just like const Base& b(Derived{}); where we bind 'b' to the Base subobject of a temporary object of type Derived. The ck_base conversion didn't have ->need_temporary_p set because we didn't need to create a temporary object just for the base, but the whole object is a temporary so we're still binding to a temporary. Fixed by the conv_is_prvalue hunk. That broke a bunch of tests. I've distilled the issue into a simple test in elision4.C. Essentially, we have struct B { /* ... */ }; struct D : B { }; B b = D(); and we set force_elide in build_over_call, but we're unable to actually elide the B::B(B&&) call, and crash on gcc_assert (!force_elide);. says that copy elision "can only apply when the object being initialized is known not to be a potentially-overlapping subobject". So I suppose we shouldn't force_elide the B::B(B&&) call. I don't belive the CWG 2327 code was added to handle derived-to-base conversions, at that time conv_binds_ref_to_prvalue wasn't checking ck_base at all. Does that make sense? If so... Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/107085 gcc/cp/ChangeLog: * call.cc (conv_is_prvalue): Return true if the base subobject is part of a temporary object. (build_over_call): Don't force_elide when it's a derived-to-base conversion. gcc/testsuite/ChangeLog: * g++.dg/ext/reference_constructs_from_temporary1.C: Adjust expected result. * g++.dg/ext/reference_converts_from_temporary1.C: Likewise. * g++.dg/cpp0x/elision4.C: New test. --- gcc/cp/call.cc | 17 +++++++++++++++-- gcc/testsuite/g++.dg/cpp0x/elision4.C | 17 +++++++++++++++++ .../ext/reference_constructs_from_temporary1.C | 2 +- .../ext/reference_converts_from_temporary1.C | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/elision4.C base-commit: e99dcbb54e07b798c3353124f38336f96a826d43 diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index bd04a1d309a..15e969d6429 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9186,7 +9186,11 @@ conv_is_prvalue (conversion *c) { if (c->kind == ck_rvalue) return true; - if (c->kind == ck_base && c->need_temporary_p) + if (c->kind == ck_base + /* We may not need a temporary object for the base itself, but if it + is the base subobject of a temporary object, we're still dealing + with a prvalue. */ + && (c->need_temporary_p || conv_is_prvalue (next_conversion (c)))) return true; if (c->kind == ck_user && !TYPE_REF_P (c->type)) return true; @@ -9417,7 +9421,16 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) && (DECL_COPY_CONSTRUCTOR_P (fn) || DECL_MOVE_CONSTRUCTOR_P (fn)) && !unsafe_return_slot_p (first_arg) - && conv_binds_ref_to_prvalue (convs[0])) + && conv_binds_ref_to_prvalue (convs[0]) + /* Converting a class to another class that then binds to this + copy/move constructor's argument is OK, but not when it's a + derived-to-base conversion. */ + && [convs] { + for (conversion *t = convs[0]; t; t = next_conversion (t)) + if (t->kind == ck_base) + return false; + return true; + }()) { force_elide = true; goto not_really_used; diff --git a/gcc/testsuite/g++.dg/cpp0x/elision4.C b/gcc/testsuite/g++.dg/cpp0x/elision4.C new file mode 100644 index 00000000000..4833b50d48e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/elision4.C @@ -0,0 +1,17 @@ +// PR c++/107085 +// { dg-do compile { target c++11 } } + +// Must be non-trivial to exhibit the ICE. +struct X { + X(); + X(X&&); +}; +struct Z : X {}; +X x1 = Z(); +X x2 = X(Z()); + +// ...but let's try the trivial path in build_over_call as well. +struct B { }; +struct D : B { }; +B b1 = D(); +B b2 = B(D()); diff --git a/gcc/testsuite/g++.dg/ext/reference_constructs_from_temporary1.C b/gcc/testsuite/g++.dg/ext/reference_constructs_from_temporary1.C index 76de905a35d..5354b1dc4e6 100644 --- a/gcc/testsuite/g++.dg/ext/reference_constructs_from_temporary1.C +++ b/gcc/testsuite/g++.dg/ext/reference_constructs_from_temporary1.C @@ -201,7 +201,7 @@ SA(!__reference_constructs_from_temporary(const int&, H)); SA(!__reference_constructs_from_temporary(int&&, G2)); SA(!__reference_constructs_from_temporary(const int&, H2)); -SA(!__reference_constructs_from_temporary(const Base&, Der)); +SA(__reference_constructs_from_temporary(const Base&, Der)); // This fails because std::is_constructible_v> is false. SA(!__reference_constructs_from_temporary(int&&, id)); diff --git a/gcc/testsuite/g++.dg/ext/reference_converts_from_temporary1.C b/gcc/testsuite/g++.dg/ext/reference_converts_from_temporary1.C index 90196c38742..e6c159e9b00 100644 --- a/gcc/testsuite/g++.dg/ext/reference_converts_from_temporary1.C +++ b/gcc/testsuite/g++.dg/ext/reference_converts_from_temporary1.C @@ -201,7 +201,7 @@ SA( __reference_converts_from_temporary(const int&, H)); SA(!__reference_converts_from_temporary(int&&, G2)); SA(!__reference_converts_from_temporary(const int&, H2)); -SA(!__reference_converts_from_temporary(const Base&, Der)); +SA(__reference_converts_from_temporary(const Base&, Der)); // This fails because std::is_constructible_v> is false. SA(!__reference_converts_from_temporary(int&&, id));