public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Joseph Myers <joseph@codesourcery.com>
To: <gcc-patches@gcc.gnu.org>
Subject: [committed] c: Diagnose auto constexpr used with a type
Date: Wed, 7 Dec 2022 22:08:56 +0000	[thread overview]
Message-ID: <cf3d8e34-802-15a9-d4e6-317cd1c324ff@codesourcery.com> (raw)

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 %<auto%>",
 	       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 ("%<auto%> used with %<constexpr%>");
       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 %<constexpr%>", scspec);
       break;
     case RID_EXTERN:
       n = csc_extern;
@@ -12393,6 +12401,10 @@ declspecs_add_scspec (location_t loc,
 	error ("%qE used with %<extern%>", scspec);
       else if (specs->storage_class == csc_typedef)
 	error ("%qE used with %<typedef%>", 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 %<auto%>", 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'" } */
 }

-- 
Joseph S. Myers
joseph@codesourcery.com

                 reply	other threads:[~2022-12-07 22:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cf3d8e34-802-15a9-d4e6-317cd1c324ff@codesourcery.com \
    --to=joseph@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).