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 F30123858D37 for ; Thu, 6 Oct 2022 17:51:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F30123858D37 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=1665078710; 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: in-reply-to:in-reply-to:references:references; bh=ulCxQrpLA0dkxtVuFOFBeXKJQ3xKySU9q/jVlhtEG8c=; b=h6VkQlrwFja9yPOOZweXTrlioHWz4rSrIn68I/5bo9BbJ7KtrK8oAujVuWdE1n9whK657m 9Fpzibsfg1xjSpRAsC27FSM5cpcz56baWyr+aAueIU3s0ZIvV07z1VcKA92Sf9P38xdusj znF+lwgdnTpb7ciaOzq0E5PaPFyzsoY= 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-360-HVPAL-XYP--EsFYQiX_DXg-1; Thu, 06 Oct 2022 13:51:49 -0400 X-MC-Unique: HVPAL-XYP--EsFYQiX_DXg-1 Received: by mail-qv1-f69.google.com with SMTP id y14-20020a0cf14e000000b004afb3c6984bso1558810qvl.21 for ; Thu, 06 Oct 2022 10:51:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ulCxQrpLA0dkxtVuFOFBeXKJQ3xKySU9q/jVlhtEG8c=; b=6gXyCFYonuK1w5KgDYZojZEzckNECPRR+OqQpGQWp+qnqnP1vnpig3YVCcQnz2xe/o NxOfUqnqKc3Xe51b2tXOcxHRfU5BvdFEohco4ZCGUoMVNClS+TROT4w7A9ckMnfGJV2a oJ+yZiX1t8VlSEWyjqrbIwME9fUTlOjZpzVXFLGPEi+TGFqdC3YHfx/caBS6uvxAFi7U WGUwFkUXVMLba8m3rh2x3v5rXz0l9T0vX27dMBFRYf4bmTn8ZNVNJFU15gHuIWO3K7oc xPhV0HstOkU+i9p4tqNOVxMqUPD/O7SE5+o/HRdBVOw8ChB1XCpdleqnrShJUTXbfanI xgpQ== X-Gm-Message-State: ACrzQf1HwPEJslxnMA7c8e3aC0aZuHp7MSRV20B0YmLsPjOI/UmNMl8N gHi7mhKtUc8eL4IBTvMHKW9ZqOPhd6YIn76z456IzCXLpfg2pSXhGkshyCtyLD4t6xmrwU54xXu pp4RxxT/BEdV+aCCx6w== X-Received: by 2002:a37:b807:0:b0:6e1:aa54:fd89 with SMTP id i7-20020a37b807000000b006e1aa54fd89mr1015844qkf.203.1665078709172; Thu, 06 Oct 2022 10:51:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7XAq0o+7ylZls4ArEaWLoFensOBwAnAkF3iilInQDfctdJ5B4MO0liCtwco2+tv8y9mJG7oQ== X-Received: by 2002:a37:b807:0:b0:6e1:aa54:fd89 with SMTP id i7-20020a37b807000000b006e1aa54fd89mr1015826qkf.203.1665078708869; Thu, 06 Oct 2022 10:51:48 -0700 (PDT) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id l12-20020a05620a28cc00b006d1debe2637sm19422242qkp.11.2022.10.06.10.51.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 10:51:48 -0700 (PDT) Date: Thu, 6 Oct 2022 13:51:46 -0400 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: Re: [PATCH v2] c++: fixes for derived-to-base reference binding [PR107085] Message-ID: References: <20221005212744.640285-1-polacek@redhat.com> <72137424-81e9-108b-74bc-841a9ea39d2b@redhat.com> MIME-Version: 1.0 In-Reply-To: <72137424-81e9-108b-74bc-841a9ea39d2b@redhat.com> User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.1 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: On Thu, Oct 06, 2022 at 10:58:44AM -0400, Jason Merrill wrote: > 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. I see. So this would be achieved using e.g. struct B { }; struct D : B { }; struct C { D d; }; const B& b = C{}.d; Except I'm not sure how to trigger this via the built-in, which takes two types. Am I missing something obvious? Marek