Debian Codegraph @

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

raw
1 /*
2 EXTRA_FILES: imports/a9741.d
3 TEST_OUTPUT:
4 ---
5 AliasSeq!(3, 4, 7, (SSS))
6 AliasSeq!(3, 4, 7, (SSS))
7 7
8 SSS
9 AliasSeq!("hello")
10 AliasSeq!('c')
11 AliasSeq!((FFF))
12 AliasSeq!(10)
13 AliasSeq!(20)
14 AliasSeq!(30)
15 AliasSeq!((Test6))
16 AliasSeq!(Test7(3, "foo"))
17 AliasSeq!((Test8!"foo"))
18 AliasSeq!((Test9!"foo"))
19 AliasSeq!(Test10(3))
20 AliasSeq!(Test11(3))
21 AliasSeq!(10)
22 AliasSeq!(20)
23 AliasSeq!()
24 AliasSeq!(40)
25 B9741
26 AliasSeq!((A9741))
27 AliasSeq!(1)
28 AliasSeq!(2)
29 ---
30
31 RUN_OUTPUT:
32 ---
33 Success
34 ---
35 */
36
37 import core.stdc.stdio;
38
39 template Tuple(T...)
40 {
41 alias T Tuple;
42 }
43
44
45
46 enum EEE = 7;
47 @("hello") struct SSS { }
48
49 @(3) { @(4)@(EEE)@(SSS) int foo; }
50
51 pragma(msg, __traits(getAttributes, foo));
52
53 alias Tuple!(__traits(getAttributes, foo)) TP;
54
55 pragma(msg, TP);
56 pragma(msg, TP[2]);
57 TP[3] a;
58 pragma(msg, typeof(a));
59
60
61 alias Tuple!(__traits(getAttributes, typeof(a))) TT;
62
63 pragma(msg, TT);
64
65 @('c') string s;
66 pragma(msg, __traits(getAttributes, s));
67
68 /************************************************/
69
70 enum FFF;
71 @(FFF) int x1;
72 pragma(msg, __traits(getAttributes, x1));
73
74 void test1()
75 {
76 alias Tuple!(__traits(getAttributes, x1)) tp;
77 assert(tp.length == 1);
78 if (!is(FFF == tp[0]))
79 assert(0);
80 }
81
82 /************************************************/
83
84 void test2()
85 {
86 int x;
87 alias Tuple!(__traits(getAttributes, x)) tp;
88 assert(tp.length == 0);
89 }
90
91 /************************************************/
92
93 void test3()
94 {
95 alias Tuple!(__traits(getAttributes, foo)) tp;
96 assert(tp.length == 4);
97 assert(tp[0] == 3);
98 assert(tp[1] == 4);
99 assert(tp[2] == 7);
100 }
101
102 /************************************************/
103
104 @(1) void foo4();
105 @(2) void foo4(int x);
106
107 void test4()
108 {
109 int i = 1;
110 foreach (o; __traits(getOverloads, uda, "foo4"))
111 {
112 alias Tuple!(__traits(getAttributes, o)) attrs;
113 pragma(msg, attrs.stringof);
114 assert(attrs[0] == i);
115 ++i;
116 }
117 }
118
119 /************************************************/
120
121 pragma(msg, __traits(getAttributes, aa));
122 alias Tuple!(__traits(getAttributes, aa)) Taa;
123 @(10) int aa;
124
125 pragma(msg, __traits(getAttributes, bb));
126 alias Tuple!(__traits(getAttributes, bb)) Tbb;
127 @(20) int bb;
128 alias Tuple!(__traits(getAttributes, bb)) Tbbc;
129
130 @(30) int cc;
131 pragma(msg, __traits(getAttributes, cc));
132 alias Tuple!(__traits(getAttributes, cc)) Tcc;
133
134 void test5()
135 {
136 assert(Taa[0] == 10);
137 assert(Tbb[0] == 20);
138 assert(Tbbc[0] == 20);
139 assert(Tcc[0] == 30);
140 }
141
142 /************************************************/
143
144 enum Test6;
145 @Test6 int x6;
146 pragma(msg, __traits(getAttributes, x6));
147
148 void test6()
149 {
150 alias Tuple!(__traits(getAttributes, x6)) tp;
151
152 assert(tp.length == 1);
153
154 if (!is(Test6 == tp[0]))
155 assert(0);
156 }
157
158 /************************************************/
159
160 struct Test7
161 {
162 int a;
163 string b;
164 }
165
166 @Test7(3, "foo") int x7;
167 pragma(msg, __traits(getAttributes, x7));
168
169 void test7()
170 {
171 alias Tuple!(__traits(getAttributes, x7)) tp;
172
173 assert(tp.length == 1);
174
175 if (!is(Test7 == typeof(tp[0])))
176 assert(0);
177
178 assert(tp[0] == Test7(3, "foo"));
179 }
180
181 /************************************************/
182
183 struct Test8 (string foo) {}
184
185 @Test8!"foo" int x8;
186 pragma(msg, __traits(getAttributes, x8));
187
188 void test8()
189 {
190 alias Tuple!(__traits(getAttributes, x8)) tp;
191
192 assert(tp.length == 1);
193
194 if (!is(Test8!("foo") == tp[0]))
195 assert(0);
196 }
197
198 /************************************************/
199
200 struct Test9 (string foo) {}
201
202 @Test9!("foo") int x9;
203 pragma(msg, __traits(getAttributes, x9));
204
205 void test9()
206 {
207 alias Tuple!(__traits(getAttributes, x9)) tp;
208
209 assert(tp.length == 1);
210
211 if (!is(Test9!("foo") == tp[0]))
212 assert(0);
213 }
214
215 /************************************************/
216
217 struct Test10 (string foo)
218 {
219 int a;
220 }
221
222 @Test10!"foo"(3) int x10;
223 pragma(msg, __traits(getAttributes, x10));
224
225 void test10()
226 {
227 alias Tuple!(__traits(getAttributes, x10)) tp;
228
229 assert(tp.length == 1);
230
231 if (!is(Test10!("foo") == typeof(tp[0])))
232 assert(0);
233
234 assert(tp[0] == Test10!("foo")(3));
235 }
236
237 /************************************************/
238
239 struct Test11 (string foo)
240 {
241 int a;
242 }
243
244 @Test11!("foo")(3) int x11;
245 pragma(msg, __traits(getAttributes, x11));
246
247 void test11()
248 {
249 alias Tuple!(__traits(getAttributes, x11)) tp;
250
251 assert(tp.length == 1);
252
253 if (!is(Test11!("foo") == typeof(tp[0])))
254 assert(0);
255
256 assert(tp[0] == Test11!("foo")(3));
257 }
258
259 /************************************************/
260
261 void test12()
262 {
263 @(1) static struct S1 { }
264 S1 s1;
265 static @(2) struct S2 { }
266 S2 s2;
267 @(1) @(2) struct S3 { }
268
269 @(1) static @(2) struct S { }
270 alias Tuple!(__traits(getAttributes, S)) tps;
271 assert(tps.length == 2);
272 assert(tps[0] == 1);
273 assert(tps[1] == 2);
274
275 @(3) int x;
276 alias Tuple!(__traits(getAttributes, x)) tpx;
277 assert(tpx.length == 1);
278 assert(tpx[0] == 3);
279 x = x + 1;
280
281 @(4) int bar() { return x; }
282 alias Tuple!(__traits(getAttributes, bar)) tpb;
283 assert(tpb.length == 1);
284 assert(tpb[0] == 4);
285 bar();
286 }
287
288 /************************************************/
289 // https://issues.dlang.org/show_bug.cgi?id=9178
290
291 void test9178()
292 {
293 static class Foo { @(1) int a; }
294
295 Foo foo = new Foo;
296 static assert(__traits(getAttributes, foo.tupleof[0])[0] == 1);
297 }
298
299 /************************************************/
300 // https://issues.dlang.org/show_bug.cgi?id=9652
301
302 struct Bug9652
303 {
304 pragma(msg, __traits(getAttributes, enum_field));
305 alias Tuple!(__traits(getAttributes, enum_field)) Tenum_field;
306 private @(10) enum enum_field = 42;
307
308 static assert(Tenum_field[0] == 10);
309 static assert(__traits(getAttributes, enum_field)[0] == 10);
310 static assert(__traits(getProtection, enum_field) == "private");
311 static assert(__traits(isSame, __traits(parent, enum_field), Bug9652));
312 static assert(__traits(isSame, enum_field, enum_field));
313
314 pragma(msg, __traits(getAttributes, anon_enum_member));
315 alias Tuple!(__traits(getAttributes, anon_enum_member)) Tanon_enum_member;
316 private @(20) enum {anon_enum_member}
317
318 static assert(Tanon_enum_member[0] == 20);
319 static assert(__traits(getAttributes, anon_enum_member)[0] == 20);
320 static assert(__traits(getProtection, anon_enum_member) == "private");
321 static assert(__traits(isSame, __traits(parent, anon_enum_member), Bug9652));
322 static assert(__traits(isSame, anon_enum_member, anon_enum_member));
323
324 pragma(msg, __traits(getAttributes, Foo.enum_member));
325 alias Tuple!(__traits(getAttributes, Foo.enum_member)) Tfoo_enum_member;
326 private @(30) enum Foo {enum_member}
327 static assert(Tfoo_enum_member.length == 0); //Foo has attributes, not Foo.enum_member
328 static assert(__traits(getAttributes, Foo.enum_member).length == 0);
329 static assert(__traits(getProtection, Foo.enum_member) == "public");
330 static assert(__traits(isSame, __traits(parent, Foo.enum_member), Foo));
331 static assert(__traits(isSame, Foo.enum_member, Foo.enum_member));
332
333 pragma(msg, __traits(getAttributes, anon_enum_member_2));
334 alias Tuple!(__traits(getAttributes, anon_enum_member_2)) Tanon_enum_member_2;
335 private @(40) enum {long anon_enum_member_2 = 2L}
336
337 static assert(Tanon_enum_member_2[0] == 40);
338 static assert(__traits(getAttributes, anon_enum_member_2)[0] == 40);
339 static assert(__traits(getProtection, anon_enum_member_2) == "private");
340 static assert(__traits(isSame, __traits(parent, anon_enum_member_2), Bug9652));
341 static assert(__traits(isSame, anon_enum_member_2, anon_enum_member_2));
342
343 template Bug(alias X, bool is_exp)
344 {
345 static assert(is_exp == !__traits(compiles, __traits(parent, X)));
346 static assert(is_exp == !__traits(compiles, __traits(getAttributes, X)));
347 static assert(is_exp == !__traits(compiles, __traits(getProtection, X)));
348 enum Bug = 0;
349 }
350 enum en = 0;
351 enum dummy1 = Bug!(5, true);
352 enum dummy2 = Bug!(en, false);
353 }
354
355 /************************************************/
356 // https://issues.dlang.org/show_bug.cgi?id=9741
357
358 import imports.a9741;
359
360 struct A9741 {}
361
362 alias X9741 = ShowAttributes!(B9741);
363
364 @A9741 struct B9741 {}
365
366 /************************************************/
367 // https://issues.dlang.org/show_bug.cgi?id=12160
368
369 auto before12160(alias Hook)(string) { return 0; }
370
371 template checkUDAs12160(alias Func)
372 {
373 enum x = __traits(getAttributes, Func).length;
374 }
375
376 void test12160()
377 {
378 int foo() { return 42; } // OK <- NG
379
380 @before12160!foo("name1")
381 void bar(int name1, double name2) {}
382
383 alias X = checkUDAs12160!(bar);
384 }
385
386 /************************************************/
387 // https://issues.dlang.org/show_bug.cgi?id=10208
388
389 @( 10) enum int x10208_01 = 100;
390 @( 20) int x10208_02;
391 @( 30) const int x10208_03;
392 @( 40) immutable int x10208_04;
393 @( 50) int x10208_05 = 500;
394 @( 60) const int x10208_06 = 600;
395 @( 70) immutable int x10208_07 = 700;
396 @( 80) __gshared enum int x10208_08 = 800;
397 @( 90) __gshared int x10208_09;
398 @(100) __gshared const int x10208_10;
399 @(110) __gshared immutable int x10208_11;
400 @(120) __gshared int x10208_12 = 1200;
401 @(130) __gshared const int x10208_13 = 1300;
402 @(140) __gshared immutable int x10208_14 = 1400;
403 static assert(__traits(getAttributes, x10208_01)[0] == 10); // OK
404 static assert(__traits(getAttributes, x10208_02)[0] == 20); // OK
405 static assert(__traits(getAttributes, x10208_03)[0] == 30); // OK
406 static assert(__traits(getAttributes, x10208_04)[0] == 40); // OK
407 static assert(__traits(getAttributes, x10208_05)[0] == 50); // OK
408 static assert(__traits(getAttributes, x10208_06)[0] == 60); // Error -> OK
409 static assert(__traits(getAttributes, x10208_07)[0] == 70); // Error -> OK
410 static assert(__traits(getAttributes, x10208_08)[0] == 80); // OK
411 static assert(__traits(getAttributes, x10208_09)[0] == 90); // OK
412 static assert(__traits(getAttributes, x10208_10)[0] == 100); // OK
413 static assert(__traits(getAttributes, x10208_11)[0] == 110); // OK
414 static assert(__traits(getAttributes, x10208_12)[0] == 120); // OK
415 static assert(__traits(getAttributes, x10208_13)[0] == 130); // Error -> OK
416 static assert(__traits(getAttributes, x10208_14)[0] == 140); // Error -> OK
417
418 /************************************************/
419 // https://issues.dlang.org/show_bug.cgi?id=11677
420 // https://issues.dlang.org/show_bug.cgi?id=11678
421
422 bool test_uda(alias func)() @safe
423 {
424 alias Tuple!(__traits(getAttributes, func)) udas;
425 static assert([udas] == [10, 20]);
426
427 func(); // @safe attribute check
428
429 return true;
430 }
431
432 @(10) @(20) @safe void func11677a() {} // OK
433 @safe @(10) @(20) void func11677b() {} // OK <- NG
434 @(10) @safe @(20) void func11677c() {} // OK <- NG
435
436 static assert(test_uda!func11677a());
437 static assert(test_uda!func11677b());
438 static assert(test_uda!func11677c());
439
440 void func11678a() @safe @(10) @(20) {} // OK <- NG
441 void func11678b() @(10) @safe @(20) {} // OK <- NG
442 void func11678c() @(10) @(20) @safe {} // OK <- NG
443
444 static assert(test_uda!func11678a());
445 static assert(test_uda!func11678b());
446 static assert(test_uda!func11678c());
447
448 /************************************************/
449 // https://issues.dlang.org/show_bug.cgi?id=11678
450
451 class C11678
452 {
453 this() @(10) @(20) {}
454 ~this() @(10) @(20) {}
455 }
456
457 //static this() @(10) @(20) {}
458 static ~this() @(10) @(20) {}
459
460 //shared static this() @(10) @(20) {}
461 shared static ~this() @(10) @(20) {}
462
463 /************************************************/
464 // https://issues.dlang.org/show_bug.cgi?id=11679
465
466 void test11679()
467 {
468 @(10) @(20) auto var = 1;
469 static assert([__traits(getAttributes, var)] == [10, 20]);
470 }
471
472 /************************************************/
473 // https://issues.dlang.org/show_bug.cgi?id=11680
474
475 @(10) gvar11680 = 1; // OK <- NG
476 @(10) gfun11680() {} // OK <- NG
477
478 void test11680()
479 {
480 @(10) lvar11680 = 1; // OK <- NG
481 @(10) lfun11680() {} // OK <- NG
482 }
483
484 /************************************************/
485 // https://issues.dlang.org/show_bug.cgi?id=11844
486
487 auto make_encode11844(T, string name)()
488 {
489 return mixin("function(T self) { return self.encodeField!\""~ name ~ "\"();}");
490 }
491
492 class FileData11844
493 {
494 ulong _member;
495
496 @make_encode11844!(FileData11844, "member")()
497 ulong member() const { return _member; }
498
499 ubyte[] encodeField(string key)() { return [1,2,3]; }
500 }
501
502 static assert(__traits(getAttributes, FileData11844.member)[0](new FileData11844()) == [1,2,3]);
503
504 /************************************************/
505
506 template AliasSeq(T...)
507 {
508 alias AliasSeq = T;
509 }
510
511 template ParameterUDA(size_t p_num, func...)
512 if (func.length == 1 && is(typeof(func[0]) PT == __parameters) && PT.length > p_num)
513 {
514 static if (is(typeof(func[0]) PT == __parameters))
515 {
516 template Get(size_t i)
517 {
518 static if (//!isFunctionPointer!func && !isDelegate!func
519 // Parameters without UDA may yield CT error.
520 /*&&*/ is(typeof(__traits(getAttributes, PT[i..i+1]))x))
521 {
522 alias Get = AliasSeq!(__traits(getAttributes, PT[i..i+1]));
523 }
524 else
525 {
526 alias Get = AliasSeq!();
527 }
528 }
529 }
530 else
531 {
532 static assert(0, func[0].stringof ~ "is not a function");
533
534 // Define dummy entities to avoid pointless errors
535 template Get(size_t i) { alias Get = AliasSeq!(); }
536 alias PT = AliasSeq!();
537 }
538
539 alias ParameterUDA = Get!p_num;
540 }
541
542 void test13x(@(10) int a, @(20) int, @(30) @(40) int[] arr...) {}
543
544 void test13()
545 {
546 static assert([ParameterUDA!(0, test13x)] == [10]);
547 static assert([ParameterUDA!(1, test13x)] == [20]);
548 static assert([ParameterUDA!(2, test13x)] == [30, 40]);
549 }
550
551 template Test13t(F)
552 {
553 static assert(!__traits(compiles, ParameterUDA!(0, F)));
554 static assert(!__traits(compiles, ParameterUDA!(1, F)));
555 static assert(!__traits(compiles, ParameterUDA!(2, F)));
556 enum Test13t = true;
557 }
558
559 alias test13t = Test13t!(typeof(test13x));
560
561 enum Test14UDA1;
562
563 struct Test14UDA2
564 {
565 string str;
566 }
567
568 Test14UDA2 test14uda3(string name)
569 {
570 return Test14UDA2(name);
571 }
572
573 struct Test14UDA4(string v)
574 {
575 }
576
577 void test14x(@Test14UDA1 int, @Test14UDA2("1") int, @test14uda3("2") int, @Test14UDA4!"3" int) {}
578 void test14()
579 {
580 static assert(is(ParameterUDA!(0, test14x)[0] == Test14UDA1));
581 static assert(ParameterUDA!(1, test14x)[0] == Test14UDA2("1"));
582 static assert(ParameterUDA!(2, test14x)[0] == test14uda3("2"));
583 static assert(is(ParameterUDA!(3, test14x)[0] == Test14UDA4!"3"));
584 }
585
586 void test15x(@(20) void delegate(int) @safe dg)
587 {
588 static assert([__traits(getAttributes, dg)] == [20]);
589 static assert(is(typeof(dg) == void delegate(int) @safe));
590 }
591
592 template MinimalFunctionTypeOf(func...)
593 if (func.length == 1)
594 {
595 static if (is(func[0] T) || is(typeof(func[0]) T) && is(T Fdlg == delegate))
596 alias MinimalFunctionTypeOf = Fdlg; // HIT: delegate
597 else
598 static assert(0);
599 }
600
601 template Parameters(func...)
602 if (func.length == 1)
603 {
604 static if (is(MinimalFunctionTypeOf!func P == function))
605 alias Parameters = P;
606 else
607 static assert(0, "argument has no parameters");
608 }
609
610 void test15y(@(20) void delegate(@Test14UDA2("2") @("test15yUDA") int) @safe dg)
611 {
612 static assert([__traits(getAttributes, dg)] == [20]);
613 static assert(is(typeof(dg) == void delegate(int) @safe));
614 auto foo = (@("myUDA") int x){
615 static assert([__traits(getAttributes, x)] == ["myUDA"]);
616 };
617 static assert(__traits(getAttributes, Parameters!dg)[0] == Test14UDA2("2"));
618 static assert(__traits(getAttributes, Parameters!dg)[1] == "test15yUDA");
619 }
620
621 void test15z()
622 {
623 test15y((@(15) @(16) int x){
624 static assert([__traits(getAttributes, x)] == [15, 16]);
625 });
626 }
627
628 void test16x(A)(@(22) A a)
629 {
630 static assert([__traits(getAttributes, a)] == [22]);
631 }
632
633 void test16()
634 {
635 static assert([ParameterUDA!(0, test16x!int)] == [22]);
636 }
637
638 void test17()
639 {
640 void test17x(A)(@(23) A a)
641 {
642 static assert([__traits(getAttributes, a)] == [23]);
643 }
644 static assert([ParameterUDA!(0, test17x!int)] == [23]);
645 }
646
647 void test18()
648 {
649 void test18a(@(Tuple!(18, "a")) int a)
650 {
651 static assert(__traits(getAttributes, a) == Tuple!(18, "a"));
652 }
653 void test18b(@Tuple!(18, "b") int a)
654 {
655 static assert(__traits(getAttributes, a) == Tuple!(18, "b"));
656 }
657
658 static assert(ParameterUDA!(0, test18a) == Tuple!(18, "a"));
659 static assert(ParameterUDA!(0, test18b) == Tuple!(18, "b"));
660 }
661
662 // Lambdas with parentheses:
663 void test19()
664 {
665 // lambdas without parentheses
666 alias test19a = @(3) b => 1 + 2;
667 alias test19b = @(3) @(4) b => 1 + 2;
668
669 alias test19c = (@(3) c, @(5) d) => 1 + 2;
670 alias test19d = (@(3) @(4) c, @(5) d) => 1 + 2;
671 auto test19e = (@(3) int c, @(5) int d) => 1 + 2;
672
673 // still allow alias function declarations
674 alias FuncType = @safe void function();
675 alias FuncType2 = @safe nothrow void function();
676 alias FuncType3 = nothrow void function();
677 alias FuncType4 = nothrow @safe void function();
678 }
679
680 void test20()
681 {
682 // Using a delegate with inferred parameter type
683 void test20a(int delegate(int) t){ t(1); }
684 test20a((@(19) a) {
685 static assert([__traits(getAttributes, a)] == [19]);
686 return a + 2;
687 });
688 }
689
690 /************************************************/
691 // https://issues.dlang.org/show_bug.cgi?id=20831
692
693 void foo20831(int f, float, @("test") string s = "test") {}
694
695 static if(is(typeof(foo20831) Params20831 == __parameters))
696 static assert([__traits(getAttributes, Params20831[1..2])] == []);
697
698 /************************************************/
699
700 /************************************************/
701 // https://issues.dlang.org/show_bug.cgi?id=15804
702
703 template test15804()
704 {
705 alias AliasSeq(T...) = T;
706
707 @(42) struct Foo(D) {}
708 auto fooFac(T)()
709 {
710 static assert(__traits(getAttributes, Foo) == AliasSeq!42);
711 static assert(__traits(getAttributes, Foo!int) == AliasSeq!42);
712 return Foo!T();
713 }
714
715 auto booFac(T)()
716 {
717 @(43) struct Boo {}
718 static assert(__traits(getAttributes, Boo) == AliasSeq!43);
719 return Boo();
720 }
721
722 auto barFac(T)()
723 {
724 @(44) struct Bar(D) {}
725 static assert(__traits(getAttributes, Bar) == AliasSeq!44); // Fixed
726 static assert(__traits(getAttributes, Bar!int) == AliasSeq!44);
727 return Bar!T();
728 }
729
730 auto bazFac(T)()
731 {
732 @(45) static struct Baz(D) {}
733 static assert(__traits(getAttributes, Baz) == AliasSeq!45); // Fixed
734 static assert(__traits(getAttributes, Baz!int) == AliasSeq!45);
735 return Baz!T();
736 }
737
738 auto foo = fooFac!int;
739 auto boo = booFac!int;
740 auto bar = barFac!int;
741 auto baz = bazFac!int;
742 }
743
744 alias a15804 = test15804!();
745
746 /************************************************/
747
748 int main()
749 {
750 test1();
751 test2();
752 test3();
753 test4();
754 test5();
755 test6();
756 test7();
757 test8();
758 test9();
759 test10();
760 test11();
761 test12();
762 test9178();
763 test13();
764 test14();
765 test16();
766 test17();
767 test18();
768 test19();
769 test20();
770
771 printf("Success\n");
772 return 0;
773 }