From patchwork Wed Feb 22 23:58:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 60726 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:7b8e:b0:9f:8c76:fea4 with SMTP id j14csp85669dyk; Wed, 22 Feb 2023 15:59:02 -0800 (PST) X-Google-Smtp-Source: AK7set8udAo0api7qlw3ASILdfyUi5Re3KMmVtqTjStTHlPsOlm8fZgaCBLRl3X0qYjbj/Lpc46D X-Received: by 2002:a17:907:7e84:b0:8e9:6f9f:7c2d with SMTP id qb4-20020a1709077e8400b008e96f9f7c2dmr3393208ejc.35.1677110342232; Wed, 22 Feb 2023 15:59:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677110342; cv=none; d=google.com; s=arc-20160816; b=mf38oX+J+RGyH6mb4mFSvZY8r83Dskg6qQU+XAuFnjTNpsGvICvmIIXaNR5p6vWWIp cbpY3RVJhHxAtQ1kSFK7xkvcq+7jYFY3t/jU018ao/ARiWLDQ+yCbr4QwxHMOSX4D7aM NWy/vMofKSBqncrfe1i6oSPp2k0hI49axX7mt6hQcKlMo+xNvprv2Vs7k3w0V01k9enh VOQ6SzfNjyQudq8WU0o/jdVx85eSqN5mmb11sNy0kagymR0ZINnfQUwJkot/W+3OEpWY BKADhvX4YLzXNiXcnmwCmdAhnZXLJGg9KUzuNHQfjxAo112IvbpmNSR/faTbCRpEukL5 Bz2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PJ6mbXLzFmjIQwm/pK11sohmcVc1ERLQoRdD+cCMtO4=; b=dWXeJj12hQE8y11uPnF6vMbWFVZNBtrmbFl6F4V5fN4JlsRUvz0vdOaRz4gEc8Xo4L UYQgvF6mCi/cC2brkCd+gmMgQEd8bfzZWJmNJuZXNTM+uUp97gzmcglB6iQce8RgIsny JKOuq4IdkUwiJnj80nLRbB1XICIQnTKKT2UT8zaiXRrOMiPZM1FbAtTA+sFI42rO5RUo zYTjR3LoHVucTl+O70VapmmlcD8lv5jw2PFnPJw8c/zojNvWoFKbBFteO+8TD1Y7jfrN g3d/XnebCsGrpMcoPKTv8Swudh764oejkrG8ctWpJWoDb6c9IOxtUWEKAPscsG8FQvbB +r5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OTIByvI7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id fh6-20020a1709073a8600b008d8279dfbe0si8894590ejc.651.2023.02.22.15.59.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Feb 2023 15:59:02 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=OTIByvI7; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 15B2338582B0 for ; Wed, 22 Feb 2023 23:59:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 15B2338582B0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677110341; bh=PJ6mbXLzFmjIQwm/pK11sohmcVc1ERLQoRdD+cCMtO4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=OTIByvI7rRIdwCHxlMgjg2d3aVa5h3I6pCeLC0V/7ZHg18Phb/4HMQhWhuUkfdtsF pEx5Sl4At9IDeMIdJiSKyS+6VvxemLgY5wZJxaK0FNRFoa4ncCZQKqZtgSUzSujLzH XLcnJVmg9SEpxJwH4wGKrVC72nTQDZtdDXnXONA8= 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 AD9D03858D33 for ; Wed, 22 Feb 2023 23:58:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AD9D03858D33 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-204-sGsiBAYtPoKsuwEde2WBrQ-1; Wed, 22 Feb 2023 18:58:14 -0500 X-MC-Unique: sGsiBAYtPoKsuwEde2WBrQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85F522800488 for ; Wed, 22 Feb 2023 23:58:14 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.18.22]) by smtp.corp.redhat.com (Postfix) with ESMTP id 57F722166B29; Wed, 22 Feb 2023 23:58:14 +0000 (UTC) To: GCC Patches , Jason Merrill , Patrick Palka Subject: [PATCH] c++: variable template and targ deduction [PR108550] Date: Wed, 22 Feb 2023 18:58:12 -0500 Message-Id: <20230222235812.185722-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758577654599853420?= X-GMAIL-MSGID: =?utf-8?q?1758577654599853420?= In this test, we get a bogus error because we failed to deduce the auto in constexpr auto is_pointer_v = is_pointer::value; to bool. Then ensure_literal_type_for_constexpr_object thinks the object isn't literal and an error is reported. This is another case of the interaction between tf_partial and 'auto', where the auto was not reduced so the deduction failed. In more detail: we have Wrap1() in the code and we need to perform OR -> fn_type_unification. The targ list is incomplete, so we do tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type; fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE); where TREE_TYPE (fn) is struct integral_constant (void). Then we substitute the return type, which results in tsubsting is_pointer_v. is_pointer_v is a variable template with a placeholder type: template constexpr auto is_pointer_v = is_pointer::value; so we find ourselves in lookup_and_finish_template_variable. tf_partial is still set, so finish_template_variable -> instantiate_template -> tsubst won't reduce the level of auto. But then we do mark_used which eventually calls do_auto_deduction which clears tf_partial, because we want to replace the auto now. But we hadn't reduced auto's level so this fails. And since we're not in an immediate context, we emit a hard error. I suppose that when we reach lookup_and_finish_template_variable it's probably time to clear tf_partial. (I added an assert and our testsuite doesn't have a test whereby we get to lookup_and_finish_template_variable while tf_partial is still active.) Does this make *any* sense to you? Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/108550 gcc/cp/ChangeLog: * pt.cc (lookup_and_finish_template_variable): Clear tf_partial. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ70.C: New test. * g++.dg/cpp1y/var-templ71.C: New test. --- gcc/cp/pt.cc | 6 ++++++ gcc/testsuite/g++.dg/cpp1y/var-templ70.C | 25 +++++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ71.C | 26 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ70.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ71.C base-commit: 1370014f2ea02ec185cf1199027575916f79fe63 diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 1a071e95004..f636bac5413 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10355,6 +10355,12 @@ lookup_and_finish_template_variable (tree templ, tree targs, if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) == 1 && !any_dependent_template_arguments_p (targs)) { + /* We may be called while doing a partial substitution, but the + type of the variable template may be auto, in which case we + will call do_auto_deduction in mark_used (which clears tf_partial) + and the auto must be properly reduced at that time for the + deduction to work. */ + complain &= ~tf_partial; var = finish_template_variable (var, complain); mark_used (var); } diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ70.C b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C new file mode 100644 index 00000000000..1d35c38c7cc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C @@ -0,0 +1,25 @@ +// PR c++/108550 +// { dg-do compile { target c++14 } } + +template +struct is_pointer +{ + static constexpr bool value = false; +}; + +template +struct integral_constant +{ + static constexpr T value = T1; +}; + + +template +constexpr auto is_pointer_v = is_pointer::value; + +template +integral_constant> Wrap1(); + +int main() { + static_assert(!decltype(Wrap1())::value, ""); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ71.C b/gcc/testsuite/g++.dg/cpp1y/var-templ71.C new file mode 100644 index 00000000000..e0cf55230d9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ71.C @@ -0,0 +1,26 @@ +// PR c++/108550 +// { dg-do compile { target c++14 } } + +template +struct integral_constant +{ + static constexpr T value = T1; +}; + +template +struct S { + template + static constexpr void foo(V) { } + + constexpr bool bar () const { foo(10); return false; } +}; + +template +constexpr auto is_pointer_v = S{}.bar(); + +template +integral_constant> Wrap1(); + +int main() { + static_assert(!decltype(Wrap1())::value, ""); +}