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 71F953836E6F for ; Wed, 1 Jun 2022 16:46:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 71F953836E6F Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-453-JgU5MbJyMA62Cy44OIM-fA-1; Wed, 01 Jun 2022 12:46:25 -0400 X-MC-Unique: JgU5MbJyMA62Cy44OIM-fA-1 Received: by mail-qt1-f200.google.com with SMTP id c16-20020a05622a059000b002f93304d0d8so1711675qtb.14 for ; Wed, 01 Jun 2022 09:46: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:from:date:to:cc:subject:in-reply-to:message-id :references:mime-version; bh=iW54vUeG6f+x8+VGE7uuc2v1TTxQXYSWNqYAulol5Gg=; b=Zqijcgz3hN/zW9XgJNHxiOMNcF4mtXW8Qy5ovG0EBgaJ0moIURnTNJxjQo8qeX3TU5 28j9j4ga9BCTxq/4lKTRGAFuSzsjz5bYvQux9LlQjXI7JyCx8OFtWoPYzzEH/oA7QwX7 yqM3cKqa5qQYNr5D09L8RtPP7jQSUb1Hcb3rhdY1AHlBLcr22RZFGr9R/FdgYO+ecyC2 PnjquTMeuWOK6tsnVvQDs8zi8oHirtYuj/8PXz6Zl2b10KsCnizm0mExvnJnGWt6l+BP intoauCD29UG5666oooH6xpJCbvu1pfD/unYLZCBBJD6RlPkHYtv4uPwk3DwNWL51K/Y qRcQ== X-Gm-Message-State: AOAM530WNMdaiYayYsXxtV9vkd4qRhsQeuMKUK49bNie9gWEygj8G73H mSS5WRi6/0CHIIcMILnrLe9rlCf+ib7EiO9ywxh9PhMng2l6U/XwOBp+YRYPmnWB/XSgBLkuI+h 5wRB8egVhyfpd/PRizw== X-Received: by 2002:a05:622a:60e:b0:303:9aca:e9fc with SMTP id z14-20020a05622a060e00b003039acae9fcmr434168qta.657.1654101984119; Wed, 01 Jun 2022 09:46:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxp4Opegw2wit3ZLjguFj5YGZ+VDMt4U379+o/WslKDuOVhARRZT16IXDGR3nIWmCsKr9BrLA== X-Received: by 2002:a05:622a:60e:b0:303:9aca:e9fc with SMTP id z14-20020a05622a060e00b003039acae9fcmr434144qta.657.1654101983829; Wed, 01 Jun 2022 09:46:23 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id i12-20020ac871cc000000b002f39b99f693sm1440758qtp.45.2022.06.01.09.46.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 09:46:23 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Wed, 1 Jun 2022 12:46:22 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH] c++: find_template_parameters and PARM_DECLs [PR105797] In-Reply-To: <20220601164437.1963270-1-ppalka@redhat.com> Message-ID: References: <20220601164437.1963270-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-14.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: Wed, 01 Jun 2022 16:46:29 -0000 On Wed, 1 Jun 2022, 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. ... and that's ultimately because tsubst_decl substitutes the DECL_CONTEXT of CONST_DECL (unlike for PARM_DECL) > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? > > 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" } > +} > -- > 2.36.1.203.g1bcf4f6271 > >