From patchwork Mon Sep 27 14:44:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 45473 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 8CF74385841D for ; Mon, 27 Sep 2021 14:44:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8CF74385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1632753893; bh=ZCYFI5Q1akOc8szvIk/RD4oGZwfYT3PSFRtoKs+YKyE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pYn41/N+fbTdMFuycz/A5WL56/hNftSfLsobubui/K7D0fgSIESc2KSagdqHw1uPa oDJjbaB1AKZ05nOSJlui7iyyP7YA2hmw4y4QpdSK7yGBtcwlJcgCX2bVsLnNGvduFj JkGaNSExkcqRKsxxyWRmYdPcu6ItGPeXz+Y+bN6I= 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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 365DA385841C for ; Mon, 27 Sep 2021 14:44:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 365DA385841C Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-125-ooxHPgPmNVm4O8M499jhWw-1; Mon, 27 Sep 2021 10:44:11 -0400 X-MC-Unique: ooxHPgPmNVm4O8M499jhWw-1 Received: by mail-qk1-f198.google.com with SMTP id bj6-20020a05620a190600b0045e164b4576so20540273qkb.8 for ; Mon, 27 Sep 2021 07:44:10 -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=ZCYFI5Q1akOc8szvIk/RD4oGZwfYT3PSFRtoKs+YKyE=; b=OSCkhVFLnu4U2FLSZXGIk9wy+G0WQtH6sqH833X0JbPG0PHVoYuFpDjIuvS6IWTWTu eZuwpIxJPRJb5sCRd/ARVi9PDCZCC4pwRiXrhLtGpxMiPXIkdeTbt5o6xpmH6iFufyBx DFggDSG54inR+SErbHo15uVFrri/2nX2Yph0LfzRVsdOXzbhLfg6UUeclFnm4gwl2HRf sgekCJvOxWu/G3++8AxQUo0cMYy+CdhhG+2fqcS9MAaC4JiD+fSgZIuvfsaFGUn3YdcH cS0+pwViYq72Xhsae6yydyatFIYRKVgC3DNisv6XpqCknLVpCg70OUA8NpoQilidYAIz XJnQ== X-Gm-Message-State: AOAM533Q1XfwghkjAan69qXjTyVdZaqpuXEfswFfuP2bSK2LA7egiR1f 1qDYMiRHRbW4RPVQsGWCGsGkizsypgWv4aR7wk+Y3cOqQWmOghyaR6ktDc6dHCNpkZiJy6X2Rgd g4uUlOLxv7ketuC+dLfwH12snSWhHzHVIWprDHQH5bxYVykBheGTA6g9bdcWsQ74PVhk= X-Received: by 2002:ac8:404f:: with SMTP id j15mr162498qtl.361.1632753850038; Mon, 27 Sep 2021 07:44:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZ0zfufLT/87/Pq2Mth8VMRp0u8D8wytieKQSLsIJlv5kwdK35Qrv8kMMVoO5HYljAIwIKJg== X-Received: by 2002:ac8:404f:: with SMTP id j15mr162460qtl.361.1632753849692; Mon, 27 Sep 2021 07:44:09 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id l1sm12950072qkj.65.2021.09.27.07.44.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Sep 2021 07:44:09 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: deduction guides and ttp rewriting [PR102479] Date: Mon, 27 Sep 2021 10:44:06 -0400 Message-Id: <20210927144406.2078673-1-ppalka@redhat.com> X-Mailer: git-send-email 2.33.0.591.gddb1055343 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-15.8 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_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" The problem here is ultimately that rewrite_tparm_list when rewriting a TEMPLATE_TEMPLATE_PARM introduces a tree cycle in the rewritten ttp that structural_comptypes can't cope with. In particular the DECL_TEMPLATE_PARMS of a ttp's TEMPLATE_DECL normally captures an empty parameter list at its own level (and so the TEMPLATE_DECL doesn't appear in its own DECL_TEMPLATE_PARMS), but rewrite_tparm_list ends up giving it a complete parameter list. In the new testcase below, this causes infinite recursion from structural_comptypes when comparing Tmpl with Tmpl (here both 'Tmpl's are rewritten). This patch fixes this by making rewrite_template_parm give a rewritten template template parm an empty parameter list at its own level, thereby avoiding the tree cycle. Testing the alias CTAD case revealed that we're not setting current_template_parms in alias_ctad_tweaks, which this patch also fixes. Also, the change to use TMPL_ARGS_LEVEL instead of TREE_VEC_ELT is needed because alias_ctad_tweaks passes only a single level of targs to rewrite_tparm_list. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/102479 gcc/cp/ChangeLog: * pt.c (rewrite_template_parm): Use TMPL_ARGS_LEVEL instead of TREE_VEC_ELT directly to properly handle one-level tsubst_args. Avoid a tree cycle when assigning the DECL_TEMPLATE_PARMS for a rewritten ttp. (alias_ctad_tweaks): Set current_template_parms accordingly. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction12.C: Also test alias CTAD in the same way. * g++.dg/cpp1z/class-deduction99.C: New test. --- gcc/cp/pt.c | 20 +++++++++-- .../g++.dg/cpp1z/class-deduction12.C | 6 ++++ .../g++.dg/cpp1z/class-deduction99.C | 35 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction99.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 6bd6ceb29be..cba0f5c8279 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -28754,7 +28754,7 @@ rewrite_template_parm (tree olddecl, unsigned index, unsigned level, const int depth = TMPL_ARGS_DEPTH (tsubst_args); tree ttargs = make_tree_vec (depth + 1); for (int i = 0; i < depth; ++i) - TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i); + TREE_VEC_ELT (ttargs, i) = TMPL_ARGS_LEVEL (tsubst_args, i + 1); TREE_VEC_ELT (ttargs, depth) = template_parms_level_to_args (ttparms); // Substitute ttargs into ttparms to fix references to @@ -28767,8 +28767,17 @@ rewrite_template_parm (tree olddecl, unsigned index, unsigned level, ttparms = tsubst_template_parms_level (ttparms, ttargs, complain); // Finally, tack the adjusted parms onto tparms. - ttparms = tree_cons (size_int (depth), ttparms, - current_template_parms); + ttparms = tree_cons (size_int (level + 1), ttparms, + copy_node (current_template_parms)); + // As with all template template parms, the parameter list captured + // by this template template parm that corresponds to its own level + // should be empty. This avoids infinite recursion when structurally + // comparing two such rewritten template template parms (102479). + gcc_assert (!TREE_VEC_LENGTH + (TREE_VALUE (TREE_CHAIN (DECL_TEMPLATE_PARMS (olddecl))))); + gcc_assert (TMPL_PARMS_DEPTH (TREE_CHAIN (ttparms)) == level); + TREE_VALUE (TREE_CHAIN (ttparms)) = make_tree_vec (0); + // All done. DECL_TEMPLATE_PARMS (newdecl) = ttparms; } } @@ -29266,6 +29275,11 @@ alias_ctad_tweaks (tree tmpl, tree uguides) ++ndlen; tree gtparms = make_tree_vec (natparms + ndlen); + /* Set current_template_parms as in build_deduction_guide. */ + auto ctp = make_temp_override (current_template_parms); + current_template_parms = copy_node (DECL_TEMPLATE_PARMS (tmpl)); + TREE_VALUE (current_template_parms) = gtparms; + /* First copy over the parms of A. */ for (j = 0; j < natparms; ++j) TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j); diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C index a31cc1526db..f0d7ea0e16b 100644 --- a/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction12.C @@ -15,3 +15,9 @@ A a(&i,2,B<42>()); template class same; template class same {}; same> s; + +#if __cpp_deduction_guides >= 201907 +template using C = A; + +same())), A> t; +#endif diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction99.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction99.C new file mode 100644 index 00000000000..6daa4b7c55c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction99.C @@ -0,0 +1,35 @@ +// PR c++/102479 +// { dg-do compile { target c++17 } } + +template struct A; + +template +struct tuple { + tuple(T); + + template class Tmpl> + tuple(Tmpl); + + template class Tmpl, typename A>::type = 0> + tuple(Tmpl); + + template class Tmpl, typename A>::type = 0> + tuple(Tmpl); +}; + +template struct B { }; + +using ty1 = tuple; +using ty1 = decltype(tuple(0)); +using ty1 = decltype(tuple(B{})); + +#if __cpp_deduction_guides >= 201907 +template using const_tuple = tuple; + +using ty2 = const_tuple; +using ty2 = decltype(const_tuple(0)); +using ty2 = decltype(const_tuple(B{})); + +using ty3 = const_tuple>; +using ty3 = decltype(const_tuple(B{})); +#endif