From patchwork Thu Feb 23 21:52:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 61086 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp552654wrd; Thu, 23 Feb 2023 13:53:37 -0800 (PST) X-Google-Smtp-Source: AK7set/bArbxOy6IAMfmwifW1hpuwwe1PokQmpQZKY+LrnKnzwrKDIicmAHjRXrJwvjfegRU+XdL X-Received: by 2002:a17:906:6a19:b0:8de:e66b:27e with SMTP id qw25-20020a1709066a1900b008dee66b027emr11158097ejc.16.1677189217100; Thu, 23 Feb 2023 13:53:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677189217; cv=none; d=google.com; s=arc-20160816; b=o/I9Ed5QSZkiaN0/rfelmDiW9tT1xhr7T2j9W4CqnDPkFQg7meS39v9CaKEz2M+NBn qjSO/U5Ur6KPr8BnYZIHrruPj0yZQXLO9SEzfS1B9L3ln4Io1OpfuDso2ffkln7TrXTd AZ8U6zbuWnNwZTcUqPf4D5dQsVYE7jCuMpZZGu3s8VrRezndeF4+So0zPWq50giQdIKS MKPnTCtbv48X5+7pzsLX1GR58pdx71VeQOuZr5+evOLu/OROKSBjd7+Xw9Pt31e7htgG Ypd/Oo5rjg+Sq7BXI+20khOSqc08bKn6m3BQGZt/YEFrCtoMoGK5BvEma3zoT5Ag42LR KghQ== 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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=hC0YzC19E3SGWTnnSXQdOKVLiegoAeDHiS8wMuPftds=; b=XPrdSE6lr/V3sfjoEMm2SKcvdlR0D4i9Hwdknrb6gGhSddyFB3Jdn8SIMPKlJBHmKq gsb9qkM8iNDdQxKyXQpaS4istfdRHiexey85OQlbg8m66GFzeZs7SaRX/hrWaNvDECFC LZ+c9CvzOc6wxDuTpyBf0bjPFyCrEnfN3wE04qCSwv20f9wuUo+R+sS4b75d7NXlv8VS gbaIGa1gHvYogrh5DGXDLoKLk/Bh8tcXLGPRD/Am4WB0WkzyGJQEfqOU05gf5N5wauQu XAE03XE4ufZj/m9Mrmw407tnM3yIWpeASUnPKX92iObmrQkBNbfPdnYIm+a9j3aWulJ4 yfZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YCI+Sfaq; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j19-20020a170906105300b008e48d70f31fsi9336155ejj.662.2023.02.23.13.53.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 13:53:37 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=YCI+Sfaq; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 DEF79385B513 for ; Thu, 23 Feb 2023 21:53:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DEF79385B513 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677189215; bh=hC0YzC19E3SGWTnnSXQdOKVLiegoAeDHiS8wMuPftds=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=YCI+SfaqOSpawk1UMqLUvv4nOsijetKWF+WZcwqlEmTv5m0nDobzcjlRak/WsGsE6 2VWVTbu3O1BaDGH3TZsE+2CZ67zxetVBzG2B6G7Ch6fmh3U2czMyEu6GJMN1z2T6Qk ZeuJCmk8xqahxPH24radsfITosEJk9gO+3XQjsoE= 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 9BBDA3858C00 for ; Thu, 23 Feb 2023 21:52:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9BBDA3858C00 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-50-YJuKA0jTMgSGDR9ZEjhWDQ-1; Thu, 23 Feb 2023 16:52:48 -0500 X-MC-Unique: YJuKA0jTMgSGDR9ZEjhWDQ-1 Received: by mail-qv1-f71.google.com with SMTP id e9-20020a0cf749000000b00571f1377451so3877908qvo.2 for ; Thu, 23 Feb 2023 13:52:48 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hC0YzC19E3SGWTnnSXQdOKVLiegoAeDHiS8wMuPftds=; b=lFXkSn/0eWqSzNje/KAA7f+bhL5eOeVnp9moDSNmj+31dL/gz7pHbmeLgKaXmjUvum NSLpl4sO7WqxRWFyuW2prYYt7OjEGuPWis4sKbolz9eboKrPZNNBMWLc2jurZ+tFDjaM s9UKtsKRwkOdvGlvFaPpz37r3K2VgnEgI232/2G8xVtK5wye2M5bKxMeCgzjbQ6f/x78 TXwnRuR6h6mPFWOU1DiHd2poA9FHa4daVPfdB+A2z2Skfb7ylRvN1FLxDKcVt0dCvsTx fiBngUXhkUD+zfHvRdNv1TmDsjIgohAeQGM6hgPsxzXrfyYQpD6CxFJiBL7WurvhTxuv yEPQ== X-Gm-Message-State: AO0yUKUuuXoKPui+GM3/VcdW/5FVWRvudy2Wnh6/vC1B2WpLgLIkAY8N 5x/wP1zTQxyIaZTm5MgP+70ZY2ybEub4+TfFmoLbEk6BuazY6I62oXfdS48rpIfY51UE8Do4Pmk Uc6GMekPbdMkBKvF7SLx/jAwezzv7ugflfEP6ASAawJFOOW50Uo4G/7hs2royQ4tuqs5+39t8 X-Received: by 2002:a05:6214:dc7:b0:56e:f417:2ac0 with SMTP id 7-20020a0562140dc700b0056ef4172ac0mr30816330qvt.10.1677189167516; Thu, 23 Feb 2023 13:52:47 -0800 (PST) X-Received: by 2002:a05:6214:dc7:b0:56e:f417:2ac0 with SMTP id 7-20020a0562140dc700b0056ef4172ac0mr30816291qvt.10.1677189167192; Thu, 23 Feb 2023 13:52:47 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id t71-20020a37aa4a000000b00741d87eb5d1sm5517333qke.105.2023.02.23.13.52.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 13:52:46 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: non-dependent variable template-id [PR108848] Date: Thu, 23 Feb 2023 16:52:45 -0500 Message-Id: <20230223215245.1513700-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.2.542.g06dd2baa8d MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka 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?1758660360205340055?= X-GMAIL-MSGID: =?utf-8?q?1758660360205340055?= Here we're incorrectly treating the non-dependent variable template-id tag as dependent ever since r226642 gave variable TEMPLATE_ID_EXPR an empty type which causes the call to finish_template_variable from finish_id_expression_1 to be dead code at template parse time. Thus we're led into treating tag.var as a dependent name at parse time. This patch fixes this by making finish_id_expression_1 instantiate a variable template-id as long as the template and args are non-dependent according to the dependence test in lookup_and_finish_template_variable and not according to type_dependent_expression_p. This patch also moves said dependence test into finish_template_variable. bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/108848 gcc/cp/ChangeLog: * pt.cc (finish_template_variable): Move dependence check to here from ... (lookup_and_finish_template_variable): ... here. * semantics.cc (finish_id_expression_1): Call finish_template_variable sooner and regardless of type_dependent_expression_p. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/noexcept1.C: Don't expect bogus "different exception specifier" error. Expect a separate "not usable in a constant expression" error. * g++.dg/cpp1y/var-templ70.C: New test. * g++.dg/cpp1y/var-templ71.C: New test. --- gcc/cp/pt.cc | 18 ++++++++++-------- gcc/cp/semantics.cc | 14 +++++--------- gcc/testsuite/g++.dg/cpp1y/noexcept1.C | 4 ++-- gcc/testsuite/g++.dg/cpp1y/var-templ70.C | 20 ++++++++++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ71.C | 10 ++++++++++ 5 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ70.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ71.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index b1ac7d4beb4..11c0baa0119 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10317,7 +10317,8 @@ lookup_template_variable (tree templ, tree arglist) return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist); } -/* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */ +/* Instantiate a variable declaration from a TEMPLATE_ID_EXPR if it's + not dependent. */ tree finish_template_variable (tree var, tsubst_flags_t complain) @@ -10325,6 +10326,12 @@ finish_template_variable (tree var, tsubst_flags_t complain) tree templ = TREE_OPERAND (var, 0); tree arglist = TREE_OPERAND (var, 1); + /* If the template or arguments are dependent, then we + can't instantiate yet. */ + if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) != 1 + || any_dependent_template_arguments_p (arglist)) + return var; + tree parms = DECL_TEMPLATE_PARMS (templ); arglist = coerce_template_parms (parms, arglist, templ, complain); if (arglist == error_mark_node) @@ -10352,13 +10359,8 @@ lookup_and_finish_template_variable (tree templ, tree targs, tsubst_flags_t complain) { tree var = lookup_template_variable (templ, targs); - if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) == 1 - && !any_dependent_template_arguments_p (targs)) - { - var = finish_template_variable (var, complain); - mark_used (var); - } - + var = finish_template_variable (var, complain); + mark_used (var); return convert_from_reference (var); } diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index c2df0b69b30..a18364dda4f 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -4208,6 +4208,11 @@ finish_id_expression_1 (tree id_expression, } else { + if (TREE_CODE (decl) == TEMPLATE_ID_EXPR + && variable_template_p (TREE_OPERAND (decl, 0)) + && !concept_check_p (decl)) + decl = finish_template_variable (decl); + bool dependent_p = type_dependent_expression_p (decl); /* If the declaration was explicitly qualified indicate @@ -4275,15 +4280,6 @@ finish_id_expression_1 (tree id_expression, /* Replace an evaluated use of the thread_local variable with a call to its wrapper. */ decl = wrap; - else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR - && !dependent_p - && variable_template_p (TREE_OPERAND (decl, 0)) - && !concept_check_p (decl)) - { - decl = finish_template_variable (decl); - mark_used (decl); - decl = convert_from_reference (decl); - } else if (concept_check_p (decl)) { /* Nothing more to do. All of the analysis for concept checks diff --git a/gcc/testsuite/g++.dg/cpp1y/noexcept1.C b/gcc/testsuite/g++.dg/cpp1y/noexcept1.C index 86e46c96148..caa4a056a2e 100644 --- a/gcc/testsuite/g++.dg/cpp1y/noexcept1.C +++ b/gcc/testsuite/g++.dg/cpp1y/noexcept1.C @@ -5,9 +5,9 @@ template bool b; template struct C { - template friend int foo() noexcept(b<1>); // { dg-error "not usable in a constant expression|different exception specifier" } + template friend int foo() noexcept(b<1>); // { dg-error "not usable in a constant expression" } }; -template int foo() noexcept(b<1>); +template int foo() noexcept(b<1>); // { dg-error "not usable in a constant expression" } auto a = C(); 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..aed2db5de88 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C @@ -0,0 +1,20 @@ +// PR c++/108848 +// { dg-do compile { target c++14 } } + +template +struct tag_t { + template + static constexpr const Sig* var = nullptr; + + template + static const Sig* fun(); +}; + +template +constexpr tag_t tag {}; + +template +void f() { + tag.var; // { dg-bogus "expected 'template' keyword" } + tag.fun(); // { dg-bogus "expected 'template' keyword" } +}; 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..df33c62ec3e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ71.C @@ -0,0 +1,10 @@ +// Verify we can evaluate a non-dependent variable template-id ahead of time. +// { dg-do compile { target c++14 } } + +template +static constexpr int var = 0; + +template +void f() { + static_assert(var); // { dg-error "assert" } +}