Debian Codegraph @

gcc/testsuite/gdc.test/runnable/declaration.d

raw
1 /*
2 TEST_OUTPUT:
3 ---
4 S7019(16), 16
5 S7019(24), 24
6 S7019(32), 32
7 ---
8
9 RUN_OUTPUT:
10 ---
11 Success
12 ---
13 */
14
15 extern(C) int printf(const char*, ...);
16
17 /***************************************************/
18 // https://issues.dlang.org/show_bug.cgi?id=6475
19
20 class Foo6475(Value)
21 {
22 template T1(size_t n){ alias int T1; }
23 }
24
25 void test6475()
26 {
27 alias Foo6475!(int) C1;
28 alias C1.T1!0 X1;
29 static assert(is(X1 == int));
30
31 alias const(Foo6475!(int)) C2;
32 alias C2.T1!0 X2;
33 static assert(is(X2 == int));
34 }
35
36 /***************************************************/
37 // https://issues.dlang.org/show_bug.cgi?id=6905
38
39 void test6905()
40 {
41 auto foo1() { static int n; return n; }
42 auto foo2() { int n; return n; }
43 auto foo3() { return 1; }
44 static assert(typeof(&foo1).stringof == "int delegate() nothrow @nogc @safe");
45 static assert(typeof(&foo2).stringof == "int delegate() pure nothrow @nogc @safe");
46 static assert(typeof(&foo3).stringof == "int delegate() pure nothrow @nogc @safe");
47
48 ref bar1() { static int n; return n; }
49 static assert(!__traits(compiles, {
50 ref bar2() { int n; return n; }
51 }));
52 static assert(!__traits(compiles, {
53 ref bar3() { return 1; }
54 }));
55
56 auto ref baz1() { static int n; return n; }
57 auto ref baz2() { int n; return n; }
58 auto ref baz3() { return 1; }
59 static assert(typeof(&baz1).stringof == "int delegate() nothrow @nogc ref @safe");
60 static assert(typeof(&baz2).stringof == "int delegate() pure nothrow @nogc @safe");
61 static assert(typeof(&baz3).stringof == "int delegate() pure nothrow @nogc @safe");
62 }
63
64 /***************************************************/
65 // https://issues.dlang.org/show_bug.cgi?id=7019
66
67 struct S7019
68 {
69 int store;
70 this(int n)
71 {
72 store = n << 3;
73 }
74 }
75
76 S7019 rt_gs = 2;
77 enum S7019 ct_gs = 2;
78 pragma(msg, ct_gs, ", ", ct_gs.store);
79
80 void test7019()
81 {
82 S7019 rt_ls = 3; // this compiles fine
83 enum S7019 ct_ls = 3;
84 pragma(msg, ct_ls, ", ", ct_ls.store);
85
86 static class C
87 {
88 S7019 rt_fs = 4;
89 enum S7019 ct_fs = 4;
90 pragma(msg, ct_fs, ", ", ct_fs.store);
91 }
92
93 auto c = new C;
94 assert(rt_gs == S7019(2) && rt_gs.store == 16);
95 assert(rt_ls == S7019(3) && rt_ls.store == 24);
96 assert(c.rt_fs == S7019(4) && c.rt_fs.store == 32);
97 static assert(ct_gs == S7019(2) && ct_gs.store == 16);
98 static assert(ct_ls == S7019(3) && ct_ls.store == 24);
99 static assert(C.ct_fs == S7019(4) && C.ct_fs.store == 32);
100
101 void foo(S7019 s = 5) // fixing bug 7152
102 {
103 assert(s.store == 5 << 3);
104 }
105 foo();
106 }
107
108 /***************************************************/
109 // https://issues.dlang.org/show_bug.cgi?id=7239
110
111 struct vec7239
112 {
113 float x, y, z, w;
114 alias x r; //! for color access
115 alias y g; //! ditto
116 alias z b; //! ditto
117 alias w a; //! ditto
118 }
119
120 void test7239()
121 {
122 vec7239 a = {x: 0, g: 0, b: 0, a: 1};
123 assert(a.r == 0);
124 assert(a.g == 0);
125 assert(a.b == 0);
126 assert(a.a == 1);
127 }
128
129 /***************************************************/
130 struct S10635
131 {
132 string str;
133
134 this(string[] v) { str = v[0]; }
135 this(string[string] v) { str = v.keys[0]; }
136 }
137
138 S10635 s10635a = ["getnonce"];
139 S10635 s10635b = ["getnonce" : "str"];
140
141 void test10635()
142 {
143 S10635 sa = ["getnonce"];
144 S10635 sb = ["getnonce" : "str"];
145 }
146
147 /***************************************************/
148 // https://issues.dlang.org/show_bug.cgi?id=8123
149
150 void test8123()
151 {
152 struct S { }
153
154 struct AS
155 {
156 alias S Alias;
157 }
158
159 struct Wrapper
160 {
161 AS as;
162 }
163
164 Wrapper w;
165 static assert(is(typeof(w.as).Alias == S)); // fail
166 static assert(is(AS.Alias == S)); // ok
167 static assert(is(typeof(w.as) == AS)); // ok
168 static assert(is(typeof(w.as).Alias == AS.Alias)); // fail
169 }
170
171 /***************************************************/
172 // https://issues.dlang.org/show_bug.cgi?id=8147
173
174 enum A8147 { a, b, c }
175
176 @property ref T front8147(T)(T[] a)
177 if (!is(T[] == void[]))
178 {
179 return a[0];
180 }
181
182 template ElementType8147(R)
183 {
184 static if (is(typeof({ R r = void; return r.front8147; }()) T))
185 alias T ElementType8147;
186 else
187 alias void ElementType8147;
188 }
189
190 void test8147()
191 {
192 auto arr = [A8147.a];
193 alias typeof(arr) R;
194 auto e = ElementType8147!R.init;
195 }
196
197 /***************************************************/
198 // https://issues.dlang.org/show_bug.cgi?id=8410
199
200 void test8410()
201 {
202 struct Foo { int[15] x; string s; }
203
204 Foo[5] a1 = Foo([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], "hello"); // OK
205 Foo f = { s: "hello" }; // OK (not static)
206 Foo[5] a2 = { s: "hello" }; // error
207 }
208
209 /***************************************************/
210 // https://issues.dlang.org/show_bug.cgi?id=8942
211
212 alias const int A8942_0;
213 static assert(is(A8942_0 == const int)); // passes
214
215 void test8942()
216 {
217 alias const int A8942_1;
218 static assert(is(A8942_1 == const int)); // passes
219
220 static struct S { int i; }
221 foreach (Unused; typeof(S.tupleof))
222 {
223 alias const(int) A8942_2;
224 static assert(is(A8942_2 == const int)); // also passes
225
226 alias const int A8942_3;
227 static assert(is(A8942_3 == const int)); // fails
228 // Error: static assert (is(int == const(int))) is false
229 }
230 }
231
232 /***************************************************/
233 // https://issues.dlang.org/show_bug.cgi?id=10144
234
235 final class TNFA10144(char_t)
236 {
237 enum Act { don }
238 const Act[] action_lookup1 = [ Act.don, ];
239 }
240 alias X10144 = TNFA10144!char;
241
242 class C10144
243 {
244 enum Act { don }
245 synchronized { enum x1 = [Act.don]; }
246 override { enum x2 = [Act.don]; }
247 abstract { enum x3 = [Act.don]; }
248 final { enum x4 = [Act.don]; }
249 synchronized { static s1 = [Act.don]; }
250 override { static s2 = [Act.don]; }
251 abstract { static s3 = [Act.don]; }
252 final { static s4 = [Act.don]; }
253 synchronized { __gshared gs1 = [Act.don]; }
254 override { __gshared gs2 = [Act.don]; }
255 abstract { __gshared gs3 = [Act.don]; }
256 final { __gshared gs4 = [Act.don]; }
257 }
258
259 /***************************************************/
260
261 // https://issues.dlang.org/show_bug.cgi?id=10142
262
263 class File10142
264 {
265 enum Access : ubyte { Read = 0x01 }
266 enum Open : ubyte { Exists = 0 }
267 enum Share : ubyte { None = 0 }
268 enum Cache : ubyte { None = 0x00 }
269
270 struct Style
271 {
272 Access access;
273 Open open;
274 Share share;
275 Cache cache;
276 }
277 enum Style ReadExisting = { Access.Read, Open.Exists };
278
279 this (const(char[]) path, Style style = ReadExisting)
280 {
281 assert(style.access == Access.Read);
282 assert(style.open == Open .Exists);
283 assert(style.share == Share .None);
284 assert(style.cache == Cache .None);
285 }
286 }
287
288 void test10142()
289 {
290 auto f = new File10142("dummy");
291 }
292
293 /***************************************************/
294 // https://issues.dlang.org/show_bug.cgi?id=11421
295
296 void test11421()
297 {
298 // AAs in array
299 const a1 = [[1:2], [3:4]]; // ok <- error
300 const int[int][] a2 = [[1:2], [3:4]]; // ok
301 static assert(is(typeof(a1) == typeof(a2)));
302
303 // AAs in AA
304 auto aa = [1:["a":1.0], 2:["b":2.0]];
305 static assert(is(typeof(aa) == double[string][int]));
306 assert(aa[1]["a"] == 1.0);
307 assert(aa[2]["b"] == 2.0);
308 }
309
310 /***************************************************/
311 // https://issues.dlang.org/show_bug.cgi?id=13776
312
313 enum a13776(T) = __traits(compiles, { T; });
314
315 enum b13776(A...) = 1;
316
317 template x13776s()
318 {
319 struct S;
320 alias x13776s = b13776!(a13776!S);
321 }
322 template y13776s()
323 {
324 struct S;
325 alias x2 = b13776!(a13776!S);
326 alias y13776s = x2;
327 }
328 template z13776s()
329 {
330 struct S;
331 alias x1 = a13776!S;
332 alias x2 = b13776!(x1);
333 alias z13776s = x2;
334 }
335
336 template x13776c()
337 {
338 class C;
339 alias x13776c = b13776!(a13776!C);
340 }
341 template y13776c()
342 {
343 class C;
344 alias x2 = b13776!(a13776!C);
345 alias y13776c = x2;
346 }
347 template z13776c()
348 {
349 class C;
350 alias x1 = a13776!C;
351 alias x2 = b13776!(x1);
352 alias z13776c = x2;
353 }
354
355 void test13776()
356 {
357 alias xs = x13776s!(); // ok <- ng
358 alias ys = y13776s!(); // ok <- ng
359 alias zs = z13776s!(); // ok
360
361 alias xc = x13776c!(); // ok <- ng
362 alias yc = y13776c!(); // ok <- ng
363 alias zc = z13776c!(); // ok
364 }
365
366 /***************************************************/
367 // https://issues.dlang.org/show_bug.cgi?id=14090
368
369 template Packed14090(Args...)
370 {
371 enum x = __traits(compiles, { Args[0] v; });
372 // Args[0] is an opaque struct Empty, so the variable declaration fails to compile.
373 // The error message creation calls TypeStruct('_Empty')->toChars(), and
374 // it wrongly calls TemplateInstance('RoundRobin!()')->toAlias().
375 // Finally it will cause incorrect "recursive template instantiation" error.
376 }
377
378 template Map14090(A...)
379 {
380 alias Map14090 = A[0];
381 }
382
383 template RoundRobin14090()
384 {
385 struct Empty;
386 alias RoundRobin14090 = Map14090!(Packed14090!(Empty));
387 }
388
389 alias roundRobin14090 = RoundRobin14090!();
390
391 /***************************************************/
392 // https://issues.dlang.org/show_bug.cgi?id=13950
393
394 template Tuple13950(T...) { alias T Tuple13950; }
395
396 void f13950(int x = 0, Tuple13950!() xs = Tuple13950!())
397 {
398 assert(x == 0);
399 assert(xs.length == 0);
400 }
401
402 void test13950()
403 {
404 f13950();
405 }
406
407 /***************************************************/
408
409
410 void testlocalref()
411 {
412 int x = 4;
413 ref int rx = x;
414 rx = 5;
415 assert(x == 5);
416 ref int r2 = rx;
417 r2 = 6;
418 assert(x == 6);
419 }
420
421 /***************************************************/
422
423 int global;
424
425 ref int tgr() { return global; }
426
427 void testglobalref()
428 {
429 auto i = tgr();
430 i = 1;
431 assert(global == 0);
432 }
433
434 /***************************************************/
435
436 void testAutoRef()
437 {
438 auto ref int x = 4;
439 auto ref int y = x;
440 auto ref z = x + 0;
441 auto ref char w;
442 auto ref float v = x; // type conversion
443
444 static assert(!__traits(isRef, x));
445 static assert( __traits(isRef, y));
446 static assert(!__traits(isRef, z));
447 static assert(!__traits(isRef, w));
448 static assert(!__traits(isRef, v));
449
450 assert(&y == &x);
451 assert(&z != &x);
452 assert(w == char.init);
453 assert(v == 4.0);
454
455 if (auto ref int a = 3)
456 static assert(!__traits(isRef, a));
457
458 while (auto ref a = x)
459 {
460 static assert(is(typeof(a) == int));
461 static assert(__traits(isRef, a));
462 break;
463 }
464 }
465
466 /***************************************************/
467
468 int main()
469 {
470 test6475();
471 test6905();
472 test7019();
473 test7239();
474 test8123();
475 test8147();
476 test8410();
477 test8942();
478 test10142();
479 test11421();
480 test13950();
481 testlocalref();
482 testglobalref();
483 testAutoRef();
484
485 printf("Success\n");
486 return 0;
487 }