Debian Codegraph @

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

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 Foo3.func()
5 Code3.func()
6 Foo4.func()
7 Foo5.func()
8 b.x = 5
9 x = 5
10 duff_for(1, 11)
11 fid = 1, 2
12 foo12
13 foo12
14 foo13 j = 1
15 foo13 j = 1
16 x14 = 6
17 x15 = 6
18 bar15() = 5
19 x16 = 6
20 bar() = 5
21 x17 = 5
22 b.x17 = 5
23 x17 = 3
24 x17 = 5
25 x17 = 4
26 x17 = 3
27 x17 = 5
28 in C20.f()
29 B22.foo()
30 5
31 5
32 a = 0
33 int
34 int
35 int
36 int
37 foo 1
38 foo 2
39 0 0
40 two
41 one
42 one
43 Class39 dtor
44 Mixed-in dtor
45 Mixed-in dtor
46 Base39 dtor
47 Success
48 ---
49 */
50
51 module mixin1;
52
53 import core.stdc.stdio;
54
55 alias TypeTuple(T...) = T;
56
57 /*******************************************/
58
59 mixin template Foo(T)
60 {
61 T x;
62 }
63
64 mixin Foo!(uint);
65
66 struct Bar
67 {
68 template Abc(T)
69 {
70 T y;
71 }
72
73 template Def(T)
74 {
75 T z;
76 }
77 }
78
79 mixin Bar.Abc!(int);
80
81 Bar b;
82 mixin typeof(b).Def!(int);
83
84 void test1()
85 {
86 x = 3;
87 assert(x == 3);
88 y = 4;
89 assert(y == 4);
90 z = 5;
91 assert(z == 5);
92 }
93
94 /*******************************************/
95
96 template Foo2(T)
97 {
98 T x2 = T.sizeof;
99 }
100
101 mixin Foo2!(uint) B2;
102 mixin Foo2!(long) C2;
103 mixin D2 = Foo2!(wchar);
104 mixin Foo2!(int);
105
106 void test2()
107 {
108 B2.x2 = 3;
109 assert(B2.x2 == 3);
110 assert(C2.x2 == long.sizeof);
111 assert(D2.x2 == 2);
112 // assert(x2 == int.sizeof);
113 }
114
115 /*******************************************/
116
117 template Foo3(T)
118 {
119 int func() { printf("Foo3.func()\n"); return 1; }
120 }
121
122 class Bar3
123 {
124 mixin Foo3!(int);
125 }
126
127 class Code3 : Bar3
128 {
129 override int func() { printf("Code3.func()\n"); return 2; }
130 }
131
132 void test3()
133 {
134 int i;
135
136 Bar3 b = new Bar3();
137 i = b.func();
138 assert(i == 1);
139
140 b = new Code3();
141 i = b.func();
142 assert(i == 2);
143 }
144
145 /*******************************************/
146
147 template Foo4(T)
148 {
149 int func() { printf("Foo4.func()\n"); return 1; }
150 }
151
152 struct Bar4
153 {
154 mixin Foo4!(int);
155 }
156
157 void test4()
158 {
159 int i;
160
161 Bar4 b;
162 i = b.func();
163 assert(i == 1);
164 }
165
166 /*******************************************/
167
168 template Foo5()
169 {
170 int func() { printf("Foo5.func()\n"); return 1; }
171 }
172
173 struct Bar5
174 {
175 mixin Foo5;
176 }
177
178 void test5()
179 {
180 int i;
181
182 Bar5 b;
183 i = b.func();
184 assert(i == 1);
185 }
186
187 /*******************************************/
188
189 template Foo6()
190 {
191 int x = 5;
192 }
193
194 struct Bar6
195 {
196 mixin Foo6;
197 }
198
199 void test6()
200 {
201 int i;
202
203 Bar6 b;
204 i = b.x;
205 assert(i == 5);
206 assert(b.sizeof == int.sizeof);
207 }
208
209 /*******************************************/
210
211 template Foo7()
212 {
213 int x = 5;
214 }
215
216 class Bar7
217 {
218 int y = 6;
219 mixin Foo7;
220 }
221
222 void test7()
223 {
224 int i;
225
226 Bar7 b = new Bar7();
227 i = b.x;
228 printf("b.x = %d\n", b.x);
229 assert(i == 5);
230 }
231
232 /*******************************************/
233
234 template Foo8()
235 {
236 int x = 5;
237 int bar() { return 7; }
238 }
239
240 void test8()
241 {
242 mixin Foo8;
243 printf("x = %d\n", x);
244 assert(x == 5);
245 assert(bar() == 7);
246 }
247
248 /*******************************************/
249
250 template Foo9()
251 {
252 int abc() { return y; }
253 }
254
255 void test9()
256 {
257 int y = 8;
258 mixin Foo9;
259 assert(abc() == 8);
260 }
261
262 /*******************************************/
263
264 template Foo10(alias b)
265 {
266 typeof(b) abc() { return b; }
267 }
268
269 void test10()
270 {
271 int y = 8;
272 mixin Foo10!(y);
273 assert(abc() == 8);
274 }
275
276
277 /*******************************************/
278
279 template Foo11(alias b)
280 {
281 int abc() { return b; }
282 }
283
284 void test11()
285 {
286 int y = 8;
287 mixin Foo11!(y) B;
288 assert(B.abc() == 8);
289
290 mixin C = Foo11!2;
291 assert(C.abc() == 2);
292 }
293
294 /*******************************************/
295
296 template duff_for(alias id1, alias id2, alias s)
297 {
298
299 void duff_for()
300 {
301 printf("duff_for(%d, %d)\n", id1, id2);
302 typeof(id1) id = id1;
303 printf("fid = %d, %d\n", id, (id2 - id) % 8);
304 switch ((id2 - id) % 8)
305 {
306 case 0:
307 while (id != id2)
308 {
309 printf("wid = %d\n", id);
310 s(); ++id;
311 goto case;
312 case 7: s(); ++id; goto case;
313 case 6: s(); ++id; goto case;
314 case 5: s(); ++id; goto case;
315 case 4: s(); ++id; goto case;
316 case 3: s(); ++id; goto case;
317 case 2: s(); ++id; goto case;
318 case 1: s(); ++id;
319 break;
320 default: assert(0);
321 }
322 }
323 }
324 }
325
326 void foo12() { printf("foo12\n"); }
327
328 void test12()
329 {
330 int i = 1;
331 int j = 11;
332
333 mixin duff_for!(i, j, delegate void() { foo12(); });
334 duff_for();
335 }
336
337 /*******************************************/
338
339 template duff(alias id1, alias id2, alias s)
340 {
341
342 void duff()
343 {
344 s();
345 s();
346 }
347 }
348
349 void foo13(int j)
350 {
351 printf("foo13 j = %d\n", j);
352 assert(j == 1);
353 }
354
355 void test13()
356 {
357 int i = 1;
358 int j = 11;
359
360 mixin duff!(i, j, delegate { foo13(i); });
361 duff();
362 }
363
364 /*******************************************/
365
366 template Foo14()
367 {
368 int x14 = 5;
369 }
370
371 void test14()
372 {
373 int x14 = 6;
374 mixin Foo14;
375 printf("x14 = %d\n", x14);
376 assert(x14 == 6);
377 }
378
379 /*******************************************/
380
381 template Foo15()
382 {
383 int x15 = 5;
384
385 int bar15() { return x15; }
386 }
387
388 int x15 = 6;
389 mixin Foo15;
390
391 void test15()
392 {
393
394 printf("x15 = %d\n", x15);
395 printf("bar15() = %d\n", bar15());
396 assert(x15 == 6);
397 assert(bar15() == 5);
398 }
399
400 /*******************************************/
401
402 template Foo16()
403 {
404 int x16 = 5;
405
406 int bar() { return x16; }
407 }
408
409 mixin Foo16 A16;
410 int x16 = 6;
411 mixin Foo16 B16;
412
413 void test16()
414 {
415
416 printf("x16 = %d\n", x16);
417 printf("bar() = %d\n", A16.bar());
418 assert(x16 == 6);
419 assert(A16.x16 == 5);
420 assert(B16.x16 == 5);
421 assert(A16.bar() == 5);
422 assert(B16.bar() == 5);
423 }
424
425 /*******************************************/
426
427 template Foo17()
428 {
429 int x17 = 5;
430 }
431
432 mixin Foo17;
433
434 struct Bar17
435 {
436 mixin Foo17;
437 }
438
439 void test17()
440 {
441 printf("x17 = %d\n", x17); // prints 5
442 assert(x17 == 5);
443 { Bar17 b;
444 int x17 = 3;
445
446 printf("b.x17 = %d\n", b.x17); // prints 5
447 assert(b.x17 == 5);
448 printf("x17 = %d\n", x17); // prints 3
449 assert(x17 == 3);
450 {
451 mixin Foo17;
452 printf("x17 = %d\n", x17); // prints 5
453 assert(x17 == 5);
454 x17 = 4;
455 printf("x17 = %d\n", x17); // prints 4
456 assert(x17 == 4);
457 }
458 printf("x17 = %d\n", x17); // prints 3
459 assert(x17 == 3);
460 }
461 printf("x17 = %d\n", x17); // prints 5
462 assert(x17 == 5);
463 }
464
465 /*******************************************/
466
467 template Foo18() { int z = 3; }
468
469 struct Bar18(alias Tmpl)
470 {
471 mixin Tmpl;
472 }
473
474 Bar18!(Foo18) b18;
475
476 void test18()
477 {
478 assert(b18.z == 3);
479 }
480
481 /*******************************************/
482
483 template Mix1(T)
484 {
485 int foo19(T a) { return 2*a; }
486 }
487
488 template Mix2(T)
489 {
490 mixin Mix1!(T);
491
492 int bar19(T a) { return foo19(a); }
493 }
494
495 mixin Mix2!(int);
496
497 void test19()
498 {
499 int i;
500
501 i = bar19(7);
502 assert(i == 14);
503 }
504
505 /*******************************************/
506
507 interface A20 { int f(); }
508
509 template Foo20()
510 {
511 int f()
512 {
513 printf("in C20.f()\n");
514 return 6;
515 }
516 }
517
518 class C20 : A20
519 {
520 mixin Foo20;
521 // void f() { printf("in C20.f()\n"); }
522 }
523
524 void test20()
525 {
526 C20 c = new C20();
527 int i = c.f();
528 assert(i == 6);
529 }
530
531 /*******************************************/
532
533 template Mix21() { this(int x) { printf("mix1\n"); }}
534
535 class Bar21
536 {
537 int myx;
538 mixin Mix21; // wouldn't compile
539
540 this() { myx = 15; }
541
542 // mixin Mix21; // placing it here compiles
543 }
544
545 void test21()
546 {
547 Bar21 bar = new Bar21();
548 }
549
550 /*******************************************/
551
552 template A22(T)
553 {
554 this()
555 { int i;
556 i = super.foo();
557 assert(i == 67);
558 }
559 }
560
561
562 class B22
563 {
564 int foo() { printf("B22.foo()\n"); return 67; }
565 }
566
567 class C22 : B22
568 {
569 mixin A22!(C22);
570 }
571
572 void test22()
573 {
574 C22 c = new C22;
575 }
576
577
578 /*******************************************/
579
580 template Foo23()
581 {
582 const int x = 5;
583 }
584
585 class C23
586 {
587 mixin Foo23 F;
588 }
589
590 struct D23
591 {
592 mixin Foo23 F;
593 }
594
595 void test23()
596 {
597 C23 c = new C23;
598
599 printf("%d\n",c.F.x);
600 assert(c.F.x == 5);
601
602 D23 d;
603
604 printf("%d\n",d.F.x);
605 assert(d.F.x == 5);
606 }
607
608 /*******************************************/
609
610 template T24()
611 {
612 void foo24() { return cast(void)0; }
613 // alias foo24 foo24;
614 }
615
616 mixin T24;
617
618 void test24()
619 {
620 foo24();
621 }
622
623
624 /*******************************************/
625
626 template ctor25()
627 {
628 this() { this(null); }
629 this( Object o ) {}
630 }
631
632 class Foo25
633 {
634 mixin ctor25;
635 }
636
637 void test25()
638 {
639 Foo25 foo = new Foo25();
640 }
641
642
643 /*******************************************/
644
645 template Get26(T)
646 {
647 Reader get (ref T x)
648 {
649 return this;
650 }
651 }
652
653 class Reader
654 {
655 mixin Get26!(byte) bar;
656 alias bar.get get;
657 mixin Get26!(int) beq;
658 alias beq.get get;
659 }
660
661 void test26()
662 {
663 Reader r = new Reader;
664 Reader s;
665 byte q;
666 s = r.get (q);
667 assert(s == r);
668 }
669
670 /*******************************************/
671
672 template Template(int L)
673 {
674 int i = L;
675 int foo(int b = Template!(9).i) {
676 return b;
677 }
678 }
679
680 void test27()
681 {
682 int i = 10;
683 int foo(int b = Template!(9).i) {
684 return b;
685 }
686 assert(foo()==9);
687 }
688
689 /*******************************************/
690
691 template Blah28(int a, alias B)
692 {
693 mixin Blah28!(a-1, B);
694 //mixin Blah28!(0, B);
695 }
696
697 template Blah28(int a:0, alias B)
698 {
699 }
700
701 void test28()
702 {
703 int a;
704 mixin Blah28!(5,a);
705 printf("a = %d\n", a);
706 }
707
708 /*******************************************/
709
710 template T29()
711 {
712 int x;
713 }
714
715 struct S29
716 {
717 mixin T29;
718 int y;
719 }
720
721 const S29 s29 = { x:2, y:3 };
722
723 void test29()
724 {
725 assert(s29.x == 2);
726 assert(s29.y == 3);
727 }
728
729 /*******************************************/
730
731 class A30
732 {
733 template ctor(Type)
734 {
735 this(Type[] arr)
736 {
737 foreach(Type v; arr)
738 {
739 const str = typeid(typeof(v)).toString();
740 printf("%.*s\n", cast(int)str.length, str.ptr);
741 }
742 }
743 }
744
745 mixin ctor!(int);
746 }
747
748 void test30()
749 {
750 static int[] ints = [0,1,2,3];
751 A30 a = new A30(ints);
752 }
753
754 /*******************************************/
755
756 template Share(T) {
757 const bool opEquals(ref const T x) { return true; }
758 }
759
760 struct List31(T) {
761
762 //int opEquals(List31 x) { return 0; }
763 mixin Share!(List31);
764 }
765
766 void test31()
767 {
768 List31!(int) x;
769 List31!(int) y;
770 int i = x == y;
771 assert(i == 1);
772 }
773
774 /*******************************************/
775
776 template Blah(int a, alias B)
777 {
778 mixin Blah!(a-1, B);
779 }
780
781 template Blah(int a:0, alias B)
782 {
783 int foo()
784 { return B + 1;
785 }
786 }
787
788 void test32()
789 {
790 int a = 3;
791 mixin Blah!(5,a);
792
793 assert(foo() == 4);
794 }
795
796 /*******************************************/
797
798 template T33( int i )
799 {
800 int foo()
801 {
802 printf("foo %d\n", i );
803 return i;
804 }
805 }
806
807
808 class C33
809 {
810 mixin T33!( 1 ) t1;
811 mixin T33!( 2 ) t2;
812 }
813
814 void test33()
815 {
816 int i;
817 C33 c1 = new C33;
818 i = c1.t1.foo();
819 assert(i == 1);
820 i = c1.t2.foo();
821 assert(i == 2);
822 }
823
824
825 /*******************************************/
826
827 template mix34()
828 {
829 int i;
830 void print()
831 {
832 printf( "%d %d\n", i, j );
833 assert(i == 0);
834 assert(j == 0);
835 }
836 }
837
838 void test34()
839 {
840 int j;
841 mixin mix34!();
842
843 print();
844 //printf( "%i\n", i );
845 }
846
847 /*******************************************/
848
849 mixin T35!(int) m35;
850
851 template T35(t)
852 {
853 t a;
854 }
855
856 void test35()
857 {
858 m35.a = 3;
859 }
860
861 /*******************************************/
862
863 struct Foo36
864 {
865 int a;
866 mixin T!(int) m;
867 template T(t)
868 {
869 t b;
870 }
871 int c;
872 }
873
874 void test36()
875 {
876 Foo36 f;
877 assert(f.sizeof == 12);
878
879 f.a = 1;
880 f.m.b = 2;
881 f.c = 3;
882
883 assert(f.a == 1);
884 assert(f.m.b == 2);
885 assert(f.c == 3);
886 }
887
888 /*******************************************/
889
890 template Foo37()
891 {
892 template func() {
893 int func() {
894 return 6;
895 }
896 }
897 }
898
899 class Baz37
900 {
901 mixin Foo37 bar;
902 }
903
904 void test37()
905 {
906 Baz37 b = new Baz37;
907 auto i = b.bar.func!()();
908 assert(i == 6);
909 i = (new Baz37).bar.func!()();
910 assert(i == 6);
911 }
912
913 /*******************************************/
914
915 template Foo38()
916 {
917 int a = 4;
918
919 ~this()
920 {
921 printf("one\n");
922 assert(a == 4);
923 assert(b == 5);
924 c++;
925 }
926 }
927
928 class Outer38
929 { int b = 5;
930
931 static int c;
932
933 mixin Foo38!() bar;
934 mixin Foo38!() abc;
935
936 ~this()
937 {
938 printf("two\n");
939 assert(b == 5);
940 assert(c == 0);
941 c++;
942 }
943 }
944
945 void test38()
946 {
947 Outer38 o = new Outer38();
948 destroy(o);
949 assert(Outer38.c == 3);
950 }
951
952 /*******************************************/
953
954 template TDtor()
955 {
956 ~this()
957 {
958 printf("Mixed-in dtor\n");
959 }
960 }
961
962 class Base39
963 {
964 ~this()
965 {
966 printf("Base39 dtor\n");
967 }
968 }
969
970 class Class39 : Base39
971 {
972 mixin TDtor A;
973 mixin TDtor B;
974
975 ~this()
976 {
977 printf("Class39 dtor\n");
978 }
979 }
980
981 void test39()
982 {
983 scope test = new Class39;
984 }
985
986
987 /*******************************************/
988
989 template Mix40()
990 {
991 int i;
992 }
993
994 struct Z40
995 {
996 union { mixin Mix40; }
997 }
998
999 void test40()
1000 {
1001 Z40 z;
1002 z.i = 3;
1003 }
1004
1005 /*******************************************/
1006
1007
1008 class X41(P...)
1009 {
1010 alias P[0] Q;
1011 mixin Q!();
1012 }
1013
1014 template MYP()
1015 {
1016 void foo() { }
1017 }
1018
1019 void test41()
1020 {
1021 X41!(MYP) x;
1022 }
1023
1024 /*******************************************/
1025 // https://issues.dlang.org/show_bug.cgi?id=2245
1026
1027 template TCALL2245a(ARGS...)
1028 {
1029 int makecall(ARGS args)
1030 {
1031 return args.length;
1032 }
1033 }
1034
1035 template TCALL2245b(int n)
1036 {
1037 int makecall2(ARGS...)(ARGS args) if (ARGS.length == n)
1038 {
1039 return args.length;
1040 }
1041 }
1042
1043 class C2245
1044 {
1045 mixin TCALL2245a!();
1046 mixin TCALL2245a!(int);
1047 mixin TCALL2245a!(int,int);
1048
1049 mixin TCALL2245b!(0);
1050 mixin TCALL2245b!(1);
1051 mixin TCALL2245b!(2);
1052 }
1053
1054 struct S2245
1055 {
1056 mixin TCALL2245a!();
1057 mixin TCALL2245a!(int);
1058 mixin TCALL2245a!(int,int);
1059
1060 mixin TCALL2245b!(0);
1061 mixin TCALL2245b!(1);
1062 mixin TCALL2245b!(2);
1063 }
1064
1065 void test2245()
1066 {
1067 auto c = new C2245;
1068 assert(c.makecall() == 0);
1069 assert(c.makecall(0) == 1);
1070 assert(c.makecall(0,1) == 2);
1071
1072 assert(c.makecall2() == 0);
1073 assert(c.makecall2(0) == 1);
1074 assert(c.makecall2(0,1) == 2);
1075
1076 assert(c.makecall2!()() == 0);
1077 assert(c.makecall2!(int)(0) == 1);
1078 assert(c.makecall2!(int, int)(0,1) == 2);
1079
1080 auto s = S2245();
1081 assert(s.makecall() == 0);
1082 assert(s.makecall(0) == 1);
1083 assert(s.makecall(0,1) == 2);
1084
1085 assert(s.makecall2() == 0);
1086 assert(s.makecall2(0) == 1);
1087 assert(s.makecall2(0,1) == 2);
1088
1089 assert(s.makecall2!()() == 0);
1090 assert(s.makecall2!(int)(0) == 1);
1091 assert(s.makecall2!(int, int)(0,1) == 2);
1092 }
1093
1094 /*******************************************/
1095 // https://issues.dlang.org/show_bug.cgi?id=2481
1096
1097 template M2481() { int i; }
1098 class Z2481a { struct { mixin M2481!(); } }
1099 class Z2481b { struct { int i; } }
1100
1101 void test2481()
1102 {
1103 Z2481a z1;
1104 Z2481b z2;
1105 static assert(z1.i.offsetof == z2.i.offsetof);
1106 }
1107
1108 /*******************************************/
1109 // https://issues.dlang.org/show_bug.cgi?id=2740
1110
1111 interface IFooable2740
1112 {
1113 bool foo();
1114 }
1115 abstract class CFooable2740
1116 {
1117 bool foo();
1118 }
1119
1120 mixin template MFoo2740()
1121 {
1122 override bool foo() { return true; }
1123 }
1124
1125 class Foo2740i1 : IFooable2740
1126 {
1127 override bool foo() { return false; }
1128 mixin MFoo2740;
1129 }
1130 class Foo2740i2 : IFooable2740
1131 {
1132 mixin MFoo2740;
1133 override bool foo() { return false; }
1134 }
1135
1136 class Foo2740c1 : CFooable2740
1137 {
1138 override bool foo() { return false; }
1139 mixin MFoo2740;
1140 }
1141 class Foo2740c2 : CFooable2740
1142 {
1143 mixin MFoo2740;
1144 override bool foo() { return false; }
1145 }
1146
1147 void test2740()
1148 {
1149 {
1150 auto p = new Foo2740i1();
1151 IFooable2740 i = p;
1152 assert(p.foo() == false);
1153 assert(i.foo() == false);
1154 }
1155 {
1156 auto p = new Foo2740i2();
1157 IFooable2740 i = p;
1158 assert(p.foo() == false);
1159 assert(i.foo() == false);
1160 }
1161
1162 {
1163 auto p = new Foo2740c1();
1164 CFooable2740 i = p;
1165 assert(p.foo() == false);
1166 assert(i.foo() == false);
1167 }
1168 {
1169 auto p = new Foo2740c2();
1170 CFooable2740 i = p;
1171 assert(p.foo() == false);
1172 assert(i.foo() == false);
1173 }
1174 }
1175
1176 /*******************************************/
1177
1178 mixin template MTestFoo()
1179 {
1180 int foo(){ return 2; }
1181 }
1182 class TestFoo
1183 {
1184 mixin MTestFoo!() test;
1185 int foo(){ return 1; }
1186 }
1187 void test42()
1188 {
1189 auto p = new TestFoo();
1190 assert(p.foo() == 1);
1191 assert(p.test.foo() == 2);
1192 }
1193
1194 /*******************************************/
1195 // https://issues.dlang.org/show_bug.cgi?id=7744
1196
1197 class ZeroOrMore7744(Expr)
1198 {
1199 enum name = "ZeroOrMore7744!("~Expr.name~")";
1200 }
1201 class Range7744(char begin, char end)
1202 {
1203 enum name = "Range7744!("~begin~","~end~")";
1204 }
1205
1206 mixin(q{
1207 class RubySource7744 : ZeroOrMore7744!(DecLiteral7744)
1208 {
1209 }
1210 class DecLiteral7744 : Range7744!('0','9')
1211 {
1212 }
1213 });
1214
1215 /*******************************************/
1216 // https://issues.dlang.org/show_bug.cgi?id=8032
1217
1218 mixin template T8032()
1219 {
1220 void f() { }
1221 }
1222
1223 class A8032a
1224 {
1225 mixin T8032; // Named mixin causes the error too
1226 void f() { }
1227 }
1228 class B8032a : A8032a
1229 {
1230 override void f() { }
1231 }
1232
1233 class A8032b
1234 {
1235 void f() { }
1236 mixin T8032; // Named mixin causes the error too
1237 }
1238 class B8032b : A8032b
1239 {
1240 override void f() { }
1241 }
1242
1243 /*********************************************/
1244 // https://issues.dlang.org/show_bug.cgi?id=9417
1245
1246 mixin template Foo9417()
1247 {
1248 void foo() {}
1249 }
1250
1251 void test9417()
1252 {
1253 struct B
1254 {
1255 mixin Foo9417;
1256 }
1257 }
1258
1259 /*******************************************/
1260 // https://issues.dlang.org/show_bug.cgi?id=11487
1261
1262 template X11487()
1263 {
1264 struct R()
1265 {
1266 C11487 c;
1267
1268 ~this()
1269 {
1270 static assert(is(typeof(c.front) == void));
1271 }
1272 }
1273 template Mix(alias R)
1274 {
1275 R!() range;
1276 @property front() inout {}
1277 }
1278 }
1279
1280 class C11487
1281 {
1282 alias X11487!() M;
1283 mixin M.Mix!(M.R);
1284 }
1285
1286 /*******************************************/
1287 // https://issues.dlang.org/show_bug.cgi?id=11767
1288
1289 mixin template M11767()
1290 {
1291 struct S11767 {}
1292 }
1293 mixin M11767!();
1294 mixin M11767!(); // OK
1295 static assert(!__traits(compiles, S11767));
1296
1297 void test11767()
1298 {
1299 mixin M11767!();
1300 alias S1 = S11767;
1301 {
1302 mixin M11767!();
1303 alias S2 = S11767;
1304 static assert(!is(S1 == S2));
1305 static assert(S1.mangleof == "S6mixin19test11767FZ8__mixin16S11767");
1306 static assert(S2.mangleof == "S6mixin19test11767FZ8__mixin26S11767");
1307 }
1308 mixin M11767!();
1309 static assert(!__traits(compiles, S11767));
1310 }
1311
1312 /*******************************************/
1313 // https://issues.dlang.org/show_bug.cgi?id=12023
1314
1315 void Delete12023(Object obj) {}
1316
1317 template MessageCode12023()
1318 {
1319 alias typeof(this) C;
1320
1321 struct MessageDeinitHelper
1322 {
1323 C m_outer;
1324
1325 ~this()
1326 {
1327 m_outer.DoDeinitMessaging();
1328 }
1329 }
1330
1331 CToClient toClient = null;
1332 TypeTuple!(CToClient) toClients;
1333
1334 class CToClient {}
1335
1336 void DoDeinitMessaging()
1337 {
1338 Delete12023(toClient);
1339 Delete12023(toClients);
1340 }
1341 }
1342
1343 class TurretCannon12023(ProjectileClass)
1344 {
1345 mixin MessageCode12023;
1346 }
1347
1348 void test12023()
1349 {
1350 auto tc = new TurretCannon12023!Object();
1351 }
1352
1353 /*******************************************/
1354 // https://issues.dlang.org/show_bug.cgi?id=14243
1355
1356 mixin template Mix14243a(int n)
1357 {
1358 static assert(n > 0);
1359 import core.stdc.stdio;
1360 enum { enumMember = 1 }
1361
1362 auto a = A14243(n);
1363 }
1364
1365 mixin template Mix14243b(int n)
1366 {
1367 static if (n > 0)
1368 {
1369 auto b = A14243(n);
1370 }
1371 }
1372
1373 template foo14243(alias v) { auto bar() { return &v; } }
1374 mixin template Mix14243c(alias v)
1375 {
1376 // instantiate template in TemplateMixin
1377 auto c = foo14243!v.bar();
1378 }
1379
1380 mixin template Mix14243d(int n)
1381 {
1382 // Type declaration in TemplateMixin
1383 struct NS { int x = n; }
1384 mixin("auto d" ~ ('0' + n) ~ " = NS();");
1385 }
1386
1387 mixin template Mix14243e(int n)
1388 {
1389 @safe:
1390 nothrow:
1391 int foo() { return var; }
1392
1393 static:
1394 struct S { int x; void f() {} }
1395 int bar() { return n; }
1396 }
1397
1398 int test14243()
1399 {
1400 int[] ctor;
1401 int[] dtor;
1402 struct A14243
1403 {
1404 int x;
1405 this(int x) { ctor ~= x; this.x = x; }
1406 ~this() { dtor ~= x; }
1407 }
1408
1409 {
1410 /**/
1411 assert(ctor == [] && dtor == []); mixin Mix14243a!(1);
1412 assert(ctor == [1] && dtor == []); mixin Mix14243b!(12) b1;
1413 assert(ctor == [1,12] && dtor == []); mixin Mix14243b!(24) b2;
1414 assert(ctor == [1,12,24] && dtor == []);
1415 assert(a.x == 1);
1416 static assert(!__traits(compiles, b > 0)); // ambiguous symbol access
1417 assert(b1.b.x == 12);
1418 assert(b2.b.x == 24);
1419
1420 int x;
1421 mixin Mix14243c!(x);
1422 assert(c == &x);
1423
1424 mixin Mix14243d!(1);
1425 mixin Mix14243d!(2);
1426 static assert(!is(typeof(d1) == typeof(d2)));
1427 assert(d1.x == 1);
1428 assert(d2.x == 2);
1429
1430 assert(ctor == [1,12,24] && dtor == []);
1431 }
1432 assert(ctor == [1,12,24] && dtor == [24,12,1]);
1433
1434 {
1435 int var = 1;
1436 mixin Mix14243e!12;
1437 static assert(is(typeof(&foo) == int delegate() @safe nothrow pure @nogc));
1438 static assert(is(typeof(&bar) == int function() @safe nothrow pure @nogc));
1439 static assert(S.sizeof == int.sizeof); // s is static struct
1440 assert(foo() == 1);
1441 assert(bar() == 12);
1442 }
1443 return 1;
1444 }
1445 static assert(test14243()); // changed to be workable
1446
1447 /*******************************************/
1448 // https://issues.dlang.org/show_bug.cgi?id=10492
1449
1450 class TestClass10492 {}
1451
1452 mixin template mix10492(string name)
1453 {
1454 mixin("scope " ~ name ~ " = new TestClass10492;" );
1455 }
1456
1457 void test10492()
1458 {
1459 mixin mix10492!("var");
1460 }
1461
1462 /*******************************************/
1463
1464 int main()
1465 {
1466 test1();
1467 test2();
1468 test3();
1469 test4();
1470 test5();
1471 test6();
1472 test7();
1473 test8();
1474 test9();
1475 test10();
1476 test11();
1477 test12();
1478 test13();
1479 test14();
1480 test15();
1481 test16();
1482 test17();
1483 test18();
1484 test19();
1485 test20();
1486 test21();
1487 test22();
1488 test23();
1489 test24();
1490 test25();
1491 test26();
1492 test27();
1493 test28();
1494 test29();
1495 test30();
1496 test31();
1497 test32();
1498 test33();
1499 test34();
1500 test35();
1501 test36();
1502 test37();
1503 test38();
1504 test39();
1505 test40();
1506 test41();
1507 test2245();
1508 test2740();
1509 test42();
1510 test9417();
1511 test11767();
1512 test12023();
1513 test14243();
1514 test10492();
1515
1516 printf("Success\n");
1517 return 0;
1518 }