From patchwork Thu Aug 10 16:08:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 134074 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp525321vqi; Thu, 10 Aug 2023 09:09:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF6RhwQOy51N4lpWnW3Bb0AJVfM2ZeN9PLMF0DnTS7Hw5754mdYDVOsiXTaRYfWOt301QZp X-Received: by 2002:a2e:9d91:0:b0:2b9:ac48:d7f5 with SMTP id c17-20020a2e9d91000000b002b9ac48d7f5mr2274164ljj.39.1691683794668; Thu, 10 Aug 2023 09:09:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691683794; cv=none; d=google.com; s=arc-20160816; b=Atj2FNiLtukbVpzOHeHnMGUEB8xns8CWSR9tivVurzlrc573sGXPBEPMHvFeGBKyds /Y5FFFS8m3EArFbiGobcM4BqctnjZRc9qIdebMZLl3/DA5lh7eMsBS5/mEVgWDmSEJiS 5OwDCacW5dDtsK5Opy92fwKpPOnUVV5rba3BzoSLmkAs8Q9LTQq5U2t1eHuAR3e6rBo2 R0w3vv2uY71fUYv2OPqCOZ5arjOY8MnYjVwJyJu6JKjX+SefU9ZmtRV0QUJ045cp8sgp O4voHO3pVD30nLEnrAltmf5zdDFQZFVV+Y+U5CPmzBZeSO8eJO0eklnSyUMRmrqIZFvl tDTw== 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=zhzY9RhSiplQvs0u6kTrg1xvH1wYi9phVp+iAYzDstc=; fh=M5GO8duY/t9+1sJXvBSRQ4u8k9z8sLFDsSO1kARbltQ=; b=Au1jiyD9T6U6TDREU9vbu22H0GhB59rLv9TO8Ng3Gi6g2EYELlgJLy4fZZZq9csorw WT14UoQ3DqPMmDdL2/pd9CXvYmui2kQoZDhQYA2nqDbds/JXfyzB9PQhyBkvh8VeWbFu 0ppS1jwBdmhSqaWoEdC0JWP7xMRgiA2zpZiufFoHaL7fCGTQF1XkvbxU4UQRp4545Occ RpetkL2FwBL0B7EXc8eQmH0X7JdXZc8zNylyr71S7gfnPWz+UWsg2qzY7hVCkEw7Sfax uLe1Ip0H89ENoEhfMDkh4GhL7HGHsUxbN990Xz+IjpNKqkRQ7Eb3ELWRFz4T+lv2fYhV PbHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vFDwwqTv; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g3-20020a170906594300b0099bcc342fc3si1722001ejr.677.2023.08.10.09.09.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 09:09:54 -0700 (PDT) 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=vFDwwqTv; 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 5F903385AE4E for ; Thu, 10 Aug 2023 16:09:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5F903385AE4E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691683769; bh=zhzY9RhSiplQvs0u6kTrg1xvH1wYi9phVp+iAYzDstc=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=vFDwwqTv+jcSPmjqOcX7Ig6KuYSEnbZTrYT7X1K+I8R7ZaUcCO1iWa+lM3smsXX5S SutyFzXm4sPGAraUxArNPUZrGp9t2wm4n33K8w8eLLg5QgEzbnZnUn81nrSRxGi4mU ci9GD1rfxrEPQEvkp8tcYLslvPYC/1o8hrVGs9SQ= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id CE9A7385772F for ; Thu, 10 Aug 2023 16:08:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CE9A7385772F Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-529-JFaBX2FpMCOoHVXgT9kRFQ-1; Thu, 10 Aug 2023 12:08:45 -0400 X-MC-Unique: JFaBX2FpMCOoHVXgT9kRFQ-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-76c95663db4so135564385a.0 for ; Thu, 10 Aug 2023 09:08:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691683724; x=1692288524; 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=zhzY9RhSiplQvs0u6kTrg1xvH1wYi9phVp+iAYzDstc=; b=evzYVeCcUfLTGfucOAVMiM7RqP4ti+PNB4KEoSlk6lCiutTp+IbFqZIMp0mG1z+joJ 9UG5XLnpgE1Xsdo5aUhRLOLUNu1pPUaNx1gPnS6WD/0lMa//1qpY/ohWn+2IBzNy6DAB f3tC/1qgNZKDpkM9knv9NVafUvpjX1ejj1fi/yxI8ibNPVeFYKjD7rs0RZ0CrMB3x+Tk qH+w/D87gllPyB3LO62TsGd/JDtNGKAW76qiHQ7ZkaC/+PTr88OzZHzxHdzzPz4tBYON R+5PMST4hE1QgXYHELrQB2zV9mobktKVEf/yUPNQb5F7O5td7FLS58DyaeYysGCRynM0 OX4A== X-Gm-Message-State: AOJu0Yy52okS9hR9id29cPVYLM2KDERipBU8JP3ZOGsO7SUfRkaAdL/S Tybl4qjRxMB62MXyY7C/c+3VlT6hbSBs4AFViDsRrO3Fxbacs9hqo4ZfR6WUOEk9BfCEvXLNEWN ugF7MVcGJvaW8yzH5713fQper2/APYwuTLOammvQB/4MX9w76hDcV2BpgWf2K4VUhOI+REU8ne/ 0= X-Received: by 2002:a37:ac0a:0:b0:76c:e2db:42b0 with SMTP id e10-20020a37ac0a000000b0076ce2db42b0mr2660084qkm.64.1691683724428; Thu, 10 Aug 2023 09:08:44 -0700 (PDT) X-Received: by 2002:a37:ac0a:0:b0:76c:e2db:42b0 with SMTP id e10-20020a37ac0a000000b0076ce2db42b0mr2660062qkm.64.1691683724084; Thu, 10 Aug 2023 09:08:44 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id g3-20020a05620a13c300b0075cd80fde9esm577629qkl.89.2023.08.10.09.08.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 09:08:43 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: recognize in-class var tmpl partial spec [PR71954] Date: Thu, 10 Aug 2023 12:08:42 -0400 Message-ID: <20230810160842.1513222-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.rc0.25.ga82fb66fed MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.6 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_H4, RCVD_IN_MSPIKE_WL, 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: INBOX X-GMAIL-THRID: 1773859026693215319 X-GMAIL-MSGID: 1773859026693215319 Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- This makes us recognize variable template partial specializations defined directly inside the class body. It seems to suffice to call check_explicit_specialization when we see a static TEMPLATE_ID_EXPR data member, which sets SET_DECL_TEMPLATE_SPECIALIZATION and which we otherwise don't call (for out-of-class partial specializations we call it from from grokvardecl which is used only for out-of-class definitions). We also need to make finish_member_template_decl return NULL_TREE for such partial specializations (matching its behavior class template partial specializations) so that its caller doesn't try to call finish_member_declaration on it. PR c++/71954 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Use 'dname' instead of 'unqualified_id' when building the VAR_DECL for a static data member. Call check_explicit_specialization for a TEMPLATE_ID_EXPR such member. * pt.cc (finish_member_template_decl): Return NULL_TREE instead of 'decl' when DECL_TEMPLATE_SPECIALIZATION is not set. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/var-templ84.C: New test. * g++.dg/cpp1y/var-templ84a.C: New test. --- gcc/cp/decl.cc | 11 ++++++++++- gcc/cp/pt.cc | 2 +- gcc/testsuite/g++.dg/cpp1y/var-templ84.C | 12 ++++++++++++ gcc/testsuite/g++.dg/cpp1y/var-templ84a.C | 19 +++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ84.C create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ84a.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 9fe3a0b98fd..2b3fb313166 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -14440,7 +14440,16 @@ grokdeclarator (const cp_declarator *declarator, /* C++ allows static class members. All other work for this is done by grokfield. */ decl = build_lang_decl_loc (id_loc, VAR_DECL, - unqualified_id, type); + dname, type); + if (unqualified_id + && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) + { + decl = check_explicit_specialization (unqualified_id, decl, + template_count, + concept_p * 8); + if (decl == error_mark_node) + return error_mark_node; + } set_linkage_for_static_data_member (decl); if (concept_p) error_at (declspecs->locations[ds_concept], diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2706fa619bf..a4809f034dc 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -314,7 +314,7 @@ finish_member_template_decl (tree decl) return DECL_TI_TEMPLATE (decl); } else - return decl; + return NULL_TREE; } else error_at (DECL_SOURCE_LOCATION (decl), diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C new file mode 100644 index 00000000000..39c2a0dc0cc --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C @@ -0,0 +1,12 @@ +// PR c++/71954 +// { dg-do compile { target c++14 } } + +struct A { + template static const int var = 0; + template static const int var = 1; + template static const int var = 2; +}; + +static_assert(A::var == 0, ""); +static_assert(A::var == 1, ""); +static_assert(A::var == 2, ""); diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C new file mode 100644 index 00000000000..4aa3a2a7245 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C @@ -0,0 +1,19 @@ +// PR c++/71954 +// A version of var-templ84.C where the partial specializations depend on +// outer template parameters. +// { dg-do compile { target c++14 } } + +template +struct A { + template static const int var = 0; + template static const int var = 1; + template static const int var = 2; +}; + +static_assert(A::var == 0, ""); +static_assert(A::var == 1, ""); +static_assert(A::var == 2, ""); + +static_assert(A::var == 0, ""); +static_assert(A::var == 0, ""); +static_assert(A::var == 0, "");