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 AB5CD395B06F for ; Thu, 2 Jun 2022 20:43:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AB5CD395B06F Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-508-rZr_zEVHNM2GbXi4cs4kkg-1; Thu, 02 Jun 2022 16:43:24 -0400 X-MC-Unique: rZr_zEVHNM2GbXi4cs4kkg-1 Received: by mail-qv1-f69.google.com with SMTP id k6-20020a0cd686000000b004625db7d2aaso4137381qvi.7 for ; Thu, 02 Jun 2022 13:43:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=pIwSX7HnE0nl7yGIuSsC2uGaSTwX6olq7rFGZJC9MdA=; b=yfhXRzn2smqRA9y4lOWP7ykNSInnIZR7qSQp7WjtxjAfD2ASBM/S4VYs+qNazvRs2S yGsKDOTD6vMLTzDCYpDtmkLLG6Yqi4WYQgNczY1Ae+Ca/56p5KuK54Ln6P2mgqvUP1+1 eTep9veR7edIcrWsdFH4ebWDbiYADaa9YyLPSlfo3OzwWF5Q1yAxB1QH93pLKjpbxOYX E/YFdtXpno2ZoBekOGqLkFARdUvQ92purFK77aUWlrbLhY8ElApywRGMF7jm7vbTIJVu ocLhShVhyQM0Hv1lYqrX0MERiBrznng8uvwRjhH6+D8UBK9D9sLtornHtUyErvHm7Wxt Jf5w== X-Gm-Message-State: AOAM530FdZ+fo7DfK519iXpno7nghk1nrgxsnyKwjR9f2fMdpLbA565B Zro30enCznfCOhjowkhnRnH2iofrnOl7Ld2i6ZDod003hXBSjbONSEZRe8eXVFTjGCB1m2cbpc+ aOFMsr14nSj8vQezCsg== X-Received: by 2002:a05:622a:1baa:b0:303:6049:8aeb with SMTP id bp42-20020a05622a1baa00b0030360498aebmr5253651qtb.10.1654202603779; Thu, 02 Jun 2022 13:43:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxBS9Jhge7DfWSUBxqafKHQDzvRlxIqLyNPyYFQC+RTr6IIye+FnentUbwEM/XO5C+IDW0Tw== X-Received: by 2002:a05:622a:1baa:b0:303:6049:8aeb with SMTP id bp42-20020a05622a1baa00b0030360498aebmr5253630qtb.10.1654202603414; Thu, 02 Jun 2022 13:43:23 -0700 (PDT) Received: from [192.168.1.100] (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 bs6-20020a05620a470600b006a32c4a2cb8sm3850581qkb.52.2022.06.02.13.43.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Jun 2022 13:43:22 -0700 (PDT) Message-ID: <3307a430-2c74-5002-489c-f30c3208cefb@redhat.com> Date: Thu, 2 Jun 2022 16:43:21 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: [PATCH] c++: find_template_parameters and PARM_DECLs [PR105797] To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20220601164437.1963270-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20220601164437.1963270-1-ppalka@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.9 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, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 02 Jun 2022 20:43:27 -0000 On 6/1/22 12:44, Patrick Palka wrote: > As explained in r11-4959-gde6f64f9556ae3, the atom cache assumes two > equivalent expressions (according to cp_tree_equal) must use the same > template parameters (according to find_template_parameters). This > assumption turned out to not hold for TARGET_EXPR, which was addressed > by that commit. > > But this assumption apparently doesn't hold for PARM_DECL either: > find_template_parameters walks its DECL_CONTEXT but cp_tree_equal by > default doesn't consider DECL_CONTEXT unless comparing_specializations > is set. Thus in the first testcase below, the atomic constraints of #1 > and #2 are equivalent according to cp_tree_equal, but according to > find_template_parameters the former uses T and the latter uses both T > and U. > > I suppose we can fix this assumption violation by setting > comparing_specializations in the atom_hasher, which would make > cp_tree_equal return false for the two atoms, but that seems overly > pessimistic here. Ideally the atoms should be considered equivalent > and we should fix find_template_paremeters to return just T for #2's > atom. > > To that end this patch makes for_each_template_parm_r stop walking the > DECL_CONTEXT of a PARM_DECL. This should be safe to do because > tsubst_copy / tsubst_decl only cares about the TREE_TYPE of a PARM_DECL > and doesn't bother substituting the DECL_CONTEXT, thus the only relevant > template parameters are those used in its type. any_template_parm_r is > currently responsible for walking its TREE_TYPE, but I suppose it now makes > sense make for_each_template_parm_r do so instead. > > In passing this patch also makes for_each_template_parm_r stop walking > the DECL_CONTEXT of a VAR_/FUNCTION_DECL since it should be unnecessary > after walking DECL_TI_ARGS. > > I experimented with not walking DECL_CONTEXT for CONST_DECL, but the > second testcase below demonstrates it's necessary to walk it. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? OK. > PR c++/105797 > > gcc/cp/ChangeLog: > > * pt.cc (for_each_template_parm_r) : > Don't walk DECL_CONTEXT. > : Likewise. Walk TREE_TYPE. > : Simplify accordingly. > (any_template_parm_r) : Don't walk TREE_TYPE. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/concepts-decltype4.C: New test. > --- > gcc/cp/pt.cc | 10 +++++----- > gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C | 16 ++++++++++++++++ > gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C | 12 ++++++++++++ > 3 files changed, 33 insertions(+), 5 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > index 4f0ace2644b..e4a473002a0 100644 > --- a/gcc/cp/pt.cc > +++ b/gcc/cp/pt.cc > @@ -10561,11 +10561,14 @@ for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d) > case VAR_DECL: > if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)) > WALK_SUBTREE (DECL_TI_ARGS (t)); > - /* Fall through. */ > + break; > > case PARM_DECL: > + WALK_SUBTREE (TREE_TYPE (t)); > + break; > + > case CONST_DECL: > - if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)) > + if (DECL_TEMPLATE_PARM_P (t)) > WALK_SUBTREE (DECL_INITIAL (t)); > if (DECL_CONTEXT (t) > && pfd->include_nondeduced_p) > @@ -10824,9 +10827,6 @@ any_template_parm_r (tree t, void *data) > break; > > case TEMPLATE_PARM_INDEX: > - case PARM_DECL: > - /* A parameter or constraint variable may also depend on a template > - parameter without explicitly naming it. */ > WALK_SUBTREE (TREE_TYPE (t)); > break; > > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C b/gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C > new file mode 100644 > index 00000000000..6683d224cf8 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-decltype4.C > @@ -0,0 +1,16 @@ > +// PR c++/105797 > +// { dg-do compile { target c++20 } } > + > +template > +concept C = requires { T(); }; > + > +template > +void f(T v) requires C; // #1 > + > +template > +void f(T v) requires C; // #2 > + > +int main() { > + f(0); > + f(0); > +} > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C b/gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C > new file mode 100644 > index 00000000000..3fa4fb82818 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-memfun3.C > @@ -0,0 +1,12 @@ > +// { dg-do compile { target c++20 } } > + > +template > +struct A { > + enum E { e = I }; > + static void f() requires (e != 0); > +}; > + > +int main() { > + A::f(); > + A::f(); // { dg-error "no match" } > +}