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