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 C9FC53857C7C for ; Wed, 5 Oct 2022 21:27:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C9FC53857C7C 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=1665005270; 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=44fyW6lbCxbulXXg3B+EPF5HJs//2kGDKlO5BbRDvUo=; b=T7exmt/0fhhLHSaWNCEn9ZxNPxA6a05ETZ+o+aeekoCH0jvK771G5qvcvPEw9eTMY4y6yF tcXjuA6ARLkJf5vP8mpWst0+QHdYCS2iZKvblIdtVwQbXSQjfc+U6yo9X+ygp55LzAFUO/ kNu0NjDOOWukPxbsxP30eb7Thx7p0EE= 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) From: Marek Polacek 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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 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)); base-commit: e99dcbb54e07b798c3353124f38336f96a826d43 -- 2.37.3