From patchwork Wed Jun 1 16:44:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 54686 Return-Path: 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 ; 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 ; 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 ; 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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) : 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" } +}