]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: extern thread_local declarations in constexpr [PR104994]
authorJakub Jelinek <jakub@redhat.com>
Thu, 24 Mar 2022 09:12:25 +0000 (10:12 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 10 May 2022 08:14:35 +0000 (10:14 +0200)
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)

gcc/cp/constexpr.c
gcc/cp/decl.c
gcc/testsuite/g++.dg/cpp2a/constexpr-nonlit7.C [new file with mode: 0644]

index 8e193edeff831761cb9e492bee859c1826b05489..dd0e955ed7b2b1e58e09e371c4789860163a7f7c 100644 (file)
@@ -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
index 47dc43177efc17a4547b805c1ef8dee5592b6c95..e923fb0fe1372d337fff4a44458283594ebe83d4 100644 (file)
@@ -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 (file)
index 0000000..0612575
--- /dev/null
@@ -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 ();