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.129.124]) by sourceware.org (Postfix) with ESMTPS id DEFAE3898380 for ; Thu, 6 Oct 2022 14:58:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DEFAE3898380 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=1665068330; 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: in-reply-to:in-reply-to:references:references; bh=1A3JzcO6VGlqUXHZCZc9U6aUk+tMRRbywvhvoFxm/2Y=; b=R1T8WnmVRdTc/ogmYKqVFGcfctD9+m8oFdnrzri1kRc2oTrUydDba6qz8w09ruENRGIpii Yumx+YrurAP64Owa/VXISa1emnaR9/Gce6nm0xpLfTHxXHh+/srCvoetwiSgPoZ/AesC4N QSAJehvelC1aR52a72m2YoszHrjfgsM= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-219-1hryAxheOemhTO8j90ao5g-1; Thu, 06 Oct 2022 10:58:47 -0400 X-MC-Unique: 1hryAxheOemhTO8j90ao5g-1 Received: by mail-qk1-f197.google.com with SMTP id d18-20020a05620a241200b006ce80a4d74aso1629111qkn.6 for ; Thu, 06 Oct 2022 07:58:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=1A3JzcO6VGlqUXHZCZc9U6aUk+tMRRbywvhvoFxm/2Y=; b=YfnCmKNIjBj6F3Z78IioRtNe17aZHJbZiq+R3osTL8uXtXYXGHZJdQFXidvjpfwbe9 X2ldvyAAfDOz5uotYhh7lUeUCL0lU33dKkNHYvIJ2vTE3aSD8qfaiEb96j+3KhUuqBiv UNJHJA+HpiSjOM2tP/iTgpEADBpWO923jgr3DlbjB6rlsaveoGKg9XFFx/xTSg47L24x riVo+HyyAbykurPuq3XJT1D/xEYY3/d2BaVHGVSw9HHuvVf48D55rVhLuPlODCCwQtik WutAB0r5DWdFHjKIg7a7UWjegk8OgGD0d0QqfdVHSRMvEKmT+b7uYFJJN/hTyt6bOq7M 3SHw== X-Gm-Message-State: ACrzQf057UDmXwkVLHyn1vGL8N7Vi1PRXIwzb5JRg3Zhj1geumkzHcpX tSFicrgPYe0ScainyUfgGAXb0GM/NfNi2lN75Ogf/3ZrkScgzxzLRcI5nQpaZlB2tGPAFehLJb2 kiiTuVYeecjP4V+QeBA== X-Received: by 2002:a05:6214:509a:b0:4b1:b089:366e with SMTP id kk26-20020a056214509a00b004b1b089366emr378068qvb.99.1665068326939; Thu, 06 Oct 2022 07:58:46 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5ipu3Et6JpKwjCiI33qSsAYgch8C4FfP1t4gibM0+p/s1uVuXHnaKbzGbBxxBWt6vRf/XQNg== X-Received: by 2002:a05:6214:509a:b0:4b1:b089:366e with SMTP id kk26-20020a056214509a00b004b1b089366emr378037qvb.99.1665068326600; Thu, 06 Oct 2022 07:58:46 -0700 (PDT) Received: from [192.168.1.101] (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 i7-20020ac85c07000000b0035d420c4ba7sm17768278qti.54.2022.10.06.07.58.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 06 Oct 2022 07:58:45 -0700 (PDT) Message-ID: <72137424-81e9-108b-74bc-841a9ea39d2b@redhat.com> Date: Thu, 6 Oct 2022 10:58:44 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.1 Subject: Re: [PATCH v2] c++: fixes for derived-to-base reference binding [PR107085] To: Marek Polacek Cc: GCC Patches References: <20221005212744.640285-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,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: On 10/6/22 10:49, Marek Polacek wrote: > On Wed, Oct 05, 2022 at 08:25:29PM -0400, Jason Merrill wrote: >> On 10/5/22 17:27, Marek Polacek wrote: >>> 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. >> >> No, the base subobject of a prvalue is an xvalue. > > Ah, so this is just like T().m where T() is a prvalue but the whole thing > is an xvalue. Duly noted. Exactly. >> I think the problem is that an expression being a prvalue is a subset of >> binding a reference to a temporary, and we shouldn't try to express both of >> those using the same function: you need a separate >> conv_binds_ref_to_temporary. > > Ack, so how about this? Thanks, > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > -- >8 -- > 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. Since the > Base subobject is an xvalue, a new function is introduced. > > PR c++/107085 > > gcc/cp/ChangeLog: > > * call.cc (conv_binds_ref_to_temporary): New. > (ref_conv_binds_directly): Use it. > > 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 | 23 ++++++++++++++++++- > gcc/testsuite/g++.dg/cpp0x/elision4.C | 15 ++++++++++++ > .../reference_constructs_from_temporary1.C | 2 +- > .../ext/reference_converts_from_temporary1.C | 2 +- > 4 files changed, 39 insertions(+), 3 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..715a83f5a69 100644 > --- a/gcc/cp/call.cc > +++ b/gcc/cp/call.cc > @@ -9210,6 +9210,27 @@ conv_binds_ref_to_prvalue (conversion *c) > return conv_is_prvalue (next_conversion (c)); > } > > +/* True iff C is a conversion that binds a reference to a temporary. > + This is a superset of conv_binds_ref_to_prvalue: here we're also > + interested in xvalues. */ > + > +static bool > +conv_binds_ref_to_temporary (conversion *c) > +{ > + if (conv_binds_ref_to_prvalue (c)) > + return true; > + if (c->kind != ck_ref_bind) > + return false; > + c = next_conversion (c); > + /* This is the case for > + struct Base {}; > + struct Derived : Base {}; > + const Base& b(Derived{}); > + where we bind 'b' to the Base subobject of a temporary object of type > + Derived. The subobject is an xvalue; the whole object is a prvalue. */ > + return (c->kind == ck_base && conv_is_prvalue (next_conversion (c))); I think you also want to check for the case of c->u.expr being a COMPONENT_REF/ARRAY_REF around a TARGET_EXPR, as you mentioned. > +} > + > /* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE does > not involve creating a temporary. Return tristate::TS_FALSE if converting > EXPR to a reference type TYPE binds the reference to a temporary. If the > @@ -9230,7 +9251,7 @@ ref_conv_binds_directly (tree type, tree expr, bool direct_init_p /*= false*/) > /*c_cast_p=*/false, flags, tf_none); > tristate ret (tristate::TS_UNKNOWN); > if (conv && !conv->bad_p) > - ret = tristate (!conv_binds_ref_to_prvalue (conv)); > + ret = tristate (!conv_binds_ref_to_temporary (conv)); > > /* Free all the conversions we allocated. */ > obstack_free (&conversion_obstack, p); > diff --git a/gcc/testsuite/g++.dg/cpp0x/elision4.C b/gcc/testsuite/g++.dg/cpp0x/elision4.C > new file mode 100644 > index 00000000000..3cc2e3afa5d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp0x/elision4.C > @@ -0,0 +1,15 @@ > +// PR c++/107085 > +// { dg-do compile { target c++11 } } > + > +struct X { > + X(); > + X(X&&); > +}; > +struct Z : X {}; > +X x1 = Z(); > +X x2 = X(Z()); > + > +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: 3ec926d36fbf7cb3ff45759471139f3a71d1c4de