Debian Codegraph @

clang/test/SemaCXX/builtin-structured-binding-size.cpp

raw
1 // RUN: %clang_cc1 %s -triple=x86_64 -std=c++2c -fsyntax-only -verify
2 // RUN: %clang_cc1 %s -triple=x86_64 -std=c++2c -fsyntax-only -verify -fexperimental-new-constant-interpreter
3
4
5 struct S0 {};
6 struct S1 {int a;};
7 struct S2 {int a; int b; static int c;};
8 struct S3 {double a; int b; int c;};
9 struct S4 {int a: 1; int b :2;};
10 struct S5 {int : 1; int b :2;};
11 struct S6 {union {int a;}; }; // #note-anon-union
12 struct S7 {int a[];};
13
14
15
16 struct SD : S1 {};
17 struct SE1 : S1 { int b;};
18
19 class P1 {int a;}; // #note-private
20
21 union U1 {};
22 union U2 {int a;};
23
24 template <typename T>
25 concept is_destructurable = requires {
26 { __builtin_structured_binding_size(T) };
27 };
28
29 static_assert(__builtin_structured_binding_size(S0) == 0);
30 static_assert(__is_same_as(decltype(__builtin_structured_binding_size(S0)), decltype(sizeof(void*))));
31
32 static_assert(__builtin_structured_binding_size(S1) == 0);
33 // expected-error@-1 {{static assertion failed due to requirement '__builtin_structured_binding_size(S1) == 0'}} \
34 // expected-note@-1 {{expression evaluates to '1 == 0'}}
35 static_assert(__builtin_structured_binding_size(S1) == 1);
36 static_assert(__builtin_structured_binding_size(S2) == 2);
37 static_assert(__builtin_structured_binding_size(S3) == 3);
38 static_assert(__builtin_structured_binding_size(S4) == 2);
39 static_assert(__builtin_structured_binding_size(S5) == 2);
40 // expected-error@-1 {{static assertion failed due to requirement '__builtin_structured_binding_size(S5) == 2'}} \
41 // expected-note@-1 {{expression evaluates to '1 == 2'}}
42 static_assert(__builtin_structured_binding_size(S6) == 2);
43 // expected-error@-1 {{cannot bind class type 'S6' because it has an anonymous union member}} \
44 // expected-error@-1 {{type 'S6' cannot be bound}} \
45 // expected-error@-1 {{static assertion expression is not an integral constant expression}} \
46 // expected-note@#note-anon-union {{declared here}}
47 static_assert(__builtin_structured_binding_size(S7) == 1);
48
49
50 static_assert(__builtin_structured_binding_size(SD) == 1);
51 static_assert(__builtin_structured_binding_size(SE1) == 1);
52 // expected-error@-1 {{cannot bind class type 'SE1': both it and its base class 'S1' have non-static data members}} \
53 // expected-error@-1 {{type 'SE1' cannot be bound}} \
54 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
55
56 static_assert(__builtin_structured_binding_size(U1) == 0);
57 // expected-error@-1 {{type 'U1' cannot be bound}} \
58 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
59 static_assert(__builtin_structured_binding_size(U2) == 0);
60 // expected-error@-1 {{type 'U2' cannot be bound}} \
61 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
62
63
64
65 static_assert(__builtin_structured_binding_size(int[0]) == 0);
66 static_assert(__builtin_structured_binding_size(int[1]) == 1);
67 static_assert(__builtin_structured_binding_size(int[42]) == 42);
68
69 using vec2 = int __attribute__((__vector_size__(2 * sizeof(int))));
70 using vec3 = int __attribute__((__vector_size__(3 * sizeof(int))));
71 static_assert(__builtin_structured_binding_size(vec2) == 2);
72 static_assert(__builtin_structured_binding_size(vec3) == 3);
73 static_assert(__builtin_structured_binding_size(decltype(__builtin_complex(0., 0.))) == 2);
74
75
76 int VLASize; // expected-note {{declared here}}
77 static_assert(__builtin_structured_binding_size(int[VLASize]) == 42);
78 // expected-error@-1 {{type 'int[VLASize]' cannot be bound}} \
79 // expected-warning@-1 {{variable length arrays in C++ are a Clang extension}} \
80 // expected-note@-1 {{read of non-const variable 'VLASize' is not allowed in a constant expression}} \
81 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
82
83
84 struct Incomplete; // expected-note {{forward declaration of 'Incomplete'}}
85 static_assert(__builtin_structured_binding_size(Incomplete) == 1);
86 // expected-error@-1 {{incomplete type 'Incomplete' where a complete type is required}} \
87 // expected-error@-1 {{type 'Incomplete' cannot be bound}} \
88 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
89 static_assert(__builtin_structured_binding_size(Incomplete[]) == 1);
90 // expected-error@-1 {{type 'Incomplete[]' cannot be bound}} \
91 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
92 static_assert(__builtin_structured_binding_size(Incomplete[0]) == 0);
93 static_assert(__builtin_structured_binding_size(Incomplete[1]) == 1);
94 static_assert(__builtin_structured_binding_size(Incomplete[42]) == 42);
95
96
97 static_assert(__builtin_structured_binding_size(P1) == 0);
98 // expected-error@-1 {{static assertion failed due to requirement '__builtin_structured_binding_size(P1) == 0'}} \
99 // expected-note@-1 {{expression evaluates to '1 == 0'}} \
100 // expected-error@-1 {{cannot bind private member 'a' of 'P1}} \
101 // expected-note@#note-private {{implicitly declared private here}}
102
103
104 void func(int array[14], int x = __builtin_structured_binding_size(decltype(array)));
105 //expected-error@-1 {{type 'decltype(array)' (aka 'int *') cannot be bound}}
106
107 struct SM {
108 static int array[14];
109 static_assert(__builtin_structured_binding_size(decltype(array)) == 14);
110 };
111
112 template <typename Ty, int N = __builtin_structured_binding_size(Ty)> // #tpl-1
113 struct T {
114 static constexpr int value = N;
115 };
116
117 T<int> t1;
118 // expected-error@#tpl-1 {{type 'int' cannot be bound}} \
119 // expected-error@#tpl-1 {{non-type template argument is not a constant expression}} \
120 // expected-note@-1 {{in instantiation of default argument for 'T<int>' required here}} \
121 // expected-note@-1 {{while checking a default template argument used here}} \
122
123 static_assert(T<S3>::value == 3);
124
125 static_assert(is_destructurable<S0>);
126 static_assert(is_destructurable<const S0>);
127 static_assert(is_destructurable<volatile S0>);
128 static_assert(!is_destructurable<S0&>);
129 static_assert(is_destructurable<S1>);
130 static_assert(!is_destructurable<S1&>);
131 static_assert(!is_destructurable<SE1>);
132 static_assert(!is_destructurable<int>);
133 static_assert(!is_destructurable<int[]>);
134 static_assert(is_destructurable<int[1]>);
135 static_assert(!is_destructurable<P1>);
136
137 template <typename T>
138 constexpr int f() {return 0;};
139 template <typename T>
140 requires is_destructurable<T>
141 constexpr int f() {return 1;};
142
143 static_assert(f<int>() == 0);
144 static_assert(f<S0>() == 1);
145
146 struct T0;
147 struct T1;
148 struct T42;
149 struct TSizeError;
150
151 namespace std {
152
153 template <typename>
154 struct tuple_size;
155
156 template <>
157 struct tuple_size<T0> {
158 static constexpr int value = 0;
159 };
160
161 template <>
162 struct tuple_size<T1> {
163 static constexpr int value = 1;
164 };
165
166 template <>
167 struct tuple_size<T42> {
168 static constexpr int value = 42;
169 };
170
171 template <>
172 struct tuple_size<TSizeError> {
173 static constexpr void* value = nullptr;
174 };
175
176 static_assert(__builtin_structured_binding_size(T0) == 0);
177
178 static_assert(is_destructurable<const T0>);
179 static_assert(is_destructurable<volatile T0>);
180 static_assert(!is_destructurable<T0&>);
181
182
183 static_assert(__builtin_structured_binding_size(T1) == 1);
184 static_assert(__builtin_structured_binding_size(T42) == 42);
185 static_assert(__builtin_structured_binding_size(TSizeError) == 42);
186 // expected-error@-1 {{cannot bind this type; 'std::tuple_size<TSizeError>::value' is not a valid integral constant expression}} \
187 // expected-error@-1 {{type 'TSizeError' cannot be bound}} \
188 // expected-error@-1 {{static assertion expression is not an integral constant expression}}
189 static_assert(!is_destructurable<TSizeError>);
190 }
191
192
193 struct S {
194 int x;
195 int y;
196 static_assert(__builtin_structured_binding_size(S) == 2);
197 //expected-error@-1 {{incomplete type 'S' where a complete type is required}} \
198 // expected-error@-1 {{type 'S' cannot be bound}} \
199 // expected-error@-1 {{static assertion expression is not an integral constant expression}} \
200 // expected-note@-4 {{definition of 'S' is not complete until the closing '}'}}
201 };
202
203 // Check we can implement std::exec::tag_of_t
204 template <typename T>
205 struct type_identity {
206 using type = T;
207 };
208 template<typename T> T &&declval();
209
210 template <typename T>
211 requires (__builtin_structured_binding_size(T) >=2)
212 consteval auto tag_of_impl(T& t) {
213 auto && [tag, ..._] = t;
214 return type_identity<decltype(auto(tag))>{};
215 }
216
217 template <typename T>
218 requires (__builtin_structured_binding_size(T) >=2) // #tag-of-constr
219 using tag_of_t = decltype(tag_of_impl(declval<T&>()))::type;
220
221 static_assert(__is_same_as(tag_of_t<S2>, int));
222 static_assert(__is_same_as(tag_of_t<S3>, double));
223
224
225 static_assert(__is_same_as(tag_of_t<S1>, int));
226 // expected-error@-1 {{constraints not satisfied for alias template 'tag_of_t' [with T = S1]}} \
227 // expected-note@#tag-of-constr {{because '__builtin_structured_binding_size(S1) >= 2' (1 >= 2) evaluated to false}}
228
229 static_assert(__is_same_as(tag_of_t<int>, int)); // error
230 // expected-error@-1 {{constraints not satisfied for alias template 'tag_of_t' [with T = int]}}
231 // expected-note@#tag-of-constr {{because substituted constraint expression is ill-formed: type 'int' cannot be bound}}
232
233 struct MinusOne;
234 template <> struct ::std::tuple_size<MinusOne> {
235 static constexpr int value = -1;
236 };
237 int minus_one = __builtin_structured_binding_size(MinusOne);
238 // expected-error@-1 {{cannot bind this type; 'std::tuple_size<MinusOne>::value' is not a valid size: -1}}
239 // expected-error@-2 {{type 'MinusOne' cannot be bound}}
240
241 struct UintMax;
242 template <> struct ::std::tuple_size<UintMax> {
243 static constexpr unsigned value = -1;
244 };
245 int uint_max = __builtin_structured_binding_size(UintMax);
246 // expected-error@-1 {{cannot bind this type; 'std::tuple_size<UintMax>::value' is not a valid size: 4294967295}}
247 // expected-error@-2 {{type 'UintMax' cannot be bound}}