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 563FD3858D33 for ; Thu, 4 May 2023 13:56:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 563FD3858D33 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=1683208581; 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=pmTBXsXK02rczLR4HuHhrNp0XyieH0wA7d3MGvcDJ8I=; b=NVWqX2voD5myminPVvJ3Xq7CVY8Ulqd9BDySTvG57cbKUKjbb17MLtopjarVPCJBCjGNoT uw7WQMfFUMRjICH3LrTgyRCJJ3qtXdZDxhDB+we3TsNYc84T7HQCVzXkFuYGxejJxaILtZ 48A05AWnZKfYhxY7jTfMzCrnxIeH5vM= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-608-LFPx1s9eMOCaaf9TgwDa5g-1; Thu, 04 May 2023 09:56:11 -0400 X-MC-Unique: LFPx1s9eMOCaaf9TgwDa5g-1 Received: by mail-qk1-f200.google.com with SMTP id af79cd13be357-7515a7ba88aso51921185a.3 for ; Thu, 04 May 2023 06:56:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683208571; x=1685800571; 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:message-id:reply-to; bh=pmTBXsXK02rczLR4HuHhrNp0XyieH0wA7d3MGvcDJ8I=; b=DyQWg+1gHvv9UrJhPzfy9GINdLUv1PpweVlFGgnU1o3POJl6kyeZ8ZWPqbh5v3FP+p QPweBnpb7UW2rbGzn0qGWVWM6Yflx+KjIZKW/wbYL8HKLEDw4V4k+lzIswF2j1eNDD10 3tQ5VKzchtnRwsAJ1izFXi5LMVvgA3jI5OVWsj6Ui1NWEQfBTDr11StJziuozv1v1uZz lt4i+0gnPEiPl9LYXl3W8xSRk3UvMls75dBkfSbE/udLY1HPpHhR0iK1YYaQcsEOhuHi FZShEhUqcQKvhh5hGGplCyhogcC+ZEwHa51eX5q7GoVwcO6+JBy7B2vZxr62dzxD4/Yk /5qA== X-Gm-Message-State: AC+VfDxFxCXZ04iJQC6wdM9pGq5qqWQsQCyigqZs5HqzOLsYcg6gRc8c wlrf1+rMnqZheKKGaHCRdoklM3yQQztDsOfGTqnRAxicYlwXJ33UFyBoOveJ1VSzRANWlAgv4y0 S/EuOisHy4PtPKLN8cQ== X-Received: by 2002:ac8:5886:0:b0:3ef:25ad:27fb with SMTP id t6-20020ac85886000000b003ef25ad27fbmr5512431qta.30.1683208571149; Thu, 04 May 2023 06:56:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5EkOv+eVgdL/coLGPc79tHq9v2duh55Qvablq3CnXpdfeKqegFLltViby6ytakkCtFM500jA== X-Received: by 2002:ac8:5886:0:b0:3ef:25ad:27fb with SMTP id t6-20020ac85886000000b003ef25ad27fbmr5512389qta.30.1683208570782; Thu, 04 May 2023 06:56:10 -0700 (PDT) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id r18-20020ac85c92000000b003f0a7c13fcdsm9058132qta.74.2023.05.04.06.56.09 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 May 2023 06:56:10 -0700 (PDT) Message-ID: Date: Thu, 4 May 2023 09:56:08 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.10.1 Subject: Re: [PATCH 1/2] c++: potentiality of templated memfn call [PR109480] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20230501195902.1915703-1-ppalka@redhat.com> <6da23365-02cd-8ac1-2fdc-91b284af6a68@redhat.com> <2aea65a6-eef7-d171-8790-bbb5b9c45d8a@idea> <3744435a-11ea-f9bf-c8de-b040e98b3c0b@idea> <8dacd562-3441-872f-ab4f-20015694eb8f@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=-15.1 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,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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 5/3/23 16:50, Patrick Palka wrote: > On Wed, 3 May 2023, Jason Merrill wrote: > >> On 5/2/23 15:53, Patrick Palka wrote: >>> on Tue, 2 May 2023, Patrick Palka wrote: >>> >>>> On Tue, 2 May 2023, Jason Merrill wrote: >>>> >>>>> On 5/1/23 15:59, Patrick Palka wrote: >>>>>> Here we're incorrectly deeming the templated call a.g() inside b's >>>>>> initializer as potentially constant, despite g being non-constexpr, >>>>>> which leads to us wastefully instantiating the initializer ahead of >>>>>> time >>>>>> and triggering a bug in access checking deferral (which will get fixed >>>>>> in the subsequent patch). >>>>>> >>>>>> This patch fixes this by calling get_fns earlier during potentiality >>>>>> checking so that we also handle the templated form of a member >>>>>> function >>>>>> call (whose overall callee is a COMPONENT_REF) when checking if the >>>>>> called >>>>>> function is constexpr etc. >>>>>> >>>>>> PR c++/109480 >>>>>> >>>>>> gcc/cp/ChangeLog: >>>>>> >>>>>> * constexpr.cc (potential_constant_expression_1) >>>>> CALL_EXPR>: >>>>>> Reorganize to call get_fns sooner. Remove dead store to >>>>>> 'fun'. >>>>>> >>>>>> gcc/testsuite/ChangeLog: >>>>>> >>>>>> * g++.dg/cpp0x/noexcept59.C: Make e() constexpr so that the >>>>>> expected "without object" diagnostic isn't replaced by a >>>>>> "call to non-constexpr function" diagnostic. >>>>>> * g++.dg/template/non-dependent25.C: New test. >>>>>> --- >>>>>> gcc/cp/constexpr.cc | 16 >>>>>> ++++++++-------- >>>>>> gcc/testsuite/g++.dg/cpp0x/noexcept59.C | 2 +- >>>>>> gcc/testsuite/g++.dg/template/non-dependent25.C | 14 ++++++++++++++ >>>>>> 3 files changed, 23 insertions(+), 9 deletions(-) >>>>>> create mode 100644 gcc/testsuite/g++.dg/template/non-dependent25.C >>>>>> >>>>>> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc >>>>>> index d1097764b10..29d872d0a5e 100644 >>>>>> --- a/gcc/cp/constexpr.cc >>>>>> +++ b/gcc/cp/constexpr.cc >>>>>> @@ -9132,6 +9132,10 @@ potential_constant_expression_1 (tree t, bool >>>>>> want_rval, bool strict, bool now, >>>>>> if (fun && is_overloaded_fn (fun)) >>>>>> { >>>>>> + if (!RECUR (fun, true)) >>>>>> + return false; >>>>>> + fun = get_fns (fun); >>>>>> + >>>>>> if (TREE_CODE (fun) == FUNCTION_DECL) >>>>>> { >>>>>> if (builtin_valid_in_constant_expr_p (fun)) >>>>>> @@ -9167,7 +9171,8 @@ potential_constant_expression_1 (tree t, bool >>>>>> want_rval, bool strict, bool now, >>>>>> expression the address will be folded away, so look >>>>>> through it now. */ >>>>>> if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) >>>>>> - && !DECL_CONSTRUCTOR_P (fun)) >>>>>> + && !DECL_CONSTRUCTOR_P (fun) >>>>>> + && !processing_template_decl) >>>>> >>>>> I don't see any rationale for this hunk? >>>> >>>> Now that we call get_fns earlier, we can reach this code path with a >>>> templated non-static memfn call, but the code that follows assumes >>>> non-templated form. >>>> >>>> I tried teaching it to handle the templated form too, but there's >>>> apparently two different templated forms for non-static memfn calls, >>>> one with a COMPONENT_REF callee and one with an ordinary BASELINK >>>> callee (without a implicit object argument). In the former the implict >>>> object argument is inside the COMPONENT_REF (and is a reference instead >>>> of a pointer), and in the latter we don't even have an implicit object >>>> argument to inspect. >>>> >>>> FWIW I think which form we use depends on whether we know if the called >>>> function is a member of the current instantiation, e.g >>>> >>>> struct A { void f(); }; >>>> >>>> template struct B; >>>> >>>> template >>>> struct C : B { >>>> void g(); >>>> >>>> void h() { >>>> A::f(); // templated form has BASELINK callee, no object arg >>>> C::g(); // templated form has COMPONENT_REF callee >>>> } >>>> }; >>>> >>>> So it seemed best to punt on templated non-static memfn calls here for >>>> now and treat that as a separate enhancement. >>> >>> And I'm not even sure if the code path in question is necessary at all >>> anymore: disabling it outright doesn't cause any regressions in the >>> testsuite. >>> It seems effectively equivalent to the body of the loop over the args a few >>> lines later: >> >> If removing that hunk doesn't regress anything, let's do it. Probably that >> should have happened in r13-55-ge9d2adc17d0dbe > > Sounds good, here's the combined patch which I'm bootstrapping for good > measure. Does it look OK for trunk if bootstrap+regtest succeeds? OK. > -- >8 -- > > Subject: [PATCH] c++: potentiality of templated memfn call [PR109480] > > Here we're incorrectly deeming the templated call a.g() inside b's > initializer as potentially constant, despite g being non-constexpr, > which leads to us wastefully instantiating the initializer ahead of time, > which incidentally tiggers a bug in access checking deferral (to be > fixed by the subsequent patch). > > This patch fixes this by calling get_fns earlier during CALL_EXPR > potentiality checking so that we're able to extract a FUNCTION_DECL out > of a templated member function call (whose overall is typically a > COMPONENT_REF) and to the usual checking if the called function is > constexpr etc. > > In passing, I noticed potential_constant_expression_1's special handling > of the object argument of a non-static member function call is effectively > the same as the generic argument handling a few lines later. So this > patch just gets rid of this special handling; otherwise we'd have to adapt > it to handle templated versions of such calls. > > PR c++/109480 > > gcc/cp/ChangeLog: > > * constexpr.cc (potential_constant_expression_1) : > Reorganize to call get_fns sooner. Remove special handling of > the object argument of a non-static member function call. Remove > dead store to 'fun'. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp0x/noexcept59.C: Make e() constexpr so that the > expected "without object" diagnostic isn't replaced by a > "call to non-constexpr function" diagnostic. > * g++.dg/template/non-dependent25.C: New test. > --- > gcc/cp/constexpr.cc | 32 ++++--------------- > gcc/testsuite/g++.dg/cpp0x/noexcept59.C | 2 +- > .../g++.dg/template/non-dependent25.C | 14 ++++++++ > 3 files changed, 21 insertions(+), 27 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/template/non-dependent25.C > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > index d1097764b10..075339f7f62 100644 > --- a/gcc/cp/constexpr.cc > +++ b/gcc/cp/constexpr.cc > @@ -9132,6 +9132,10 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, > > if (fun && is_overloaded_fn (fun)) > { > + if (!RECUR (fun, true)) > + return false; > + fun = get_fns (fun); > + > if (TREE_CODE (fun) == FUNCTION_DECL) > { > if (builtin_valid_in_constant_expr_p (fun)) > @@ -9162,36 +9166,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, > explain_invalid_constexpr_fn (fun); > return false; > } > - /* A call to a non-static member function takes the address > - of the object as the first argument. But in a constant > - expression the address will be folded away, so look > - through it now. */ > - if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun) > - && !DECL_CONSTRUCTOR_P (fun)) > - { > - tree x = get_nth_callarg (t, 0); > - if (is_this_parameter (x)) > - return true; > - /* Don't require an immediately constant value, as > - constexpr substitution might not use the value. */ > - bool sub_now = false; > - if (!potential_constant_expression_1 (x, rval, strict, > - sub_now, fundef_p, > - flags, jump_target)) > - return false; > - i = 1; > - } > - } > - else > - { > - if (!RECUR (fun, true)) > - return false; > - fun = get_first_fn (fun); > } > + > + fun = OVL_FIRST (fun); > /* Skip initial arguments to base constructors. */ > if (DECL_BASE_CONSTRUCTOR_P (fun)) > i = num_artificial_parms_for (fun); > - fun = DECL_ORIGIN (fun); > } > else if (fun) > { > diff --git a/gcc/testsuite/g++.dg/cpp0x/noexcept59.C b/gcc/testsuite/g++.dg/cpp0x/noexcept59.C > index c752601ba09..1dc826d3111 100644 > --- a/gcc/testsuite/g++.dg/cpp0x/noexcept59.C > +++ b/gcc/testsuite/g++.dg/cpp0x/noexcept59.C > @@ -3,7 +3,7 @@ > > template class A > { > - void e (); > + constexpr bool e () { return true; }; > bool f (int() noexcept(this->e())); // { dg-error "this" } > bool g (int() noexcept(e())); // { dg-error "without object" } > }; > diff --git a/gcc/testsuite/g++.dg/template/non-dependent25.C b/gcc/testsuite/g++.dg/template/non-dependent25.C > new file mode 100644 > index 00000000000..c00e44532b0 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/non-dependent25.C > @@ -0,0 +1,14 @@ > +// PR c++/109480 > + > +template > +struct A { > + void f() { > + A a; > + const bool b = a.g(); // { dg-bogus "private" } > + } > + > +private: > + bool g() const; > +}; > + > +template struct A;