56 lines · cpp
1// RUN: %clang_cc1 -std=c++2a -verify %s2 3constinit int a;4constinit thread_local int b;5constinit static int c;6 7void 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 13namespace 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 22struct 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};28int S::a; // expected-warning {{'constinit' specifier missing}}29int S::b; // expected-warning {{'constinit' specifier missing}}30const int S::c;31inline const int S::d;32 33struct 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};39constinit int T::a;40constinit const int T::b; // expected-error {{'constinit' specifier added after initialization}}41constinit const int T::c; // expected-error {{'constinit' specifier added after initialization}}42constinit inline const int T::d; // expected-error {{'constinit' specifier added after initialization}}43 44constinit void g() {} // expected-error {{constinit can only be used in variable declarations}}45 46// (These used to trigger crashes.)47void h();48constinit void h(); // expected-error {{constinit can only be used in variable declarations}}49constexpr void i(); // expected-note {{here}}50constinit 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 53typedef constinit int type; // expected-error {{typedef cannot be constinit}}54using type = constinit int; // expected-error {{type name does not allow constinit specifier}}55auto q() -> int constinit; // expected-error {{type name does not allow constinit specifier}}56