From patchwork Thu Nov 17 18:38:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 21870 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp561321wrr; Thu, 17 Nov 2022 10:39:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf4hhAIgsViUas4uFqkpXxyLKYMbPy5fuRO3k21nL27l+6Mqq+ldel9r0vagYBD608PkFHVj X-Received: by 2002:a05:6402:28a9:b0:461:f5ce:a478 with SMTP id eg41-20020a05640228a900b00461f5cea478mr3287044edb.304.1668710382733; Thu, 17 Nov 2022 10:39:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668710382; cv=none; d=google.com; s=arc-20160816; b=WRN7QVTnAilhWMFLDWTdg4bBOuTjhFEXnl3LKzdhy0Dji0jUzd71v7ofXQ8SEpsC1y 02ISiGDMy70wxrl2N64Sb37EirPK5ZBkp3M1zE+fTQ9u2k5HJxQSV9MC2LIrci3289cD OVo5dTDRyP/Z6o80nNi5pNT5A+z8qep6pezW8KxiesHipJ18kwbqzdcbARCoX0/5x6N7 MGWOOoc6t2HJd/U8fcibcDr/DFs33IqnSx4GUmCnfTMfJPJTA4GS3TfwzurXo/SJ2/xT I5LUYu379igQGLSe4afGiG44L31b6kWEJrwfFnt5l0Xwoe9T0x0jRvHDV0jfmmWOai9r HVag== 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=72kyylUAJre+C07vU14fj37IL/gPhEMZ5o/giSchT88=; b=uJdxVOEKv15vmHJK/qXRXLYz0fX1Ql4v/t4xic/RsbEJr7xTlu64wUPTIxkz0aNTwu cgU9+vaJ87y3/sNOjAc565aA4pqEh1yeTxgV8SymB41fk+J0PY7PlJ87r23LdkoMHxBQ 4G/w9FHd09n+yk6S+jvOVYtSWPWbLfu52Crk+74fgawyGTVD5ww4UTl4+cgvuP3p++ib yKs2TyLh800F2QpfaKA1D5EC2GOzdKhP5I+MsMdx7nalLSBPeR7oxUlxD1FROBp1hesT skX2UTyR/e1SZm3SdwMKZTZ5flbF4Ot+fcNV4abqz6kM/E8/3ZQ8q0E4KUDDev1HfMIM R4xg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=jr9TJYZ4; 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 hd10-20020a170907968a00b007aeebbe2ec4si1473104ejc.784.2022.11.17.10.39.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 10:39:42 -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=jr9TJYZ4; 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 7D1D03853D55 for ; Thu, 17 Nov 2022 18:39:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7D1D03853D55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668710381; bh=72kyylUAJre+C07vU14fj37IL/gPhEMZ5o/giSchT88=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jr9TJYZ4oaku/srIDQQ/SHplPH0EEdvskwOfxZfs97zHHIlBj4vpDgOgcy4AklhQ6 KjDyRqXOjQwsSJIaQEKCnMSTMgIIJiG7uqBd6j0BnU8v2nwJ7fWIJlpMn1ORrhTQTC MkOs0sKQvcr2daEDl575S/50wvKZXNOxqXnhTXzU= 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 E3CF33853D5D for ; Thu, 17 Nov 2022 18:38:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E3CF33853D5D 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-472-37PjgXPvM5uRME8cLQfrgw-1; Thu, 17 Nov 2022 13:38:32 -0500 X-MC-Unique: 37PjgXPvM5uRME8cLQfrgw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0AE551C05EB3 for ; Thu, 17 Nov 2022 18:38:32 +0000 (UTC) Received: from pdp-11.redhat.com (unknown [10.22.10.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id E3205111DCE7; Thu, 17 Nov 2022 18:38:31 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: constinit on pointer to function [PR104066] Date: Thu, 17 Nov 2022 13:38:10 -0500 Message-Id: <20221117183810.33353-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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?1749769658585366889?= X-GMAIL-MSGID: =?utf-8?q?1749769658585366889?= [dcl.constinit]: "The constinit specifier shall be applied only to a declaration of a variable with static or thread storage duration." Thus, this ought to be OK: constinit void (*p)() = nullptr; but the error message I introduced when implementing constinit was not looking at funcdecl_p, so the code above was rejected. Fixed thus. I'm checking constinit_p first because I think that's far more likely to be false than funcdecl_p. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? I think I'd like to backport this all the way back to 10. PR c++/104066 gcc/cp/ChangeLog: * decl.cc (grokdeclarator): Check funcdecl_p before complaining about constinit. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constinit18.C: New test. --- gcc/cp/decl.cc | 2 +- gcc/testsuite/g++.dg/cpp2a/constinit18.C | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constinit18.C base-commit: ee892832ea19b21a3420ef042e582204fac852a2 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index d28889ed865..9a7b1a6c381 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -13071,7 +13071,7 @@ grokdeclarator (const cp_declarator *declarator, "an array", name); return error_mark_node; } - if (constinit_p) + if (constinit_p && funcdecl_p) { error_at (declspecs->locations[ds_constinit], "% on function return type is not " diff --git a/gcc/testsuite/g++.dg/cpp2a/constinit18.C b/gcc/testsuite/g++.dg/cpp2a/constinit18.C new file mode 100644 index 00000000000..51b4f0273be --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constinit18.C @@ -0,0 +1,12 @@ +// PR c++/104066 +// { dg-do compile { target c++20 } } + +constinit void (*p)() = nullptr; +constinit void (*pp)() = nullptr; +void fn(); +constinit void (&r)() = fn; + +extern constinit long (* const syscall_reexported) (long, ...); + +constinit void bad (); // { dg-error ".constinit. on function return type is not allowed" } +constinit void bad () { } // { dg-error ".constinit. on function return type is not allowed" }