From patchwork Fri Jun 4 03:46:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1487551 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=lAzecv53; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Fx7xt0hmNz9sRK for ; Fri, 4 Jun 2021 13:47:28 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E6085388A827 for ; Fri, 4 Jun 2021 03:47:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6085388A827 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1622778445; bh=zBRFxyQvCuR753mmfX7zJ+GUYqWa2rvGa3y2nOuZhcM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lAzecv53QQLeNIk4C02aH7rX8w8FPByLd/IF1LVgy5MF+GZS22hcIykMpp7Xei/w5 +wbngHEj22toJK2W2hJpczrV0kKkLKMufYROJR+o4fubhAc4hkCBD2Y59KAVsTQDUm D9PzCjGfG3bmxAKg/jjZcuhssotYCYf7ZaT6UsyY= 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 597C4385E449 for ; Fri, 4 Jun 2021 03:46:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 597C4385E449 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-117-YC-46cLGNMuZ2y17kxxclA-1; Thu, 03 Jun 2021 23:46:39 -0400 X-MC-Unique: YC-46cLGNMuZ2y17kxxclA-1 Received: by mail-qv1-f69.google.com with SMTP id b24-20020a0cb3d80000b02901e78b82d74aso5871754qvf.20 for ; Thu, 03 Jun 2021 20:46:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=zBRFxyQvCuR753mmfX7zJ+GUYqWa2rvGa3y2nOuZhcM=; b=lRfTiILuK5N7WypPjiqIrrdy8VADfuEaEfh8/msXWGhEagwYObSDaXBJzAvUsEK1fa gkrIF1cMlUs5wS9CnxlNTNHMiIq40B6Wyb7lbWkI3a6DIGFZABv8cXLOOESPJMDgx5H+ 58PJwhHlXxYYXqSWZ7pVVk2p/S+zGhciFOa7s/sfP8ZBSuUjgUtGFFXSR2EGtqgLBB0r Lx/g7WK2uzasJ0FiKq/ewyaov31PRuUxq6lqiwh8P10DR7RQiQLCcxredTPURSmtqTAi RYwwB6Bt1c2lgwstAtj0T+dVq2AW2mddfCphUWS4hKY0nueFt+ayOpbPt5VBxSnr7fYD MGLg== X-Gm-Message-State: AOAM530fdl6cVvzvxW0i9U/rsGAiVKYM6EOzGCmmXp+G3R2z80cWEBoF HboLarLCreD01tMbc2w2sENHMb8IeHdR3ULc9ZPXxrTWv//flX7DEY/bw44P+T+IZUi0AXydYaM sJdK/G7oev9EceCYAIvfd0O3VsdQYYwtSkPdbzPNJQR3FLeT90nC8WwfG5SZTyxCKRRE= X-Received: by 2002:a05:620a:787:: with SMTP id 7mr2496419qka.397.1622778399031; Thu, 03 Jun 2021 20:46:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJypu0DsHZeSpn1TknnWXktdDNTr7VZMWETRutAoIZYi4XmPsoFsBCisIFCopb5BlEZB8Knb2Q== X-Received: by 2002:a05:620a:787:: with SMTP id 7mr2496406qka.397.1622778398683; Thu, 03 Jun 2021 20:46:38 -0700 (PDT) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id 17sm2814847qtw.44.2021.06.03.20.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Jun 2021 20:46:38 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: tsubst_function_decl and excess arg levels [PR100102] Date: Thu, 3 Jun 2021 23:46:07 -0400 Message-Id: <20210604034607.3582211-1-ppalka@redhat.com> X-Mailer: git-send-email 2.32.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-16.1 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Here, when instantiating the dependent alias template duration::__is_harmonic with args={{T,U},{int}}, we find ourselves substituting the function decl _S_gcd. Since we have more arg levels than _S_gcd has parm levels, an old special case in tsubst_function_decl causes us to unwantedly reduce args to its innermost level, yielding args={int}, which leads to a nonsensical substitution into the decl's context and an eventual crash. The comment for this special case refers to three examples for which we ought to see more arg levels than parm levels here, but none of the examples actually demonstrate this. In the first example, when defining S::f(U) parms_depth is 2 and args_depth is 1, and later when instantiating say S::f both depths are 2. In the second example, when substituting the template friend declaration parms_depth is 2 and args_depth is 1, and later when instantiating f both depths are 1. Finally, the third example is invalid since we can't specialize a member template of an unspecialized class template like that. Given that this reduction code seems no longer relevant for its documented purpose and that it causes problems as in the PR, this patch just removes it. Note that as far as bootstrap/regtest is concerned, this code is dead; the below two tests would be the first to trigger the removed code. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps backports? Also tested on various other libraries, e.g. range-v3 and cmcstl2. PR c++/100102 gcc/cp/ChangeLog: * pt.c (tsubst_function_decl): Remove old code for reducing args when it has excess levels. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-72.C: New test. * g++.dg/cpp0x/alias-decl-72a.C: New test. --- gcc/cp/pt.c | 39 --------------------- gcc/testsuite/g++.dg/cpp0x/alias-decl-72.C | 9 +++++ gcc/testsuite/g++.dg/cpp0x/alias-decl-72a.C | 9 +++++ 3 files changed, 18 insertions(+), 39 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-72.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-72a.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 3cac073ed50..a6acdf864d1 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -13909,45 +13909,6 @@ tsubst_function_decl (tree t, tree args, tsubst_flags_t complain, if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash)) return spec; } - - /* We can see more levels of arguments than parameters if - there was a specialization of a member template, like - this: - - template struct S { template void f(); } - template <> template void S::f(U); - - Here, we'll be substituting into the specialization, - because that's where we can find the code we actually - want to generate, but we'll have enough arguments for - the most general template. - - We also deal with the peculiar case: - - template struct S { - template friend void f(); - }; - template void f() {} - template S; - template void f(); - - Here, the ARGS for the instantiation of will be {int, - double}. But, we only need as many ARGS as there are - levels of template parameters in CODE_PATTERN. We are - careful not to get fooled into reducing the ARGS in - situations like: - - template struct S { template void f(U); } - template template <> void S::f(int) {} - - which we can spot because the pattern will be a - specialization in this case. */ - int args_depth = TMPL_ARGS_DEPTH (args); - int parms_depth = - TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t))); - - if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t)) - args = get_innermost_template_args (args, parms_depth); } else { diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-72.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-72.C new file mode 100644 index 00000000000..8009756dcba --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-72.C @@ -0,0 +1,9 @@ +// PR c++/100102 +// { dg-do compile { target c++11 } } + +template struct ratio; +template struct duration { + static constexpr int _S_gcd(); + template using __is_harmonic = ratio<_S_gcd>; + using type = __is_harmonic; +}; diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-72a.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-72a.C new file mode 100644 index 00000000000..a4443e18f9d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-72a.C @@ -0,0 +1,9 @@ +// PR c++/100102 +// { dg-do compile { target c++11 } } + +template struct ratio; +template struct duration { + static constexpr int _S_gcd(); + template using __is_harmonic = ratio<(duration::_S_gcd)()>; + using type = __is_harmonic; +};