From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 111221 invoked by alias); 16 Dec 2019 20:55:56 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 111184 invoked by uid 89); 16 Dec 2019 20:55:56 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.3 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_2,GIT_PATCH_3,KAM_SHORT autolearn=ham version=3.3.1 spammy=H*Ad:U*jason X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 16 Dec 2019 20:55:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1576529753; 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=CUZW2EFbTgWS+lfeSbFvs0N3ibA/N0UXtqRp6Z9koxI=; b=ASAdNnnShDrGTB/FgmSMsMwXtIekreVLWcJqHvPIbArA+whrSWvlx51cuNOCHFiFvWqI7P foxJIcuk3DdB88zvLZzfTuANBpWk6wK1wBlJZ+zAEMtGUOP021tdjAHp3F4wyqYlLabPcD KWh9a+ri0xwkAbj9b8OtQqKc/9rkyCY= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-42-HwcRjXYjNga4ers869PVYQ-1; Mon, 16 Dec 2019 15:55:49 -0500 Received: by mail-qv1-f70.google.com with SMTP id a14so2279536qvy.23 for ; Mon, 16 Dec 2019 12:55:48 -0800 (PST) Return-Path: Received: from [192.168.1.148] (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id i90sm4210365qtd.49.2019.12.16.12.55.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Dec 2019 12:55:46 -0800 (PST) Subject: Re: C++ PATCH for c++/88337 - Implement P1327R1: Allow dynamic_cast in constexpr To: Marek Polacek Cc: GCC Patches References: <20191108212449.GZ21634@redhat.com> <20191211224915.GH1118066@redhat.com> <43a2c2bc-3e2e-7624-72cd-0765de785c4c@redhat.com> <20191213202054.GB2968@redhat.com> <20191214212522.GD2968@redhat.com> From: Jason Merrill Message-ID: Date: Mon, 16 Dec 2019 21:00:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.3.0 MIME-Version: 1.0 In-Reply-To: <20191214212522.GD2968@redhat.com> X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-IsSubscribed: yes X-SW-Source: 2019-12/txt/msg01143.txt.bz2 On 12/14/19 4:25 PM, Marek Polacek wrote: > On Fri, Dec 13, 2019 at 05:56:57PM -0500, Jason Merrill wrote: >> On 12/13/19 3:20 PM, Marek Polacek wrote: >>> + /* Given dynamic_cast(v), >>> + >>> + [expr.dynamic.cast] If C is the class type to which T points or refers, >>> + the runtime check logically executes as follows: >>> + >>> + If, in the most derived object pointed (referred) to by v, v points >>> + (refers) to a public base class subobject of a C object, and if only >>> + one object of type C is derived from the subobject pointed (referred) >>> + to by v the result points (refers) to that C object. >>> + >>> + In this case, HINT >= 0. */ >>> + if (hint >= 0) >> >> Won't this code work for hint == -3 as well? > > Yes, it does. In fact, none of the tests was testing the hint == -3 case, so > I've fixed the code up and added constexpr-dynamic15.C to test it. > >>> + { >>> + /* Look for a component with type TYPE. */ >>> + obj = get_component_with_type (obj, type); >> >> You don't seem to use mdtype at all in this case. Shouldn't >> get_component_with_type stop at mdtype if it hasn't found type yet? > > It was used for diagnostics but not in get_component_with_type. It makes > sense to stop at MDTYPE; I've adjusted the code to do so. E.g., if > we have OBJ in the form of g.D.2121.D.2122.D.2123.D.2124, usually the > component with the most derived type is "g", but in a 'tor, it can be > a different component too. > >>> + /* If not found or not accessible, give an error. */ >>> + if (obj == NULL_TREE || obj == error_mark_node) >>> + { >>> + if (reference_p) >>> + { >>> + if (!ctx->quiet) >>> + { >>> + error_at (loc, "reference % failed"); >>> + if (obj == NULL_TREE) >>> + inform (loc, "dynamic type %qT of its operand does not " >>> + "have an unambiguous public base class %qT", >>> + mdtype, type); >>> + else >>> + inform (loc, "static type %qT of its operand is a " >>> + "non-public base class of dynamic type %qT", >>> + objtype, type); >>> + >>> + } >>> + *non_constant_p = true; >>> + } >>> + return integer_zero_node; >>> + } >>> + else >>> + /* The result points to the TYPE object. */ >>> + return cp_build_addr_expr (obj, complain); >>> + } >>> + /* Otherwise, if v points (refers) to a public base class subobject of the >>> + most derived object, and the type of the most derived object has a base >>> + class, of type C, that is unambiguous and public, the result points >>> + (refers) to the C subobject of the most derived object. >>> + >>> + But it can also be an invalid case. */ >> >> And I think we need to fall through to this code if the hint turns out to be >> wrong, i.e. V is a public base of C, but v is not that subobject, but rather >> a sibling base of C, like > > True. HINT is really just an optimization hint, nothing more. I've adjusted > the code to fall through to the normal processing if the HINT >= 0 or -3 case > doesn't succeed. > >> struct A { virtual void f(); }; >> struct B1: A { }; >> struct B2: A { }; >> struct C: B1, B2 { }; >> int main() >> { >> C c; >> A* ap = (B1*)c; >> constexpr auto p = dynamic_cast(ap); // should succeed >> } > > Whew, there's always One More Case. :/ New constexpr-dynamic16.c covers it. > >>> --- /dev/null >>> +++ gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic11.C >>> @@ -0,0 +1,34 @@ >>> +// PR c++/88337 - Implement P1327R1: Allow dynamic_cast/typeid in constexpr. >>> +// { dg-do compile { target c++2a } } >>> + >>> +// dynamic_cast in a constructor. >>> + >>> +struct V { >>> + virtual void f(); >>> +}; >>> + >>> +struct A : V { }; >>> + >>> +struct B : V { >>> + constexpr B(V*, A*); >>> +}; >>> + >>> +struct D : A, B { >>> + constexpr D() : B((A*)this, this) { } // { dg-message "in 'constexpr' expansion of" } >>> +}; >>> + >>> +constexpr B::B(V* v, A* a) >>> +{ >>> + // well-defined: v of type V*, V base of B results in B* >>> + B* b = dynamic_cast(v); >>> + if (b != nullptr) >>> + __builtin_abort (); >>> + >>> + B& br = dynamic_cast(*v); // { dg-error "reference .dynamic_cast. failed" } >>> +// { dg-message "dynamic type .A. of its operand does not have an unambiguous public base class .B." "" { target *-*-* } .-1 } >>> + >>> + // FIXME: UB in constexpr should be detected. >>> + dynamic_cast(a); // undefined behavior, a has type A*, A not a base of B >> >> What undefined behavior? Seems to me the dynamic_cast should just fail and >> return a null pointer. > > This is : > "If the operand of the dynamic_cast refers to the object under construction > or destruction and the static type of the operand is not a pointer to or > object of the constructor or destructor's own class or one of its bases, the > dynamic_cast results in undefined behavior." > > Clang++ doesn't detect this either. Ah, interesting. That text goes back to C++98. I guess the reason for that was that the vtable pointer might give problematic answers in that situation. It should be straightforward to detect this in constexpr evaluation; if mdtype is not the actual complete object, we can see if *this is also part of mdtype (success), or otherwise the complete object (undefined). It looks like get_component_with_type (<*this>, type, mdtype) should work well for that check. Jason