1 // REQUIRED_ARGS:
2 /*
3 TEST_OUTPUT:
4 ---
5 null
6 ---
7 */
8
9 import core.stdc.stdio;
10
11 /*******************************************/
12
bar(int a)13 int bar(int a)
14 {
15 int foo(int b) { return b + 1; }
16
17 return foo(a);
18 }
19
test1()20 void test1()
21 {
22 assert(bar(3) == 4);
23 }
24
25 /*******************************************/
26
bar2(int a)27 int bar2(int a)
28 {
29 static int c = 4;
30
31 int foo(int b) { return b + c + 1; }
32
33 return foo(a);
34 }
35
test2()36 void test2()
37 {
38 assert(bar2(3) == 8);
39 }
40
41
42 /*******************************************/
43
bar3(int a)44 int bar3(int a)
45 {
46 static int foo(int b) { return b + 1; }
47
48 return foo(a);
49 }
50
test3()51 void test3()
52 {
53 assert(bar3(3) == 4);
54 }
55
56 /*******************************************/
57
bar4(int a)58 int bar4(int a)
59 {
60 static int c = 4;
61
62 static int foo(int b) { return b + c + 1; }
63
64 return foo(a);
65 }
66
test4()67 void test4()
68 {
69 assert(bar4(3) == 8);
70 }
71
72
73 /*******************************************/
74
bar5(int a)75 int bar5(int a)
76 {
77 int c = 4;
78
79 int foo(int b) { return b + c + 1; }
80
81 return c + foo(a);
82 }
83
test5()84 void test5()
85 {
86 assert(bar5(3) == 12);
87 }
88
89
90 /*******************************************/
91
bar6(int a)92 int bar6(int a)
93 {
94 int c = 4;
95
96 int foob(int b) { return b + c + 1; }
97 int fooa(int b) { return foob(c + b) * 7; }
98
99 return fooa(a);
100 }
101
test6()102 void test6()
103 {
104 assert(bar6(3) == 84);
105 }
106
107
108 /*******************************************/
109
bar7(int a)110 int bar7(int a)
111 {
112 static int c = 4;
113
114 static int foob(int b) { return b + c + 1; }
115
116 int function(int) fp = &foob;
117
118 return fp(a);
119 }
120
test7()121 void test7()
122 {
123 assert(bar7(3) == 8);
124 }
125
126 /*******************************************/
127
bar8(int a)128 int bar8(int a)
129 {
130 int c = 4;
131
132 int foob(int b) { return b + c + 1; }
133
134 int delegate(int) fp = &foob;
135
136 return fp(a);
137 }
138
test8()139 void test8()
140 {
141 assert(bar8(3) == 8);
142 }
143
144
145 /*******************************************/
146
147 struct Abc9
148 {
149 int a;
150 int b;
151 int c = 7;
152
barAbc9153 int bar(int x)
154 {
155 Abc9 *foo() { return &this; }
156
157 Abc9 *p = foo();
158 assert(p == &this);
159 return p.c + x;
160 }
161 }
162
test9()163 void test9()
164 {
165 Abc9 x;
166
167 assert(x.bar(3) == 10);
168 }
169
170 /*******************************************/
171
172 class Abc10
173 {
174 int a;
175 int b;
176 int c = 7;
177
bar(int x)178 int bar(int x)
179 {
180 Abc10 foo() { return this; }
181
182 Abc10 p = foo();
183 assert(p == this);
184 return p.c + x;
185 }
186
187 }
188
test10()189 void test10()
190 {
191 Abc10 x = new Abc10();
192
193 assert(x.bar(3) == 10);
194 }
195
196
197 /*******************************************/
198
199 class Collection
200 {
201 int[3] array;
202
opApply(void delegate (int)fp)203 void opApply(void delegate(int) fp)
204 {
205 for (int i = 0; i < array.length; i++)
206 fp(array[i]);
207 }
208 }
209
func11(Collection c)210 int func11(Collection c)
211 {
212 int max = int.min;
213
214 void comp_max(int i)
215 {
216 if (i > max)
217 max = i;
218 }
219
220 c.opApply(&comp_max);
221 return max;
222 }
223
test11()224 void test11()
225 {
226 Collection c = new Collection();
227
228 c.array[0] = 7;
229 c.array[1] = 26;
230 c.array[2] = 25;
231
232 int m = func11(c);
233 assert(m == 26);
234 }
235
236
237 /*******************************************/
238
SimpleNestedFunction()239 void SimpleNestedFunction ()
240 {
241 int nest () { return 432; }
242
243 assert (nest () == 432);
244 int delegate () func = &nest;
245 assert (func () == 432);
246 }
247
AccessParentScope()248 void AccessParentScope ()
249 {
250 int value = 9;
251
252 int nest () { assert (value == 9); return 9; }
253
254 assert (nest () == 9);
255 }
256
CrossNestedScope()257 void CrossNestedScope ()
258 {
259 int x = 45;
260
261 void foo () { assert (x == 45); }
262 void bar () { int z = 16; foo (); }
263 bar ();
264 }
265
BadMultipleNested()266 void BadMultipleNested ()
267 {
268 int x;
269
270 void foo ()
271 {
272 void bar ()
273 {
274 //erroneous x = 4; // Should fail.
275 }
276 }
277 }
278
279 /* This one kind of depends upon memory layout. GlobalScopeSpoof should
280 be called with no "this" pointer; this is trying to ensure that
281 everything is working properly. Of course, in the DMD calling
282 convention it'll fail if the caller passes too much/little data. */
283
GlobalScopeSpoof(int x,int y)284 void GlobalScopeSpoof (int x, int y)
285 {
286 assert (x == y && y == 487);
287 }
288
GlobalScope()289 void GlobalScope ()
290 {
291 void bar () { GlobalScopeSpoof (487, 487); }
292 bar ();
293 }
294
295 class TestClass
296 {
297 int x = 6400;
298
foo()299 void foo ()
300 {
301 void bar () { assert (x == 6400); }
302 bar ();
303 }
304 }
305
test12()306 void test12()
307 {
308 SimpleNestedFunction ();
309 AccessParentScope ();
310 CrossNestedScope ();
311 GlobalScope ();
312 (new TestClass).foo ();
313 }
314
315
316 /*******************************************/
317
test13()318 void test13()
319 {
320 struct Abc
321 {
322 int x = 3;
323 int y = 4;
324 }
325
326 Abc a;
327
328 assert(a.x == 3 && a.y == 4);
329 }
330
331
332 /*******************************************/
333
test14()334 void test14()
335 {
336 struct Abc
337 {
338 int x = 3;
339 int y = 4;
340
341 int foo() { return y; }
342 }
343
344 Abc a;
345
346 assert(a.foo() == 4);
347 }
348
349
350 /*******************************************/
351
test15()352 void test15()
353 {
354 static int z = 5;
355
356 struct Abc
357 {
358 int x = 3;
359 int y = 4;
360
361 int foo() { return y + z; }
362 }
363
364 Abc a;
365
366 assert(a.foo() == 9);
367 }
368
369
370 /*******************************************/
371
test16()372 void test16()
373 {
374 static int z = 5;
375
376 static class Abc
377 {
378 int x = 3;
379 int y = 4;
380
381 int foo() { return y + z; }
382 }
383
384 Abc a = new Abc();
385
386 assert(a.foo() == 9);
387 }
388
389
390 /*******************************************/
391
test17()392 void test17()
393 {
394 int function(int x) fp;
395
396 fp = function int(int y) { return y + 3; };
397 assert(fp(7) == 10);
398 }
399
400 /*******************************************/
401
test18()402 void test18()
403 {
404 static int a = 3;
405 int function(int x) fp;
406
407 fp = function int(int y) { return y + a; };
408 assert(fp(7) == 10);
409 }
410
411 /*******************************************/
412
test19()413 void test19()
414 {
415 int a = 3;
416
417 int delegate(int x) fp;
418
419 fp = delegate int(int y) { return y + a; };
420 assert(fp(7) == 10);
421 }
422
423
424 /*******************************************/
425
426 class Collection20
427 {
428 int[3] array;
429
opApply(void delegate (int)fp)430 void opApply(void delegate(int) fp)
431 {
432 for (int i = 0; i < array.length; i++)
433 fp(array[i]);
434 }
435 }
436
func20(Collection20 c)437 int func20(Collection20 c)
438 {
439 int max = int.min;
440
441 c.opApply(delegate(int i) { if (i > max) max = i; });
442 return max;
443 }
444
test20()445 void test20()
446 {
447 Collection20 c = new Collection20();
448
449 c.array[0] = 7;
450 c.array[1] = 26;
451 c.array[2] = 25;
452
453 int m = func20(c);
454 assert(m == 26);
455 }
456
457
458 /*******************************************/
459
bar21(int a)460 int bar21(int a)
461 {
462 int c = 3;
463
464 int foo(int b)
465 {
466 b += c; // 4 is added to b
467 c++; // bar.c is now 5
468 return b + c; // 12 is returned
469 }
470 c = 4;
471 int i = foo(a); // i is set to 12
472 return i + c; // returns 17
473 }
474
test21()475 void test21()
476 {
477 int i = bar21(3); // i is assigned 17
478 assert(i == 17);
479 }
480
481 /*******************************************/
482
foo22(void delegate ()baz)483 void foo22(void delegate() baz)
484 {
485 baz();
486 }
487
bar22(int i)488 void bar22(int i)
489 {
490 int j = 14;
491 printf("%d,%d\n",i,j);
492
493 void fred()
494 {
495 printf("%d,%d\n",i,j);
496 assert(i == 12 && j == 14);
497 }
498
499 fred();
500 foo22(&fred);
501 }
502
test22()503 void test22()
504 {
505 bar22(12);
506 }
507
508
509 /*******************************************/
510
frelled(void delegate ()baz)511 void frelled(void delegate() baz)
512 {
513 baz();
514 }
515
516 class Foo23
517 {
bar(int i)518 void bar(int i)
519 {
520 int j = 14;
521 printf("%d,%d\n",i,j);
522
523 void fred()
524 {
525 printf("%d,%d\n",i,j);
526 assert(i == 12);
527 assert(j == 14);
528 }
529
530 frelled(&fred);
531 }
532 }
533
test23()534 void test23()
535 {
536 Foo23 f = new Foo23();
537
538 f.bar(12);
539 }
540
541
542 /*******************************************/
543
544 void delegate () function (int x) store24;
545
delegate()546 void delegate () zoom24(int x)
547 {
548 return delegate void () { };
549 }
550
test24()551 void test24()
552 {
553 store24 = &zoom24;
554 store24 (1) ();
555 }
556
557
558 /*******************************************/
559
test25()560 void test25()
561 {
562 delegate() { printf("stop the insanity!\n"); }();
563 delegate() { printf("stop the insanity! 2\n"); }();
564 }
565
566 /*******************************************/
567
568 alias bool delegate(int) callback26;
569
570
foo26(callback26 a)571 bool foo26(callback26 a)
572 {
573 return a(12);
574 }
575
576 class Bar26
577 {
func(int v)578 int func(int v)
579 {
580 printf("func(v=%d)\n", v);
581 foo26(delegate bool(int a)
582 {
583 printf("%d %d\n",a,v); return true;
584 assert(a == 12);
585 assert(v == 15);
586 assert(0);
587 } );
588 return v;
589 }
590 }
591
592
test26()593 void test26()
594 {
595 Bar26 b = new Bar26();
596
597 b.func(15);
598 }
599
600
601 /*******************************************/
602
603 class A27
604 {
myFunc()605 uint myFunc()
606 {
607 uint myInt = 13;
608 uint mySubFunc()
609 {
610 return myInt;
611 }
612 return mySubFunc();
613 }
614 }
615
test27()616 void test27()
617 {
618 A27 myInstance = new A27;
619 int i = myInstance.myFunc();
620 printf("%d\n", i);
621 assert(i == 13);
622 }
623
624
625 /*******************************************/
626
Foo28(void delegate ()call)627 void Foo28(void delegate() call)
628 {
629 call();
630 }
631
632 class Bar28
633 {
func()634 int func()
635 {
636 int count = 0;
637
638 Foo28(delegate void() { ++count; } );
639 return count;
640 }
641 }
642
test28()643 void test28()
644 {
645 Bar28 b = new Bar28();
646 int i = b.func();
647 assert(i == 1);
648 }
649
650
651 /*******************************************/
652
653 class Foo29
654 {
Func(void delegate ()call)655 void Func(void delegate() call)
656 {
657 for(int i = 0; i < 10; ++i)
658 call();
659 }
660 }
661
662 class Bar29
663 {
Func()664 int Func()
665 {
666 int count = 0;
667 Foo29 ic = new Foo29();
668
669 ic.Func(delegate void() { ++count; } );
670 return count;
671 }
672 }
673
test29()674 void test29()
675 {
676 Bar29 b = new Bar29();
677 int i = b.Func();
678 assert(i == 10);
679 }
680
681 /*******************************************/
682
683 struct Foo30
684 {
685 int[] arr;
686 }
687
Func30(Foo30 bar)688 void Func30(Foo30 bar)
689 {
690 void InnerFunc(int x, int y)
691 {
692 int a = bar.arr[y]; // Ok
693
694 if(bar.arr[y]) // Access violation
695 {
696 }
697 }
698
699 InnerFunc(5,5);
700 }
701
702
test30()703 void test30()
704 {
705 Foo30 abc;
706
707 abc.arr.length = 10;
708 Func30(abc);
709 }
710
711
712 /*******************************************/
713
call31(int d,void delegate (int d)f)714 void call31(int d, void delegate(int d) f)
715 {
716 assert(d == 100 || d == 200);
717 printf("d = %d\n", d);
718 f(d);
719 }
720
test31()721 void test31()
722 {
723 call31(100, delegate void(int d1)
724 {
725 printf("d1 = %d\n", d1);
726 assert(d1 == 100);
727 call31(200, delegate void(int d2)
728 {
729 printf("d1 = %d\n", d1);
730 printf("d2 = %d\n", d2);
731 assert(d1 == 100);
732 assert(d2 == 200);
733 });
734 });
735 }
736
737
738 /*******************************************/
739
call32(int d,void delegate (int d)f)740 void call32(int d, void delegate(int d) f)
741 {
742 assert(d == 100 || d == 200);
743 printf("d = %d\n", d);
744 f(d);
745 }
746
test32()747 void test32()
748 {
749 call32(100, delegate void(int d1)
750 {
751 int a = 3;
752 int b = 4;
753 printf("d1 = %d, a = %d, b = %d\n", d1, a, b);
754 assert(a == 3);
755 assert(b == 4);
756 assert(d1 == 100);
757
758 call32(200, delegate void(int d2)
759 {
760 printf("d1 = %d, a = %d\n", d1, a);
761 printf("d2 = %d, b = %d\n", d2, b);
762 assert(a == 3);
763 assert(b == 4);
764 assert(d1 == 100);
765 assert(d2 == 200);
766 });
767 });
768 }
769
770
771 /*******************************************/
772
test33()773 void test33()
774 {
775 extern (C) int Foo1(int a, int b, int c)
776 {
777 assert(a == 1);
778 assert(b == 2);
779 assert(c == 3);
780 return 1;
781 }
782
783 extern (D) int Foo2(int a, int b, int c)
784 {
785 assert(a == 1);
786 assert(b == 2);
787 assert(c == 3);
788 return 2;
789 }
790
791 extern (Windows) int Foo3(int a, int b, int c)
792 {
793 assert(a == 1);
794 assert(b == 2);
795 assert(c == 3);
796 return 3;
797 }
798
799 assert(Foo1(1, 2, 3) == 1);
800 assert(Foo2(1, 2, 3) == 2);
801 assert(Foo3(1, 2, 3) == 3);
802
803 printf("test33 success\n");
804 }
805
806 /*******************************************/
807
808 class Foo34
809 {
810 int x;
811
812 class Bar
813 {
814 int y;
815
delegate()816 int delegate() getDelegate()
817 {
818 assert(y == 8);
819 auto i = sayHello();
820 assert(i == 23);
821 return &sayHello;
822 }
823 }
824 Bar bar;
825
sayHello()826 int sayHello()
827 {
828 printf("Hello\n");
829 assert(x == 47);
830 return 23;
831 }
832
this()833 this()
834 {
835 x = 47;
836 bar = new Bar();
837 bar.y = 8;
838 }
839 }
840
test34()841 void test34()
842 {
843 Foo34 foo = new Foo34();
844 int delegate() saydg = foo.bar.getDelegate();
845 printf("This should print Hello:\n");
846 auto i = saydg();
847 assert(i == 23);
848 }
849
850 /*******************************************/
851
852 class Foo35
853 {
854 int x = 42;
bar()855 void bar()
856 {
857 int y = 43;
858 new class Object
859 {
860 this()
861 {
862 //writefln("x = %s", x);
863 //writefln("y = %s", y);
864 assert(x == 42);
865 assert(y == 43);
866 //static assert(is(typeof(this.outer) == void*)); // Bugzilla 14442
867 static assert(is(typeof(this.outer) == Foo35)); // Bugzilla 15839
868 }
869 };
870 }
871 }
872
test35()873 void test35()
874 {
875 Foo35 f = new Foo35();
876 f.bar();
877 }
878
879 /*******************************************/
880
881 class Foo36
882 {
883 int x = 42;
this()884 this()
885 {
886 int y = 43;
887 new class Object
888 {
889 this()
890 {
891 //writefln("x = %s", x);
892 //writefln("y = %s", y);
893 assert(x == 42);
894 assert(y == 43);
895 }
896 };
897 }
898 }
899
test36()900 void test36()
901 {
902 Foo36 f = new Foo36();
903 }
904
905 /*******************************************/
906
907 class Foo37
908 {
909 int x = 42;
bar()910 void bar()
911 {
912 int y = 43;
913 void abc()
914 {
915 new class Object
916 {
917 this()
918 {
919 //writefln("x = %s", x);
920 //writefln("y = %s", y);
921 assert(x == 42);
922 assert(y == 43);
923 }
924 };
925 }
926
927 abc();
928 }
929 }
930
test37()931 void test37()
932 {
933 Foo37 f = new Foo37();
934 f.bar();
935 }
936
937 /*******************************************/
938
test38()939 void test38()
940 {
941 int status = 3;
942
943 int delegate() foo()
944 {
945 class C
946 {
947 int dg()
948 {
949 return ++status;
950 }
951 }
952
953 C c = new C();
954
955 return &c.dg;
956 }
957
958 int delegate() bar = foo();
959
960 if(status != 3)
961 {
962 assert(0);
963 }
964
965 if(bar() != 4)
966 {
967 assert(0);
968 }
969
970 if(status != 4)
971 {
972 assert(0);
973 }
974 }
975
976 /*******************************************/
977
test39()978 void test39()
979 {
980 int status;
981
982 int delegate() foo()
983 {
984 return &(new class
985 {
986 int dg()
987 {
988 return ++status;
989 }
990 }
991 ).dg;
992 }
993
994 int delegate() bar = foo();
995
996 if(status != 0)
997 {
998 assert(0);
999 }
1000
1001 if(bar() != 1)
1002 {
1003 assert(0);
1004 }
1005
1006 if(status != 1)
1007 {
1008 assert(0);
1009 }
1010 }
1011
1012 /*******************************************/
1013
1014 interface I40
1015 {
1016 void get( string s );
1017 }
1018
1019 class C40
1020 {
1021 int a = 4;
1022
init()1023 void init()
1024 {
1025 I40 i = new class() I40
1026 {
1027 void get( string s )
1028 {
1029 func();
1030 }
1031 };
1032 i.get("hello");
1033 }
func()1034 void func( ){ assert(a == 4); }
1035 }
1036
test40()1037 void test40()
1038 {
1039 C40 c = new C40();
1040 c.init();
1041 }
1042
1043 /*******************************************/
1044
1045 class C41
1046 { int a = 3;
1047
init()1048 void init()
1049 {
1050 class N
1051 {
1052 void get()
1053 {
1054 func();
1055 }
1056 }
1057 N n = new N();
1058 n.get();
1059 }
func()1060 void func()
1061 {
1062 assert(a == 3);
1063 }
1064 }
1065
1066
test41()1067 void test41()
1068 {
1069 C41 c = new C41();
1070 c.init();
1071 }
1072
1073 /*******************************************/
1074
1075 class C42
1076 { int a = 3;
1077
init()1078 void init()
1079 {
1080 class N
1081 {
1082 void init()
1083 {
1084 class M
1085 {
1086 void get()
1087 {
1088 func();
1089 }
1090 }
1091 M m = new M();
1092 m.get();
1093 }
1094 }
1095 N n = new N();
1096 n.init();
1097 }
func()1098 void func()
1099 {
1100 assert(a == 3);
1101 }
1102 }
1103
test42()1104 void test42()
1105 {
1106 C42 c = new C42();
1107 c.init();
1108 }
1109
1110
1111 /*******************************************/
1112
foo43(alias X)1113 int foo43(alias X)() { return X; }
1114
test43()1115 void test43()
1116 {
1117 int x = 3;
1118
1119 void bar()
1120 {
1121 int y = 4;
1122 assert(foo43!(x)() == 3);
1123 assert(foo43!(y)() == 4);
1124 }
1125
1126 bar();
1127
1128 assert(foo43!(x)() == 3);
1129 }
1130
1131
1132 /*******************************************/
1133
1134 class Comb
1135 {
1136 }
1137
Foo44(Comb delegate ()[]c...)1138 Comb Foo44(Comb delegate()[] c...)
1139 {
1140 Comb ec = c[0]();
1141 printf("1ec = %p\n", ec);
1142 ec.toString();
1143 printf("2ec = %p\n", ec);
1144 return ec;
1145 }
1146
1147 Comb c44;
1148
this()1149 static this()
1150 {
1151 c44 = new Comb;
1152 }
1153
test44()1154 void test44()
1155 {
1156 c44 = Foo44(Foo44(c44));
1157 }
1158
1159 /*******************************************/
1160
1161 class Bar45
1162 {
test()1163 void test()
1164 {
1165 a = 4;
1166 Inner i = new Inner;
1167 i.foo();
1168 }
1169
1170 class Inner
1171 {
foo()1172 void foo()
1173 {
1174 assert(a == 4);
1175 Inner i = new Inner;
1176 i.bar();
1177 }
1178
bar()1179 void bar()
1180 {
1181 assert(a == 4);
1182 }
1183 }
1184 int a;
1185 }
1186
test45()1187 void test45()
1188 {
1189 Bar45 b = new Bar45;
1190 assert(b.a == 0);
1191 b.test();
1192 }
1193
1194 /*******************************************/
1195
1196 class Adapter
1197 {
1198 int a = 2;
1199
func()1200 int func()
1201 {
1202 return 73;
1203 }
1204 }
1205
1206 class Foo46
1207 {
1208 int b = 7;
1209
1210 class AnonAdapter : Adapter
1211 {
1212 int aa = 8;
1213
this()1214 this()
1215 {
1216 assert(b == 7);
1217 assert(aa == 8);
1218 }
1219 }
1220
func()1221 void func()
1222 {
1223 Adapter a = cast( Adapter )( new AnonAdapter() );
1224 assert(a.func() == 73);
1225 assert(a.a == 2);
1226 }
1227 }
1228
test46()1229 void test46()
1230 {
1231 Foo46 f = new Foo46();
1232 f.func();
1233 }
1234
1235
1236 /*******************************************/
1237
test47()1238 void test47()
1239 {
1240 void delegate() test =
1241 {
1242 struct Foo {int x=3;}
1243 Foo f;
1244 assert(f.x == 3);
1245 };
1246 test();
1247 }
1248
1249 /*******************************************/
1250
1251 struct Outer48
1252 {
1253 class Inner
1254 {
thisOuter481255 this(int i) { b = i; }
1256 int b;
1257 }
1258
1259 int a = 6;
1260
fOuter481261 void f()
1262 {
1263 int nested()
1264 {
1265 auto x = new Inner(a);
1266 return x.b + 1;
1267 }
1268 int i = nested();
1269 assert(i == 7);
1270 }
1271 }
1272
1273
test48()1274 void test48()
1275 {
1276 Outer48 s;
1277 s.f();
1278 }
1279
1280 /*******************************************/
1281
test49()1282 void test49()
1283 {
1284 int j = 10;
1285 void mainlocal(int x)
1286 {
1287 printf("mainlocal: j = %d, x = %d\n", j, x);
1288 assert(j == 10);
1289 assert(x == 1);
1290 }
1291
1292 void fun2()
1293 {
1294 int k = 20;
1295 void fun2local(int x)
1296 {
1297 printf("fun2local: k = %d, x = %d\n", k, x);
1298 assert(j == 10);
1299 assert(k == 20);
1300 assert(x == 2);
1301 }
1302
1303 void fun1()
1304 {
1305 mainlocal(1);
1306 fun2local(2);
1307 }
1308
1309 fun1();
1310 }
1311
1312 fun2();
1313 }
1314
1315 /*******************************************/
1316
funa50(alias pred1,alias pred2)1317 void funa50(alias pred1, alias pred2)()
1318 {
1319 pred1(1);
1320 pred2(2);
1321 }
1322
funb50(alias pred1)1323 void funb50(alias pred1)()
1324 { int k = 20;
1325 void funb50local(int x)
1326 {
1327 printf("funb50local: k = %d, x = %d\n", k, x);
1328 assert(k == 20);
1329 assert(x == 2);
1330 }
1331 funa50!(pred1, funb50local)();
1332 }
1333
test50()1334 void test50()
1335 {
1336 int j = 10;
1337 void mainlocal(int x)
1338 {
1339 printf("mainlocal: j = %d, x = %d\n", j, x);
1340 assert(j == 10);
1341 assert(x == 1);
1342 }
1343 funb50!(mainlocal)();
1344 }
1345
1346 /*******************************************/
1347
funa51(alias pred1,alias pred2)1348 void funa51(alias pred1, alias pred2)()
1349 {
1350 pred1(2);
1351 pred2(1);
1352 }
1353
funb51(alias pred1)1354 void funb51(alias pred1)()
1355 { int k = 20;
1356 void funb51local(int x)
1357 {
1358 printf("funb51local: k = %d, x = %d\n", k, x);
1359 assert(k == 20);
1360 assert(x == 2);
1361 }
1362 funa51!(funb51local, pred1)();
1363 }
1364
test51()1365 void test51()
1366 {
1367 int j = 10;
1368 void mainlocal(int x)
1369 {
1370 printf("mainlocal: j = %d, x = %d\n", j, x);
1371 assert(j == 10);
1372 assert(x == 1);
1373 }
1374 funb51!(mainlocal)();
1375 }
1376
1377 /*******************************************/
1378
1379 C52 c52;
1380
1381 class C52
1382 {
1383 int index = 7;
test1()1384 void test1(){
1385 printf( "this = %p, index = %d\n", this, index );
1386 assert(index == 7);
1387 assert(this == c52);
1388 }
test()1389 void test()
1390 {
1391 class N
1392 {
1393 void callI()
1394 {
1395 printf("test1\n");
1396 test1();
1397 printf("test2\n");
1398 if (index is -1)
1399 { // Access to the outer-super-field triggers the bug
1400 printf("test3\n");
1401 }
1402 }
1403 }
1404 auto i = new N();
1405 i.callI();
1406 }
1407 }
1408
test52()1409 void test52()
1410 {
1411 auto c = new C52;
1412 printf("c = %p\n", c);
1413 c52 = c;
1414 c.test();
1415 }
1416
1417 /*******************************************/
1418
foo53(int i)1419 void foo53(int i)
1420 {
1421 struct SS
1422 {
1423 int x,y;
1424 int bar() { return x + i + 1; }
1425 }
1426 SS s;
1427 s.x = 3;
1428 assert(s.bar() == 11);
1429 }
1430
test53()1431 void test53()
1432 {
1433 foo53(7);
1434 }
1435
1436 /*******************************************/
1437
test54()1438 void test54()
1439 {
1440 int x = 40;
1441 int fun(int i) { return x + i; }
1442
1443 struct A
1444 {
1445 int bar(int i) { return fun(i); }
1446 }
1447
1448 A makeA()
1449 {
1450 // A a; return a;
1451 return A();
1452 }
1453
1454 A makeA2()
1455 {
1456 A a; return a;
1457 //return A();
1458 }
1459
1460 A a = makeA();
1461 assert(a.bar(2) == 42);
1462
1463 A b = makeA2();
1464 assert(b.bar(3) == 43);
1465
1466 auto c = new A;
1467 assert(c.bar(4) == 44);
1468 }
1469 /*******************************************/
1470
test55()1471 void test55()
1472 {
1473 int localvar = 7;
1474
1475 int inner(int delegate(ref int) dg) {
1476 int k = localvar;
1477 return 0;
1478 }
1479
1480 int a = localvar * localvar; // This modifies the EAX register
1481
1482 foreach (entry; &inner)
1483 {
1484 }
1485 }
1486
1487 /*******************************************/
1488 // 4401
1489
test4401()1490 void test4401()
1491 {
1492 auto foo() {
1493 return 3;
1494 }
1495
1496 auto bar() nothrow pure {
1497 return 3;
1498 }
1499
1500 auto baz() @property pure @safe {
1501 return 3;
1502 }
1503
1504 auto zoo()() @property pure @safe nothrow {
1505 return 3;
1506 }
1507 }
1508
1509 /*******************************************/
1510
1511 alias void delegate() dg_t;
1512
Y(dg_t delegate (dg_t)y)1513 void Y(dg_t delegate (dg_t) y)
1514 {
1515 struct F { void delegate(F) f; }
1516
1517 version (all)
1518 { // generates error
1519 (dg_t delegate(F) a){return a(F((F b){return y(a(b))();})); }
1520 ((F b){return (){return b.f(b);};});
1521 }
1522 else
1523 {
1524 auto abc(dg_t delegate(F) a)
1525 {
1526 return a(F((F b){return y(a(b))();}));
1527 }
1528
1529 abc((F b){return (){return b.f(b);};});
1530 }
1531 }
1532
1533
test7428()1534 void test7428(){
1535 dg_t foo(dg_t self)
1536 {
1537 void bar() { self(); }
1538 return &bar;
1539 }
1540
1541 Y(&foo);
1542 }
1543
1544 /*******************************************/
1545 // 4612
1546
S4612a(alias x)1547 struct S4612a(alias x)
1548 {
1549 void* func() { void* p = &x; return p; }
1550 }
1551
S4612b(alias x)1552 struct S4612b(alias x)
1553 {
1554 int i;
1555 void* func() { void* p = &x; return p; }
1556 }
1557
test4612()1558 void test4612()
1559 {
1560 int a;
1561
1562 auto sa = S4612a!a();
1563 assert(sa.func() == &a);
1564
1565 auto sb = S4612b!(a)();
1566 assert(sb.func() == &a);
1567 }
1568
1569 /*******************************************/
1570
S4841(alias pred)1571 struct S4841(alias pred)
1572 {
1573 void unused_func();
1574 }
1575
abc4841()1576 void abc4841() {
1577 int w;
1578 S4841!(w) m;
1579 }
1580
test4841()1581 void test4841() {
1582 abc4841();
1583 }
1584
1585 /*******************************************/
1586
1587
index7199()1588 void index7199()
1589 {
1590 void find()
1591 {
1592 bool hay()
1593 {
1594 return true;
1595 }
1596 }
1597
1598 find();
1599 }
1600
test7199()1601 void test7199()
1602 {
1603 index7199();
1604 }
1605
1606 /*******************************************/
1607 // 7965
1608
test7965()1609 void test7965()
1610 {
1611 int x;
1612 static int* px;
1613 px = &x;
1614
1615 printf("&x = %p in main()\n", &x);
1616 struct S1
1617 {
1618 char y;
1619 void boom() {
1620 printf("&x = %p in S1.boom()\n", &x);
1621 assert(&x == px);
1622 //x = 42; // makes the struct nested
1623 }
1624 }
1625 S1 s1;
1626 s1.boom();
1627
1628 struct S2
1629 {
1630 this(int n) {
1631 printf("&x = %p in S2.this()\n", &x);
1632 assert(&x == px);
1633 }
1634 char y;
1635 }
1636 S2 s2 = S2(10);
1637 }
1638
1639 struct S7965
1640 {
1641 string str;
1642 uint unused1, unused2 = 0;
1643 }
1644
f7965(alias fun)1645 auto f7965(alias fun)()
1646 {
1647 struct Result
1648 {
1649 S7965 s;
1650 this(S7965 _s) { s = _s; } // required for the problem
1651 void g() { assert(fun(s.str) == "xa"); }
1652 }
1653
1654 return Result(S7965("a"));
1655 }
1656
test7965a()1657 void test7965a()
1658 {
1659 string s = "x";
1660 f7965!(a => s ~= a)().g();
1661 assert(s == "xa");
1662 }
1663
1664 /*******************************************/
1665 // 8188
1666
Print8188(b...)1667 mixin template Print8188(b...)
1668 {
1669 int doprint()
1670 {
1671 return b[0] * b[1];
1672 }
1673 }
1674
1675 class A8188
1676 {
1677 int x, y;
1678 mixin Print8188!(x, y);
1679 }
1680
test8188()1681 void test8188()
1682 {
1683 auto a = new A8188;
1684 a.x = 2;
1685 a.y = 5;
1686 assert(a.doprint() == 10);
1687 }
1688
1689 /*******************************************/
1690 // 5082
1691
1692 struct S5082 { float x; }
1693
Map5082a(alias fun)1694 struct Map5082a(alias fun)
1695 {
1696 typeof({ return fun(int.init); }()) cache;
1697 }
1698
Map5082b(alias fun)1699 struct Map5082b(alias fun)
1700 {
1701 typeof({ return fun(int.init); }()) cache;
1702
1703 S5082 front(int i) { return fun(i); }
1704 }
1705
test5082()1706 void test5082()
1707 {
1708 auto temp = S5082(1);
1709 auto func = (int v){ return temp; };
1710 auto map1 = Map5082a!func();
1711 auto map2 = Map5082b!func();
1712 assert(map2.front(1) == temp);
1713 }
1714
1715
1716 /*******************************************/
1717 // 8194
1718
test8194()1719 void test8194()
1720 {
1721 int foo;
1722 static void bar()
1723 {
1724 typeof(foo) baz;
1725 static assert(is(typeof(baz) == int));
1726 }
1727 }
1728
1729 /*******************************************/
1730 // 8339
1731
map8339a(fun...)1732 template map8339a(fun...)
1733 {
1734 auto map8339a(Range)(Range r) {
1735 struct Result {
1736 this(double[] input) {}
1737 }
1738 return Result(r);
1739 }
1740 }
map8339b(fun...)1741 template map8339b(fun...)
1742 {
1743 auto map8339b(Range)(Range r) {
1744 struct Result {
1745 this(double[] input) { fun[0](input.length); }
1746 }
1747 return Result(r);
1748 }
1749 }
map8339c(fun...)1750 template map8339c(fun...)
1751 {
1752 auto map8339c(Range)(Range r) {
1753 static struct Result {
1754 this(double[] input) {}
1755 }
1756 return Result(r);
1757 }
1758 }
map8339d(fun...)1759 template map8339d(fun...)
1760 {
1761 auto map8339d(Range)(Range r) {
1762 static struct Result {
1763 this(double[] input) { fun[0](input.length); }
1764 }
1765 return Result(r);
1766 }
1767 }
copy8339(T)1768 void copy8339(T)(T x)
1769 {
1770 T xSaved;
1771 }
test8339a()1772 void test8339a()
1773 {
1774 double[] x;
1775 int n;
1776
1777 // Result has context pointer, so cannot copy
1778 static assert (!is(typeof({ copy8339(map8339a!(a=>a)(x)); })));
1779 static assert (!is(typeof({ copy8339(map8339a!(a=>n)(x)); })));
1780
1781 // same as
1782 static assert (!is(typeof({ copy8339(map8339b!(a=>a)(x)); })));
1783 static assert (!is(typeof({ copy8339(map8339b!(a=>n)(x)); })));
1784
1785 // fun is never instantiated
1786 copy8339(map8339c!(a=>a)(x));
1787 copy8339(map8339c!(a=>n)(x));
1788
1789 // static nested struct doesn't have contest pointer
1790 copy8339(map8339d!(a=>a)(x));
1791 //copy8339(map8339d!(a=>n)(x)); // too strict case
1792
1793 }
1794
filter8339(alias pred)1795 template filter8339(alias pred)
1796 {
1797 auto filter8339(R)(R r) {
1798 struct Result {
1799 R range;
1800 this(R r) { range = r; }
1801 auto front() { return pred(0); }
1802 }
1803 return Result(r);
1804 }
1805 }
test8339b()1806 void test8339b()
1807 {
1808 static makefilter() { int n; return filter8339!(a=>n)([]); }
1809
1810 auto r1 = makefilter();
1811 filter8339!(a=>a)(r1);
1812 }
1813
test8339c()1814 void test8339c()
1815 {
1816 class C(X) { X x; }
1817 class C1 { C!C1 x; }
1818 alias C!C1 C2;
1819
1820 struct Pair { C1 t; C2 u; void func(){} }
1821 Pair pair;
1822 }
1823
1824 /*******************************************/
1825 // 8704
1826
check8704(T,int num)1827 void check8704(T, int num)()
1828 {
1829 static if (num == 1) T t0;
1830 static if (num == 2) T t1 = T();
1831 static if (num == 3) T t2 = T(1);
1832 }
1833
test8704()1834 void test8704()
1835 {
1836 struct S
1837 {
1838 int n;
1839 void foo(){}
1840 }
1841
1842 static assert(!is(typeof(check8704!(S, 1)())));
1843 static assert(!is(typeof(check8704!(S, 2)())));
1844 static assert(!is(typeof(check8704!(S, 3)())));
1845
1846 static assert(!__traits(compiles, check8704!(S, 1)()));
1847 static assert(!__traits(compiles, check8704!(S, 2)()));
1848 static assert(!__traits(compiles, check8704!(S, 3)()));
1849 }
1850
1851 /*******************************************/
1852 // 8923
1853
test8923a()1854 void test8923a()
1855 {
1856 int val;
1857
1858 struct S // is nested struct
1859 {
1860 void foo() { val = 1; } // access to val through the hidden frame pointer
1861 }
1862 S s1a; s1a.foo();
1863 S s1b = S(); s1b.foo();
1864 S[1] s2a; s2a[0].foo();
1865 S[1] s2b = S(); s2b[0].foo();
1866
1867 static struct U { S s; }
1868 U u1a; u1a.s.foo();
1869 U u1b = U(); u1b.s.foo();
1870 U u1c = U(s1a); u1c.s.foo();
1871 U[1] u2a; u2a[0].s.foo();
1872 U[1] u2b = U(); u2b[0].s.foo();
1873 U[1] u2c = U(s1a); u2c[0].s.foo();
1874 static struct V { S[1] s; }
1875 V v1a; v1a.s[0].foo();
1876 V v1b = V(); v1b.s[0].foo();
1877 V v1c = V(s1a); v1c.s[0].foo();
1878 V[1] v2a; v2a[0].s[0].foo();
1879 V[1] v2b = V(); v2b[0].s[0].foo();
1880 V[1] v2c = V(s1a); v2c[0].s[0].foo();
1881
1882 static struct W { S s; this(S s){ this.s = s; } }
1883 W w1a; w1a.s.foo();
1884 W w1b = W(); w1b.s.foo();
1885 W w1c = W(s1a); w1c.s.foo();
1886 W[1] w2a; w2a[0].s.foo();
1887 W[1] w2b = W(); w2b[0].s.foo();
1888 W[1] w2c = W(s1a); w2c[0].s.foo();
1889 static struct X { S[1] s; this(S s){ this.s[] = s; } }
1890 X x1a; x1a.s[0].foo();
1891 X x1b = X(); x1b.s[0].foo();
1892 X x1c = X(s1a); x1c.s[0].foo();
1893 X[1] x2a; x2a[0].s[0].foo();
1894 X[1] x2b = X(); x2b[0].s[0].foo();
1895 X[1] x2c = X(s1a); x2c[0].s[0].foo();
1896
1897 // Both declarations, Y and Z should raise errors,
1898 // because their ctors don't initialize their field 's'.
1899 static assert(!__traits(compiles, {
1900 static struct Y { S s; this(S){} }
1901 }));
1902 /+ Y y1a; //y1a.s.foo();
1903 Y y1b = Y(); y1b.s.foo();
1904 Y y1c = Y(s1a);//y1c.s.foo();
1905 Y[1] y2a; //y2a[0].s.foo();
1906 Y[1] y2b = Y(); y2b[0].s.foo();
1907 Y[1] y2c = Y(s1a);//y2c[0].s.foo(); +/
1908 static assert(!__traits(compiles, {
1909 static struct Z { S[1] s; this(S){} }
1910 }));
1911 /+ Z z1a; //z1a.s[0].foo();
1912 Z z1b = Z(); z1b.s[0].foo();
1913 Z z1c = Z(s1a);//z1c.s[0].foo();
1914 Z[1] z2a; //z1a.s[0].foo();
1915 Z[1] z2b = Z(); z1b.s[0].foo();
1916 Z[1] z2c = Z(s1a);//z1c.s[0].foo(); // +/
1917 }
1918
Tuple8923(int v,T...)1919 struct Tuple8923(int v, T...)
1920 {
1921 T field;
1922 static if (v == 1) this(T args) { } // should be an error
1923 static if (v == 2) this(T args) { field = args; }
1924 static if (v == 3) this(U...)(U args) { } // should be an error
1925 static if (v == 4) this(U...)(U args) { field = args; }
1926 //alias field this;
1927 }
test8923b()1928 void test8923b()
1929 {
1930 int val;
1931 struct S { void foo() { val = 1; } }
1932
1933 static assert(!__traits(compiles, Tuple8923!(1, S)(S()) ));
1934 static assert(!__traits(compiles, Tuple8923!(3, S)(S()) ));
1935
1936 auto tup2 = Tuple8923!(2, S)(S());
1937 tup2.field[0].foo(); // correctly initialized
1938
1939 auto tup4 = Tuple8923!(4, S)(S());
1940 tup4.field[0].foo(); // correctly initialized
1941 }
1942
test8923c()1943 void test8923c()
1944 {
1945 int val;
1946
1947 struct S // is nested struct
1948 {
1949 void foo() { val = 1; } // access to val through the hidden frame pointer
1950 }
1951 S s1a; s1a.foo();
1952 S s1b = S(); s1b.foo();
1953 S[1] s2a; s2a[0].foo();
1954 S[1] s2b = S(); s2b[0].foo();
1955
1956 // U,V,W,X are nested struct, but should work same as non-nested.
1957 // 1: bare struct object. 2: static array of structs.
1958 // a: default construction.
1959 // b: construction by literal syntax which has no argument.
1960 // c: construction by literal syntax which has one or more arguments.
1961
1962 struct U
1963 {
1964 S s;
1965 void foo() { val = 2; }
1966 }
1967 U u1a; u1a.foo(); u1a.s.foo();
1968 U u1b = U(); u1b.foo(); u1b.s.foo();
1969 U u1c = U(s1a); u1c.foo(); u1c.s.foo();
1970 U[1] u2a; u2a[0].foo(); u2a[0].s.foo();
1971 U[1] u2b = U(); u2b[0].foo(); u2b[0].s.foo();
1972 U[1] u2c = U(s1a); u2c[0].foo(); u2c[0].s.foo();
1973
1974 struct V
1975 {
1976 S[1] s;
1977 void foo() { val = 2; }
1978 }
1979 V v1a; v1a.foo(); v1a.s[0].foo();
1980 V v1b = V(); v1b.foo(); v1b.s[0].foo();
1981 V v1c = V(s1a); v1c.foo(); v1c.s[0].foo();
1982 V[1] v2a; v2a[0].foo(); v2a[0].s[0].foo();
1983 V[1] v2b = V(); v2b[0].foo(); v2b[0].s[0].foo();
1984 V[1] v2c = V(s1a); v2c[0].foo(); v2c[0].s[0].foo();
1985
1986 struct W
1987 {
1988 S s;
1989 this(S s) { this.s = s; }
1990 void foo() { val = 2; }
1991 }
1992 W w1a; w1a.foo(); w1a.s.foo();
1993 W w1b = W(); w1b.foo(); w1b.s.foo();
1994 W w1c = W(s1a); w1c.foo(); w1c.s.foo();
1995 W[1] w2a; w2a[0].foo(); w2a[0].s.foo();
1996 W[1] w2b = W(); w2b[0].foo(); w2b[0].s.foo();
1997 W[1] w2c = W(s1a); w2c[0].foo(); w2c[0].s.foo();
1998
1999 struct X
2000 {
2001 S[1] s;
2002 this(S s) { this.s[] = s; }
2003 void foo() { val = 2; }
2004 }
2005 X x1a; x1a.foo(); x1a.s[0].foo();
2006 X x1b = X(); x1b.foo(); x1b.s[0].foo();
2007 X x1c = X(s1a); x1c.foo(); x1c.s[0].foo();
2008 X[1] x2a; x2a[0].foo(); x2a[0].s[0].foo();
2009 X[1] x2b = X(); x2b[0].foo(); x2b[0].s[0].foo();
2010 X[1] x2c = X(s1a); x2c[0].foo(); x2c[0].s[0].foo();
2011
2012 // Both declarations, Y and Z should raise errors,
2013 // because their ctors don't initialize their field 's'.
2014 static assert(!__traits(compiles, {
2015 struct Y1 { S s; this(S){} void foo() { val = 2; } }
2016 }));
2017 static assert(!__traits(compiles, {
2018 struct Y2 { S s; this(T)(S){} void foo() { val = 2; } }
2019 auto y2 = Y2!S(S()); // instantiate ctor
2020 }));
2021
2022 static assert(!__traits(compiles, {
2023 struct Z1 { S[1] s; this(S){} void foo() { val = 2; } }
2024 }));
2025 static assert(!__traits(compiles, {
2026 struct Z2 { S[1] s; this(T)(S){} void foo() { val = 2; } }
2027 auto z2 = Z2!S(S()); // instantiate ctor
2028 }));
2029 }
2030
2031 /*******************************************/
2032 // 9003
2033
test9003()2034 void test9003()
2035 {
2036 int i;
2037 struct NS {
2038 int n1; // Comment to pass all asserts
2039 int n2; // Uncomment to fail assert on line 19
2040 int f() { return i; }
2041 }
2042
2043 static struct SS1 {
2044 NS ns;
2045 }
2046 SS1 ss1;
2047 assert(ss1.ns != NS.init);
2048
2049 static struct SS2 {
2050 NS ns1, ns2;
2051 }
2052 SS2 ss2;
2053 assert(ss2.ns1 != NS.init); // line 19
2054 assert(ss2.ns2 != NS.init);
2055
2056 static struct SS3 {
2057 int i;
2058 NS ns;
2059 }
2060
2061 SS3 ss3;
2062 assert(ss3.ns != NS.init);
2063
2064 static struct SS4 {
2065 int i;
2066 NS ns1, ns2;
2067 }
2068
2069 SS4 ss4;
2070 assert(ss4.ns1 != NS.init); // fails
2071 assert(ss4.ns2 != NS.init); // fails
2072 }
2073
2074 /*******************************************/
2075 // 9006
2076
test9006()2077 void test9006()
2078 {
2079 int i;
2080 struct NS
2081 {
2082 int n;
2083 int[3] a; // Uncomment to fail assert on line 20 and pass on line 23
2084 int f() { return i; }
2085 }
2086 NS ns;
2087 assert(ns != NS.init);
2088 ns = NS.init;
2089 assert(ns == NS.init);
2090
2091 static struct SS { NS ns; }
2092 assert(SS.init.ns == NS.init); // fails
2093 assert(SS.init.ns != NS()); // fails
2094
2095 SS s;
2096 assert(s.ns != NS.init); // line 20
2097 assert(s != SS.init); // fails
2098 s = SS.init;
2099 assert(s.ns == NS.init); // line 23, fails
2100 assert(s == SS.init);
2101 }
2102
2103 /*******************************************/
2104 // 9035
2105
test9035()2106 void test9035()
2107 {
2108 static struct S {}
2109
2110 void f(T)(auto ref T t)
2111 {
2112 static assert(!__traits(isRef, t));
2113 }
2114
2115 f(S.init); // ok, rvalue
2116 f(S()); // ok, rvalue
2117
2118 int i;
2119 struct Nested
2120 {
2121 int j = 0; void f() { ++i; }
2122 }
2123
2124 f(Nested()); // ok, rvalue
2125 f(Nested.init); // fails, lvalue
2126
2127 assert(Nested.init.j == 0);
2128 //(ref n) { n.j = 5; }(Nested.init);
2129 assert(Nested.init.j == 0); // fails, j is 5
2130 }
2131
test9035a()2132 void test9035a()
2133 {
2134 int x;
2135 struct S {
2136 // no field
2137 void foo() { x = 1; }
2138 }
2139 S s1;
2140 S s2 = S();
2141 assert(s1 != S.init); // OK
2142 assert(s2 != S.init); // OK
2143 assert(S() != S.init); // NG -> OK
2144 }
2145
2146 /*******************************************/
2147 // 9036
2148
test9036()2149 void test9036()
2150 {
2151 static int i;
2152 static struct S
2153 {
2154 this(this) { ++i; }
2155 }
2156
2157 S s = S.init;
2158 assert(i == 0); // postblit not called
2159 s = S.init;
2160 assert(i == 0); // postblit not called
2161
2162 int k;
2163 static int j = 0;
2164 struct N
2165 {
2166 this(this)
2167 {
2168 ++j;
2169 assert(this.tupleof[$-1] != null); // fails
2170 }
2171 void f() { ++k; }
2172 }
2173
2174 N n = N.init;
2175 assert(j == 0); // fails, j = 1, postblit called
2176 n = N.init;
2177 assert(j == 0); // fails, j = 2, postblit called
2178 }
2179
2180 /*******************************************/
2181
2182 /+
2183 auto fun8863(T)(T* ret) { *ret = T(); }
2184
2185 void test8863()
2186 {
2187 int x = 1;
2188 struct A
2189 {
2190 auto f()
2191 {
2192 assert(x == 1);
2193 }
2194 }
2195
2196 A a;
2197 a.f();
2198 fun8863!A(&a);
2199 a.f();
2200 }
2201 +/
2202
2203 /*******************************************/
2204 // 8774
2205
popFront8774()2206 void popFront8774()
2207 {
2208 int[20] abc; // smash stack
2209 }
2210
MapResult8774(alias fun)2211 struct MapResult8774(alias fun)
2212 {
2213 void delegate() front()
2214 {
2215 return fun(1);
2216 }
2217 }
2218
test8774()2219 void test8774() {
2220
2221 int sliceSize = 100;
2222
2223 void delegate() foo( int i )
2224 {
2225 void delegate() closedPartialSum()
2226 {
2227 int ii = i ;
2228 void bar()
2229 {
2230 printf("%d\n", sliceSize);
2231 assert(sliceSize == 100);
2232 }
2233 return &bar;
2234 }
2235 return closedPartialSum();
2236 }
2237
2238 auto threads = MapResult8774!foo();
2239
2240 auto dg = threads.front();
2241 popFront8774();
2242
2243 printf("calling dg()\n");
2244 dg();
2245 }
2246
2247 /*******************************************/
2248
Bug8832(alias X)2249 int Bug8832(alias X)()
2250 {
2251 return X();
2252 }
2253
delegate()2254 int delegate() foo8832()
2255 {
2256 int stack;
2257 int heap = 3;
2258
2259 int nested_func()
2260 {
2261 ++heap;
2262 return heap;
2263 }
2264 return delegate int() { return Bug8832!(nested_func); };
2265 }
2266
test8832()2267 void test8832()
2268 {
2269 auto z = foo8832();
2270 auto p = foo8832();
2271 assert(z() == 4);
2272 p();
2273 assert(z() == 5);
2274 }
2275
2276 /*******************************************/
2277 // 9315
2278
test9315()2279 auto test9315()
2280 {
2281 struct S
2282 {
2283 int i;
2284 void bar() {}
2285 }
2286 pragma(msg, S.init.tupleof[$-1]);
2287 }
2288
2289 /*******************************************/
2290 // 9244
2291
test9244()2292 void test9244()
2293 {
2294 union U {
2295 int i;
2296 @safe int x() { return i; }
2297 }
2298 }
2299
2300 /*******************************************/
2301 // 10495
2302
2303 struct X10495
2304 {
2305 @disable this();
2306 }
2307
2308 struct Y10495(alias f)
2309 {
2310 void g() {}
2311 }
2312
2313 class C10495
2314 {
2315 X10495 s = X10495.init;
2316
2317 void h()
2318 {
2319 Y10495!(a => a) st;
2320 }
2321 }
2322
2323 /*******************************************/
2324 // 11385
2325
2326 auto map11385(alias fun, R)(R range)
2327 {
2328 return MapResult11385!(fun, R)(range);
2329 }
2330 struct MapResult11385(alias fun, R)
2331 {
2332 R range;
2333 auto front() { return fun(range[0]); }
2334 }
2335 void test11385()
2336 {
2337 //import std.algorithm;
2338 static auto fun1(T)(T a) { return a * 2; }
2339 auto fun2(T)(T a) { return a * 2; }
2340 [1].map11385!(a=>fun1(a)); // OK
2341 [1].map11385!(a=>fun2(a)); // NG:XXX is a nested function and cannot be accessed from XXX
2342 }
2343
2344 /*******************************************/
2345
2346 void xmap(alias g)(int t)
2347 {
2348 g(t);
2349 }
2350
2351 enum foo11297 = function (int x)
2352 {
2353 //int bar(int y) { return x; } xmap!bar(7);
2354 xmap!(y => x)(7);
2355 };
2356
2357 void xreduce(alias f)()
2358 {
2359 f(4);
2360 }
2361
2362 void test11297()
2363 {
2364 xreduce!foo11297();
2365 }
2366
2367 /*******************************************/
2368 // 11886
2369
2370 struct Lambda11886(alias fun)
2371 {
2372 auto opCall(A...)(A args) { return fun(args); }
2373 }
2374 void test11886()
2375 {
2376 int n = 10;
2377 Lambda11886!(x => x + n) f;
2378 assert(f(1) == 11); // Line 9
2379
2380 struct NS
2381 {
2382 auto foo(T)(T t) { return t * n; }
2383 }
2384 static assert(NS.tupleof.length == 1);
2385 static assert(NS.sizeof == (void*).sizeof);
2386 NS ns;
2387 assert(ns.foo(2) == 20);
2388 }
2389
2390 /*******************************************/
2391 // 12234
2392
2393 void test12234()
2394 {
2395 class B
2396 {
2397 int a;
2398 this(int aa) { a = aa; }
2399 }
2400 auto foo = {
2401 return new B(1);
2402 };
2403 static assert(is(typeof(foo) == delegate));
2404
2405 auto b = foo();
2406 assert(b.a == 1);
2407 }
2408
2409 /*******************************************/
2410 // 12981
2411
2412 template Mix12981(T)
2413 {
2414 class A
2415 {
2416 alias typeof(this.outer) x;
2417 }
2418 }
2419
2420 class B12981
2421 {
2422 mixin Mix12981!(int);
2423
2424 static assert(is(A.x == B12981));
2425 }
2426
2427 /*******************************************/
2428 // 13861
2429
2430 struct Foo13861(alias f)
2431 {
2432 struct Bar
2433 {
2434 Bar func()
2435 {
2436 return Bar(); // OK <- Segfault
2437 }
2438 }
2439 }
2440
2441 void test13861()
2442 {
2443 Foo13861!(n => n) a;
2444 }
2445
2446 /*******************************************/
2447 // 14398
2448
2449 void test14398()
2450 {
2451 int outer;
2452
2453 struct Inner
2454 {
2455 this(this)
2456 {
2457 outer += 42;
2458 }
2459 }
2460
2461 struct Outer
2462 {
2463 Inner inner;
2464
2465 this(int dummy)
2466 {
2467 inner = Inner();
2468
2469 // hidden fields correctly set
2470 assert(this.tupleof[$-1] !is null);
2471 assert(inner.tupleof[$-1] !is null);
2472 }
2473 }
2474
2475 Outer[1] arr1 = [Outer(0)];
2476 assert(outer == 0); // no postblit called on arr1 construction
2477 auto arr2 = arr1;
2478 assert(outer == 42); // inner is copied successfully
2479 }
2480
2481 /*******************************************/
2482 // 14846
2483
2484 void foo14846(Dg)(scope Dg code)
2485 {
2486 static assert(is(Dg == delegate));
2487 code();
2488 }
2489
2490 void test14846()
2491 {
2492 int x;
2493
2494 struct S
2495 {
2496 this(int n) { x = n; }
2497 ~this() { x = 99; }
2498 }
2499
2500 foo14846({ S s; });
2501 foo14846({ S s = S(); });
2502 foo14846({ S s = S(1); });
2503 foo14846({ S[3] s; });
2504
2505 foo14846({ S* p = new S(); });
2506 foo14846({ S* p = new S(1); });
2507 foo14846({ S[] a = [S()]; });
2508 foo14846({ S[] a = [S(1)]; });
2509 }
2510
2511 /*******************************************/
2512 // 15422
2513
2514 class App15422(T)
2515 {
2516 this() {}
2517
2518 auto test1(T val)
2519 in {} body // necessary to reproduce the crash
2520 {
2521 struct Foo
2522 {
2523 this(int k) {}
2524 T a;
2525 }
2526
2527 Foo foo;
2528 foo.a = val;
2529
2530 // Frame of test2 function, allocated on heap.
2531 assert(foo.tupleof[$-1] !is null);
2532
2533 //printf("&foo = %p\n", &foo); // stack
2534 //printf("&this = %p\n", &this); // stack?
2535 //printf("foo.vthis = %p\n", foo.tupleof[$-1]); // stack...!?
2536 //assert(cast(void*)&this !is *cast(void**)&foo.tupleof[$-1], "bad");
2537 // BUG: currently foo.vthis set to the address of 'this' variable on the stack.
2538 // It's should be stomped to null, because Foo.vthis is never be used.
2539
2540 int[Foo] map;
2541 map[foo] = 1; // OK <- crash
2542
2543 return foo;
2544 }
2545
2546 auto test2(T val)
2547 //in {} body
2548 {
2549 int closVar;
2550 struct Foo
2551 {
2552 this(int k) { closVar = k; }
2553 // Make val a closure variable.
2554
2555 T a;
2556 }
2557
2558 Foo foo;
2559 foo.a = val;
2560
2561 // Frame of test2 function, allocated on heap.
2562 assert(foo.tupleof[$-1] !is null);
2563
2564 return foo;
2565 }
2566 }
2567
2568 void test15422a()
2569 {
2570 alias App = App15422!int;
2571 App app1 = new App;
2572 {
2573 auto x = app1.test1(1);
2574 auto y = app1.test1(1);
2575 static assert(is(typeof(x) == typeof(y)));
2576
2577 // int (bitwise comparison)
2578 assert(x.a == y.a);
2579
2580 assert(*cast(void**)&x.tupleof[$-1] is *cast(void**)&y.tupleof[$-1]);
2581
2582 // bitwise equality (needOpEquals() and needToHash() returns false)
2583 assert(x == y);
2584
2585 // BUG
2586 //assert(*cast(void**)&x.tupleof[$-1] is null);
2587 //assert(*cast(void**)&y.tupleof[$-1] is null);
2588 auto getZ() { auto z = app1.test1(1); return z; }
2589 auto z = getZ();
2590 assert(x.a == z.a);
2591 //assert(x.tupleof[$-1] is z.tupleof[$-1]); // should pass
2592 //assert(x == z); // should pass
2593
2594 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2595 }
2596 App app2 = new App;
2597 {
2598 auto x = app1.test2(1);
2599 auto y = app2.test2(1);
2600 static assert(is(typeof(x) == typeof(y)));
2601
2602 // int (bitwise comparison)
2603 assert(x.a == y.a);
2604
2605 // closure envirionments
2606 assert(*cast(void**)&x.tupleof[$-1] !is *cast(void**)&y.tupleof[$-1]);
2607
2608 // Changed to bitwise equality (needOpEquals() and needToHash() returns false)
2609 assert(x != y); // OK <- crash
2610
2611 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2612 }
2613 }
2614
2615 void test15422b()
2616 {
2617 alias App = App15422!string;
2618 App app1 = new App;
2619 {
2620 auto x = app1.test1("a".idup);
2621 auto y = app1.test1("a".idup);
2622 static assert(is(typeof(x) == typeof(y)));
2623
2624 // string (element-wise comparison)
2625 assert(x.a == y.a);
2626
2627 assert(*cast(void**)&x.tupleof[$-1] is *cast(void**)&y.tupleof[$-1]);
2628
2629 // memberwise equality (needToHash() returns true)
2630 assert(x == y);
2631 // Lowered to: x.a == y.a && x.tupleof[$-1] is y.tupleof[$-1]
2632
2633 // BUG
2634 //assert(*cast(void**)&x.tupleof[$-1] is null);
2635 //assert(*cast(void**)&y.tupleof[$-1] is null);
2636 auto getZ() { auto z = app1.test1("a".idup); return z; }
2637 auto z = getZ();
2638 assert(x.a == z.a);
2639 //assert(x.tupleof[$-1] is z.tupleof[$-1]); // should pass
2640 //assert(x == z); // should pass
2641
2642 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2643 }
2644 App app2 = new App;
2645 {
2646 auto x = app1.test2("a".idup);
2647 auto y = app2.test2("a".idup);
2648 static assert(is(typeof(x) == typeof(y)));
2649
2650 // string (element-wise comparison)
2651 assert(x.a == y.a);
2652
2653 // closure envirionments
2654 assert(*cast(void**)&x.tupleof[$-1] !is *cast(void**)&y.tupleof[$-1]);
2655
2656 // Changed to memberwise equality (needToHash() returns true)
2657 // Lowered to: x.a == y.a && x.tupleof[$-1] is y.tupleof[$-1]
2658 assert(x != y); // OK <- crash
2659
2660 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2661 }
2662 }
2663
2664 /***************************************************/
2665 // 15757
2666
2667 template map15757(fun...)
2668 {
2669 auto map15757(R)(R r)
2670 {
2671 return MapResult15757!(fun, R)(r);
2672 }
2673 }
2674
2675 struct MapResult15757(alias fun, R)
2676 {
2677 R _input;
2678
2679 this(R input)
2680 {
2681 _input = input;
2682 }
2683 }
2684
2685 void wrap15757(R)(R r)
2686 {
2687 struct M(R)
2688 {
2689 this(R r)
2690 {
2691 payload = r;
2692 }
2693 R payload;
2694 }
2695
2696 M!R m = M!R(r);
2697 }
2698
2699 void test15757() @safe
2700 {
2701 [1,2,3].map15757!(x => x*x).wrap15757;
2702 }
2703
2704 /***************************************************/
2705
2706 int main()
2707 {
2708 test1();
2709 test2();
2710 test3();
2711 test4();
2712 test5();
2713 test6();
2714 test7();
2715 test8();
2716 test9();
2717 test10();
2718 test11();
2719 test12();
2720 test13();
2721 test14();
2722 test15();
2723 test16();
2724 test17();
2725 test18();
2726 test19();
2727 test20();
2728 test21();
2729 test22();
2730 test23();
2731 test24();
2732 test25();
2733 test26();
2734 test27();
2735 test28();
2736 test29();
2737 test30();
2738 test31();
2739 test32();
2740 test33();
2741 test34();
2742 test35();
2743 test36();
2744 test37();
2745 test38();
2746 test39();
2747 test40();
2748 test41();
2749 test42();
2750 test43();
2751 test44();
2752 test45();
2753 test46();
2754 test47();
2755 test48();
2756 test49();
2757 test50();
2758 test51();
2759 test52();
2760 test53();
2761 test54();
2762 test55();
2763 test4401();
2764 test7428();
2765 test4612();
2766 test4841();
2767 test7199();
2768 test7965();
2769 test7965a();
2770 test8188();
2771
2772 test5082();
2773 test8194();
2774 test8339a();
2775 test8339b();
2776 test8339c();
2777 test8923a();
2778 test8923b();
2779 test8923c();
2780 test9003();
2781 test9006();
2782 test9035();
2783 test9035a();
2784 test9036();
2785 // test8863();
2786 test8774();
2787 test8832();
2788 test9315();
2789 test9244();
2790 test11385();
2791 test11297();
2792 test11886();
2793 test12234();
2794 test13861();
2795 test14398();
2796 test14846();
2797 test15422a();
2798 test15422b();
2799 test15757();
2800
2801 printf("Success\n");
2802 return 0;
2803 }
2804