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 230D23851C05 for ; Thu, 6 Oct 2022 00:25:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 230D23851C05 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=1665015933; 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: in-reply-to:in-reply-to:references:references; bh=n4+B60Rul0y+kMnNSKUw8xqzleT+MuAdrm1GC60Dr64=; b=bJ+ioY/Gu9n8sa6KUYw8VfL9wLtmAKwG/6HaLtv+3Brf5B9bVWP5a3V5pU6S0mQqZnbTw6 Cn3xdPWaSGA8oBnUxCtDFccV9soPj1W2EhMhqgIoM697IhUnpWlB9kzh+7+fUc/Uxz0+sf OUKS0Kp56/JBdn3/zDa9TP7McmFGU5Q= 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-378-Qj7vZ1MJOxCiEbPUEQkJXQ-1; Wed, 05 Oct 2022 20:25:32 -0400 X-MC-Unique: Qj7vZ1MJOxCiEbPUEQkJXQ-1 Received: by mail-qk1-f198.google.com with SMTP id k2-20020a05620a414200b006ceec443c8bso188951qko.14 for ; Wed, 05 Oct 2022 17:25:31 -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:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=n4+B60Rul0y+kMnNSKUw8xqzleT+MuAdrm1GC60Dr64=; b=hwKcNuPnQu7z+J+/0cA41JaUOpnzkkuNnQj1DZ+hRgZbDzDAqE7iB1jrIfF6YrqxzP 8LNQQ2Rz4BvHv8+7vNBN1Evx+MAH3Zm8GWef1h/wnvIk7VqwbTq/VaoM/zn+9kpxeLS7 7JHKMnGuNJrHedJQUqLpbkqPUCWVVFZ55VBV2kOyYj7f/ZYuUNMtAO5Ynx9faGh67JeR MplTwJ8s3R5C7VGhOwqvmGfSznZZ3DdWQySCYiGUyTQtgQCOy04L5LL9ZFGnSpZLc9MU Ne443SxxOdE9c1RIHHAx6sTAPMviWz365Flp6ofCnRNMHxf+yy3b+13BC6ufmXWjbu1U 2Zxg== X-Gm-Message-State: ACrzQf2sRUj8rDLsXZlSXSaXT7boI7aOCKxMLP7rBDltLNskq+TmxvJh PwfZ5CnFy/JNWBPS6F1LjXPwiShD+1LwUCji9PFv2WkmSDS4mEOW/NYty5PNFQG5ORzQ+s8q2dN N5AT4DM1dxQoIJ5i+RQ== X-Received: by 2002:a05:620a:1192:b0:6ce:634f:b294 with SMTP id b18-20020a05620a119200b006ce634fb294mr1538230qkk.65.1665015930788; Wed, 05 Oct 2022 17:25:30 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5VqTIa68rP9vquSvP1jfouwGMIDNZk7OFBJ3mrqujzfmCU0zo1xqZ6VgvKTmcRIZ1vGo210w== X-Received: by 2002:a05:620a:1192:b0:6ce:634f:b294 with SMTP id b18-20020a05620a119200b006ce634fb294mr1538212qkk.65.1665015930374; Wed, 05 Oct 2022 17:25:30 -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 u22-20020a05620a431600b006af1f0af045sm19539702qko.107.2022.10.05.17.25.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Oct 2022 17:25:29 -0700 (PDT) Message-ID: Date: Wed, 5 Oct 2022 20:25:29 -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] c++: fixes for derived-to-base reference binding [PR107085] To: Marek Polacek , GCC Patches References: <20221005212744.640285-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20221005212744.640285-1-polacek@redhat.com> 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/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. 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. > (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