From patchwork Wed Dec 7 22:08:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 61685 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9AF7438A8155 for ; Wed, 7 Dec 2022 22:09:25 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa2.mentor.iphmx.com (esa2.mentor.iphmx.com [68.232.141.98]) by sourceware.org (Postfix) with ESMTPS id 6BD1F388FB78 for ; Wed, 7 Dec 2022 22:09:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6BD1F388FB78 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.96,225,1665475200"; d="scan'208";a="89344048" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa2.mentor.iphmx.com with ESMTP; 07 Dec 2022 14:09:01 -0800 IronPort-SDR: 9GZKC9//ahJa4FVgCclOj7NWd57HIBEj124al87oHF2bUO3KAeeX+9+WHMVVAKURd1MaJWBrH1 05Mg/OFJviNbZFGK57MkMOkBDSv73fatAMSdsO0OUh4zl//QuUpglg9bpeza2tid/MjGgaCy4x wfqPlPEAm+Ioamep7ogVyyd8EIddsrjBuwmMBh9oESBzKReU2mbYN4V/IW//rtb4uIrh2ghPuP GPeFau5SvhysK7WYiNU82N1meGtK88VDWalg5A1ue6spgvgRVABnhLaQ7UrymsV2nDpIrxG4l5 fbk= Date: Wed, 7 Dec 2022 22:08:56 +0000 From: Joseph Myers To: Subject: [committed] c: Diagnose auto constexpr used with a type Message-ID: MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: SVR-IES-MBX-08.mgc.mentorg.com (139.181.222.8) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-3115.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, 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: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The constraints on auto in C2x disallow use with other storage-class specifiers unless the type is inferred from an initializer. That includes constexpr; add the missing checks for this case (the combination of auto, constexpr and a type specifier). Bootstrapped with no regressions for x86_64-pc-linux-gnu. gcc/c/ * c-decl.cc (declspecs_add_type, declspecs_add_scspec): Check for auto, constexpr and a type used together. gcc/testsuite/ * gcc.dg/c2x-constexpr-1.c: Do not use auto, constexpr and a type together. * gcc.dg/c2x-constexpr-3.c: Add tests of auto, constexpr and type used together. diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 111f05e2a40..e47ca6718b3 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -11430,6 +11430,10 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs, else if (specs->thread_p) error ("%qs used with %", specs->thread_gnu_p ? "__thread" : "_Thread_local"); + else if (specs->constexpr_p) + /* auto may only be used with another storage class specifier, + such as constexpr, if the type is inferred. */ + error ("% used with %"); else specs->storage_class = csc_auto; } @@ -12363,6 +12367,10 @@ declspecs_add_scspec (location_t loc, return specs; } n = csc_auto; + /* auto may only be used with another storage class specifier, + such as constexpr, if the type is inferred. */ + if (specs->constexpr_p) + error ("%qE used with %", scspec); break; case RID_EXTERN: n = csc_extern; @@ -12393,6 +12401,10 @@ declspecs_add_scspec (location_t loc, error ("%qE used with %", scspec); else if (specs->storage_class == csc_typedef) error ("%qE used with %", scspec); + else if (specs->storage_class == csc_auto) + /* auto may only be used with another storage class specifier, + such as constexpr, if the type is inferred. */ + error ("%qE used with %", scspec); else if (specs->thread_p) error ("%qE used with %qs", scspec, specs->thread_gnu_p ? "__thread" : "_Thread_local"); diff --git a/gcc/testsuite/gcc.dg/c2x-constexpr-1.c b/gcc/testsuite/gcc.dg/c2x-constexpr-1.c index f7f64e2d300..d43d95ddd7c 100644 --- a/gcc/testsuite/gcc.dg/c2x-constexpr-1.c +++ b/gcc/testsuite/gcc.dg/c2x-constexpr-1.c @@ -180,10 +180,10 @@ f0 () { constexpr int fv0 = 3; static_assert (fv0 == 3); - auto constexpr int fv1 = 4; + auto constexpr fv1 = 4; static_assert (fv1 == 4); register constexpr float fv2 = 1.0; - constexpr auto int fv3 = 123; + constexpr auto fv3 = 123; static_assert (fv3 == 123); constexpr register void *fv4 = (void *) 0; const int *fv5 = &(constexpr int) { 234 }; diff --git a/gcc/testsuite/gcc.dg/c2x-constexpr-3.c b/gcc/testsuite/gcc.dg/c2x-constexpr-3.c index 16e56db2835..29fedc03afd 100644 --- a/gcc/testsuite/gcc.dg/c2x-constexpr-3.c +++ b/gcc/testsuite/gcc.dg/c2x-constexpr-3.c @@ -225,4 +225,12 @@ f0 () constexpr typeof (nullptr) not_npc = nullptr; int *ptr = 0; (void) (ptr == not_npc); /* { dg-error "invalid operands" } */ + /* auto may only be used with another storage class specifier, such as + constexpr, if the type is inferred. */ + auto constexpr int a_c_t = 1; /* { dg-error "'auto' used with 'constexpr'" } */ + constexpr auto int c_a_t = 1; /* { dg-error "'auto' used with 'constexpr'" } */ + auto int constexpr a_t_c = 1; /* { dg-error "'constexpr' used with 'auto'" } */ + constexpr int auto c_t_a = 1; /* { dg-error "'auto' used with 'constexpr'" } */ + int auto constexpr t_a_c = 1; /* { dg-error "'constexpr' used with 'auto'" } */ + int constexpr auto t_c_a = 1; /* { dg-error "'auto' used with 'constexpr'" } */ }