llvm-project
203 строки · 10.2 Кб
1// RUN: %clang_cc1 -std=c++17 %s -triple x86_64-unknown-linux-gnu -verify=expected,cxx17,pre2c -fcxx-exceptions
2// RUN: %clang_cc1 -std=c++2b %s -triple x86_64-unknown-linux-gnu -verify=expected,cxx2b,pre2c,post2b -fcxx-exceptions
3// RUN: %clang_cc1 -std=c++2c %s -triple x86_64-unknown-linux-gnu -verify=expected,cxx2c,post2b -fcxx-exceptions
4// RUN: not %clang_cc1 -std=c++17 %s -triple x86_64-unknown-linux-gnu -emit-llvm-only -fcxx-exceptions
5
6struct S { int a, b, c; };
7
8// A simple-declaration can be a decompsition declaration.
9namespace SimpleDecl {
10auto [a_x, b_x, c_x] = S();
11
12void f(S s) {
13auto [a, b, c] = S();
14{
15for (auto [a, b, c] = S();;) {}
16if (auto [a, b, c] = S(); true) {}
17switch (auto [a, b, c] = S(); 0) { case 0:; }
18}
19}
20}
21
22// A for-range-declaration can be a decomposition declaration.
23namespace ForRangeDecl {
24extern S arr[10];
25void h() {
26for (auto [a, b, c] : arr) {
27}
28}
29}
30
31// Other kinds of declaration cannot.
32namespace OtherDecl {
33// A parameter-declaration is not a simple-declaration.
34// This parses as an array declaration.
35void f(auto [a, b, c]); // cxx17-error {{'auto' not allowed in function prototype}} expected-error {{'a'}}
36
37void g() {
38// A condition is allowed as a Clang extension.
39// See commentary in test/Parser/decomposed-condition.cpp
40for (; auto [a, b, c] = S(); ) {} // expected-warning {{ISO C++17 does not permit structured binding declaration in a condition}} expected-error {{value of type 'S' is not contextually convertible to 'bool'}}
41if (auto [a, b, c] = S()) {} // expected-warning {{ISO C++17 does not permit structured binding declaration in a condition}} expected-error {{value of type 'S' is not contextually convertible to 'bool'}}
42if (int n; auto [a, b, c] = S()) {} // expected-warning {{ISO C++17 does not permit structured binding declaration in a condition}} expected-error {{value of type 'S' is not contextually convertible to 'bool'}}
43switch (auto [a, b, c] = S()) {} // expected-warning {{ISO C++17 does not permit structured binding declaration in a condition}} expected-error {{statement requires expression of integer type ('S' invalid)}}
44switch (int n; auto [a, b, c] = S()) {} // expected-warning {{ISO C++17 does not permit structured binding declaration in a condition}} expected-error {{statement requires expression of integer type ('S' invalid)}}
45while (auto [a, b, c] = S()) {} // expected-warning {{ISO C++17 does not permit structured binding declaration in a condition}} expected-error {{value of type 'S' is not contextually convertible to 'bool'}}
46
47// An exception-declaration is not a simple-declaration.
48try {}
49catch (auto [a, b, c]) {} // expected-error {{'auto' not allowed in exception declaration}} expected-error {{'a'}}
50}
51
52// A member-declaration is not a simple-declaration.
53class A {
54auto [a, b, c] = S(); // expected-error {{not permitted in this context}}
55static auto [a, b, c] = S(); // expected-error {{not permitted in this context}}
56};
57}
58
59namespace GoodSpecifiers {
60void f() {
61int n[1];
62const volatile auto &[a] = n; // post2b-warning {{volatile qualifier in structured binding declaration is deprecated}}
63}
64}
65
66namespace BadSpecifiers {
67typedef int I1[1];
68I1 n;
69struct S { int n; } s;
70void f() {
71// storage-class-specifiers
72static auto &[a] = n; // cxx17-warning {{declared 'static' is a C++20 extension}}
73thread_local auto &[b] = n; // cxx17-warning {{declared 'thread_local' is a C++20 extension}}
74extern auto &[c] = n; // expected-error {{cannot be declared 'extern'}} expected-error {{declaration of block scope identifier with linkage cannot have an initializer}}
75struct S {
76mutable auto &[d] = n; // expected-error {{not permitted in this context}}
77
78// function-specifiers
79virtual auto &[e] = n; // expected-error {{not permitted in this context}}
80explicit auto &[f] = n; // expected-error {{not permitted in this context}}
81
82// misc decl-specifiers
83friend auto &[g] = n; // expected-error {{'auto' not allowed}} expected-error {{friends can only be classes or functions}}
84};
85typedef auto &[h] = n; // expected-error {{cannot be declared 'typedef'}}
86constexpr auto &[i] = n; // expected-error {{cannot be declared 'constexpr'}}
87}
88
89static constexpr inline thread_local auto &[j1] = n; // expected-error {{cannot be declared with 'constexpr inline' specifiers}}
90static thread_local auto &[j2] = n; // cxx17-warning {{declared with 'static thread_local' specifiers is a C++20 extension}}
91
92inline auto &[k] = n; // expected-error {{cannot be declared 'inline'}}
93
94const int K = 5;
95auto ([c]) = s; // expected-error {{decomposition declaration cannot be declared with parentheses}}
96void g() {
97// defining-type-specifiers other than cv-qualifiers and 'auto'
98S [a] = s; // expected-error {{cannot be declared with type 'S'}}
99decltype(auto) [b] = s; // expected-error {{cannot be declared with type 'decltype(auto)'}}
100auto ([c2]) = s; // cxx17-error {{decomposition declaration cannot be declared with parenthese}} \
101// post2b-error {{use of undeclared identifier 'c2'}} \
102// post2b-error {{expected body of lambda expression}} \
103
104// FIXME: This error is not very good.
105auto [d]() = s; // expected-error {{expected ';'}} expected-error {{expected expression}}
106auto [e][1] = s; // expected-error {{expected ';'}} expected-error {{requires an initializer}}
107
108// FIXME: This should fire the 'misplaced array declarator' diagnostic.
109int [K] arr = {0}; // expected-error {{expected ';'}} expected-error {{cannot be declared with type 'int'}} expected-error {{decomposition declaration '[K]' requires an initializer}}
110int [5] arr = {0}; // expected-error {{place the brackets after the name}}
111
112auto *[f] = s; // expected-error {{cannot be declared with type 'auto *'}} expected-error {{incompatible initializer}}
113auto S::*[g] = s; // expected-error {{cannot be declared with type 'auto BadSpecifiers::S::*'}} expected-error {{incompatible initializer}}
114
115// ref-qualifiers are OK.
116auto &&[ok_1] = S();
117auto &[ok_2] = s;
118
119// attributes are OK.
120[[]] auto [ok_3] = s;
121alignas(S) auto [ok_4] = s;
122
123auto [bad_attr_2] [[]] = s; // expected-error {{expected ';'}} expected-error {{}}
124}
125}
126
127namespace MultiDeclarator {
128struct S { int n; };
129void f(S s) {
130auto [a] = s, [b] = s; // expected-error {{must be the only declaration}}
131auto [c] = s, d = s; // expected-error {{must be the only declaration}}
132auto e = s, [f] = s; // expected-error {{must be the only declaration}}
133auto g = s, h = s, i = s, [j] = s; // expected-error {{must be the only declaration}}
134}
135}
136
137namespace Template {
138int n[3];
139// FIXME: There's no actual rule against this...
140template<typename T> auto [a, b, c] = n; // expected-error {{decomposition declaration template not supported}}
141}
142
143namespace Init {
144void f() {
145int arr[1];
146struct S { int n; };
147auto &[bad1]; // expected-error {{decomposition declaration '[bad1]' requires an initializer}}
148const auto &[bad2](S{}, S{}); // expected-error {{initializer for variable '[bad2]' with type 'const auto &' contains multiple expressions}}
149const auto &[bad3](); // expected-error {{expected expression}}
150auto &[good1] = arr;
151auto &&[good2] = S{};
152const auto &[good3](S{});
153S [goodish3] = { 4 }; // expected-error {{cannot be declared with type 'S'}}
154S [goodish4] { 4 }; // expected-error {{cannot be declared with type 'S'}}
155}
156}
157
158
159namespace attributes {
160
161struct S{
162int a;
163int b = 0;
164};
165
166void err() {
167auto [[]] = S{0}; // expected-error {{expected unqualified-id}}
168auto [ alignas(42) a, foo ] = S{0}; // expected-error {{an attribute list cannot appear here}}
169auto [ c, [[]] d ] = S{0}; // expected-error {{an attribute list cannot appear here}}
170auto [ e, alignas(42) f ] = S{0}; // expected-error {{an attribute list cannot appear here}}
171}
172
173void ok() {
174auto [ a alignas(42) [[]], b alignas(42) [[]]] = S{0}; // expected-error 2{{'alignas' attribute only applies to variables, data members and tag types}} \
175// pre2c-warning 2{{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
176auto [ c [[]] alignas(42), d [[]] alignas(42) [[]]] = S{0}; // expected-error 2{{'alignas' attribute only applies to variables, data members and tag types}} \
177// pre2c-warning 2{{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
178}
179
180
181auto [G1 [[deprecated]], G2 [[deprecated]]] = S{42}; // #deprecated-here
182// pre2c-warning@-1 2{{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
183
184int test() {
185return G1 + G2; // expected-warning {{'G1' is deprecated}} expected-note@#deprecated-here {{here}} \
186// expected-warning {{'G2' is deprecated}} expected-note@#deprecated-here {{here}}
187}
188
189void invalid_attributes() {
190// pre2c-warning@+1 {{an attribute specifier sequence attached to a structured binding declaration is a C++2c extension}}
191auto [a alignas(42) // expected-error {{'alignas' attribute only applies to variables, data members and tag types}}
192[[assume(true), // expected-error {{'assume' attribute cannot be applied to a declaration}}
193carries_dependency, // expected-error {{'carries_dependency' attribute only applies to parameters, Objective-C methods, and functions}}
194fallthrough, // expected-error {{'fallthrough' attribute cannot be applied to a declaration}}
195likely, // expected-error {{'likely' attribute cannot be applied to a declaration}}
196unlikely, // expected-error {{'unlikely' attribute cannot be applied to a declaration}}
197nodiscard, // expected-warning {{'nodiscard' attribute only applies to Objective-C methods, enums, structs, unions, classes, functions, function pointers, and typedefs}}
198noreturn, // expected-error {{'noreturn' attribute only applies to functions}}
199no_unique_address]], // expected-error {{'no_unique_address' attribute only applies to non-bit-field non-static data members}}
200b] = S{0};
201}
202
203}
204