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.129.124]) by sourceware.org (Postfix) with ESMTPS id F2E5D3858D3C for ; Fri, 2 Dec 2022 16:16:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F2E5D3858D3C 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=1669997810; 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=0n86i49T24g+dfT6UgEdLayCsqfgn9Mx4lfBt0WO6Ig=; b=X/4Q6rm02cowYQUusxgZlUDPU34nUBP1j4vmAtVyKPZBY1b2NRFXCFnxXQ6yK8Xm4DgGDG MARhaqkUQL4VUoE6o4pLoXrRzk91/Gj9rXXBb43nHGCDmCrkCso3bfMy9eWnXIeOAHXvcs MTFrfuUyk40wG4qgh7MVHOMpQKCZ/Ig= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-151-nhFOjFeVNmCMKJgk5Npepg-1; Fri, 02 Dec 2022 11:16:48 -0500 X-MC-Unique: nhFOjFeVNmCMKJgk5Npepg-1 Received: by mail-qt1-f197.google.com with SMTP id fz10-20020a05622a5a8a00b003a4f466998cso19155903qtb.16 for ; Fri, 02 Dec 2022 08:16:48 -0800 (PST) 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:message-id:reply-to; bh=0n86i49T24g+dfT6UgEdLayCsqfgn9Mx4lfBt0WO6Ig=; b=iV2fngcjT++GOm7AImEATMEIWE/2gS7hySWGSyi82Gr5PvaYSj6wJl3lrKL1AiAdqh wg6jZey7zBRcG425ilay7RktmqlnygE9EDLD0ecTAV90Ag02Eo4HFLFBH4f4lR91WXLi JFptu1clbjojjJkyh8rwVtpZHS7mjk7gJ0Acx1G28CLH+SMVI3rjt/lnmOTxkAU3ZbZP 689a3fiCiVwWDoS3XaiGostlmF2gyUY95Q0+V2DajG6Qdn/LlNIjoopYNb8j8qyzk7UC SLheH8vxDvl81uRTbdzvcnRciK0n8gOnNq+N0VCC2O+lhSNkWpW+iJdt1IKqxOiBqNGU P51Q== X-Gm-Message-State: ANoB5pmyjpzqM9g9aWJuMjoTXUmz1ofjDfawX5PxNoUh6pJWJJNBdqRN EEvXUcPGqYtjZj9bO14TvRTHUwttuD8NHbkuWI1saB4eoP5LUiD04qtkTRq7Jw05Z38yz1J++Ep 7HubpvRAMgXRpeKEPPQ== X-Received: by 2002:a0c:9c43:0:b0:4c6:62af:5e17 with SMTP id w3-20020a0c9c43000000b004c662af5e17mr49353546qve.95.1669997808154; Fri, 02 Dec 2022 08:16:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf7yDt8NRH47QYFpdldsxWv0nJsaVYtI2DwjZHywaRtSiPsGZB+bP8lpK5P7NbDDLeLV6B8rtA== X-Received: by 2002:a0c:9c43:0:b0:4c6:62af:5e17 with SMTP id w3-20020a0c9c43000000b004c662af5e17mr49353523qve.95.1669997807784; Fri, 02 Dec 2022 08:16:47 -0800 (PST) Received: from [192.168.1.108] (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 az20-20020a05620a171400b006e42a8e9f9bsm5658110qkb.121.2022.12.02.08.16.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 02 Dec 2022 08:16:47 -0800 (PST) Message-ID: <10b990d6-e1a1-2cbe-19f8-6f59d4e22130@redhat.com> Date: Fri, 2 Dec 2022 11:16:41 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.1 Subject: Re: [PATCH] c++: explicit spec of constrained member tmpl [PR107522] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20221201163752.2176490-1-ppalka@redhat.com> <967940e9-3ed4-bcfe-20f4-73eaf38d41a4@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=-12.5 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 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 12/2/22 09:30, Patrick Palka wrote: > On Thu, 1 Dec 2022, Jason Merrill wrote: > >> On 12/1/22 14:51, Patrick Palka wrote: >>> On Thu, 1 Dec 2022, Jason Merrill wrote: >>> >>>> On 12/1/22 11:37, Patrick Palka wrote: >>>>> When defining a explicit specialization of a constrained member template >>>>> (of a class template) such as f and g in the below testcase, the >>>>> DECL_TEMPLATE_PARMS of the corresponding TEMPLATE_DECL are partially >>>>> instantiated, whereas its associated constraints are carried over >>>>> from the original template and thus are in terms of the original >>>>> DECL_TEMPLATE_PARMS. >>>> >>>> But why are they carried over? We wrote a specification of the >>>> constraints in >>>> terms of the template parameters of the specialization, why are we >>>> throwing >>>> that away? >>> >>> Using the partially instantiated constraints would require adding a >>> special case to satisfaction since during satisfaction we currently >>> always use the full set of template arguments (relative to the most >>> general template). >> >> But not for partial specializations, right? It seems natural to handle this >> explicit instantiation the way we handle partial specializations, as both have >> their constraints written in terms of their template parameters. > > True, but what about the general rule that we don't partially instantiate > constraints outside of declaration matching? Checking satisfaction of > partially instantiated constraints here can introduce hard errors during > normalization, e.g. > > template > concept C1 = __same_as(T, void); > > template > concept C2 = C1; > > template > concept D = (N == 42); > > template > struct A { > template > static void f() requires C2 || D; > }; > > template<> > template > void A::f() requires C2 || D { } > > int main() { > A::f<42>(); > } > > Normalization of the the partially instantiated constraints will give a > hard error due to 'int::type' being ill-formed, whereas the uninstantiated > constraints are fine. Hmm, interesting point, but in this example that happens because the specialization is nonsensical: we wouldn't be normalizing the partially-instantiated constraints so much as the ones that the user explicitly wrote, so a hard error seems justified. >>> For satisfaction of the partially instantiated >>> constraints, we'd instead have to use the template arguments relative to >>> the explicit specialization, e.g. {42} instead of {{int},{42}} for >>> A::f<42>. Not sure if that would be preferable, but it seems >>> doable. >>> >>>> >>>>> So during normalization for such an explicit >>>>> specialization we need to consider the (parameters of) the most general >>>>> template, since that's what the constraints are in terms of and since we >>>>> always use the full set of template arguments during satisfaction. >>>>> >>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for >>>>> trunk and perhaps 12? >>>>> >>>>> PR c++/107522 >>>>> >>>>> gcc/cp/ChangeLog: >>>>> >>>>> * constraint.cc (get_normalized_constraints_from_decl): Use the >>>>> most general template for an explicit specialization of a >>>>> member template. >>>>> >>>>> gcc/testsuite/ChangeLog: >>>>> >>>>> * g++.dg/cpp2a/concepts-explicit-spec7.C: New test. >>>>> --- >>>>> gcc/cp/constraint.cc | 18 ++++++++--- >>>>> .../g++.dg/cpp2a/concepts-explicit-spec7.C | 31 >>>>> +++++++++++++++++++ >>>>> 2 files changed, 44 insertions(+), 5 deletions(-) >>>>> create mode 100644 >>>>> gcc/testsuite/g++.dg/cpp2a/concepts-explicit-spec7.C >>>>> >>>>> diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc >>>>> index ab0f66b3d7e..f1df84c2a1c 100644 >>>>> --- a/gcc/cp/constraint.cc >>>>> +++ b/gcc/cp/constraint.cc >>>>> @@ -973,11 +973,19 @@ get_normalized_constraints_from_decl (tree d, bool >>>>> diag = false) >>>>> accepting the latter causes the template parameter level of U >>>>> to be reduced in a way that makes it overly difficult substitute >>>>> concrete arguments (i.e., eventually {int, int} during >>>>> satisfaction. >>>>> */ >>>>> - if (tmpl) >>>>> - { >>>>> - if (DECL_LANG_SPECIFIC(tmpl) && !DECL_TEMPLATE_SPECIALIZATION >>>>> (tmpl)) >>>>> - tmpl = most_general_template (tmpl); >>>>> - } >>>>> + if (tmpl && DECL_LANG_SPECIFIC (tmpl) >>>>> + && (!DECL_TEMPLATE_SPECIALIZATION (tmpl) >>>>> + /* DECL_TEMPLATE_SPECIALIZATION means we're dealing with either a >>>>> + partial specialization or an explicit specialization of a member >>>>> + template. In the former case all is well: the constraints are in >>>>> + terms in TMPL's parameters. But in the latter case TMPL's >>>>> + parameters are partially instantiated whereas its constraints >>>>> + aren't, so we need to consider (the parameters of) the most >>>>> + general template. The following test distinguishes between a >>>>> + partial specialization and such an explicit specialization. */ >>>>> + || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) >>>>> + < TMPL_ARGS_DEPTH (DECL_TI_ARGS (tmpl))))) >>>>> + tmpl = most_general_template (tmpl); >>>>> d = tmpl ? tmpl : decl; >>>>> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-explicit-spec7.C >>>>> b/gcc/testsuite/g++.dg/cpp2a/concepts-explicit-spec7.C >>>>> new file mode 100644 >>>>> index 00000000000..5b5a6df20ff >>>>> --- /dev/null >>>>> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-explicit-spec7.C >>>>> @@ -0,0 +1,31 @@ >>>>> +// PR c++/107522 >>>>> +// { dg-do compile { target c++20 } } >>>>> + >>>>> +template >>>>> +struct A >>>>> +{ >>>>> + template >>>>> + static void f() requires (N == 42); >>>>> + >>>>> + template >>>>> + struct B { >>>>> + template >>>>> + static void g() requires (T(N) == 42); >>>>> + }; >>>>> +}; >>>>> + >>>>> +template<> >>>>> +template >>>>> +void A::f() requires (N == 42) { } >>>>> + >>>>> +template<> >>>>> +template<> >>>>> +template >>>>> +void A::B::g() requires (int(N) == 42) { } >>>>> + >>>>> +int main() { >>>>> + A::f<42>(); >>>>> + A::f<43>(); // { dg-error "no match" } >>>>> + A::B::g<42>(); >>>>> + A::B::g<43>(); // { dg-error "no match" } >>>>> +} >>>> >>>> >>> >> >> >