Message ID | 20220601164437.1963270-1-ppalka@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8D546383666C for <patchwork@sourceware.org>; Wed, 1 Jun 2022 16:45:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8D546383666C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1654101926; bh=qnhbk0y4Y1Bq3lUbGZOhGzfYP3kYzikS8QQmx+XEscg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jr5hAjCFNDuVEzxin0N9GIUDw595CxXpVqOlhifNm2k4f+8Ka8bBL7Ak25LhAyCRR RM1glWOEQfdyG1lgp+WA0vMoSOMQLwYF/R29N7trGvOoexfqfyjtQsGD8RBFMbWcZ4 pRozetx9+X1pPpPPWt/2PwW6i5oY6fnNOsj0s2Y4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 5BAB63834E48 for <gcc-patches@gcc.gnu.org>; Wed, 1 Jun 2022 16:44:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5BAB63834E48 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.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-182-kpGH0ojLNiWSj44J6olYsg-1; Wed, 01 Jun 2022 12:44:40 -0400 X-MC-Unique: kpGH0ojLNiWSj44J6olYsg-1 Received: by mail-qk1-f200.google.com with SMTP id az40-20020a05620a172800b006a5faff65c8so1722526qkb.7 for <gcc-patches@gcc.gnu.org>; Wed, 01 Jun 2022 09:44:40 -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:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=qnhbk0y4Y1Bq3lUbGZOhGzfYP3kYzikS8QQmx+XEscg=; b=Nv3IfMneV2JwUtlUdXY6vosE8OFlAJQw+IXw/I6ghLOp2LRiwvHgdNQF/KJbeKEhn9 jx0t7VURtBUcvjezmPkSKbVKvvsmJ699z5zyjY/8cY7+FgTHryTHtmGm3w5Tqc85qCgG hRQQqMYpJctO36WIsJZaDJC/aAWf93spQCx4cAwFOPetEksxx000AA6h64lyFbyellMR 0Ckhm2xSBL8YBwAF0OqfrBcLm1cupRJyVNu5e3O20utxnu7y72oSCKgbvyH4hWgcWKQz dzIr5SA9VcOVQ1zig9RmWT+gwgTB4kjaJINr1ML4KwXs1TuDI138HpVStjFxGpjIm/yd ArhA== X-Gm-Message-State: AOAM5327PCL73TMpm5zVolI6CemjhskwlwEDiISdus9moTmKHAWSYpZx kXzLvRuIPTw0kNgB7beh5YL3Qvvkn7cR+zggdoU/+HGHwnMShM7yziSPzE0fGz8BKTZrR2tut1p F0ln1rEjtI2LmBJbRjnID80IuBi6762pyeu580uJNVEUfFKPoXhareuS3SAta1TN9Xnw= X-Received: by 2002:a05:6214:19cc:b0:461:fb6b:d408 with SMTP id j12-20020a05621419cc00b00461fb6bd408mr54333853qvc.64.1654101880245; Wed, 01 Jun 2022 09:44:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2sHesQM0LW6khEvSY7BJioW2rVLHrYo+QFlUctvwkBrdy/+qOZ5uvw6KUrdKA1NDJb5Lhxg== X-Received: by 2002:a05:6214:19cc:b0:461:fb6b:d408 with SMTP id j12-20020a05621419cc00b00461fb6bd408mr54333826qvc.64.1654101879906; Wed, 01 Jun 2022 09:44:39 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id gb3-20020a05622a598300b002f93554c009sm1350526qtb.59.2022.06.01.09.44.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 09:44:39 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: find_template_parameters and PARM_DECLs [PR105797] Date: Wed, 1 Jun 2022 12:44:37 -0400 Message-Id: <20220601164437.1963270-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.1.203.g1bcf4f6271 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.2 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Patrick Palka via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Patrick Palka <ppalka@redhat.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
c++: find_template_parameters and PARM_DECLs [PR105797]
|
|
Commit Message
Patrick Palka
June 1, 2022, 4:44 p.m. UTC
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? PR c++/105797 gcc/cp/ChangeLog: * pt.cc (for_each_template_parm_r) <case FUNCTION_DECL, VAR_DECL>: Don't walk DECL_CONTEXT. <case PARM_DECL>: Likewise. Walk TREE_TYPE. <case CONST_DECL>: Simplify accordingly. (any_template_parm_r) <case PARM_DECL>: 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
Comments
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) <case FUNCTION_DECL, VAR_DECL>: > Don't walk DECL_CONTEXT. > <case PARM_DECL>: Likewise. Walk TREE_TYPE. > <case CONST_DECL>: Simplify accordingly. > (any_template_parm_r) <case PARM_DECL>: 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<class T> > +concept C = requires { T(); }; > + > +template<class T> > +void f(T v) requires C<decltype(v)>; // #1 > + > +template<class T, class U> > +void f(T v) requires C<decltype(v)>; // #2 > + > +int main() { > + f<int, int>(0); > + f<int>(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<class T, int I> > +struct A { > + enum E { e = I }; > + static void f() requires (e != 0); > +}; > + > +int main() { > + A<int, 1>::f(); > + A<int, 0>::f(); // { dg-error "no match" } > +} > -- > 2.36.1.203.g1bcf4f6271 > >
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) <case FUNCTION_DECL, VAR_DECL>: > Don't walk DECL_CONTEXT. > <case PARM_DECL>: Likewise. Walk TREE_TYPE. > <case CONST_DECL>: Simplify accordingly. > (any_template_parm_r) <case PARM_DECL>: 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<class T> > +concept C = requires { T(); }; > + > +template<class T> > +void f(T v) requires C<decltype(v)>; // #1 > + > +template<class T, class U> > +void f(T v) requires C<decltype(v)>; // #2 > + > +int main() { > + f<int, int>(0); > + f<int>(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<class T, int I> > +struct A { > + enum E { e = I }; > + static void f() requires (e != 0); > +}; > + > +int main() { > + A<int, 1>::f(); > + A<int, 0>::f(); // { dg-error "no match" } > +}
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<class T> +concept C = requires { T(); }; + +template<class T> +void f(T v) requires C<decltype(v)>; // #1 + +template<class T, class U> +void f(T v) requires C<decltype(v)>; // #2 + +int main() { + f<int, int>(0); + f<int>(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<class T, int I> +struct A { + enum E { e = I }; + static void f() requires (e != 0); +}; + +int main() { + A<int, 1>::f(); + A<int, 0>::f(); // { dg-error "no match" } +}