public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-10698] c++: extern thread_local declarations in constexpr [PR104994]
Date: Tue, 10 May 2022 08:25:30 +0000 (GMT)	[thread overview]
Message-ID: <20220510082530.E4C0E3838008@sourceware.org> (raw)

https://gcc.gnu.org/g:125234a3b78138e22112bb3f207df987817b6818

commit r10-10698-g125234a3b78138e22112bb3f207df987817b6818
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Mar 24 10:12:25 2022 +0100

    c++: extern thread_local declarations in constexpr [PR104994]
    
    C++14 to C++20 apparently should allow extern thread_local declarations in
    constexpr functions, however useless they are there (because accessing
    such vars is not valid in a constant expression, perhaps sizeof/decltype).
    P2242 changed that for C++23 to passing through declaration but
    https://cplusplus.github.io/CWG/issues/2552.html
    has been filed for it yesterday.
    
    2022-03-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/104994
            * constexpr.c (potential_constant_expression_1): Don't diagnose extern
            thread_local declarations.
            * decl.c (start_decl): Likewise.
    
            * g++.dg/cpp2a/constexpr-nonlit7.C: New test.
    
    (cherry picked from commit 72124f487ccb5c8065dd5f7b8fba254600b7e611)

Diff:
---
 gcc/cp/constexpr.c                             | 8 ++++----
 gcc/cp/decl.c                                  | 2 +-
 gcc/testsuite/g++.dg/cpp2a/constexpr-nonlit7.C | 6 ++++++
 3 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 8e193edeff8..dd0e955ed7b 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -8297,18 +8297,18 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
       tmp = DECL_EXPR_DECL (t);
       if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
 	{
-	  if (TREE_STATIC (tmp))
+	  if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
 	    {
 	      if (flags & tf_error)
 		error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
-			  "%<static%> in %<constexpr%> context", tmp);
+			  "%<thread_local%> in %<constexpr%> context", tmp);
 	      return false;
 	    }
-	  else if (CP_DECL_THREAD_LOCAL_P (tmp))
+	  else if (TREE_STATIC (tmp))
 	    {
 	      if (flags & tf_error)
 		error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
-			  "%<thread_local%> in %<constexpr%> context", tmp);
+			  "%<static%> in %<constexpr%> context", tmp);
 	      return false;
 	    }
 	  else if (!check_for_uninitialized_const_var
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 47dc43177ef..e923fb0fe13 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5447,7 +5447,7 @@ start_decl (const cp_declarator *declarator,
       && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
     {
       bool ok = false;
-      if (CP_DECL_THREAD_LOCAL_P (decl))
+      if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl))
 	error_at (DECL_SOURCE_LOCATION (decl),
 		  "%qD declared %<thread_local%> in %qs function", decl,
 		  DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-nonlit7.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-nonlit7.C
new file mode 100644
index 00000000000..06125756f25
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-nonlit7.C
@@ -0,0 +1,6 @@
+// PR c++/104994
+// CWG2552
+// { dg-do compile { target c++14 } }
+
+constexpr bool foo () { extern thread_local int t; return true; }
+static constexpr bool a = foo ();


                 reply	other threads:[~2022-05-10  8:25 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=20220510082530.E4C0E3838008@sourceware.org \
    --to=jakub@gcc.gnu.org \
    --cc=gcc-cvs@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).