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 1B39B3858D37 for ; Thu, 6 Oct 2022 18:00:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1B39B3858D37 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=1665079244; 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=ZDPYsIsc2PKtHwpdJdbyBB3zK2GqTrcjtsChhiFfnVo=; b=Alyp+2V/hgeVZ+2+jSXCxK3OT55Yq0N3sPSTS/5a9oMOpiDPb3Q2lwreUKFnXroj4sMPQO SatKEb4xddXiOIoX3Q4Gq7YpsPLwCSXP3zqyMFxjxCGcsxcG+cRaz6Vvumkq8PLA0cVBiK 7Mr7uVbsw7Xi1uJ3BlmaTyEiw83y/C0= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-504-SLCr3znVP1KaysTbw0V2bA-1; Thu, 06 Oct 2022 14:00:43 -0400 X-MC-Unique: SLCr3znVP1KaysTbw0V2bA-1 Received: by mail-qk1-f198.google.com with SMTP id n13-20020a05620a294d00b006cf933c40feso2049247qkp.20 for ; Thu, 06 Oct 2022 11:00:43 -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=ZDPYsIsc2PKtHwpdJdbyBB3zK2GqTrcjtsChhiFfnVo=; b=gOXYKjTV3iRRpez6P9oarKNudjI4uN8pLj/15L0OcOH0h9rMYNOoBff4boz8wUZn6t mF4zEUOB+2acWAf2m/3OZ2/iqw6RFndBvAucUqdeIZp1s2s/tFGXiehSqRYkuvMSQjC+ jjKmGLJGOgvidumEyrTXNm/JY9ZPqKZS/vl/kLzotVrV1fuCJzAFFYiHCehUAaN1+14i /8U+iP/F2w5smQiGhwEq+AaU4ASQlMXUX0q6aw8MNaoy1hEB3+VSVQnUkHc+6FJQW1AB F39A9oRTCVwzWAjZJXvzrm7JqVQ3XS2KM3AwSkQj467rY2F0zcw7li2BG3zDkZkunqji haQA== X-Gm-Message-State: ACrzQf1pYUz8oXV2iqzWYr7j0g6JFT48DZTYZAiI8r3JO8o14xSunceo zvgDCIa5RLWHiN5EyotY/1AghlM5XkacbPZd9XnEqkUCJfOqsPtxVc13jaQKH0al5nIva68bbqy fi9oWmtRjyC9MBkXwpw== X-Received: by 2002:a05:622a:1883:b0:35b:b7fc:24ac with SMTP id v3-20020a05622a188300b0035bb7fc24acmr1239289qtc.182.1665079242923; Thu, 06 Oct 2022 11:00:42 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6UqUzbH+Ssw6vaOuPLwDZwS14lFacPuoWoB+S/RgKoUgAnVXEeIQr9tyQLTSe/xWtLOKRWpA== X-Received: by 2002:a05:622a:1883:b0:35b:b7fc:24ac with SMTP id v3-20020a05622a188300b0035bb7fc24acmr1239246qtc.182.1665079242495; Thu, 06 Oct 2022 11:00:42 -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 bj12-20020a05620a190c00b006cdd0939ffbsm20219157qkb.86.2022.10.06.11.00.41 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 06 Oct 2022 11:00:41 -0700 (PDT) Message-ID: <77265139-4274-e922-62c4-2b619fa0a4b4@redhat.com> Date: Thu, 6 Oct 2022 14:00:40 -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> <72137424-81e9-108b-74bc-841a9ea39d2b@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 13:51, Marek Polacek wrote: > 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; Yes. > Except I'm not sure how to trigger this via the built-in, which takes two types. > Am I missing something obvious? Indeed, it can't be triggered by the built-in. But I see ref_conv_binds_directly is also called from warn_for_range_copy, which ought to be able to trigger it. Incidentally, ref_conv_binds_directly should also probably be reversed to ref_conv_binds_to_temporary since you can "bind directly" to an xvalue that refers to a temporary. Jason