• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++2a -verify %s
2 
3 constinit int a;
4 constinit thread_local int b;
5 constinit static int c;
6 
f()7 void f() {
8   constinit static int a;
9   constinit thread_local int b;
10   constinit int c; // expected-error {{local variable cannot be declared 'constinit'}}
11 }
12 
13 namespace missing {
14   int a; // expected-note {{add the 'constinit' specifier}}
15   extern constinit int a; // expected-error {{added after initialization}}
16 
17   // We allow inheriting 'constinit' from a forward declaration as an extension.
18   extern constinit int b; // expected-note {{here}}
19   int b; // expected-warning {{'constinit' specifier missing}}
20 }
21 
22 struct S {
23   static constinit int a; // expected-note {{here}}
24   static constinit constexpr int b; // expected-error {{cannot combine with previous}} expected-note {{here}}
25   static constinit const int c = 1;
26   static constinit const int d = 1;
27 };
28 int S::a; // expected-warning {{'constinit' specifier missing}}
29 int S::b; // expected-warning {{'constinit' specifier missing}}
30 const int S::c;
31 inline const int S::d;
32 
33 struct T {
34   static int a;
35   static constexpr int b = 1; // expected-note {{add the 'constinit' specifier}}
36   static const int c = 1; // expected-note {{add the 'constinit' specifier}}
37   static const int d = 1; // expected-note {{add the 'constinit' specifier}}
38 };
39 constinit int T::a;
40 constinit const int T::b; // expected-error {{'constinit' specifier added after initialization}}
41 constinit const int T::c; // expected-error {{'constinit' specifier added after initialization}}
42 constinit inline const int T::d; // expected-error {{'constinit' specifier added after initialization}}
43 
g()44 constinit void g() {} // expected-error {{constinit can only be used in variable declarations}}
45 
46 // (These used to trigger crashes.)
47 void h();
48 constinit void h(); // expected-error {{constinit can only be used in variable declarations}}
49 constexpr void i(); // expected-note {{here}}
50 constinit void i(); // expected-error {{non-constexpr declaration of 'i' follows constexpr declaration}}
51 // expected-error@-1 {{constinit can only be used in variable declarations}}
52 
53 typedef constinit int type; // expected-error {{typedef cannot be constinit}}
54 using type = constinit int; // expected-error {{type name does not allow constinit specifier}}
55 auto q() -> int constinit; // expected-error {{type name does not allow constinit specifier}}
56