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*)); // https://issues.dlang.org/show_bug.cgi?id=14442
867 static assert(is(typeof(this.outer) == Foo35)); // https://issues.dlang.org/show_bug.cgi?id=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
1489 enum dg56 = delegate { return 5; };
1490
test56()1491 void test56()
1492 {
1493 auto inner() {
1494 return dg56();
1495 }
1496
1497 assert(inner() == 5);
1498 }
1499
1500 /*******************************************/
1501 // https://issues.dlang.org/show_bug.cgi?id=4401
1502
test4401()1503 void test4401()
1504 {
1505 auto foo() {
1506 return 3;
1507 }
1508
1509 auto bar() nothrow pure {
1510 return 3;
1511 }
1512
1513 auto baz() @property pure @safe {
1514 return 3;
1515 }
1516
1517 auto zoo()() @property pure @safe nothrow {
1518 return 3;
1519 }
1520 }
1521
1522 /*******************************************/
1523
1524 alias void delegate() dg_t;
1525
Y(dg_t delegate (dg_t)y)1526 void Y(dg_t delegate (dg_t) y)
1527 {
1528 struct F { void delegate(F) f; }
1529
1530 version (all)
1531 { // generates error
1532 (dg_t delegate(F) a){return a(F((F b){return y(a(b))();})); }
1533 ((F b){return (){return b.f(b);};});
1534 }
1535 else
1536 {
1537 auto abc(dg_t delegate(F) a)
1538 {
1539 return a(F((F b){return y(a(b))();}));
1540 }
1541
1542 abc((F b){return (){return b.f(b);};});
1543 }
1544 }
1545
1546
test7428()1547 void test7428(){
1548 dg_t foo(dg_t self)
1549 {
1550 void bar() { self(); }
1551 return &bar;
1552 }
1553
1554 Y(&foo);
1555 }
1556
1557 /*******************************************/
1558 // https://issues.dlang.org/show_bug.cgi?id=4612
1559
S4612a(alias x)1560 struct S4612a(alias x)
1561 {
1562 void* func() { void* p = &x; return p; }
1563 }
1564
S4612b(alias x)1565 struct S4612b(alias x)
1566 {
1567 int i;
1568 void* func() { void* p = &x; return p; }
1569 }
1570
test4612()1571 void test4612()
1572 {
1573 int a;
1574
1575 auto sa = S4612a!a();
1576 assert(sa.func() == &a);
1577
1578 auto sb = S4612b!(a)();
1579 assert(sb.func() == &a);
1580 }
1581
1582 /*******************************************/
1583
S4841(alias pred)1584 struct S4841(alias pred)
1585 {
1586 void unused_func();
1587 }
1588
abc4841()1589 void abc4841() {
1590 int w;
1591 S4841!(w) m;
1592 }
1593
test4841()1594 void test4841() {
1595 abc4841();
1596 }
1597
1598 /*******************************************/
1599
1600
index7199()1601 void index7199()
1602 {
1603 void find()
1604 {
1605 bool hay()
1606 {
1607 return true;
1608 }
1609 }
1610
1611 find();
1612 }
1613
test7199()1614 void test7199()
1615 {
1616 index7199();
1617 }
1618
1619 /*******************************************/
1620 // https://issues.dlang.org/show_bug.cgi?id=7965
1621
test7965()1622 void test7965()
1623 {
1624 int x;
1625 static int* px;
1626 px = &x;
1627
1628 printf("&x = %p in main()\n", &x);
1629 struct S1
1630 {
1631 char y;
1632 void boom() {
1633 printf("&x = %p in S1.boom()\n", &x);
1634 assert(&x == px);
1635 //x = 42; // makes the struct nested
1636 }
1637 }
1638 S1 s1;
1639 s1.boom();
1640
1641 struct S2
1642 {
1643 this(int n) {
1644 printf("&x = %p in S2.this()\n", &x);
1645 assert(&x == px);
1646 }
1647 char y;
1648 }
1649 S2 s2 = S2(10);
1650 }
1651
1652 struct S7965
1653 {
1654 string str;
1655 uint unused1, unused2 = 0;
1656 }
1657
f7965(alias fun)1658 auto f7965(alias fun)()
1659 {
1660 struct Result
1661 {
1662 S7965 s;
1663 this(S7965 _s) { s = _s; } // required for the problem
1664 void g() { assert(fun(s.str) == "xa"); }
1665 }
1666
1667 return Result(S7965("a"));
1668 }
1669
test7965a()1670 void test7965a()
1671 {
1672 string s = "x";
1673 f7965!(a => s ~= a)().g();
1674 assert(s == "xa");
1675 }
1676
1677 /*******************************************/
1678 // https://issues.dlang.org/show_bug.cgi?id=8188
1679
Print8188(b...)1680 mixin template Print8188(b...)
1681 {
1682 int doprint()
1683 {
1684 return b[0] * b[1];
1685 }
1686 }
1687
1688 class A8188
1689 {
1690 int x, y;
1691 mixin Print8188!(x, y);
1692 }
1693
test8188()1694 void test8188()
1695 {
1696 auto a = new A8188;
1697 a.x = 2;
1698 a.y = 5;
1699 assert(a.doprint() == 10);
1700 }
1701
1702 /*******************************************/
1703 // https://issues.dlang.org/show_bug.cgi?id=5082
1704
1705 struct S5082 { float x; }
1706
Map5082a(alias fun)1707 struct Map5082a(alias fun)
1708 {
1709 typeof({ return fun(int.init); }()) cache;
1710 }
1711
Map5082b(alias fun)1712 struct Map5082b(alias fun)
1713 {
1714 typeof({ return fun(int.init); }()) cache;
1715
1716 S5082 front(int i) { return fun(i); }
1717 }
1718
test5082()1719 void test5082()
1720 {
1721 auto temp = S5082(1);
1722 auto func = (int v){ return temp; };
1723 auto map1 = Map5082a!func();
1724 auto map2 = Map5082b!func();
1725 assert(map2.front(1) == temp);
1726 }
1727
1728
1729 /*******************************************/
1730 // https://issues.dlang.org/show_bug.cgi?id=8194
1731
test8194()1732 void test8194()
1733 {
1734 int foo;
1735 static void bar()
1736 {
1737 typeof(foo) baz;
1738 static assert(is(typeof(baz) == int));
1739 }
1740 }
1741
1742 /*******************************************/
1743 // https://issues.dlang.org/show_bug.cgi?id=8339
1744
map8339a(fun...)1745 template map8339a(fun...)
1746 {
1747 auto map8339a(Range)(Range r) {
1748 struct Result {
1749 this(double[] input) {}
1750 }
1751 return Result(r);
1752 }
1753 }
map8339b(fun...)1754 template map8339b(fun...)
1755 {
1756 auto map8339b(Range)(Range r) {
1757 struct Result {
1758 this(double[] input) { fun[0](input.length); }
1759 }
1760 return Result(r);
1761 }
1762 }
map8339c(fun...)1763 template map8339c(fun...)
1764 {
1765 auto map8339c(Range)(Range r) {
1766 static struct Result {
1767 this(double[] input) {}
1768 }
1769 return Result(r);
1770 }
1771 }
map8339d(fun...)1772 template map8339d(fun...)
1773 {
1774 auto map8339d(Range)(Range r) {
1775 static struct Result {
1776 this(double[] input) { fun[0](input.length); }
1777 }
1778 return Result(r);
1779 }
1780 }
copy8339(T)1781 void copy8339(T)(T x)
1782 {
1783 T xSaved;
1784 }
test8339a()1785 void test8339a()
1786 {
1787 double[] x;
1788 int n;
1789
1790 // Result has context pointer, so cannot copy
1791 static assert (!is(typeof({ copy8339(map8339a!(a=>a)(x)); })));
1792 static assert (!is(typeof({ copy8339(map8339a!(a=>n)(x)); })));
1793
1794 // same as
1795 static assert (!is(typeof({ copy8339(map8339b!(a=>a)(x)); })));
1796 static assert (!is(typeof({ copy8339(map8339b!(a=>n)(x)); })));
1797
1798 // fun is never instantiated
1799 copy8339(map8339c!(a=>a)(x));
1800 copy8339(map8339c!(a=>n)(x));
1801
1802 // static nested struct doesn't have contest pointer
1803 copy8339(map8339d!(a=>a)(x));
1804 //copy8339(map8339d!(a=>n)(x)); // too strict case
1805
1806 }
1807
filter8339(alias pred)1808 template filter8339(alias pred)
1809 {
1810 auto filter8339(R)(R r) {
1811 struct Result {
1812 R range;
1813 this(R r) { range = r; }
1814 auto front() { return pred(0); }
1815 }
1816 return Result(r);
1817 }
1818 }
test8339b()1819 void test8339b()
1820 {
1821 static makefilter() { int n; return filter8339!(a=>n)([]); }
1822
1823 auto r1 = makefilter();
1824 filter8339!(a=>a)(r1);
1825 }
1826
test8339c()1827 void test8339c()
1828 {
1829 class C(X) { X x; }
1830 class C1 { C!C1 x; }
1831 alias C!C1 C2;
1832
1833 struct Pair { C1 t; C2 u; void func(){} }
1834 Pair pair;
1835 }
1836
1837 /*******************************************/
1838 // https://issues.dlang.org/show_bug.cgi?id=8704
1839
check8704(T,int num)1840 void check8704(T, int num)()
1841 {
1842 static if (num == 1) T t0;
1843 static if (num == 2) T t1 = T();
1844 static if (num == 3) T t2 = T(1);
1845 }
1846
test8704()1847 void test8704()
1848 {
1849 struct S
1850 {
1851 int n;
1852 void foo(){}
1853 }
1854
1855 static assert(!is(typeof(check8704!(S, 1)())));
1856 static assert(!is(typeof(check8704!(S, 2)())));
1857 static assert(!is(typeof(check8704!(S, 3)())));
1858
1859 static assert(!__traits(compiles, check8704!(S, 1)()));
1860 static assert(!__traits(compiles, check8704!(S, 2)()));
1861 static assert(!__traits(compiles, check8704!(S, 3)()));
1862 }
1863
1864 /*******************************************/
1865 // https://issues.dlang.org/show_bug.cgi?id=8923
1866
test8923a()1867 void test8923a()
1868 {
1869 int val;
1870
1871 struct S // is nested struct
1872 {
1873 void foo() { val = 1; } // access to val through the hidden frame pointer
1874 }
1875 S s1a; s1a.foo();
1876 S s1b = S(); s1b.foo();
1877 S[1] s2a; s2a[0].foo();
1878 S[1] s2b = S(); s2b[0].foo();
1879
1880 static struct U { S s; }
1881 U u1a; u1a.s.foo();
1882 U u1b = U(); u1b.s.foo();
1883 U u1c = U(s1a); u1c.s.foo();
1884 U[1] u2a; u2a[0].s.foo();
1885 U[1] u2b = U(); u2b[0].s.foo();
1886 U[1] u2c = U(s1a); u2c[0].s.foo();
1887 static struct V { S[1] s; }
1888 V v1a; v1a.s[0].foo();
1889 V v1b = V(); v1b.s[0].foo();
1890 V v1c = V(s1a); v1c.s[0].foo();
1891 V[1] v2a; v2a[0].s[0].foo();
1892 V[1] v2b = V(); v2b[0].s[0].foo();
1893 V[1] v2c = V(s1a); v2c[0].s[0].foo();
1894
1895 static struct W { S s; this(S s){ this.s = s; } }
1896 W w1a; w1a.s.foo();
1897 W w1b = W(); w1b.s.foo();
1898 W w1c = W(s1a); w1c.s.foo();
1899 W[1] w2a; w2a[0].s.foo();
1900 W[1] w2b = W(); w2b[0].s.foo();
1901 W[1] w2c = W(s1a); w2c[0].s.foo();
1902 static struct X { S[1] s; this(S s){ this.s[] = s; } }
1903 X x1a; x1a.s[0].foo();
1904 X x1b = X(); x1b.s[0].foo();
1905 X x1c = X(s1a); x1c.s[0].foo();
1906 X[1] x2a; x2a[0].s[0].foo();
1907 X[1] x2b = X(); x2b[0].s[0].foo();
1908 X[1] x2c = X(s1a); x2c[0].s[0].foo();
1909
1910 // Both declarations, Y and Z should raise errors,
1911 // because their ctors don't initialize their field 's'.
1912 static assert(!__traits(compiles, {
1913 static struct Y { S s; this(S){} }
1914 }));
1915 /+ Y y1a; //y1a.s.foo();
1916 Y y1b = Y(); y1b.s.foo();
1917 Y y1c = Y(s1a);//y1c.s.foo();
1918 Y[1] y2a; //y2a[0].s.foo();
1919 Y[1] y2b = Y(); y2b[0].s.foo();
1920 Y[1] y2c = Y(s1a);//y2c[0].s.foo(); +/
1921 static assert(!__traits(compiles, {
1922 static struct Z { S[1] s; this(S){} }
1923 }));
1924 /+ Z z1a; //z1a.s[0].foo();
1925 Z z1b = Z(); z1b.s[0].foo();
1926 Z z1c = Z(s1a);//z1c.s[0].foo();
1927 Z[1] z2a; //z1a.s[0].foo();
1928 Z[1] z2b = Z(); z1b.s[0].foo();
1929 Z[1] z2c = Z(s1a);//z1c.s[0].foo(); // +/
1930 }
1931
Tuple8923(int v,T...)1932 struct Tuple8923(int v, T...)
1933 {
1934 T field;
1935 static if (v == 1) this(T args) { } // should be an error
1936 static if (v == 2) this(T args) { field = args; }
1937 static if (v == 3) this(U...)(U args) { } // should be an error
1938 static if (v == 4) this(U...)(U args) { field = args; }
1939 //alias field this;
1940 }
test8923b()1941 void test8923b()
1942 {
1943 int val;
1944 struct S { void foo() { val = 1; } }
1945
1946 static assert(!__traits(compiles, Tuple8923!(1, S)(S()) ));
1947 static assert(!__traits(compiles, Tuple8923!(3, S)(S()) ));
1948
1949 auto tup2 = Tuple8923!(2, S)(S());
1950 tup2.field[0].foo(); // correctly initialized
1951
1952 auto tup4 = Tuple8923!(4, S)(S());
1953 tup4.field[0].foo(); // correctly initialized
1954 }
1955
test8923c()1956 void test8923c()
1957 {
1958 int val;
1959
1960 struct S // is nested struct
1961 {
1962 void foo() { val = 1; } // access to val through the hidden frame pointer
1963 }
1964 S s1a; s1a.foo();
1965 S s1b = S(); s1b.foo();
1966 S[1] s2a; s2a[0].foo();
1967 S[1] s2b = S(); s2b[0].foo();
1968
1969 // U,V,W,X are nested struct, but should work same as non-nested.
1970 // 1: bare struct object. 2: static array of structs.
1971 // a: default construction.
1972 // b: construction by literal syntax which has no argument.
1973 // c: construction by literal syntax which has one or more arguments.
1974
1975 struct U
1976 {
1977 S s;
1978 void foo() { val = 2; }
1979 }
1980 U u1a; u1a.foo(); u1a.s.foo();
1981 U u1b = U(); u1b.foo(); u1b.s.foo();
1982 U u1c = U(s1a); u1c.foo(); u1c.s.foo();
1983 U[1] u2a; u2a[0].foo(); u2a[0].s.foo();
1984 U[1] u2b = U(); u2b[0].foo(); u2b[0].s.foo();
1985 U[1] u2c = U(s1a); u2c[0].foo(); u2c[0].s.foo();
1986
1987 struct V
1988 {
1989 S[1] s;
1990 void foo() { val = 2; }
1991 }
1992 V v1a; v1a.foo(); v1a.s[0].foo();
1993 V v1b = V(); v1b.foo(); v1b.s[0].foo();
1994 V v1c = V(s1a); v1c.foo(); v1c.s[0].foo();
1995 V[1] v2a; v2a[0].foo(); v2a[0].s[0].foo();
1996 V[1] v2b = V(); v2b[0].foo(); v2b[0].s[0].foo();
1997 V[1] v2c = V(s1a); v2c[0].foo(); v2c[0].s[0].foo();
1998
1999 struct W
2000 {
2001 S s;
2002 this(S s) { this.s = s; }
2003 void foo() { val = 2; }
2004 }
2005 W w1a; w1a.foo(); w1a.s.foo();
2006 W w1b = W(); w1b.foo(); w1b.s.foo();
2007 W w1c = W(s1a); w1c.foo(); w1c.s.foo();
2008 W[1] w2a; w2a[0].foo(); w2a[0].s.foo();
2009 W[1] w2b = W(); w2b[0].foo(); w2b[0].s.foo();
2010 W[1] w2c = W(s1a); w2c[0].foo(); w2c[0].s.foo();
2011
2012 struct X
2013 {
2014 S[1] s;
2015 this(S s) { this.s[] = s; }
2016 void foo() { val = 2; }
2017 }
2018 X x1a; x1a.foo(); x1a.s[0].foo();
2019 X x1b = X(); x1b.foo(); x1b.s[0].foo();
2020 X x1c = X(s1a); x1c.foo(); x1c.s[0].foo();
2021 X[1] x2a; x2a[0].foo(); x2a[0].s[0].foo();
2022 X[1] x2b = X(); x2b[0].foo(); x2b[0].s[0].foo();
2023 X[1] x2c = X(s1a); x2c[0].foo(); x2c[0].s[0].foo();
2024
2025 // Both declarations, Y and Z should raise errors,
2026 // because their ctors don't initialize their field 's'.
2027 static assert(!__traits(compiles, {
2028 struct Y1 { S s; this(S){} void foo() { val = 2; } }
2029 }));
2030 static assert(!__traits(compiles, {
2031 struct Y2 { S s; this(T)(S){} void foo() { val = 2; } }
2032 auto y2 = Y2!S(S()); // instantiate ctor
2033 }));
2034
2035 static assert(!__traits(compiles, {
2036 struct Z1 { S[1] s; this(S){} void foo() { val = 2; } }
2037 }));
2038 static assert(!__traits(compiles, {
2039 struct Z2 { S[1] s; this(T)(S){} void foo() { val = 2; } }
2040 auto z2 = Z2!S(S()); // instantiate ctor
2041 }));
2042 }
2043
2044 /*******************************************/
2045 // https://issues.dlang.org/show_bug.cgi?id=9003
2046
test9003()2047 void test9003()
2048 {
2049 int i;
2050 struct NS {
2051 int n1; // Comment to pass all asserts
2052 int n2; // Uncomment to fail assert on line 19
2053 int f() { return i; }
2054 }
2055
2056 static struct SS1 {
2057 NS ns;
2058 }
2059 SS1 ss1;
2060 assert(ss1.ns != NS.init);
2061
2062 static struct SS2 {
2063 NS ns1, ns2;
2064 }
2065 SS2 ss2;
2066 assert(ss2.ns1 != NS.init); // line 19
2067 assert(ss2.ns2 != NS.init);
2068
2069 static struct SS3 {
2070 int i;
2071 NS ns;
2072 }
2073
2074 SS3 ss3;
2075 assert(ss3.ns != NS.init);
2076
2077 static struct SS4 {
2078 int i;
2079 NS ns1, ns2;
2080 }
2081
2082 SS4 ss4;
2083 assert(ss4.ns1 != NS.init); // fails
2084 assert(ss4.ns2 != NS.init); // fails
2085 }
2086
2087 /*******************************************/
2088 // https://issues.dlang.org/show_bug.cgi?id=9006
2089
test9006()2090 void test9006()
2091 {
2092 int i;
2093 struct NS
2094 {
2095 int n;
2096 int[3] a; // Uncomment to fail assert on line 20 and pass on line 23
2097 int f() { return i; }
2098 }
2099 NS ns;
2100 assert(ns != NS.init);
2101 ns = NS.init;
2102 assert(ns == NS.init);
2103
2104 static struct SS { NS ns; }
2105 assert(SS.init.ns == NS.init); // fails
2106 assert(SS.init.ns != NS()); // fails
2107
2108 SS s;
2109 assert(s.ns != NS.init); // line 20
2110 assert(s != SS.init); // fails
2111 s = SS.init;
2112 assert(s.ns == NS.init); // line 23, fails
2113 assert(s == SS.init);
2114 }
2115
2116 /*******************************************/
2117 // https://issues.dlang.org/show_bug.cgi?id=9035
2118
test9035()2119 void test9035()
2120 {
2121 static struct S {}
2122
2123 void f(T)(auto ref T t)
2124 {
2125 static assert(!__traits(isRef, t));
2126 }
2127
2128 f(S.init); // ok, rvalue
2129 f(S()); // ok, rvalue
2130
2131 int i;
2132 struct Nested
2133 {
2134 int j = 0; void f() { ++i; }
2135 }
2136
2137 f(Nested()); // ok, rvalue
2138 f(Nested.init); // fails, lvalue
2139
2140 assert(Nested.init.j == 0);
2141 //(ref n) { n.j = 5; }(Nested.init);
2142 assert(Nested.init.j == 0); // fails, j is 5
2143 }
2144
test9035a()2145 void test9035a()
2146 {
2147 int x;
2148 struct S {
2149 // no field
2150 void foo() { x = 1; }
2151 }
2152 S s1;
2153 S s2 = S();
2154 assert(s1 != S.init); // OK
2155 assert(s2 != S.init); // OK
2156 assert(S() != S.init); // NG -> OK
2157 }
2158
2159 /*******************************************/
2160 // https://issues.dlang.org/show_bug.cgi?id=9036
2161
test9036()2162 void test9036()
2163 {
2164 static int i;
2165 static struct S
2166 {
2167 this(this) { ++i; }
2168 }
2169
2170 S s = S.init;
2171 assert(i == 0); // postblit not called
2172 s = S.init;
2173 assert(i == 0); // postblit not called
2174
2175 int k;
2176 static int j = 0;
2177 struct N
2178 {
2179 this(this)
2180 {
2181 ++j;
2182 assert(this.tupleof[$-1] != null); // fails
2183 }
2184 void f() { ++k; }
2185 }
2186
2187 N n = N.init;
2188 assert(j == 0); // fails, j = 1, postblit called
2189 n = N.init;
2190 assert(j == 0); // fails, j = 2, postblit called
2191 }
2192
2193 /*******************************************/
2194
2195 /+
2196 auto fun8863(T)(T* ret) { *ret = T(); }
2197
2198 void test8863()
2199 {
2200 int x = 1;
2201 struct A
2202 {
2203 auto f()
2204 {
2205 assert(x == 1);
2206 }
2207 }
2208
2209 A a;
2210 a.f();
2211 fun8863!A(&a);
2212 a.f();
2213 }
2214 +/
2215
2216 /*******************************************/
2217 // https://issues.dlang.org/show_bug.cgi?id=8774
2218
popFront8774()2219 void popFront8774()
2220 {
2221 int[20] abc; // smash stack
2222 }
2223
MapResult8774(alias fun)2224 struct MapResult8774(alias fun)
2225 {
2226 void delegate() front()
2227 {
2228 return fun(1);
2229 }
2230 }
2231
test8774()2232 void test8774() {
2233
2234 int sliceSize = 100;
2235
2236 void delegate() foo( int i )
2237 {
2238 void delegate() closedPartialSum()
2239 {
2240 int ii = i ;
2241 void bar()
2242 {
2243 printf("%d\n", sliceSize);
2244 assert(sliceSize == 100);
2245 }
2246 return &bar;
2247 }
2248 return closedPartialSum();
2249 }
2250
2251 auto threads = MapResult8774!foo();
2252
2253 auto dg = threads.front();
2254 popFront8774();
2255
2256 printf("calling dg()\n");
2257 dg();
2258 }
2259
2260 /*******************************************/
2261
Bug8832(alias X)2262 int Bug8832(alias X)()
2263 {
2264 return X();
2265 }
2266
delegate()2267 int delegate() foo8832()
2268 {
2269 int stack;
2270 int heap = 3;
2271
2272 int nested_func()
2273 {
2274 ++heap;
2275 return heap;
2276 }
2277 return delegate int() { return Bug8832!(nested_func); };
2278 }
2279
test8832()2280 void test8832()
2281 {
2282 auto z = foo8832();
2283 auto p = foo8832();
2284 assert(z() == 4);
2285 p();
2286 assert(z() == 5);
2287 }
2288
2289 /*******************************************/
2290 // https://issues.dlang.org/show_bug.cgi?id=9315
2291
test9315()2292 auto test9315()
2293 {
2294 struct S
2295 {
2296 int i;
2297 void bar() {}
2298 }
2299 pragma(msg, S.init.tupleof[$-1]);
2300 }
2301
2302 /*******************************************/
2303 // https://issues.dlang.org/show_bug.cgi?id=9244
2304
test9244()2305 void test9244()
2306 {
2307 union U {
2308 int i;
2309 @safe int x() { return i; }
2310 }
2311 }
2312
2313 /*******************************************/
2314 // https://issues.dlang.org/show_bug.cgi?id=10495
2315
2316 struct X10495
2317 {
2318 @disable this();
2319 }
2320
2321 struct Y10495(alias f)
2322 {
2323 void g() {}
2324 }
2325
2326 class C10495
2327 {
2328 X10495 s = X10495.init;
2329
2330 void h()
2331 {
2332 Y10495!(a => a) st;
2333 }
2334 }
2335
2336 /*******************************************/
2337 // https://issues.dlang.org/show_bug.cgi?id=11385
2338
2339 auto map11385(alias fun, R)(R range)
2340 {
2341 return MapResult11385!(fun, R)(range);
2342 }
2343 struct MapResult11385(alias fun, R)
2344 {
2345 R range;
2346 auto front() { return fun(range[0]); }
2347 }
2348 void test11385()
2349 {
2350 //import std.algorithm;
2351 static auto fun1(T)(T a) { return a * 2; }
2352 auto fun2(T)(T a) { return a * 2; }
2353 [1].map11385!(a=>fun1(a)); // OK
2354 [1].map11385!(a=>fun2(a)); // NG:XXX is a nested function and cannot be accessed from XXX
2355 }
2356
2357 /*******************************************/
2358
2359 void xmap(alias g)(int t)
2360 {
2361 g(t);
2362 }
2363
2364 enum foo11297 = function (int x)
2365 {
2366 //int bar(int y) { return x; } xmap!bar(7);
2367 xmap!(y => x)(7);
2368 };
2369
2370 enum goo11297 = delegate (int x)
2371 {
2372 //int bar(int y) { return x; } xmap!bar(7);
2373 xmap!(y => x)(7);
2374 };
2375
2376 void xreduce(alias f)()
2377 {
2378 f(4);
2379 }
2380
2381 void test11297()
2382 {
2383 xreduce!foo11297();
2384 xreduce!goo11297();
2385 }
2386
2387 /*******************************************/
2388 // https://issues.dlang.org/show_bug.cgi?id=11886
2389
2390 struct Lambda11886(alias fun)
2391 {
2392 auto opCall(A...)(A args) { return fun(args); }
2393 }
2394 void test11886()
2395 {
2396 int n = 10;
2397 Lambda11886!(x => x + n) f;
2398 assert(f(1) == 11); // Line 9
2399
2400 struct NS
2401 {
2402 auto foo(T)(T t) { return t * n; }
2403 }
2404 static assert(NS.tupleof.length == 1);
2405 static assert(NS.sizeof == (void*).sizeof);
2406 NS ns;
2407 assert(ns.foo(2) == 20);
2408 }
2409
2410 /*******************************************/
2411 // https://issues.dlang.org/show_bug.cgi?id=12234
2412
2413 void test12234()
2414 {
2415 class B
2416 {
2417 int a;
2418 this(int aa) { a = aa; }
2419 }
2420 auto foo = {
2421 return new B(1);
2422 };
2423 static assert(is(typeof(foo) == delegate));
2424
2425 auto b = foo();
2426 assert(b.a == 1);
2427 }
2428
2429 /*******************************************/
2430 // https://issues.dlang.org/show_bug.cgi?id=12981
2431
2432 template Mix12981(T)
2433 {
2434 class A
2435 {
2436 alias typeof(this.outer) x;
2437 }
2438 }
2439
2440 class B12981
2441 {
2442 mixin Mix12981!(int);
2443
2444 static assert(is(A.x == B12981));
2445 }
2446
2447 /*******************************************/
2448 // https://issues.dlang.org/show_bug.cgi?id=13861
2449
2450 struct Foo13861(alias f)
2451 {
2452 struct Bar
2453 {
2454 Bar func()
2455 {
2456 return Bar(); // OK <- Segfault
2457 }
2458 }
2459 }
2460
2461 void test13861()
2462 {
2463 Foo13861!(n => n) a;
2464 }
2465
2466 /*******************************************/
2467 // https://issues.dlang.org/show_bug.cgi?id=14398
2468
2469 void test14398()
2470 {
2471 int outer;
2472
2473 struct Inner
2474 {
2475 this(this)
2476 {
2477 outer += 42;
2478 }
2479 }
2480
2481 struct Outer
2482 {
2483 Inner inner;
2484
2485 this(int dummy)
2486 {
2487 inner = Inner();
2488
2489 // hidden fields correctly set
2490 assert(this.tupleof[$-1] !is null);
2491 assert(inner.tupleof[$-1] !is null);
2492 }
2493 }
2494
2495 Outer[1] arr1 = [Outer(0)];
2496 assert(outer == 0); // no postblit called on arr1 construction
2497 auto arr2 = arr1;
2498 assert(outer == 42); // inner is copied successfully
2499 }
2500
2501 /*******************************************/
2502 // https://issues.dlang.org/show_bug.cgi?id=14846
2503
2504 void foo14846(Dg)(scope Dg code)
2505 {
2506 static assert(is(Dg == delegate));
2507 code();
2508 }
2509
2510 void test14846()
2511 {
2512 int x;
2513
2514 struct S
2515 {
2516 this(int n) { x = n; }
2517 ~this() { x = 99; }
2518 }
2519
2520 foo14846({ S s; });
2521 foo14846({ S s = S(); });
2522 foo14846({ S s = S(1); });
2523 foo14846({ S[3] s; });
2524
2525 foo14846({ S* p = new S(); });
2526 foo14846({ S* p = new S(1); });
2527 foo14846({ S[] a = [S()]; });
2528 foo14846({ S[] a = [S(1)]; });
2529 }
2530
2531 /*******************************************/
2532 // https://issues.dlang.org/show_bug.cgi?id=15422
2533
2534 class App15422(T)
2535 {
2536 this() {}
2537
2538 auto test1(T val)
2539 in {} do // necessary to reproduce the crash
2540 {
2541 struct Foo
2542 {
2543 this(int k) {}
2544 T a;
2545 }
2546
2547 Foo foo;
2548 foo.a = val;
2549
2550 // Frame of test2 function, allocated on heap.
2551 assert(foo.tupleof[$-1] !is null);
2552
2553 //printf("&foo = %p\n", &foo); // stack
2554 //printf("&this = %p\n", &this); // stack?
2555 //printf("foo.vthis = %p\n", foo.tupleof[$-1]); // stack...!?
2556 //assert(cast(void*)&this !is *cast(void**)&foo.tupleof[$-1], "bad");
2557 // BUG: currently foo.vthis set to the address of 'this' variable on the stack.
2558 // It's should be stomped to null, because Foo.vthis is never be used.
2559
2560 int[Foo] map;
2561 map[foo] = 1; // OK <- crash
2562
2563 return foo;
2564 }
2565
2566 auto test2(T val)
2567 //in {} do
2568 {
2569 int closVar;
2570 struct Foo
2571 {
2572 this(int k) { closVar = k; }
2573 // Make val a closure variable.
2574
2575 T a;
2576 }
2577
2578 Foo foo;
2579 foo.a = val;
2580
2581 // Frame of test2 function, allocated on heap.
2582 assert(foo.tupleof[$-1] !is null);
2583
2584 return foo;
2585 }
2586 }
2587
2588 void test15422a()
2589 {
2590 alias App = App15422!int;
2591 App app1 = new App;
2592 {
2593 auto x = app1.test1(1);
2594 auto y = app1.test1(1);
2595 static assert(is(typeof(x) == typeof(y)));
2596
2597 // int (bitwise comparison)
2598 assert(x.a == y.a);
2599
2600 assert(*cast(void**)&x.tupleof[$-1] is *cast(void**)&y.tupleof[$-1]);
2601
2602 // bitwise equality (needOpEquals() and needToHash() returns false)
2603 assert(x == y);
2604
2605 // BUG
2606 //assert(*cast(void**)&x.tupleof[$-1] is null);
2607 //assert(*cast(void**)&y.tupleof[$-1] is null);
2608 auto getZ() { auto z = app1.test1(1); return z; }
2609 auto z = getZ();
2610 assert(x.a == z.a);
2611 //assert(x.tupleof[$-1] is z.tupleof[$-1]); // should pass
2612 //assert(x == z); // should pass
2613
2614 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2615 }
2616 App app2 = new App;
2617 {
2618 auto x = app1.test2(1);
2619 auto y = app2.test2(1);
2620 static assert(is(typeof(x) == typeof(y)));
2621
2622 // int (bitwise comparison)
2623 assert(x.a == y.a);
2624
2625 // closure envirionments
2626 assert(*cast(void**)&x.tupleof[$-1] !is *cast(void**)&y.tupleof[$-1]);
2627
2628 // Changed to bitwise equality (needOpEquals() and needToHash() returns false)
2629 assert(x != y); // OK <- crash
2630
2631 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2632 }
2633 }
2634
2635 void test15422b()
2636 {
2637 alias App = App15422!string;
2638 App app1 = new App;
2639 {
2640 auto x = app1.test1("a".idup);
2641 auto y = app1.test1("a".idup);
2642 static assert(is(typeof(x) == typeof(y)));
2643
2644 // string (element-wise comparison)
2645 assert(x.a == y.a);
2646
2647 assert(*cast(void**)&x.tupleof[$-1] is *cast(void**)&y.tupleof[$-1]);
2648
2649 // memberwise equality (needToHash() returns true)
2650 assert(x == y);
2651 // Lowered to: x.a == y.a && x.tupleof[$-1] is y.tupleof[$-1]
2652
2653 // BUG
2654 //assert(*cast(void**)&x.tupleof[$-1] is null);
2655 //assert(*cast(void**)&y.tupleof[$-1] is null);
2656 auto getZ() { auto z = app1.test1("a".idup); return z; }
2657 auto z = getZ();
2658 assert(x.a == z.a);
2659 //assert(x.tupleof[$-1] is z.tupleof[$-1]); // should pass
2660 //assert(x == z); // should pass
2661
2662 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2663 }
2664 App app2 = new App;
2665 {
2666 auto x = app1.test2("a".idup);
2667 auto y = app2.test2("a".idup);
2668 static assert(is(typeof(x) == typeof(y)));
2669
2670 // string (element-wise comparison)
2671 assert(x.a == y.a);
2672
2673 // closure envirionments
2674 assert(*cast(void**)&x.tupleof[$-1] !is *cast(void**)&y.tupleof[$-1]);
2675
2676 // Changed to memberwise equality (needToHash() returns true)
2677 // Lowered to: x.a == y.a && x.tupleof[$-1] is y.tupleof[$-1]
2678 assert(x != y); // OK <- crash
2679
2680 x = y; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2681 }
2682 }
2683
2684 /***************************************************/
2685 // https://issues.dlang.org/show_bug.cgi?id=15757
2686
2687 template map15757(fun...)
2688 {
2689 auto map15757(R)(R r)
2690 {
2691 return MapResult15757!(fun, R)(r);
2692 }
2693 }
2694
2695 struct MapResult15757(alias fun, R)
2696 {
2697 R _input;
2698
2699 this(R input)
2700 {
2701 _input = input;
2702 }
2703 }
2704
2705 void wrap15757(R)(R r)
2706 {
2707 struct M(R)
2708 {
2709 this(R r)
2710 {
2711 payload = r;
2712 }
2713 R payload;
2714 }
2715
2716 M!R m = M!R(r);
2717 }
2718
2719 void test15757() @safe
2720 {
2721 [1,2,3].map15757!(x => x*x).wrap15757;
2722 }
2723
2724 /***************************************************/
2725
2726 // https://issues.dlang.org/show_bug.cgi?id=19384
2727
2728 struct Vec
2729 {
2730 uint item;
2731
2732 ref uint august() return
2733 {
2734 return item;
2735 // commenting next line removes bug
2736 foreach(ref val; range()) return val;
2737 assert(false);
2738 }
2739
2740 uint* august2() return
2741 {
2742 return &item;
2743 foreach(ref val; range()) return &val;
2744 assert(false);
2745 }
2746 }
2747
2748 struct range
2749 {
2750 int opApply(scope int delegate(ref uint) dg) { return 0; }
2751 }
2752
2753 void test19384()
2754 {
2755 Vec preds = Vec(0xDEAD);
2756 void* ptr2 = &preds.august();
2757 void* ptr3 = preds.august2();
2758 assert(&preds == ptr2);
2759 assert(&preds == ptr3);
2760 }
2761
2762
2763 /***************************************************/
2764
2765 int main()
2766 {
2767 test1();
2768 test2();
2769 test3();
2770 test4();
2771 test5();
2772 test6();
2773 test7();
2774 test8();
2775 test9();
2776 test10();
2777 test11();
2778 test12();
2779 test13();
2780 test14();
2781 test15();
2782 test16();
2783 test17();
2784 test18();
2785 test19();
2786 test20();
2787 test21();
2788 test22();
2789 test23();
2790 test24();
2791 test25();
2792 test26();
2793 test27();
2794 test28();
2795 test29();
2796 test30();
2797 test31();
2798 test32();
2799 test33();
2800 test34();
2801 test35();
2802 test36();
2803 test37();
2804 test38();
2805 test39();
2806 test40();
2807 test41();
2808 test42();
2809 test43();
2810 test44();
2811 test45();
2812 test46();
2813 test47();
2814 test48();
2815 test49();
2816 test50();
2817 test51();
2818 test52();
2819 test53();
2820 test54();
2821 test55();
2822 test56();
2823 test4401();
2824 test7428();
2825 test4612();
2826 test4841();
2827 test7199();
2828 test7965();
2829 test7965a();
2830 test8188();
2831
2832 test5082();
2833 test8194();
2834 test8339a();
2835 test8339b();
2836 test8339c();
2837 test8923a();
2838 test8923b();
2839 test8923c();
2840 test9003();
2841 test9006();
2842 test9035();
2843 test9035a();
2844 test9036();
2845 // test8863();
2846 test8774();
2847 test8832();
2848 test9315();
2849 test9244();
2850 test11385();
2851 test11297();
2852 test11886();
2853 test12234();
2854 test13861();
2855 test14398();
2856 test14846();
2857 test15422a();
2858 test15422b();
2859 test15757();
2860 test19384();
2861
2862 printf("Success\n");
2863 return 0;
2864 }
2865