1 /*
2 TEST_OUTPUT:
3 ---
4 true
5 g
6 &Test109S(&Test109S(<recursion>))
7 runnable/interpret.d(3197): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
8 runnable/interpret.d(3199): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
9 runnable/interpret.d(3202): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
10 runnable/interpret.d(3205): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
11 runnable/interpret.d(3206): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
12 runnable/interpret.d(3212): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
13 runnable/interpret.d(3213): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
14 runnable/interpret.d(3216): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead.
15 tfoo
16 tfoo
17 Crash!
18 ---
19 */
20
21 import core.stdc.stdio;
22
Tuple(A...)23 template Tuple(A...)
24 {
25 alias A Tuple;
26 }
27
eval(A...)28 template eval(A...)
29 {
30 const typeof(A[0]) eval = A[0];
31 }
32
33 /************************************************/
34
Foo1(int i)35 int Foo1(int i)
36 {
37 if (i == 0)
38 return 1;
39 else
40 return i * Foo1(i - 1);
41 }
42
test1()43 void test1()
44 {
45 static int f = Foo1(5);
46 printf("%d %d\n", f, 5*4*3*2);
47 assert(f == 120);
48 }
49
50 /************************************************/
51
find2(string s,char c)52 int find2(string s, char c)
53 {
54 if (s.length == 0)
55 return -1;
56 else if (c == s[0])
57 return 0;
58 else
59 return 1 + find2(s[1..$], c);
60 }
61
test2()62 void test2()
63 {
64 static int f = find2("hello", 'l');
65 printf("%d\n", f);
66 assert(f == 2);
67 }
68
69 /************************************************/
70
bar3(int i)71 int bar3(int i)
72 {
73 int j;
74 while (i)
75 {
76 j += i;
77 i--;
78 }
79 return j;
80 }
81
test3()82 void test3()
83 {
84 static b = bar3(7);
85 printf("b = %d, %d\n", b, bar3(7));
86 assert(b == 28);
87 }
88
89 /************************************************/
90
bar4(int i)91 int bar4(int i)
92 {
93 for (int j = 0; j < 10; j++)
94 i += j;
95 return i;
96 }
97
test4()98 void test4()
99 {
100 static b = bar4(7);
101 printf("b = %d, %d\n", b, bar4(7));
102 assert(b == 52);
103 }
104
105 /************************************************/
106
bar5(int i)107 int bar5(int i)
108 {
109 int j;
110 do
111 {
112 i += j;
113 j++;
114 } while (j < 10);
115 return i;
116 }
117
test5()118 void test5()
119 {
120 static b = bar5(7);
121 printf("b = %d, %d\n", b, bar5(7));
122 assert(b == 52);
123 }
124
125 /************************************************/
126
bar6(int i)127 int bar6(int i)
128 {
129 int j;
130 do
131 {
132 i += j;
133 j++;
134 if (j == 4)
135 break;
136 } while (j < 10);
137 return i;
138 }
139
test6()140 void test6()
141 {
142 static b = bar6(7);
143 printf("b = %d, %d\n", b, bar6(7));
144 assert(b == 13);
145 }
146
147 /************************************************/
148
bar7(int i)149 int bar7(int i)
150 {
151 int j;
152 do
153 {
154 i += j;
155 j++;
156 if (j == 4)
157 return 80;
158 } while (j < 10);
159 return i;
160 }
161
test7()162 void test7()
163 {
164 static b = bar7(7);
165 printf("b = %d, %d\n", b, bar7(7));
166 assert(b == 80);
167 }
168
169 /************************************************/
170
bar8(int i)171 int bar8(int i)
172 {
173 int j;
174 do
175 {
176 j++;
177 if (j == 4)
178 continue;
179 i += j;
180 } while (j < 10);
181 return i;
182 }
183
test8()184 void test8()
185 {
186 static b = bar8(7);
187 printf("b = %d, %d\n", b, bar8(7));
188 assert(b == 58);
189 }
190
191 /************************************************/
192
bar9(int i)193 int bar9(int i)
194 {
195 int j;
196 while (j < 10)
197 {
198 j++;
199 if (j == 4)
200 continue;
201 i += j;
202 }
203 return i;
204 }
205
test9()206 void test9()
207 {
208 static b = bar9(7);
209 printf("b = %d, %d\n", b, bar9(7));
210 assert(b == 58);
211 }
212
213 /************************************************/
214
bar10(int i)215 int bar10(int i)
216 {
217 int j;
218 while (j < 10)
219 {
220 j++;
221 if (j == 4)
222 break;
223 i += j;
224 }
225 return i;
226 }
227
test10()228 void test10()
229 {
230 static b = bar10(7);
231 printf("b = %d, %d\n", b, bar10(7));
232 assert(b == 13);
233 }
234
235 /************************************************/
236
bar11(int i)237 int bar11(int i)
238 {
239 int j;
240 while (j < 10)
241 {
242 j++;
243 if (j == 4)
244 return i << 3;
245 i += j;
246 }
247 return i;
248 }
249
test11()250 void test11()
251 {
252 static b = bar11(7);
253 printf("b = %d, %d\n", b, bar11(7));
254 assert(b == 104);
255 }
256
257 /************************************************/
258
bar12(int i)259 int bar12(int i)
260 {
261 for (int j; j < 10; j++)
262 {
263 if (j == 4)
264 return i << 3;
265 i += j;
266 }
267 return i;
268 }
269
test12()270 void test12()
271 {
272 static b = bar12(7);
273 printf("b = %d, %d\n", b, bar12(7));
274 assert(b == 104);
275 }
276
277 /************************************************/
278
bar13(int i)279 int bar13(int i)
280 {
281 for (int j; j < 10; j++)
282 {
283 if (j == 4)
284 break;
285 i += j;
286 }
287 return i;
288 }
289
test13()290 void test13()
291 {
292 static b = bar13(7);
293 printf("b = %d, %d\n", b, bar13(7));
294 assert(b == 13);
295 }
296
297 /************************************************/
298
bar14(int i)299 int bar14(int i)
300 {
301 for (int j; j < 10; j++)
302 {
303 if (j == 4)
304 continue;
305 i += j;
306 }
307 return i;
308 }
309
test14()310 void test14()
311 {
312 static b = bar14(7);
313 printf("b = %d, %d\n", b, bar14(7));
314 assert(b == 48);
315 }
316
317 /************************************************/
318
bar15(int i)319 int bar15(int i)
320 {
321 foreach (k, v; "hello")
322 {
323 i <<= 1;
324 if (k == 4)
325 continue;
326 i += v;
327 }
328 return i;
329 }
330
test15()331 void test15()
332 {
333 static b = bar15(7);
334 printf("b = %d, %d\n", b, bar15(7));
335 assert(b == 3344);
336 }
337
338 /************************************************/
339
bar16(int i)340 int bar16(int i)
341 {
342 foreach_reverse (k, v; "hello")
343 {
344 i <<= 1;
345 if (k == 4)
346 continue;
347 i += v;
348 }
349 return i;
350 }
351
test16()352 void test16()
353 {
354 static b = bar16(7);
355 printf("b = %d, %d\n", b, bar16(7));
356 assert(b == 1826);
357 }
358
359 /************************************************/
360
bar17(int i)361 int bar17(int i)
362 {
363 foreach (k, v; "hello")
364 {
365 i <<= 1;
366 if (k == 2)
367 break;
368 i += v;
369 }
370 return i;
371 }
372
test17()373 void test17()
374 {
375 static b = bar17(7);
376 printf("b = %d, %d\n", b, bar17(7));
377 assert(b == 674);
378 }
379
380 /************************************************/
381
bar18(int i)382 int bar18(int i)
383 {
384 foreach_reverse (k, v; "hello")
385 {
386 i <<= 1;
387 if (k == 2)
388 break;
389 i += v;
390 }
391 return i;
392 }
393
test18()394 void test18()
395 {
396 static b = bar18(7);
397 printf("b = %d, %d\n", b, bar18(7));
398 assert(b == 716);
399 }
400
401 /************************************************/
402
bar19(int i)403 int bar19(int i)
404 {
405 assert(i > 0);
406 foreach_reverse (k, v; "hello")
407 {
408 i <<= 1;
409 if (k == 2)
410 return 8;
411 i += v;
412 }
413 return i;
414 }
415
test19()416 void test19()
417 {
418 static b = bar19(7);
419 printf("b = %d, %d\n", b, bar19(7));
420 assert(b == 8);
421 }
422
423 /************************************************/
424
bar20(int i)425 int bar20(int i)
426 {
427 assert(i > 0);
428 foreach (k, v; "hello")
429 {
430 i <<= 1;
431 if (k == 2)
432 return 8;
433 i += v;
434 }
435 return i;
436 }
437
test20()438 void test20()
439 {
440 static b = bar20(7);
441 printf("b = %d, %d\n", b, bar20(7));
442 assert(b == 8);
443 }
444
445 /************************************************/
446
bar21(int i)447 int bar21(int i)
448 {
449 assert(i > 0);
450 foreach (v; Tuple!(57, 23, 8))
451 {
452 i <<= 1;
453 i += v;
454 }
455 return i;
456 }
457
test21()458 void test21()
459 {
460 static b = bar21(7);
461 printf("b = %d, %d\n", b, bar21(7));
462 assert(b == 338);
463 }
464
465 /************************************************/
466
bar22(int i)467 int bar22(int i)
468 {
469 assert(i > 0);
470 foreach_reverse (v; Tuple!(57, 23, 8))
471 {
472 i <<= 1;
473 i += v;
474 }
475 return i;
476 }
477
test22()478 void test22()
479 {
480 static b = bar22(7);
481 printf("b = %d, %d\n", b, bar22(7));
482 assert(b == 191);
483 }
484
485 /************************************************/
486
bar23(int i)487 int bar23(int i)
488 {
489 assert(i > 0);
490 foreach_reverse (v; Tuple!(57, 23, 8))
491 {
492 i <<= 1;
493 if (v == 23)
494 return i + 1;
495 i += v;
496 }
497 return i;
498 }
499
test23()500 void test23()
501 {
502 static b = bar23(7);
503 printf("b = %d, %d\n", b, bar23(7));
504 assert(b == 45);
505 }
506
507 /************************************************/
508
bar24(int i)509 int bar24(int i)
510 {
511 assert(i > 0);
512 foreach (v; Tuple!(57, 23, 8))
513 {
514 i <<= 1;
515 if (v == 23)
516 return i + 1;
517 i += v;
518 }
519 return i;
520 }
521
test24()522 void test24()
523 {
524 static b = bar24(7);
525 printf("b = %d, %d\n", b, bar24(7));
526 assert(b == 143);
527 }
528
529 /************************************************/
530
bar25(int i)531 int bar25(int i)
532 {
533 assert(i > 0);
534 foreach_reverse (v; Tuple!(57, 23, 8))
535 {
536 i <<= 1;
537 if (v == 23)
538 break;
539 i += v;
540 }
541 return i;
542 }
543
test25()544 void test25()
545 {
546 static b = bar25(7);
547 printf("b = %d, %d\n", b, bar25(7));
548 assert(b == 44);
549 }
550
551 /************************************************/
552
bar26(int i)553 int bar26(int i)
554 {
555 assert(i > 0);
556 foreach (v; Tuple!(57, 23, 8))
557 {
558 i <<= 1;
559 if (v == 23)
560 break;
561 i += v;
562 }
563 return i;
564 }
565
test26()566 void test26()
567 {
568 static b = bar26(7);
569 printf("b = %d, %d\n", b, bar26(7));
570 assert(b == 142);
571 }
572
573 /************************************************/
574
bar27(int i)575 int bar27(int i)
576 {
577 foreach_reverse (v; Tuple!(57, 23, 8))
578 {
579 i <<= 1;
580 if (v == 23)
581 continue;
582 i += v;
583 }
584 return i;
585 }
586
test27()587 void test27()
588 {
589 static b = bar27(7);
590 printf("b = %d, %d\n", b, bar27(7));
591 assert(b == 145);
592 }
593
594 /************************************************/
595
bar28(int i)596 int bar28(int i)
597 {
598 foreach (v; Tuple!(57, 23, 8))
599 {
600 i <<= 1;
601 if (v == 23)
602 continue;
603 i += v;
604 }
605 return i;
606 }
607
test28()608 void test28()
609 {
610 static b = bar28(7);
611 printf("b = %d, %d\n", b, bar28(7));
612 assert(b == 292);
613 }
614
615 /************************************************/
616
bar29(int i)617 int bar29(int i)
618 {
619 switch (i)
620 {
621 case 1:
622 i = 4;
623 break;
624 case 7:
625 i = 3;
626 break;
627 default: assert(0);
628 }
629 return i;
630 }
631
test29()632 void test29()
633 {
634 static b = bar29(7);
635 printf("b = %d, %d\n", b, bar29(7));
636 assert(b == 3);
637 }
638
639 /************************************************/
640
bar30(int i)641 int bar30(int i)
642 {
643 switch (i)
644 {
645 case 1:
646 i = 4;
647 break;
648 case 8:
649 i = 2;
650 break;
651 default:
652 i = 3;
653 break;
654 }
655 return i;
656 }
657
test30()658 void test30()
659 {
660 static b = bar30(7);
661 printf("b = %d, %d\n", b, bar30(7));
662 assert(b == 3);
663 }
664
665 /************************************************/
666
bar31(string s)667 int bar31(string s)
668 { int i;
669
670 switch (s)
671 {
672 case "hello":
673 i = 4;
674 break;
675 case "betty":
676 i = 2;
677 break;
678 default:
679 i = 3;
680 break;
681 }
682 return i;
683 }
684
test31()685 void test31()
686 {
687 static b = bar31("betty");
688 printf("b = %d, %d\n", b, bar31("betty"));
689 assert(b == 2);
690 }
691
692 /************************************************/
693
bar32(int i)694 int bar32(int i)
695 {
696 switch (i)
697 {
698 case 7:
699 i = 4;
700 goto case;
701 case 5:
702 i = 2;
703 break;
704 default:
705 i = 3;
706 break;
707 }
708 return i;
709 }
710
test32()711 void test32()
712 {
713 static b = bar32(7);
714 printf("b = %d, %d\n", b, bar32(7));
715 assert(b == 2);
716 }
717
718 /************************************************/
719
bar33(int i)720 int bar33(int i)
721 {
722 switch (i)
723 {
724 case 5:
725 i = 2;
726 break;
727 case 7:
728 i = 4;
729 goto case 5;
730 default:
731 i = 3;
732 break;
733 }
734 return i;
735 }
736
test33()737 void test33()
738 {
739 static b = bar33(7);
740 printf("b = %d, %d\n", b, bar33(7));
741 assert(b == 2);
742 }
743
744 /************************************************/
745
bar34(int i)746 int bar34(int i)
747 {
748 switch (i)
749 {
750 default:
751 i = 3;
752 break;
753 case 5:
754 i = 2;
755 break;
756 case 7:
757 i = 4;
758 goto default;
759 }
760 return i;
761 }
762
test34()763 void test34()
764 {
765 static b = bar34(7);
766 printf("b = %d, %d\n", b, bar34(7));
767 assert(b == 3);
768 }
769
770 /************************************************/
771
bar35(int i)772 int bar35(int i)
773 {
774 L1:
775 switch (i)
776 {
777 default:
778 i = 3;
779 break;
780 case 5:
781 i = 2;
782 break;
783 case 3:
784 return 8;
785 case 7:
786 i = 4;
787 goto default;
788 }
789 goto L1;
790 }
791
test35()792 void test35()
793 {
794 static b = bar35(7);
795 printf("b = %d, %d\n", b, bar35(7));
796 assert(b == 8);
797 }
798
799 /************************************************/
800
square36(int x)801 int square36(int x)
802 {
803 return x * x;
804 }
805
806 const int foo36 = square36(5);
807
test36()808 void test36()
809 {
810 assert(foo36 == 25);
811 }
812
813 /************************************************/
814
someCompileTimeFunction()815 string someCompileTimeFunction()
816 {
817 return "printf(\"Wowza!\n\");";
818 }
819
test37()820 void test37()
821 {
822 mixin(someCompileTimeFunction());
823 }
824
825 /************************************************/
826
NReps(string x,int n)827 string NReps(string x, int n)
828 {
829 string ret = "";
830 for (int i = 0; i < n; i++)
831 {
832 ret ~= x;
833 }
834 return ret;
835 }
836
test38()837 void test38()
838 {
839 static x = NReps("3", 6);
840 assert(x == "333333");
841 }
842
843 /************************************************/
844
func39()845 bool func39() { return true; }
846
847 static if (func39())
848 {
849 pragma(msg, "true");
850 }
851 else
852 {
853 pragma(msg, "false");
854 }
855
test39()856 void test39()
857 {
858 }
859
860 /************************************************/
861
UpToSpace(string x)862 string UpToSpace(string x)
863 {
864 int i = 0;
865 while (i < x.length && x[i] != ' ')
866 {
867 i++;
868 }
869 return x[0..i];
870 }
871
test40()872 void test40()
873 {
874 const y = UpToSpace("first space was after first");
875 assert(y == "first");
876 }
877
878 /************************************************/
879
bar41(ref int j)880 int bar41(ref int j)
881 {
882 return 5;
883 }
884
foo41(int i)885 int foo41(int i)
886 {
887 int x;
888 x = 3;
889 bar41(x);
890 return i + x;
891 }
892
test41()893 void test41()
894 {
895 const y = foo41(3);
896 assert(y == 6);
897 }
898
899 /************************************************/
900
bar42(ref int j)901 int bar42(ref int j)
902 {
903 return 5;
904 }
905
foo42(int i)906 int foo42(int i)
907 {
908 int x;
909 x = 3;
910 bar42(x);
911 return i + x;
912 }
913
test42()914 void test42()
915 {
916 const y = foo42(3);
917 assert(y == 6);
918 }
919
920 /************************************************/
921
bar(string a)922 int bar(string a)
923 {
924 int v;
925 for (int i = 0; i < a.length; i++)
926 {
927 if (a[i] != ' ')
928 {
929 v += a.length;
930 }
931 }
932 return v;
933 }
934
test43()935 void test43()
936 {
937 const int foo = bar("a b c d");
938 assert(foo == 28);
939 }
940
941 /************************************************/
942
foo44()943 string foo44() { return ("bar"); }
944
test44()945 void test44()
946 {
947 const string bar = foo44();
948 assert(bar == "bar");
949 }
950
951 /************************************************/
952
square45(int n)953 int square45(int n) { return (n * n); }
954
test45()955 void test45()
956 {
957 int bar = eval!(square45(5));
958 assert(bar == 25);
959 }
960
961 /************************************************/
962
963 const int[5] foo46 = [0,1,2,3,4];
964
test46()965 void test46()
966 {
967 printf("%d\n", eval!(foo46[3]));
968 }
969
970 /************************************************/
971
foo47()972 string foo47()
973 {
974 string s;
975 s = s ~ 't';
976 return s ~ "foo";
977 }
978
test47()979 void test47()
980 {
981 static const x = foo47();
982 pragma(msg, x);
983 assert(x == "tfoo");
984 }
985
986 /************************************************/
987
foo48()988 string foo48()
989 {
990 string s;
991 s = s ~ 't';
992 s = s.idup;
993 return s ~ "foo";
994 }
995
test48()996 void test48()
997 {
998 static const x = foo48();
999 pragma(msg, x);
1000 assert(x == "tfoo");
1001 }
1002
1003 /************************************************/
1004
testd49(dstring input)1005 dstring testd49(dstring input)
1006 {
1007 if (input[3..5] != "rt")
1008 {
1009 return input[1..3];
1010 }
1011 return "my";
1012 }
1013
test49()1014 void test49()
1015 {
1016 static x = testd49("hello");
1017 assert(x == "el");
1018 }
1019
1020 /************************************************/
1021
makePostfix50(int x)1022 string makePostfix50(int x)
1023 {
1024 string first;
1025 first = "bad";
1026 if (x)
1027 {
1028 first = "ok";
1029 makePostfix50(0);
1030 }
1031 return first;
1032 }
1033
test50()1034 void test50()
1035 {
1036 static const char [] q2 = makePostfix50(1);
1037 static assert(q2 == "ok", q2);
1038 }
1039
1040 /************************************************/
1041
exprLength(string s)1042 int exprLength(string s)
1043 {
1044 int numParens=0;
1045 for (int i = 0; i < s.length; ++i)
1046 {
1047 if (s[i] == '(') { numParens++; }
1048 if (s[i] == ')') { numParens--; }
1049 if (numParens == 0) { return i; }
1050 }
1051 assert(0);
1052 }
1053
makePostfix51(string operations)1054 string makePostfix51(string operations)
1055 {
1056 if (operations.length < 2)
1057 return "x";
1058 int x = exprLength(operations);
1059 string first="bad";
1060 if (x > 0)
1061 {
1062 first = "ok";
1063 string ignore = makePostfix51(operations[1..x]);
1064 }
1065 return first;
1066 }
1067
1068
test51()1069 void test51()
1070 {
1071 string q = makePostfix51("(a+b)*c");
1072 assert(q == "ok");
1073 static const string q2 = makePostfix51("(a+b)*c");
1074 static assert(q2 == "ok");
1075 static assert(makePostfix51("(a+b)*c") == "ok");
1076 }
1077
1078 /************************************************/
1079
foo52(ref int x)1080 int foo52(ref int x)
1081 {
1082 x = 7;
1083 return 3;
1084 }
1085
bar52(int y)1086 int bar52(int y)
1087 {
1088 y = 4;
1089 foo52(y);
1090 return y;
1091 }
1092
test52()1093 void test52()
1094 {
1095 printf("%d\n", bar52(2));
1096 static assert(bar52(2) == 7);
1097 }
1098
1099 /************************************************/
1100
bar53(out int x)1101 void bar53(out int x) { x = 2; }
1102
foo53()1103 int foo53() { int y; bar53(y); return y; }
1104
test53()1105 void test53()
1106 {
1107 const int z = foo53();
1108 assert(z == 2);
1109 }
1110
1111 /************************************************/
1112
test54()1113 void test54()
1114 {
1115 static assert(equals54("alphabet", "alphabet"));
1116 }
1117
equals54(string a,string b)1118 bool equals54(string a, string b)
1119 {
1120 return (a == b);
1121 }
1122
1123 /************************************************/
1124
1125 const string[2] foo55 = ["a", "b"];
retsth55(int i)1126 string retsth55(int i) { return foo55[i]; }
1127
test55()1128 void test55()
1129 {
1130 enum res1 = eval!(foo55[0]);
1131 printf("%.*s\n", cast(int)res1.length, res1.ptr);
1132 enum res2 = eval!(retsth55(0));
1133 printf("%.*s\n", cast(int)res2.length, res2.ptr);
1134 }
1135
1136 /************************************************/
1137
retsth56(int i)1138 string retsth56(int i)
1139 {
1140 static const string[2] foo = ["a", "b"];
1141 return foo[i];
1142 }
1143
test56()1144 void test56()
1145 {
1146 enum result = eval!(retsth56(0));
1147 printf("%.*s\n", cast(int)result.length, result.ptr);
1148 }
1149
1150 /************************************************/
1151
g57()1152 int g57()
1153 {
1154 pragma(msg, "g");
1155 return 2;
1156 }
1157
1158 const int a57 = g57();
1159
test57()1160 void test57()
1161 {
1162 assert(a57 == 2);
1163 }
1164
1165 /************************************************/
1166
Fun58(int x)1167 int[] Fun58(int x)
1168 {
1169 int[] result;
1170 result ~= x + 1;
1171 return result;
1172 }
1173
test58()1174 void test58()
1175 {
1176 static b = Fun58(1) ~ Fun58(2);
1177 assert(b.length == 2);
1178 assert(b[0] == 2);
1179 assert(b[1] == 3);
1180 }
1181
1182 /************************************************/
1183
Index59()1184 int Index59()
1185 {
1186 int[] data = [1];
1187 return data[0];
1188 }
1189
test59()1190 void test59()
1191 {
1192 static assert(Index59() == 1);
1193 }
1194
1195 /************************************************/
1196
foo60()1197 string[int] foo60()
1198 {
1199 return [3:"hello", 4:"betty"];
1200 }
1201
test60()1202 void test60()
1203 {
1204 static assert(foo60()[3] == "hello");
1205 static assert(foo60()[4] == "betty");
1206 }
1207
1208 /************************************************/
1209
foo61()1210 string[int] foo61()
1211 {
1212 return [3:"hello", 4:"betty", 3:"world"];
1213 }
1214
test61()1215 void test61()
1216 {
1217 static assert(foo61()[3] == "world");
1218 static assert(foo61()[4] == "betty");
1219 }
1220
1221 /************************************************/
1222
foo62(int k)1223 string foo62(int k)
1224 {
1225 string[int] aa;
1226 aa = [3:"hello", 4:"betty"];
1227 return aa[k];
1228 }
1229
test62()1230 void test62()
1231 {
1232 static assert(foo62(3) == "hello");
1233 static assert(foo62(4) == "betty");
1234 }
1235
1236 /************************************************/
1237
test63()1238 void test63()
1239 {
1240 static auto x = foo63();
1241 }
1242
foo63()1243 int foo63()
1244 {
1245 pragma(msg, "Crash!");
1246 return 2;
1247 }
1248
1249 /************************************************/
1250
testd64(dstring input)1251 dstring testd64(dstring input)
1252 {
1253 debug int x = 10;
1254 return "my";
1255 }
1256
test64()1257 void test64()
1258 {
1259 static x = testd64("hello");
1260 }
1261
1262 /************************************************/
1263
1264 struct S65
1265 {
1266 int i;
1267 int j = 3;
1268 }
1269
foo(S65 s1,S65 s2)1270 int foo(S65 s1, S65 s2)
1271 {
1272 return s1 == s2;
1273 }
1274
test65()1275 void test65()
1276 {
1277 static assert(foo(S65(1, 5), S65(1, 5)) == 1);
1278 static assert(foo(S65(1, 5), S65(1, 4)) == 0);
1279 }
1280
1281 /************************************************/
1282
1283 struct S66
1284 {
1285 int i;
1286 int j = 3;
1287 }
1288
foo66(S66 s1)1289 int foo66(S66 s1)
1290 {
1291 return s1.j;
1292 }
1293
test66()1294 void test66()
1295 {
1296 static assert(foo66(S66(1, 5)) == 5);
1297 }
1298
1299 /************************************************/
1300
1301 struct S67
1302 {
1303 int i;
1304 int j = 3;
1305 }
1306
foo67(S67 s1)1307 int foo67(S67 s1)
1308 {
1309 s1.j = 3;
1310 int i = (s1.j += 2);
1311 assert(i == 5);
1312 return s1.j + 4;
1313 }
1314
test67()1315 void test67()
1316 {
1317 static assert(foo67(S67(1, 5)) == 9);
1318 }
1319
1320 /************************************************/
1321
foo68(int[]a)1322 int foo68(int[] a)
1323 {
1324 a[1] = 3;
1325 int x = (a[0] += 7);
1326 assert(x == 8);
1327 return a[0] + a[1];
1328 }
1329
test68()1330 void test68()
1331 {
1332 static assert(foo68( [1,5] ) == 11);
1333 }
1334
1335 /************************************************/
1336
foo69(char[]a)1337 int foo69(char[] a)
1338 {
1339 a[1] = 'c';
1340 char x = (a[0] += 7);
1341 assert(x == 'h');
1342 assert(x == a[0]);
1343 return a[0] + a[1] - 'a';
1344 }
1345
test69()1346 void test69()
1347 {
1348 static assert(foo69(['a', 'b']) == 'j');
1349 }
1350
1351 /************************************************/
1352
foo70(int[string]a)1353 int foo70(int[string] a)
1354 {
1355 a["world"] = 5;
1356 auto x = (a["hello"] += 7);
1357 assert(x == 10);
1358 assert(x == a["hello"]);
1359 return a["hello"] + a["betty"] + a["world"];
1360 }
1361
test70()1362 void test70()
1363 {
1364 static assert(foo70(["hello":3, "betty":4]) == 19);
1365 }
1366
1367 /************************************************/
1368
foo71(int[string]a)1369 size_t foo71(int[string] a)
1370 {
1371 return a.length;
1372 }
1373
test71()1374 void test71()
1375 {
1376 static assert(foo71(["hello":3, "betty":4]) == 2);
1377 }
1378
1379 /************************************************/
1380
foo72(int[string]a)1381 string[] foo72(int[string] a)
1382 {
1383 return a.keys;
1384 }
1385
test72()1386 void test72()
1387 {
1388 static assert(foo72(["hello":3, "betty":4]) == ["hello", "betty"]);
1389 }
1390
1391 /************************************************/
1392
foo73(int[string]a)1393 int[] foo73(int[string] a)
1394 {
1395 return a.values;
1396 }
1397
test73()1398 void test73()
1399 {
1400 static assert(foo73(["hello":3, "betty":4]) == [3, 4]);
1401 }
1402
1403 /************************************************/
1404
b74()1405 bool b74()
1406 {
1407 string a = "abc";
1408 return (a[$-1] == 'c');
1409 }
1410
1411 const c74 = b74();
1412
test74()1413 void test74()
1414 {
1415 assert(c74 == true);
1416 }
1417
1418 /************************************************/
1419
1420 struct FormatSpec
1421 {
1422 uint leading;
1423 bool skip;
1424 uint width;
1425 char modifier;
1426 char format;
1427 uint formatStart;
1428 uint formatLength;
1429 uint length;
1430 }
1431
GetFormat(string s)1432 FormatSpec GetFormat(string s)
1433 {
1434 FormatSpec result;
1435 return result;
1436 }
1437
GetFormat2(string s)1438 FormatSpec GetFormat2(string s)
1439 {
1440 FormatSpec result = FormatSpec();
1441 result.length = 0;
1442 assert(result.length < s.length);
1443 while (result.length < s.length)
1444 {
1445 ++result.length;
1446 }
1447 return result;
1448 }
1449
test75()1450 void test75()
1451 {
1452 static FormatSpec spec = GetFormat("asd");
1453
1454 assert(spec.leading == 0);
1455 assert(spec.modifier == char.init);
1456
1457 static FormatSpec spec2 = GetFormat2("asd");
1458 assert(spec2.length == 3);
1459 }
1460
1461 /************************************************/
1462
f76()1463 int f76()
1464 {
1465 int[3] a = void;
1466 a[0] = 1;
1467 assert(a[0] == 1);
1468 return 1;
1469 }
1470
1471 const i76 = f76();
1472
test76()1473 void test76()
1474 {
1475 }
1476
1477 /************************************************/
1478
1479 struct V77
1480 {
1481 int a;
1482 int b;
1483 }
1484
f77()1485 V77 f77()
1486 {
1487 int q = 0;
1488 int unused;
1489 int unused2;
1490 return V77(q, 0);
1491 }
1492
test77()1493 void test77()
1494 {
1495 const w = f77();
1496 const v = f77().b;
1497 }
1498
1499 /************************************************/
1500
1501 struct Bar78
1502 {
1503 int x;
1504 }
1505
foo78()1506 int foo78()
1507 {
1508 Bar78 b = Bar78.init;
1509 Bar78 c;
1510 b.x = 1;
1511 b = bar(b);
1512 return b.x;
1513 }
1514
bar(Bar78 b)1515 Bar78 bar(Bar78 b)
1516 {
1517 return b;
1518 }
1519
test78()1520 void test78()
1521 {
1522 static x = foo78();
1523 }
1524
1525 /************************************************/
1526
1527 struct Bar79
1528 {
1529 int y,x;
1530 }
1531
foo79()1532 int foo79()
1533 {
1534 Bar79 b = Bar79.init;
1535
1536 b.x = 100;
1537
1538 for (size_t i = 0; i < b.x; i++) { }
1539
1540 b.x++;
1541 b.x = b.x + 1;
1542
1543 return b.x;
1544 }
1545
test79()1546 void test79()
1547 {
1548 static x = foo79();
1549 printf("x = %d\n", x);
1550 assert(x == 102);
1551 }
1552
1553 /************************************************/
1554
test80()1555 void test80()
1556 {
1557 }
1558
1559 /************************************************/
1560
foo81()1561 string foo81()
1562 {
1563 return "";
1564 }
1565
rod81(string[]a)1566 string rod81(string[] a)
1567 {
1568 return a[0];
1569 }
1570
test81()1571 void test81()
1572 {
1573 static x = rod81([foo81(), ""]);
1574 assert(x == "");
1575 }
1576
1577
1578 /************************************************/
1579
1580 struct S82
1581 {
1582 string name;
1583 }
1584
1585 const S82 item82 = {"item"};
1586
mixItemList82()1587 string mixItemList82()
1588 {
1589 return item82.name;
1590 }
1591
1592 const string s82 = mixItemList82();
1593
test82()1594 void test82()
1595 {
1596 assert(s82 == "item");
1597 }
1598
1599 /************************************************/
1600
1601 struct S83
1602 {
1603 string name;
1604 }
1605
1606 const S83[] items83 =
1607 [
1608 {"item"},
1609 ];
1610
mixItemList83()1611 string mixItemList83()
1612 {
1613 string s;
1614 foreach (item; items83)
1615 s ~= item.name;
1616 return s;
1617 }
1618
1619 const string s83 = mixItemList83();
1620
test83()1621 void test83()
1622 {
1623 assert(s83 == "item");
1624 }
1625
1626 /************************************************/
1627
1628 struct S84 { int a; }
1629
func84()1630 int func84()
1631 {
1632 S84 [] s = [S84(7)];
1633 return s[0].a; // Error: cannot evaluate func() at compile time
1634 }
1635
test84()1636 void test84()
1637 {
1638 const int x = func84();
1639 assert(x == 7);
1640 }
1641
1642 /************************************************/
1643
1644 struct S85
1645 {
1646 int a;
1647 }
1648
func85()1649 size_t func85()
1650 {
1651 S85 [] s;
1652 s ~= S85(7);
1653 return s.length;
1654 }
1655
test85()1656 void test85()
1657 {
1658 const size_t x = func85();
1659 assert(x == 1);
1660 }
1661
1662 /************************************************/
1663
1664 struct Bar86
1665 {
1666 int x;
1667 char[] s;
1668 }
1669
foo86()1670 char[] foo86()
1671 {
1672 Bar86 bar;
1673 return bar.s;
1674 }
1675
test86()1676 void test86()
1677 {
1678 static x = foo86();
1679 assert(x == null);
1680 }
1681
1682 /************************************************/
1683
1684 struct Bar87
1685 {
1686 int x;
1687 }
1688
foo87()1689 int foo87()
1690 {
1691 Bar87 bar;
1692 bar.x += 1;
1693 bar.x++;
1694 return bar.x;
1695 }
1696
test87()1697 void test87()
1698 {
1699 static x = foo87();
1700 assert(x == 2);
1701 }
1702
1703 /************************************************/
1704
foo88()1705 int foo88()
1706 {
1707 char[] s;
1708 int i;
1709
1710 if (s)
1711 {
1712 i |= 1;
1713 }
1714
1715 if (s == null)
1716 {
1717 i |= 2;
1718 }
1719
1720 if (s is null)
1721 {
1722 i |= 4;
1723 }
1724
1725 if (s == "")
1726 {
1727 i |= 8;
1728 }
1729
1730 if (s.length)
1731 {
1732 i |= 16;
1733 }
1734
1735 if (s == ['c'][0..0])
1736 {
1737 i |= 32;
1738 }
1739
1740
1741 if (null == s)
1742 {
1743 i |= 64;
1744 }
1745
1746 if (null is s)
1747 {
1748 i |= 128;
1749 }
1750
1751 if ("" == s)
1752 {
1753 i |= 256;
1754 }
1755
1756 if (['c'][0..0] == s)
1757 {
1758 i |= 512;
1759 }
1760
1761 return i;
1762 }
1763
test88()1764 void test88()
1765 {
1766 static x = foo88();
1767 printf("x = %x\n", x);
1768 assert(x == (2|4|8|32|64|128|256|512));
1769 }
1770
1771 /************************************************/
1772
Tuple89(T...)1773 template Tuple89(T...)
1774 {
1775 alias T val;
1776 }
1777
1778 alias Tuple89!(int) Tup89;
1779
gen89()1780 string gen89()
1781 {
1782 foreach (i, type; Tup89.val)
1783 {
1784 assert(i == 0);
1785 assert(is(type == int));
1786 }
1787 return null;
1788 }
1789
test89()1790 void test89()
1791 {
1792 static const string text = gen89();
1793 assert(text is null);
1794 }
1795
1796 /************************************************/
1797
bar90(string z)1798 string bar90(string z)
1799 {
1800 return z;
1801 }
1802
foo90(string a,string b)1803 string foo90(string a, string b)
1804 {
1805 string f = a.length == 1 ? a: foo90("B", "C");
1806 string g = b.length == 1 ? b: bar90(foo90("YYY", "A"));
1807 return f;
1808 }
1809
test90()1810 void test90()
1811 {
1812 static const string xxx = foo90("A", "xxx");
1813 printf("%.*s\n", cast(int)xxx.length, xxx.ptr);
1814 assert(xxx == "A");
1815 }
1816
1817 /************************************************/
1818
1819 struct PR91
1820 {
1821 }
1822
foo91()1823 int foo91()
1824 {
1825 PR91 pr;
1826 pr = PR91();
1827 return 0;
1828 }
1829
test91()1830 void test91()
1831 {
1832 static const i = foo91();
1833 }
1834
1835 /************************************************/
1836
find92(immutable (char)[7]buf)1837 char find92(immutable(char)[7] buf)
1838 {
1839 return buf[3];
1840 }
1841
1842
test92()1843 void test92()
1844 {
1845 static const pos = find92("abcdefg");
1846 assert(pos == 'd');
1847 }
1848
1849 /************************************************/
1850
hello93()1851 static string hello93()
1852 {
1853 string result = "";
1854 int i = 0;
1855 for (;;)
1856 {
1857 result ~= `abc`;
1858 i += 1;
1859 if (i == 3)
1860 break;
1861 }
1862 return result;
1863 }
1864
test93()1865 void test93()
1866 {
1867 static string s = hello93();
1868 assert(s == "abcabcabc");
1869 }
1870
1871 /************************************************/
1872
foo94(string[]list,string s)1873 int foo94 (string[] list, string s)
1874 {
1875 if (list.length == 0)
1876 return 1;
1877 else
1878 {
1879 return 2 + foo94(list[1..$], list[0]);
1880 }
1881 }
1882
test94()1883 void test94()
1884 {
1885 printf("test94\n");
1886 static const int x = foo94(["a", "b"], "");
1887 assert(x == 5);
1888 }
1889
1890 /************************************************/
1891
func95(immutable char[]s)1892 char[] func95(immutable char[] s)
1893 {
1894 char[] u = "".dup;
1895 u ~= s;
1896 u = u ~ s;
1897 return u;
1898 }
1899
test95()1900 void test95()
1901 {
1902 mixin(func95("{}"));
1903 }
1904
1905 /************************************************/
1906
func96(string s)1907 char[] func96(string s)
1908 {
1909 char[] u = "".dup;
1910 u ~= s;
1911 u = u ~ s;
1912 return u;
1913 }
1914
test96()1915 void test96()
1916 {
1917 mixin(func96("{}"));
1918 }
1919
1920 /************************************************/
1921
foo97()1922 string foo97()
1923 {
1924 string a;
1925 a ~= "abc"; // ok
1926 string[] b;
1927 b ~= "abc"; // ok
1928 string[][] c;
1929 c ~= ["abc", "def"];
1930 string[][] d = [];
1931 d ~= ["abc", "def"]; // ok
1932 return "abc";
1933 }
1934
test97()1935 void test97()
1936 {
1937 static const xx97 = foo97();
1938 }
1939
1940 /************************************************/
1941
immutable(int)1942 immutable(int)[] foo98(immutable(int)[][] ss)
1943 {
1944 immutable(int)[] r;
1945 r ~= ss[0]; // problem here
1946 return r;
1947 }
1948
test98()1949 void test98()
1950 {
1951 const r = foo98([[1], [2]]);
1952 }
1953
1954 /************************************************/
1955
1956 struct Number
1957 {
1958 public int value;
opCallNumber1959 static Number opCall(int value)
1960 {
1961 Number n = void;
1962 n.value = value;
1963 return n;
1964 }
1965 }
1966
1967 class Crash
1968 {
1969 Number number = Number(0);
1970 }
1971
test99()1972 void test99()
1973 {
1974 }
1975
1976 /************************************************/
1977
1978 int[] map100 = ([4:true, 5:true]).keys;
1979 bool[] foo100 = ([4:true, 5:true]).values;
1980
test100()1981 void test100()
1982 {
1983 }
1984
1985 /************************************************/
1986
foo101()1987 int foo101()
1988 {
1989 immutable bool [int] map = [4:true, 5:true];
1990 foreach (x; map.keys) {}
1991 return 3;
1992 }
1993
1994 static int x101 = foo101();
1995
test101()1996 void test101()
1997 {
1998 }
1999
2000 /************************************************/
2001
foo102()2002 int foo102()
2003 {
2004 foreach (i; 0 .. 1)
2005 return 1;
2006 return 0;
2007 }
2008 static assert(foo102() == 1);
2009
bar102()2010 int bar102()
2011 {
2012 foreach_reverse (i; 0 .. 1)
2013 return 1;
2014 return 0;
2015 }
2016 static assert(bar102() == 1);
2017
test102()2018 void test102()
2019 {
2020 }
2021
2022 /************************************************/
2023
foo103()2024 int foo103()
2025 {
2026 foreach (c; '0' .. '9') { }
2027 foreach_reverse (c; '9' .. '0') { }
2028 return 0;
2029 }
2030
2031 enum x103 = foo103();
2032
test103()2033 void test103()
2034 {
2035 }
2036
2037 /************************************************/
2038
2039 struct S
2040 {
2041 int x;
2042 char y;
2043 }
2044
2045 // Functions which should fail CTFE
2046
badfoo()2047 int badfoo()
2048 {
2049 S[2] c;
2050 int w = 4;
2051 c[w].x = 6; // array bounds error
2052 return 7;
2053 }
2054
2055 int badglobal = 1;
2056
badfoo3()2057 int badfoo3()
2058 {
2059 S[2] c;
2060 c[badglobal].x = 6; // global index error
2061 return 7;
2062 }
2063
badfoo4()2064 int badfoo4()
2065 {
2066 static S[2] c;
2067 c[0].x = 6; // Cannot access static
2068 return 7;
2069 }
2070
2071 /+ // This doesn't compile at runtime
2072 int badfoo5()
2073 {
2074 S[] c = void;
2075 c[0].x = 6; // c is uninitialized, and not a static array.
2076 return 1;
2077 }
2078 +/
2079
badfoo6()2080 int badfoo6()
2081 {
2082 S[] b = [S(7), S(15), S(56), S(12)];
2083 b[-2..4] = S(17); // exceeding (negative) array bounds
2084 return 1;
2085 }
2086
badfoo7()2087 int badfoo7()
2088 {
2089 S[] b = [S(7), S(15), S(56), S(12), S(67)];
2090 S[] c = [S(17), S(4)];
2091 b[1..4] = c[]; // slice mismatch in dynamic array
2092 return 1;
2093 }
2094
badfoo8()2095 int badfoo8()
2096 {
2097 S[] b;
2098 b[1..3] = [S(17), S(4)]; // slice assign to uninitialized dynamic array
2099 return 1;
2100 }
2101
Compileable(int z)2102 template Compileable(int z) { bool OK = true;}
2103 static assert(!is(typeof(Compileable!(badfoo()).OK)));
2104 static assert(!is(typeof(Compileable!(
2105 (){
2106 S[] c;
2107 return c[7].x; // uninitialized error
2108 }()).OK
2109 )));
2110 static assert( is(typeof(Compileable!(0).OK)));
2111 static assert(!is(typeof(Compileable!(badfoo3()).OK)));
2112 static assert(!is(typeof(Compileable!(badfoo4()).OK)));
2113 //static assert(!is(typeof(Compileable!(badfoo5()).OK)));
2114 static assert(!is(typeof(Compileable!(badfoo6()).OK)));
2115 static assert(!is(typeof(Compileable!(badfoo7()).OK)));
2116 static assert(!is(typeof(Compileable!(badfoo8()).OK)));
2117
2118 // Functions which should pass CTFE
2119
goodfoo1()2120 int goodfoo1()
2121 {
2122 int[8] w; // use static array in CTFE
2123 w[] = 7; // full slice assign
2124 w[$ - 1] = 538; // use of $ in index assignment
2125 assert(w[6] == 7);
2126 return w[7];
2127 }
2128 static assert(goodfoo1() == 538);
2129
goodfoo2()2130 int goodfoo2()
2131 {
2132 S[4] w = S(101); // Block-initialize array of structs
2133 w[$ - 2].x = 917; // use $ in index member assignment
2134 w[$ - 2].y = 58; // this must not clobber the prev assignment
2135 return w[2].x; // check we got the correct one
2136 }
2137 static assert(goodfoo2() == 917);
2138
2139 static assert(is(typeof(Compileable!(
2140 (){
2141 S[4] w = void; // uninitialized array of structs
2142 w[$ - 2].x = 217; // initialize one member
2143 return w[2].x;
2144 }()).OK
2145 )));
2146
goodfoo4()2147 int goodfoo4()
2148 {
2149 S[4] b = [S(7), S(15), S(56), S(12)]; // assign from array literal
2150 assert(b[3] == S(12));
2151 return b[2].x - 55;
2152 }
2153 static assert(goodfoo4()==1);
2154
goodfoo5()2155 int goodfoo5()
2156 {
2157 S[4] b = [S(7), S(15), S(56), S(12)];
2158 b[0..2] = [S(2), S(6)]; // slice assignment from array literal
2159 assert(b[3] == S(12));
2160 assert(b[1] == S(6));
2161 return b[0].x;
2162 }
2163 static assert(goodfoo5() == 2);
2164 static assert(goodfoo5() == 2); // check for memory corruption
2165
goodfoo6()2166 int goodfoo6()
2167 {
2168 S[6] b = void;
2169 b[2..5] = [S(2), S(6), S(17)]; // slice assign to uninitialized var
2170 assert(b[4] == S(17));
2171 return b[3].x;
2172 }
2173 static assert(goodfoo6() == 6);
2174
goodfoo7()2175 int goodfoo7()
2176 {
2177 S[8] b = void;
2178 b[2..5] = S(217); // slice assign to uninitialized var
2179 assert(b[4] == S(217));
2180 return b[3].x;
2181 }
2182 static assert(goodfoo7() == 217);
2183
goodfoo8()2184 int goodfoo8()
2185 {
2186 S[] b = [S(7), S(15), S(56), S(12), S(67)];
2187 b[2..4] = S(17); // dynamic array block slice assign
2188 assert(b[3] == S(17));
2189 assert(b[4] == S(67));
2190 return b[0].x;
2191 }
2192 static assert(goodfoo8() == 7);
2193
2194 // --------- CTFE MEMBER FUNCTION TESTS --------
2195 struct Q
2196 {
2197 int x;
2198 char y;
2199 int opOpAssign(string op)(int w) if (op == "+")
2200 {
2201 x += w;
2202 return x + w;
2203 }
2204 Q opOpAssign(string op)(int w) if (op == "-")
2205 {
2206 x -= w;
versionQ2207 version(D_Version2) { mixin("return this;"); } else { mixin("return *this;"); }
2208 }
booQ2209 int boo() { return 4; }
cooQ2210 int coo() { return x; }
fooQ2211 int foo() { return coo(); }
dooQ2212 int doo(int a)
2213 {
2214 Q z = Q(a, 'x');
2215 z.x += 5;
2216 return z.coo() + 3 * x;
2217 }
gooQ2218 void goo(int z) { x = z; }
hooQ2219 int hoo(int y, int z) { return y + z; }
jooQ2220 void joo(int z)
2221 {
2222 x += z;
2223 }
2224 }
2225
memtest1()2226 int memtest1()
2227 {
2228 Q b = Q(15, 'a');
2229 return b.hoo(3, 16); // simple const function
2230 }
2231
2232 static assert(memtest1() == 19);
2233
memtest2()2234 int memtest2()
2235 {
2236 Q b = Q(15, 'x');
2237 b.x -= 10;
2238 return b.coo();
2239 }
2240
2241 static assert(memtest2() == 5);
2242
memtest3()2243 int memtest3()
2244 {
2245 Q b = Q(15, 'x');
2246 b.x -= 10;
2247 return b.foo();
2248 }
2249
2250 static assert(memtest3() == 5);
2251
memtest4()2252 int memtest4()
2253 {
2254 Q b = Q(12, 'x');
2255 return b.doo(514);
2256 }
2257 static assert(memtest4() == 519 + 3 * 12);
2258
2259
memtest5()2260 int memtest5()
2261 {
2262 Q b = Q(132, 'x');
2263 b.goo(4178); // Call modifying member
2264 return b.x;
2265 }
2266 static assert(memtest5() == 4178);
2267
memtest6()2268 int memtest6()
2269 {
2270 Q q = Q(1);
2271 q += 3; // operator overloading
2272 return q.x;
2273 }
2274 static assert(memtest6() == 4);
2275
2276 static assert(!is(typeof(Compileable!(Q += 2).OK))); // Mustn't cause segfault
2277
memtest7()2278 int memtest7()
2279 {
2280 Q q = Q(57);
2281 q -= 35;
2282 return q.x;
2283 }
2284
2285 static assert(memtest7() == 57 - 35);
2286
memtest8()2287 int memtest8()
2288 {
2289 Q[3] w;
2290 w[2].x = 17;
2291 w[2].joo(6); // Modify member of array
2292 w[1].x += 18;
2293 return w[2].coo();
2294 }
2295
2296 static assert(memtest8() == 6 + 17);
2297
2298 // --------- CTFE REF PASSING TESTS --------
2299
2300 // https://issues.dlang.org/show_bug.cgi?id=1950 - CTFE doesn't work correctly for structs passed by ref
2301 struct S1950
2302 {
2303 int x;
2304 }
2305
foo1950()2306 int foo1950()
2307 {
2308 S1950 s = S1950(5); // explicitly initialized
2309 bar1950(s);
2310 return s.x;
2311 }
2312
bar1950(ref S1950 w)2313 void bar1950(ref S1950 w)
2314 {
2315 w.x = 10;
2316 }
2317
2318 static assert(foo1950() == 10); // OK <- Fails, x is 0
2319
foo1950b()2320 int foo1950b()
2321 {
2322 S1950 s; // uninitialized
2323 bar1950(s);
2324 return s.x;
2325 }
2326
2327 static assert(foo1950b() == 10); // OK <- Fails, x is 0
2328
2329
2330 // More extreme case, related to 1950
2331
bar1950c(ref int w)2332 void bar1950c(ref int w)
2333 {
2334 w = 87;
2335 }
2336
foo1950c()2337 int foo1950c()
2338 {
2339 int[5] x;
2340 x[] = 56;
2341 bar1950c(x[1]); // Non-trivial ref parameters
2342 return x[1];
2343 }
2344
2345 static assert(foo1950c() == 87);
2346
bar1950d(ref int[]w)2347 void bar1950d(ref int[] w)
2348 {
2349 w[1..$] = 87;
2350 w[0] += 15;
2351 }
2352
foo1950d()2353 int foo1950d()
2354 {
2355 int[] x = [1, 2, 3, 4, 5];
2356 x[1..$] = 56;
2357 bar1950d(x); // Non-trivial ref parameters
2358 assert(x[0] == 16);
2359 return x[1];
2360 }
2361
2362 static assert(foo1950d() == 87);
2363
2364 // Nested functions
nested(int x)2365 int nested(int x)
2366 {
2367 int y = 3;
2368 int inner(int w)
2369 {
2370 int z = 2;
2371 ++z;
2372 y += w;
2373 return x + 3;
2374 }
2375
2376 int z = inner(14);
2377 assert(y == 17);
2378 inner(8);
2379 assert(y == 17 + 8);
2380 return z + y;
2381 }
2382
2383 static assert(nested(7) == 17 + 8 + 10);
2384 static assert(nested(7) == 17 + 8 + 10);
2385
2386 // Recursive nested functions
2387
nested2(int x)2388 int nested2(int x)
2389 {
2390 int y = 3;
2391 int inner(int w)
2392 {
2393 int z = 2;
2394 ++z;
2395 ++y;
2396 if (w <= 1)
2397 return x + 3;
2398 else
2399 return inner(w - 1);
2400 }
2401
2402 int z = inner(14);
2403 assert(y == 17);
2404
2405 inner(8);
2406 assert(y == 17 + 8);
2407 return z + y;
2408 }
2409
2410 static assert(nested2(7) == 17 + 8 + 10);
2411
2412 // https://issues.dlang.org/show_bug.cgi?id=1605
2413 // D1 & D2. break in switch with goto breaks in ctfe
bug1605()2414 int bug1605()
2415 {
2416 int i = 0;
2417 while (true)
2418 {
2419 goto LABEL;
2420 LABEL:
2421 if (i != 0)
2422 return i;
2423 i = 27;
2424 }
2425 assert(i == 27);
2426 return 88; // unreachable
2427 }
2428
2429 static assert(bug1605() == 27);
2430
2431 // https://issues.dlang.org/show_bug.cgi?id=2564
2432 // D2 only. CTFE: the index in a tuple foreach is uninitialized (bogus error)
2433 // NOTE: Beware of optimizer bug 3264.
2434
bug2564()2435 int bug2564()
2436 {
2437 version(D_Version2) { mixin("enum int Q = 0;"); }else {mixin("int Q = 0;"); }
2438 string [2] s = ["a", "b"];
2439 assert(s[Q].dup == "a");
2440 return 0;
2441 }
2442
2443 static int bug2564b = bug2564();
2444
2445
2446 // https://issues.dlang.org/show_bug.cgi?id=1461
2447 // D1 + D2. Local variable as template alias parameter breaks CTFE
bug1461()2448 void bug1461()
2449 {
2450 int x;
2451 static assert(Gen1461!(x).generate() == null);
2452 }
2453
Gen1461(alias A)2454 template Gen1461(alias A)
2455 {
2456 string generate()
2457 {
2458 return null;
2459 }
2460 }
2461
2462 /************************************************/
2463
foo104(string[]a...)2464 string foo104(string[] a...)
2465 {
2466 string result = "";
2467 foreach (s; a)
2468 result ~= s;
2469 return result;
2470 }
2471
2472 mixin (foo104("int ", "x;"));
2473
2474 /************************************************/
2475
2476 struct SwineFlu
2477 {
2478 int a;
2479 int b;
2480 }
2481
2482 struct Infection
2483 {
2484 SwineFlu y;
2485 }
2486
2487 struct IveGotSwineFlu
2488 {
2489 Infection x;
2490 int z;
oinkIveGotSwineFlu2491 int oink() { return x.y.a + 10; }
2492 }
2493
quarantine()2494 int quarantine()
2495 {
2496 IveGotSwineFlu d;
2497 return d.oink();
2498 }
2499
2500 struct Mexico
2501 {
2502 Infection x;
2503 int z = 2;
oinkMexico2504 int oink() { return z + x.y.b; }
2505 }
2506
mediafrenzy()2507 int mediafrenzy()
2508 {
2509 Mexico m;
2510 return m.oink;
2511 }
2512
2513 static assert(quarantine() == 10);
2514 static assert(mediafrenzy() == 2);
2515
2516 /************************************************/
2517
ctfeArrayTest(int z)2518 int ctfeArrayTest(int z)
2519 {
2520 int[] a = new int[z];
2521 a[$ - 3] = 6;
2522 assert(a.length == z);
2523 return a[$ - 3];
2524 }
2525 static assert(ctfeArrayTest(15) == 6);
2526
2527 /************************************************/
2528
bugzilla1298()2529 char bugzilla1298()
2530 {
2531 char [4] q = "abcd".dup;
2532 char [4] r = ['a', 'b', 'c', 'd'];
2533 assert(q == r);
2534 q[0..2] = "xy";
2535 q[2] += 3;
2536 return q[2];
2537 }
2538
2539 static assert(bugzilla1298() == 'f');
2540
bugzilla1790(Types...)2541 int bugzilla1790(Types...)()
2542 {
2543 foreach (T; Types)
2544 {
2545 }
2546 return 0;
2547 }
2548
2549 const int bugs1790 = bugzilla1790!("")();
2550
ctfeStrTest1()2551 char ctfeStrTest1()
2552 {
2553 char [8] s = void;
2554 s[2..4] = 'x';
2555
2556 assert(s.length == 8);
2557 return s[3];
2558 }
2559
2560 static assert(ctfeStrTest1() == 'x');
2561
2562 //--------- DELEGATE TESTS ------
2563
2564 // Function + delegate literals inside CTFE
delegtest1()2565 int delegtest1()
2566 {
2567 assert(function int(int a){ return 7 + a; }(16) == 23);
2568 return delegate int(int a){ return 7 + a; }(6);
2569 }
2570
delegtest2()2571 int delegtest2()
2572 {
2573 int innerfunc1()
2574 {
2575 return delegate int(int a){ return 7 + a; }(6);
2576 }
2577 int delegate() f = &innerfunc1;
2578 return 3 * f();
2579 }
2580
delegtest3()2581 int delegtest3()
2582 {
2583 int function() f = &delegtest1;
2584 return 3 * f();
2585 }
2586
2587 struct DelegStruct
2588 {
2589 int a;
barDelegStruct2590 int bar(int x) { return a + x; }
2591 }
2592
delegtest4()2593 int delegtest4()
2594 {
2595 DelegStruct s;
2596 s.a = 5;
2597 auto f = &s.bar;
2598 return f(3);
2599 }
2600
2601 alias int delegate(int) DelegType;
2602
2603 // Test arrays of delegates
delegtest5()2604 int delegtest5()
2605 {
2606 DelegStruct s;
2607 s.a = 5;
2608 DelegType[4] w;
2609 w[] = &s.bar;
2610 return w[2](3);
2611 }
2612
2613 // Test arrays of structs of delegates
2614 struct FoolishStruct
2615 {
2616 DelegType z;
2617 }
2618
delegtest6()2619 int delegtest6()
2620 {
2621 DelegStruct s;
2622 s.a = 5;
2623 FoolishStruct[3] k;
2624 DelegType u = &s.bar;
2625 k[1].z = u;
2626 return k[1].z(3);
2627 }
2628
2629 static assert(delegtest1() == 13);
2630 static assert(delegtest2() == 39);
2631 static assert(delegtest3() == 39);
2632 static assert(delegtest4() == 8);
2633 static assert(delegtest5() == 8);
2634 static assert(delegtest6() == 8);
2635
2636 // Function + delegate literals, module scope
2637 static assert(function int(int a){ return 17 + a; }(16) == 33);
2638 static assert( (int a){ return 7 + a; }(16) == 23);
2639
2640 // --- Test lazy ---
lazyTest1(lazy int y)2641 int lazyTest1(lazy int y)
2642 {
2643 return y + 1;
2644 }
2645
lazyTest2(int x)2646 int lazyTest2(int x)
2647 {
2648 return lazyTest1(x);
2649 }
2650
2651 static assert(lazyTest1(7) == 8);
2652 static assert(lazyTest2(17) == 18);
2653
2654 /************************************************/
2655
version(D_Version2)2656 version(D_Version2)
2657 {
2658 // https://issues.dlang.org/show_bug.cgi?id=4020
2659 // https://issues.dlang.org/show_bug.cgi?id=4027
2660 // D2 only
2661 struct PostblitCrash
2662 {
2663 int x;
2664 mixin("this(this) { ++x; }");
2665 }
2666
2667 int bug4020()
2668 {
2669 PostblitCrash f;
2670 f.x = 3;
2671 f = f;
2672 f = f;
2673 return f.x;
2674 }
2675 static assert(bug4020() == 5);
2676
2677 string delegate() bug4027(string s)
2678 {
2679 return { return s; };
2680 }
2681
2682 // If it compiles, it must not generate wrong code on D2.
2683 static if (is(typeof((){ static const s = bug4027("aaa")(); }()))) {
2684 static assert(bug4027("aaa")() == "aaa");
2685 static assert(bug4027("bbb")() == "bbb");
2686 }
2687 }
2688
2689 // ---
2690
bug4004a(ref int a)2691 void bug4004a(ref int a)
2692 {
2693 assert(a == 7);
2694 a += 3;
2695 }
2696
bug4004b(ref int b)2697 void bug4004b(ref int b)
2698 {
2699 b = 7;
2700 bug4004a(b);
2701 }
2702
bug4004c()2703 int bug4004c()
2704 {
2705 int offset = 5;
2706 bug4004b(offset);
2707 return offset;
2708 }
2709
2710 static assert(bug4004c() == 10);
2711
2712 // ---
2713
bug4019()2714 int bug4019()
2715 {
2716 int[int] aa;
2717 aa[1] = 2;
2718 aa[4] = 6;
2719 return aa[1] + aa[4];
2720 }
2721 static assert(bug4019() == 8);
2722
2723 // ---
2724
delegate()2725 string delegate() bug4029a()
2726 {
2727 return { return "abc"[]; };
2728 }
2729
bug4029()2730 string bug4029()
2731 {
2732 return bug4029a()();
2733 }
2734
2735 static assert(bug4029() == "abc");
2736
2737 /************************************************/
2738
bug4078()2739 int bug4078()
2740 {
2741 int[] arr = new int[1];
2742 return arr[0];
2743 }
2744 static assert(bug4078() == 0);
2745
bug4052()2746 int bug4052()
2747 {
2748 int[] arr = new int[1];
2749 int s;
2750 foreach (x; arr)
2751 s += x;
2752 foreach (x; arr)
2753 s += x * x;
2754 return 4052;
2755 }
2756 static assert(bug4052() == 4052);
2757
bug4252()2758 int bug4252()
2759 {
2760 char [] s = "abc".dup;
2761 s[15] = 'd'; // Array bounds error
2762 return 3;
2763 }
2764
2765 static assert(!is(typeof(Compileable!(bug4252()))));
2766
setlen1()2767 size_t setlen1()
2768 {
2769 int[] w = new int[4];
2770 w[] = 7;
2771 w.length = 6;
2772 return 21 + w.length;
2773 }
2774
2775 static assert(setlen1() == 27);
2776
setlen2()2777 size_t setlen2()
2778 {
2779 int[] w;
2780 w.length = 15;
2781 assert(w[3] == 0);
2782 w[2] = 8;
2783 w[14] = 7;
2784 w.length = 12; // check shrinking
2785 assert(w[2] == 8);
2786 return 2 + w.length;
2787 }
2788
2789 static assert(setlen2() == 14);
2790
2791 /************************************************/
2792
bug4257(ref int x)2793 int bug4257(ref int x)
2794 {
2795 return 3;
2796 }
2797
bug4257c(int x)2798 int bug4257c(int x)
2799 {
2800 return 3;
2801 }
2802
2803 struct Struct4257
2804 {
fooStruct42572805 int foo() { return 2; }
2806 }
2807
bug4257b()2808 void bug4257b()
2809 {
2810 int y;
2811 static assert(!is(typeof(Compileable!(bug4257(y)))));
2812 static assert(!is(typeof(Compileable!(bug4257c(y)))));
2813 Struct4257 s;
2814 static assert(!is(typeof(Compileable!(s.foo()))));
2815 }
2816
2817 /************************************************/
2818 // https://issues.dlang.org/show_bug.cgi?id=5117
2819
2820 static int dummy5117 = test5117();
2821
test5117()2822 int test5117()
2823 {
2824 S5117 s;
2825 s.change();
2826 assert(s.value == 1); // (7) succeeds
2827
2828 R5117 r;
2829 r.s.change();
2830 assert(r.s.value == 1); // (11) fails, value == 0
2831
2832 return 0;
2833 }
2834
2835 struct S5117
2836 {
2837 int value;
changeS51172838 void change() { value = 1; }
2839 }
2840
2841 struct R5117
2842 {
2843 S5117 s;
2844 }
2845
2846 /************************************************/
2847
2848 enum dummy5117b = test5117b();
2849
test5117b()2850 int test5117b()
2851 {
2852 S5117b s;
2853 getRef5117b(s).change();
2854 assert(s.value == 1); // fails, value == 0
2855 return 0;
2856 }
getRef5117b(return ref S5117b s)2857 ref S5117b getRef5117b(return ref S5117b s) { return s; }
2858
2859 struct S5117b
2860 {
2861 int value;
changeS5117b2862 void change() { value = 1; }
2863 }
2864
2865 /************************************************/
2866 // https://issues.dlang.org/show_bug.cgi?id=6439
2867
2868 struct A6439
2869 {
this(uint a,uint b)2870 this(uint a, uint b)
2871 {
2872 begin = a;
2873 end = b;
2874 }
2875 union
2876 {
2877 struct
2878 {
2879 uint begin, end;
2880 }
2881 uint[2] arr;
2882 }
2883 }
2884
2885 void test6439()
2886 {
2887 enum y = A6439(10, 20);
2888 A6439 y2 = A6439(10, 20);
2889 assert(y2.begin == y.begin && y2.end == y.end); //passes
2890 assert(y.arr != [0,0]);
2891 assert(y.arr == [10,20]);
2892 assert(y.arr == y2.arr);
2893 }
2894
2895 /************************************************/
2896 // from tests/fail_compilation/fail147
2897
2898 static assert(!is(typeof(Compileable!(
2899 (int i){
2900 int x = void;
2901 ++x; // used before initialization
2902 return i + x;
2903 }(3)
2904 ))));
2905
2906 // https://issues.dlang.org/show_bug.cgi?id=6504 regression
2907 void test6504()
2908 {
2909 for (int i = 0; i < 3; ++i)
2910 {
2911 char[] x2 = "xxx" ~ ['c'];
2912 assert(x2[1] == 'x');
2913 x2[1] = 'q';
2914 }
2915 }
2916
2917 // https://issues.dlang.org/show_bug.cgi?id=8818 regression
2918 void test8818()
2919 {
2920 static bool test()
2921 {
2922 string op1 = "aa";
2923 string op2 = "b";
2924 assert("b" >= "aa");
2925 assert(op2 >= op1);
2926 return true;
2927 }
2928 static assert(test());
2929 assert(test());
2930 }
2931
2932 /************************************************/
2933
2934 struct Test104Node
2935 {
2936 int val;
2937 Test104Node* next;
2938 }
2939
2940 Test104Node* CreateList(int[] arr)
2941 {
2942 if (!arr.length)
2943 return null;
2944 Test104Node* ret = new Test104Node;
2945 ret.val = arr[0];
2946 ret.next = CreateList(arr[1..$]);
2947 return ret;
2948 }
2949
2950 const(Test104Node)* root = CreateList([1, 2, 3, 4, 5]);
2951
2952 void test104()
2953 {
2954 assert(root.val == 1);
2955 assert(root.next.val == 2);
2956 assert(root.next.next.val == 3);
2957 assert(root.next.next.next.val == 4);
2958 assert(root.next.next.next.next.val == 5);
2959 }
2960
2961 /************************************************/
2962
2963 interface ITest105a
2964 {
2965 string test105a() const;
2966 }
2967
2968 class Test105a : ITest105a
2969 {
2970 char a;
2971 int b;
2972 char c = 'C';
2973 int d = 42;
2974 string test105a() const { return "test105a"; }
2975 }
2976
2977 interface ITest105b
2978 {
2979 string test105b() const;
2980 }
2981
2982 class Test105b : Test105a, ITest105b
2983 {
2984 char e;
2985 int f;
2986 this(char _e, int _f, char _a, int _b) pure
2987 {
2988 e = _e;
2989 f = _f;
2990 a = _a;
2991 b = _b;
2992 }
2993 string test105b() const { return "test105b"; }
2994 }
2995
2996 const Test105b t105b = new Test105b('E', 88, 'A', 99);
2997 const Test105a t105a = new Test105b('E', 88, 'A', 99);
2998 const ITest105b t105ib = new Test105b('E', 88, 'A', 99);
2999 const ITest105a t105ia = new Test105b('E', 88, 'A', 99);
3000 __gshared Test105b t105gs = new Test105b('E', 88, 'A', 99);
3001 shared Test105b t105bs = new shared(Test105b)('E', 88, 'A', 99);
3002 immutable Test105b t105bi = new immutable(Test105b)('E', 88, 'A', 99);
3003
3004 void test105()
3005 {
3006 assert(t105b.a == 'A');
3007 assert(t105b.b == 99);
3008 assert(t105b.c == 'C');
3009 assert(t105b.d == 42);
3010 assert(t105b.e == 'E');
3011 assert(t105b.f == 88);
3012 assert(t105b.test105a() == "test105a");
3013 assert(t105b.test105b() == "test105b");
3014
3015 assert(t105a.a == 'A');
3016 assert(t105a.b == 99);
3017 assert(t105a.c == 'C');
3018 assert(t105a.d == 42);
3019 assert(t105a.test105a() == "test105a");
3020
3021 assert(t105ia.test105a() == "test105a");
3022 assert(t105ib.test105b() == "test105b");
3023
3024 assert(t105a.classinfo is Test105b.classinfo);
3025 //t105b.d = -1;
3026 //assert(t105b.d == -1);
3027 //assert(t105a.d == 42);
3028
3029 assert(t105gs.a == 'A');
3030 assert(t105gs.b == 99);
3031 assert(t105gs.c == 'C');
3032 assert(t105gs.d == 42);
3033 assert(t105gs.e == 'E');
3034 assert(t105gs.f == 88);
3035 assert(t105gs.test105a() == "test105a");
3036 assert(t105gs.test105b() == "test105b");
3037
3038 assert(t105bs.a == 'A');
3039 assert(t105bs.b == 99);
3040 assert(t105bs.c == 'C');
3041 assert(t105bs.d == 42);
3042 assert(t105bs.e == 'E');
3043 assert(t105bs.f == 88);
3044
3045 assert(t105bi.a == 'A');
3046 assert(t105bi.b == 99);
3047 assert(t105bi.c == 'C');
3048 assert(t105bi.d == 42);
3049 assert(t105bi.e == 'E');
3050 assert(t105bi.f == 88);
3051 assert(t105bi.test105a() == "test105a");
3052 assert(t105bi.test105b() == "test105b");
3053 }
3054
3055 int bug9938()
3056 {
3057 assert(t105ia.test105a() == "test105a");
3058 return 1;
3059 }
3060
3061 static assert(t105ia.test105a() == "test105a");
3062 static assert(bug9938());
3063
3064 /************************************************/
3065
3066 struct Test106
3067 {
3068 Test106* f;
3069 Test106* s;
3070 }
3071
3072 Test106* ctfe106()
3073 {
3074 auto s = new Test106;
3075 auto s2 = new Test106;
3076 s.f = s2;
3077 s.s = s2;
3078 assert(s.f is s.s);
3079 return s;
3080 }
3081
3082 const(Test106)* t106 = ctfe106();
3083
3084 void test106()
3085 {
3086 assert(t106.f is t106.s);
3087 }
3088
3089 /************************************************/
3090
3091 class Test107
3092 {
3093 Test107 a;
3094 Test107 b;
3095
3096 this()
3097 {
3098 }
3099
3100 this(int)
3101 {
3102 a = new Test107();
3103 b = a;
3104 assert(a is b);
3105 }
3106 }
3107
3108 const Test107 t107 = new Test107(1);
3109
3110 void test107()
3111 {
3112 assert(t107.a is t107.b);
3113 }
3114
3115 /************************************************/
3116
3117 /*
3118 interface Getter
3119 {
3120 int getNum() const;
3121 }
3122
3123 class Test108 : Getter
3124 {
3125 int f;
3126 this(int v) inout
3127 {
3128 f = v;
3129 }
3130
3131 int getNum() const
3132 {
3133 return f;
3134 }
3135 }
3136
3137 enum const(Test108) t108 = new Test108(38);
3138
3139 void test108()
3140 {
3141 const Test108 obj = t108;
3142 assert(obj.classinfo is Test108.classinfo);
3143 assert(obj.f == 38);
3144
3145 const Getter iobj = t108;
3146 assert(iobj.getNum() == 38);
3147 assert((cast(Object)iobj).classinfo is Test108.classinfo);
3148 assert(t108 is t108);
3149 }
3150 */
3151
3152 /***** https://issues.dlang.org/show_bug.cgi?id=5678 *****/
3153
3154 /*
3155 struct Bug5678
3156 {
3157 this(int) {}
3158 }
3159
3160 enum const(Bug5678)* b5678 = new const(Bug5678)(0);
3161
3162 void test5678()
3163 {
3164 assert(b5678 is b5678);
3165 }*/
3166
3167 /************************************************/
3168
3169 class Test109C { this(){ this.c = this; } Test109C c; }
3170 const t109c = new Test109C();
3171
3172 struct Test109S { this(int){ this.s = &this; } Test109S* s; }
3173 const t109s = new Test109S(0);
3174 pragma(msg, t109s); // Make sure there is no infinite recursion.
3175
3176 void test109()
3177 {
3178 assert(t109c.c is t109c);
3179 assert(t109s.s is t109s);
3180 }
3181
3182 /************************************************/
3183
3184 struct Test110f { int f1; Test110s f2; }
3185 struct Test110s { this(int, int, int){} }
3186 auto test110 = [Test110f(1, Test110s(1, 2, 3))];
3187
3188 /************************************************/
3189 // https://issues.dlang.org/show_bug.cgi?id=6907
3190
3191 int test6907()
3192 {
3193 int dtor1;
3194 class C { ~this() { ++dtor1; } }
3195
3196 // delete on Object
3197 { Object o; delete o; }
3198 { scope o = new Object(); }
3199 { Object o = new Object(); delete o; }
3200
3201 // delete on C
3202 { C c; delete c; }
3203 { { scope c = new C(); } assert(dtor1 == 1); }
3204 { { scope Object o = new C(); } assert(dtor1 == 2); }
3205 { C c = new C(); delete c; assert(dtor1 == 3); }
3206 { Object o = new C(); delete o; assert(dtor1 == 4); }
3207
3208 int dtor2;
3209 struct S1 { ~this() { ++dtor2; } }
3210
3211 // delete on S1
3212 { S1* p; delete p; }
3213 { S1* p = new S1(); delete p; assert(dtor2 == 1); }
3214
3215 // delete on S1[]
3216 { S1[] a = [S1(), S1()]; delete a; assert(dtor2 == 3); }
3217
3218 return 1;
3219 }
3220 static assert(test6907());
3221
3222 /************************************************/
3223 // https://issues.dlang.org/show_bug.cgi?id=9023
3224
3225 bool test9023()
3226 {
3227 string[][string] aas;
3228 assert(aas.length == 0);
3229 aas["a"] ~= "anything";
3230 assert(aas.length == 1);
3231 assert(aas["a"] == ["anything"]);
3232 aas["a"] ~= "more";
3233 assert(aas.length == 1);
3234 assert(aas["a"] == ["anything", "more"]);
3235
3236 int[int] aan;
3237 assert(aan.length == 0);
3238 auto x = aan[0]++;
3239 assert(x == 0);
3240 assert(aan.length == 1);
3241 assert(aan[0] == 1);
3242
3243 return true;
3244 }
3245 static assert(test9023());
3246
3247 /************************************************/
3248 // https://issues.dlang.org/show_bug.cgi?id=15817
3249
3250 S[] split15817(S)(S s)
3251 {
3252 size_t istart;
3253 S[] result;
3254
3255 foreach (i, c ; s)
3256 result ~= s[istart .. i];
3257 return result;
3258 }
3259
3260 int test15817()
3261 {
3262 auto targets = `a1`.split15817;
3263 uint[string] counts;
3264 foreach (a; targets)
3265 counts[a]++;
3266 assert(counts == ["":1u, "a":1]);
3267 return 1;
3268 }
3269 static assert(test15817());
3270
3271 /************************************************/
3272
3273 interface IBug9954
3274 {
3275 string foo() const;
3276 }
3277
3278 class Bug9954 : IBug9954
3279 {
3280 string foo() const { return "hello"; }
3281 }
3282
3283 IBug9954 makeIBug9954()
3284 {
3285 return new Bug9954;
3286 }
3287
3288 const IBug9954 b9954 = makeIBug9954();
3289
3290 void test9954()
3291 {
3292 assert(b9954.foo() == "hello");
3293 }
3294
3295 /************************************************/
3296 // https://issues.dlang.org/show_bug.cgi?id=10483
3297
3298 struct Bug10483
3299 {
3300 int[3][4] val;
3301 }
3302
3303 struct Outer10483
3304 {
3305 Bug10483 p = Bug10483(67);
3306 }
3307
3308 int k10483a = Outer10483.init.p.val[2][2]; // ICE(expression.c)
3309
3310 void test10483()
3311 {
3312 int k10483b = Outer10483.init.p.val[2][2]; // Segfault (backend/type.c)
3313 }
3314
3315 /************************************************/
3316
3317 struct S10669 { uint x; }
3318
3319 static const S10669 iid0_10669 = S10669(0);
3320
3321 class C10669
3322 {
3323 static const S10669 iid1_10669 = S10669(1);
3324 };
3325
3326 const S10669 IID0_10669 = iid0_10669;
3327 const S10669 IID1_10669 = C10669.iid1_10669;
3328
3329 /************************************************/
3330
3331 TypeInfo getTi()
3332 {
3333 return typeid(int);
3334 }
3335
3336 auto t112 = getTi();
3337
3338 void test112()
3339 {
3340 assert(t112.toString() == "int");
3341 }
3342
3343 /************************************************/
3344 // https://issues.dlang.org/show_bug.cgi?id=10687
3345
3346 enum Foo10687 : uint { A, B, C, D, E }
3347 immutable uint[5][] m10687 = [[0, 1, 2, 3, 4]];
3348
3349 void test10687()
3350 {
3351 static immutable uint[5] a1 = [0, 1, 2, 3, 4];
3352 auto a2 = cast(immutable(Foo10687[5]))a1;
3353 static a3 = cast(immutable(Foo10687[5]))a1;
3354
3355 auto foos1 = cast(immutable(Foo10687[5][]))m10687;
3356 static foos2 = cast(immutable(Foo10687[5][]))m10687;
3357 }
3358
3359 /************************************************/
3360
3361 void test113()
3362 {
3363 import core.math;
3364
3365 static void compare(real a, real b)
3366 {
3367 assert(fabs(a - b) < 128 * real.epsilon);
3368 }
3369
3370 static if (__traits(compiles, (){ enum real ctval1 = yl2x(3.14, 1); }))
3371 {
3372 enum real ctval1 = yl2x(3.14, 1);
3373 enum real ctval2 = yl2x(2e1500L, 3);
3374 enum real ctval3 = yl2x(1, 5);
3375
3376 real rtval1 = yl2x(3.14, 1);
3377 real rtval2 = yl2x(2e1500L, 3);
3378 real rtval3 = yl2x(1, 5);
3379
3380 compare(ctval1, rtval1);
3381 compare(ctval2, rtval2);
3382 compare(ctval3, rtval3);
3383 }
3384
3385 static if (__traits(compiles, (){ enum real ctval4 = yl2xp1(3.14, 1); }))
3386 {
3387 enum real ctval4 = yl2xp1(3.14, 1);
3388 enum real ctval5 = yl2xp1(2e1500L, 3);
3389 enum real ctval6 = yl2xp1(1, 5);
3390
3391 real rtval4 = yl2xp1(3.14, 1);
3392 real rtval5 = yl2xp1(2e1500L, 3);
3393 real rtval6 = yl2xp1(1, 5);
3394
3395 compare(ctval4, rtval4);
3396 compare(ctval5, rtval5);
3397 compare(ctval6, rtval6);
3398 }
3399 }
3400
3401 /************************************************/
3402 // https://issues.dlang.org/show_bug.cgi?id=14140
3403
3404 struct S14140
3405 {
3406 union
3407 {
3408 float[3][1] A;
3409 float[3] flat;
3410 }
3411
3412 this(in float[] args...)
3413 {
3414 flat[] = args[];
3415 }
3416 }
3417
3418 class C14140
3419 {
3420 union
3421 {
3422 float[3][1] A;
3423 float[3] flat;
3424 }
3425
3426 this(in float[] args...)
3427 {
3428 flat[] = args[];
3429 }
3430 }
3431
3432 immutable s14140 = S14140(0, 1, 0);
3433 const c14140 = new C14140(0, 1, 0);
3434
3435 void test14140()
3436 {
3437 auto s = s14140;
3438 assert(s.flat == [0, 1, 0]);
3439
3440 auto c = c14140;
3441 assert(c.flat == [0, 1, 0]);
3442 }
3443
3444 /************************************************/
3445 // https://issues.dlang.org/show_bug.cgi?id=14862
3446
3447 struct S14862
3448 {
3449 union
3450 {
3451 struct { uint hi, lo; }
3452 ulong data;
3453 }
3454
3455 this(ulong data)
3456 {
3457 this.data = data;
3458 }
3459 }
3460
3461 void test14862()
3462 {
3463 S14862 s14862 = S14862(123UL);
3464 enum S14862 e14862 = S14862(123UL);
3465 static S14862 g14862 = S14862(123UL);
3466
3467 assert(s14862.data == 123UL); // OK
3468 assert(e14862.data == 123UL); // OK
3469 assert(g14862.data == 123UL); // OK <- fail
3470 }
3471
3472 /************************************************/
3473 // https://issues.dlang.org/show_bug.cgi?id=15681
3474
3475 void test15681()
3476 {
3477 static struct A { float value; }
3478
3479 static struct S
3480 {
3481 A[2] values;
3482
3483 this(float)
3484 {
3485 values[0].value = 0;
3486 values[1].value = 1;
3487 }
3488 }
3489
3490 auto s1 = S(1.0f);
3491 assert(s1.values[0].value == 0); // OK
3492 assert(s1.values[1].value == 1); // OK
3493
3494 enum s2 = S(1.0f);
3495 static assert(s2.values[0].value == 0); // OK <- NG
3496 static assert(s2.values[1].value == 1); // OK
3497 assert(s2.values[0].value == 0); // OK <- NG
3498 assert(s2.values[1].value == 1); // OK
3499 }
3500
3501 /************************************************/
3502 // toPrec
3503
3504 void testToPrec()
3505 {
3506 import core.math;
3507
3508 enum real ctpir = 0xc.90fdaa22168c235p-2;
3509 enum double ctpid = 0x1.921fb54442d18p+1;
3510 enum float ctpif = 0x1.921fb6p+1;
3511 static assert(toPrec!float(ctpir) == ctpif);
3512 static assert(toPrec!double(ctpir) == ctpid);
3513 static assert(toPrec!real(ctpir) == ctpir);
3514 static assert(toPrec!float(ctpid) == ctpif);
3515 static assert(toPrec!double(ctpid) == ctpid);
3516 static assert(toPrec!real(ctpid) == ctpid);
3517 static assert(toPrec!float(ctpif) == ctpif);
3518 static assert(toPrec!double(ctpif) == ctpif);
3519 static assert(toPrec!real(ctpif) == ctpif);
3520
3521 assert(toPrec!float(ctpir) == ctpif);
3522 assert(toPrec!double(ctpir) == ctpid);
3523 assert(toPrec!real(ctpir) == ctpir);
3524 assert(toPrec!float(ctpid) == ctpif);
3525 assert(toPrec!double(ctpid) == ctpid);
3526 assert(toPrec!real(ctpid) == ctpid);
3527 assert(toPrec!float(ctpif) == ctpif);
3528 assert(toPrec!double(ctpif) == ctpif);
3529 assert(toPrec!real(ctpif) == ctpif);
3530
3531 static real rtpir = 0xc.90fdaa22168c235p-2;
3532 static double rtpid = 0x1.921fb54442d18p+1;
3533 static float rtpif = 0x1.921fb6p+1;
3534 assert(toPrec!float(rtpir) == rtpif);
3535 assert(toPrec!double(rtpir) == rtpid);
3536 assert(toPrec!real(rtpir) == rtpir);
3537 assert(toPrec!float(rtpid) == rtpif);
3538 assert(toPrec!double(rtpid) == rtpid);
3539 assert(toPrec!real(rtpid) == rtpid);
3540 assert(toPrec!float(rtpif) == rtpif);
3541 assert(toPrec!double(rtpif) == rtpif);
3542 assert(toPrec!real(rtpif) == rtpif);
3543 }
3544
3545 /************************************************/
3546
3547 auto test20366()
3548 {
3549 const(char)[] s = ['h', 'e', 'l', '\xef', '\xbd', '\x8c', 'o'];
3550
3551 foreach_reverse (dchar c; s)
3552 {
3553 }
3554
3555 return true;
3556 }
3557 static assert(test20366());
3558
3559 /************************************************/
3560
3561 bool test20400()
3562 {
3563 char[] s = cast(char[])"1234";
3564 char[] ret = s[2 .. $];
3565 ret.length += 1;
3566 ret[$-1] = '5';
3567 assert(ret == "345");
3568
3569 return true;
3570 }
3571 static assert(test20400());
3572
3573 /************************************************/
3574 // https://issues.dlang.org/show_bug.cgi?id=21878
3575
3576 struct A21878
3577 {
3578 int i;
3579 ref inout(int) opIndex(size_t idx) inout return { return i; }
3580 }
3581
3582 struct B21878
3583 {
3584 A21878[1] a;
3585 ref inout(int) opIndex(size_t idx) inout return { return a[0][idx]; }
3586 }
3587
3588 bool ctfeFunc21878()
3589 {
3590 A21878 a;
3591 a[0] = 42;
3592 assert(a[0] == 42); // OK
3593
3594 B21878 b;
3595 b[0] = 42;
3596 assert(b[0] == 42); // OK <- fails
3597
3598 return true;
3599 }
3600
3601 void test21878()
3602 {
3603 enum eval = ctfeFunc21878();
3604 ctfeFunc21878(); // succeeds at runtime
3605 }
3606
3607 /************************************************/
3608
3609 int main()
3610 {
3611 test1();
3612 test2();
3613 test3();
3614 test4();
3615 test5();
3616 test6();
3617 test7();
3618 test8();
3619 test9();
3620 test10();
3621 test11();
3622 test12();
3623 test13();
3624 test14();
3625 test15();
3626 test16();
3627 test17();
3628 test18();
3629 test19();
3630 test20();
3631 test21();
3632 test22();
3633 test23();
3634 test24();
3635 test25();
3636 test26();
3637 test27();
3638 test28();
3639 test29();
3640 test30();
3641 test31();
3642 test32();
3643 test33();
3644 test34();
3645 test35();
3646 test36();
3647 test37();
3648 test38();
3649 test39();
3650 test40();
3651 test41();
3652 test42();
3653 test43();
3654 test44();
3655 test45();
3656 test46();
3657 test47();
3658 test48();
3659 test49();
3660 test50();
3661 test51();
3662 test52();
3663 test53();
3664 test54();
3665 test55();
3666 test56();
3667 test57();
3668 test58();
3669 test59();
3670 test60();
3671 test61();
3672 test62();
3673 test63();
3674 test64();
3675 test65();
3676 test66();
3677 test67();
3678 test68();
3679 test69();
3680 test70();
3681 test71();
3682 test72();
3683 test73();
3684 test74();
3685 test75();
3686 test76();
3687 test77();
3688 test78();
3689 test79();
3690 test80();
3691 test81();
3692 test82();
3693 test83();
3694 test84();
3695 test85();
3696 test86();
3697 test87();
3698 test88();
3699 test89();
3700 test90();
3701 test91();
3702 test92();
3703 test93();
3704 test94();
3705 test95();
3706 test96();
3707 test97();
3708 test98();
3709 test99();
3710 test100();
3711 test101();
3712 test102();
3713 test103();
3714 test104();
3715 test105();
3716 test106();
3717 test107();
3718 //test108();
3719 test109();
3720 test112();
3721 test113();
3722 test6439();
3723 test6504();
3724 test8818();
3725 test6907();
3726 test9023();
3727 test15817();
3728 test9954();
3729 test14140();
3730 test14862();
3731 test15681();
3732 test20366();
3733 test20400();
3734 test21878();
3735
3736 printf("Success\n");
3737 return 0;
3738 }
3739