]> 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>
Thu, 24 Mar 2022 09:12:25 +0000 (10:12 +0100)
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.

The following patch implements the proposed wording of CWG 2552 in addition
to fixing the C++14 - C++20 handling bug.
If you'd like instead to keep the current pedantic C++23 wording for now,
that would mean taking out the first hunk (cxx_eval_constant_expression) and
g++.dg/cpp23/constexpr-nonlit2.C hunk.

2022-03-24  Jakub Jelinek  <jakub@redhat.com>

PR c++/104994
* constexpr.cc (cxx_eval_constant_expression): Don't diagnose passing
through extern thread_local declarations.  Change wording from
declaration to definition.
(potential_constant_expression_1): Don't diagnose extern thread_local
declarations.  Change wording from declared to defined.
* decl.cc (start_decl): Likewise.

* g++.dg/diagnostic/constexpr1.C: Change expected diagnostic wording
from declared to defined.
* g++.dg/cpp23/constexpr-nonlit1.C: Likewise.
(garply): Change dg-error into dg-bogus.
* g++.dg/cpp23/constexpr-nonlit2.C: Change expected diagnostic wording
from declaration to definition.
* g++.dg/cpp23/constexpr-nonlit6.C: Change expected diagnostic wording
from declared to defined.
* g++.dg/cpp23/constexpr-nonlit7.C: New test.
* g++.dg/cpp2a/constexpr-try5.C: Change expected diagnostic wording
from declared to defined.
* g++.dg/cpp2a/consteval3.C: Likewise.

gcc/cp/constexpr.cc
gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp23/constexpr-nonlit1.C
gcc/testsuite/g++.dg/cpp23/constexpr-nonlit2.C
gcc/testsuite/g++.dg/cpp23/constexpr-nonlit6.C
gcc/testsuite/g++.dg/cpp23/constexpr-nonlit7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/consteval3.C
gcc/testsuite/g++.dg/cpp2a/constexpr-try5.C
gcc/testsuite/g++.dg/diagnostic/constexpr1.C

index a3136ce819dd9b6beebd7e04e4fda132cb765c9f..ce4ef8edfacbe2e86e0ef3850e8f26ffecada04b 100644 (file)
@@ -6723,17 +6723,18 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
          }
 
        if (VAR_P (r)
-           && (TREE_STATIC (r) || CP_DECL_THREAD_LOCAL_P (r))
+           && (TREE_STATIC (r)
+               || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
            /* Allow __FUNCTION__ etc.  */
            && !DECL_ARTIFICIAL (r))
          {
            if (!ctx->quiet)
              {
                if (CP_DECL_THREAD_LOCAL_P (r))
-                 error_at (loc, "control passes through declaration of %qD "
+                 error_at (loc, "control passes through definition of %qD "
                                 "with thread storage duration", r);
                else
-                 error_at (loc, "control passes through declaration of %qD "
+                 error_at (loc, "control passes through definition of %qD "
                                 "with static storage duration", r);
              }
            *non_constant_p = true;
@@ -9188,17 +9189,17 @@ 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 (CP_DECL_THREAD_LOCAL_P (tmp))
+         if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
            {
              if (flags & tf_error)
-               error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
+               error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined "
                          "%<thread_local%> in %<constexpr%> context", tmp);
              return false;
            }
          else if (TREE_STATIC (tmp))
            {
              if (flags & tf_error)
-               error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
+               error_at (DECL_SOURCE_LOCATION (tmp), "%qD defined "
                          "%<static%> in %<constexpr%> context", tmp);
              return false;
            }
index efef1b7370f43b488daf176f3e440aa4540ff0dd..68741bbf5d2af69be5c26d822f989362be678e32 100644 (file)
@@ -5774,15 +5774,15 @@ start_decl (const cp_declarator *declarator,
       && cxx_dialect < cxx23)
     {
       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 only "
+                 "%qD defined %<thread_local%> in %qs function only "
                  "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
                  DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
                  ? "consteval" : "constexpr");
       else if (TREE_STATIC (decl))
        error_at (DECL_SOURCE_LOCATION (decl),
-                 "%qD declared %<static%> in %qs function only available "
+                 "%qD defined %<static%> in %qs function only available "
                  "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl,
                  DECL_IMMEDIATE_FUNCTION_P (current_function_decl)
                  ? "consteval" : "constexpr");
index c80ea38732f5a3db4e71c07692176c7aa33fbc68..c60fb8cdbd8aec78db26573c9dceb5192a1a6750 100644 (file)
@@ -23,7 +23,7 @@ baz (int x)
 {
   if (!x)
     return 1;
-  static int a;        // { dg-error "'a' declared 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+  static int a;        // { dg-error "'a' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
   return ++a;  // { dg-error "uninitialized variable 'a' in 'constexpr' function" "" { target c++17_down } .-1 }
 }
 
@@ -32,7 +32,7 @@ qux (int x)
 {
   if (!x)
     return 1;
-  thread_local int a;  // { dg-error "'a' declared 'thread_local' in 'constexpr' function only available with" "" { target c++20_down } }
+  thread_local int a;  // { dg-error "'a' defined 'thread_local' in 'constexpr' function only available with" "" { target c++20_down } }
   return ++a;  // { dg-error "uninitialized variable 'a' in 'constexpr' function" "" { target c++17_down } .-1 }
 }
 
@@ -41,7 +41,7 @@ garply (int x)
 {
   if (!x)
     return 1;
-  extern thread_local int a;   // { dg-error "'a' declared 'thread_local' in 'constexpr' function only available with" "" { target c++20_down } }
+  extern thread_local int a;   // { dg-bogus "'thread_local' in 'constexpr' function only available with" "" { target c++20_down } }
   return ++a;
 }
 
index 0f7b2299e7a5fe55f49fd191dada7a6b0d8689f0..b01f728291d25414dfdf68dd86104d29ac15ce68 100644 (file)
@@ -24,7 +24,7 @@ baz (int x)
 {
   if (!x)
     return 1;
-  static int a;                // { dg-error "control passes through declaration of 'a' with static storage duration" }
+  static int a;                // { dg-error "control passes through definition of 'a' with static storage duration" }
   return ++a;
 }
 
@@ -33,7 +33,7 @@ qux (int x)
 {
   if (!x)
     return 1;
-  thread_local int a;  // { dg-error "control passes through declaration of 'a' with thread storage duration" }
+  thread_local int a;  // { dg-error "control passes through definition of 'a' with thread storage duration" }
   return ++a;
 }
 
index 11cb5189c85d2ae9b131dbd2da815add779aa09b..fbeb83075b05be95bac3772a4e96ca3d48f7ed37 100644 (file)
@@ -13,13 +13,13 @@ lab:
 constexpr int
 bar ()
 {
-  static int a;                // { dg-error "'a' declared 'static' in 'constexpr' context" }
+  static int a;                // { dg-error "'a' defined 'static' in 'constexpr' context" }
   return ++a;
 }
 
 constexpr int
 baz (int x)
 {
-  thread_local int a;  // { dg-error "'a' declared 'thread_local' in 'constexpr' context" }
+  thread_local int a;  // { dg-error "'a' defined 'thread_local' in 'constexpr' context" }
   return ++a;
 }
diff --git a/gcc/testsuite/g++.dg/cpp23/constexpr-nonlit7.C b/gcc/testsuite/g++.dg/cpp23/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 ();
index 8f9316411b5ad40d87304d8a3de30857cfc6da91..8346386866847b12ef49e1e3ba591cd1c860a2c6 100644 (file)
@@ -56,8 +56,8 @@ template consteval float f12 (float x); // { dg-error "explicit instantiation sh
 consteval int
 f13 (int x)
 {
-  static int a = 5;            // { dg-error "'a' declared 'static' in 'consteval' function only available with" "" { target c++20_only } }
-                               // { dg-error "'a' declared 'static' in 'constexpr' context" "" { target c++23 } .-1 }
-  thread_local int b = 6;      // { dg-error "'b' declared 'thread_local' in 'consteval' function only available with" "" { target c++20_only } }
+  static int a = 5;            // { dg-error "'a' defined 'static' in 'consteval' function only available with" "" { target c++20_only } }
+                               // { dg-error "'a' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 }
+  thread_local int b = 6;      // { dg-error "'b' defined 'thread_local' in 'consteval' function only available with" "" { target c++20_only } }
   return x;
 }
index ed5e40dff955479542ad90b1fe3e509826e62b78..216634dc56c3014d130f594506dfd6a5d43ec9ab 100644 (file)
@@ -5,14 +5,14 @@
 constexpr int foo ()
 try {                  // { dg-warning "function-try-block body of 'constexpr' function only available with" "" { target c++17_down } }
   int a;               // { dg-error "uninitialized variable 'a' in 'constexpr' function" "" { target c++17_down } }
-  static double b = 1.0;// { dg-error "'b' declared 'static' in 'constexpr' function only available with" "" { target c++20_down } }
-                       // { dg-error "'b' declared 'static' in 'constexpr' context" "" { target c++23 } .-1 }
+  static double b = 1.0;// { dg-error "'b' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+                       // { dg-error "'b' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 }
   goto l;              // { dg-error "'goto' in 'constexpr' function only available with" "" { target c++20_down } }
   l:;
   return 0;
 } catch (...) {
   long int c;          // { dg-error "uninitialized variable 'c' in 'constexpr' function" "" { target c++17_down } }
-  static float d = 2.0f;// { dg-error "'d' declared 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+  static float d = 2.0f;// { dg-error "'d' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
   goto l2;             // { dg-error "'goto' in 'constexpr' function only available with" "" { target c++20_down } }
   l2:;
   return -1;
@@ -21,20 +21,20 @@ try {                       // { dg-warning "function-try-block body of 'constexpr' function only av
 constexpr int bar ()
 {
   int a;               // { dg-error "uninitialized variable 'a' in 'constexpr' function" "" { target c++17_down } }
-  static long double b = 3.0;// { dg-error "'b' declared 'static' in 'constexpr' function only available with" "" { target c++20_down } }
-                       // { dg-error "'b' declared 'static' in 'constexpr' context" "" { target c++23 } .-1 }
+  static long double b = 3.0;// { dg-error "'b' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+                       // { dg-error "'b' defined 'static' in 'constexpr' context" "" { target c++23 } .-1 }
   goto l;              // { dg-error "'goto' in 'constexpr' function only available with" "" { target c++20_down } }
   l:;
   try {                        // { dg-warning "'try' in 'constexpr' function only available with" "" { target c++17_down } }
     short c;           // { dg-error "uninitialized variable 'c' in 'constexpr' function" "" { target c++17_down } }
-    static float d;    // { dg-error "'d' declared 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+    static float d;    // { dg-error "'d' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
                        // { dg-error "uninitialized variable 'd' in 'constexpr' function" "" { target c++17_down } .-1 }
     goto l2;           // { dg-error "'goto' in 'constexpr' function only available with" "" { target c++20_down } }
     l2:;
     return 0;
   } catch (int) {
     char e;            // { dg-error "uninitialized variable 'e' in 'constexpr' function" "" { target c++17_down } }
-    static int f = 5;  // { dg-error "'f' declared 'static' in 'constexpr' function only available with" "" { target c++20_down } }
+    static int f = 5;  // { dg-error "'f' defined 'static' in 'constexpr' function only available with" "" { target c++20_down } }
     goto l3;           // { dg-error "'goto' in 'constexpr' function only available with" "" { target c++20_down } }
     l3:;
     return 1;
index f2bcec6e4e16a6d7cff9a8acda6cf4a14c97f277..c962a60c847be805670ab0c518f646c5e0b14232 100644 (file)
@@ -1,7 +1,7 @@
 // { dg-do compile { target c++11 } }
 
-constexpr int foo() { thread_local int i __attribute__((unused)) {}; return 1; }  // { dg-error "40:.i. declared .thread_local." "" { target c++20_down } }
-// { dg-error "40:.i. declared .thread_local. in .constexpr. context" "" { target c++23 } .-1 }
+constexpr int foo() { thread_local int i __attribute__((unused)) {}; return 1; }  // { dg-error "40:.i. defined .thread_local." "" { target c++20_down } }
+// { dg-error "40:.i. defined .thread_local. in .constexpr. context" "" { target c++23 } .-1 }
 
-constexpr int bar() { static int i __attribute__((unused)) {}; return 1; }  // { dg-error "34:.i. declared .static." "" { target c++20_down } }
-// { dg-error "34:.i. declared .static. in .constexpr. context" "" { target c++23 } .-1 }
+constexpr int bar() { static int i __attribute__((unused)) {}; return 1; }  // { dg-error "34:.i. defined .static." "" { target c++20_down } }
+// { dg-error "34:.i. defined .static. in .constexpr. context" "" { target c++23 } .-1 }