1 // PERMUTE_ARGS: -inline
2 /*
3 TEST_OUTPUT:
4 ---
5 compilable/interpret3.d(2914): Deprecation: `case` variables have to be `const` or `immutable`
6 compilable/interpret3.d(6351): Deprecation: identity comparison of static arrays implicitly coerces them to slices, which are compared by reference
7 ---
8 */
9 
compiles(int T)10 template compiles(int T)
11 {
12     bool compiles = true;
13 }
14 
15 alias TypeTuple(T...) = T;
16 
17 /**************************************************
18     3901 Arbitrary struct assignment, ref return
19 **************************************************/
20 
21 struct ArrayRet
22 {
23     int x;
24 }
25 
arrayRetTest(int z)26 int arrayRetTest(int z)
27 {
28     ArrayRet[6] w;
29     int q = (w[3].x = z);
30     return q;
31 }
32 static assert(arrayRetTest(51) == 51);
33 
34 // Bugzilla 3842 -- must not segfault
ice3842(int z)35 int ice3842(int z)
36 {
37     ArrayRet w;
38     return arrayRetTest((*(&w)).x);
39 }
40 static assert(true || is(typeof(compiles!(ice3842(51)))));
41 
arrayret2()42 int arrayret2()
43 {
44     int[5] a;
45     int[3] b;
46     b[] = a[1 .. $-1] = 5;
47     return b[1];
48 }
49 static assert(arrayret2() == 5);
50 
51 struct DotVarTest
52 {
53     ArrayRet z;
54 }
55 
56 struct DotVarTest2
57 {
58     ArrayRet z;
59     DotVarTest p;
60 }
61 
dotvar1()62 int dotvar1()
63 {
64     DotVarTest w;
65     w.z.x = 3;
66     return w.z.x;
67 }
68 static assert(dotvar1() == 3);
69 
dotvar2()70 int dotvar2()
71 {
72     DotVarTest2[4] m;
73     m[2].z.x = 3;
74     m[1].p.z.x = 5;
75     return m[2].z.x + 7;
76 }
77 static assert(dotvar2() == 10);
78 
79 
80 struct RetRefStruct
81 {
82     int x;
83     char c;
84 }
85 
86 // Return value reference tests, for D2 only.
87 
reffunc1(ref RetRefStruct a)88 ref RetRefStruct reffunc1(ref RetRefStruct a)
89 {
90     int y = a.x;
91     return a;
92 }
93 
reffunc2(ref RetRefStruct a)94 ref RetRefStruct reffunc2(ref RetRefStruct a)
95 {
96     RetRefStruct z = a;
97     return reffunc1(a);
98 }
99 
reffunc7(ref RetRefStruct aa)100 ref int reffunc7(ref RetRefStruct aa)
101 {
102     return reffunc1(aa).x;
103 }
104 
reffunc3(ref int a)105 ref int reffunc3(ref int a)
106 {
107     return a;
108 }
109 
110 struct RefTestStruct
111 {
112     RetRefStruct r;
113 
reffunc4RefTestStruct114     ref RefTestStruct reffunc4(ref RetRefStruct[3] a)
115     {
116         return this;
117     }
118 
reffunc6RefTestStruct119     ref int reffunc6()
120     {
121         return this.r.x;
122     }
123 }
124 
reffunc5(ref RetRefStruct[3]a)125 ref RetRefStruct reffunc5(ref RetRefStruct[3] a)
126 {
127     int t = 1;
128     for (int i = 0; i < 10; ++i)
129     {
130         if (i == 7)
131             ++t;
132     }
133     return a[reffunc3(t)];
134 }
135 
retRefTest1()136 int retRefTest1()
137 {
138     RetRefStruct b = RetRefStruct(0, 'a');
139     reffunc1(b).x = 3;
140     return b.x - 1;
141 }
142 
retRefTest2()143 int retRefTest2()
144 {
145     RetRefStruct b = RetRefStruct(0, 'a');
146     reffunc2(b).x = 3;
147     RetRefStruct[3] z;
148     RefTestStruct w;
149     w.reffunc4(z).reffunc4(z).r.x = 4;
150     assert(w.r.x == 4);
151     w.reffunc6() = 218;
152     assert(w.r.x == 218);
153     z[2].x = 3;
154     int q = 4;
155     int u = reffunc5(z).x + reffunc3(q);
156     assert(u == 7);
157     reffunc5(z).x += 7;
158     assert(z[2].x == 10);
159     RetRefStruct m = RetRefStruct(7, 'c');
160     m.x = 6;
161     reffunc7(m) += 3;
162     assert(m.x == 9);
163     return b.x - 1;
164 }
165 
retRefTest3()166 int retRefTest3()
167 {
168     RetRefStruct b = RetRefStruct(0, 'a');
169     auto deleg = function (RetRefStruct a){ return a; };
170     typeof(deleg)[3] z;
171     z[] = deleg;
172     auto y = deleg(b).x + 27;
173     b.x = 5;
174     assert(y == 27);
175     y = z[1](b).x + 22;
176     return y - 1;
177 }
178 
retRefTest4()179 int retRefTest4()
180 {
181     RetRefStruct b = RetRefStruct(0, 'a');
182     reffunc3(b.x) = 218;
183     assert(b.x == 218);
184     return b.x;
185 }
186 
187 static assert(retRefTest1() == 2);
188 static assert(retRefTest2() == 2);
189 static assert(retRefTest3() == 26);
190 static assert(retRefTest4() == 218);
191 
192 /**************************************************
193     Bug 7887 assign to returned reference
194 **************************************************/
195 
test7887()196 bool test7887()
197 {
198     ref int f(ref int x) { return x; }
199     int a;
200     f(a) = 42;
201     return (a == 42);
202 }
203 static assert(test7887());
204 
205 /**************************************************
206     Bug 7473 struct non-ref
207 **************************************************/
208 
209 struct S7473
210 {
211     int i;
212 }
213 
214 static assert({
215     S7473 s = S7473(1);
216     assert(s.i == 1);
217     bug7473(s);
218     assert(s.i == 1);
219     return true;
220 }());
221 
bug7473(S7473 s)222 void bug7473(S7473 s)
223 {
224     s.i = 2;
225 }
226 
227 struct S7473b
228 {
229     S7473 m;
230 }
231 
232 static assert({
233     S7473b s = S7473b(S7473(7));
234     assert(s.m.i == 7);
235     bug7473b(s);
236     assert(s.m.i == 7);
237     return true;
238 }());
239 
bug7473b(S7473b s)240 void bug7473b(S7473b s)
241 {
242     s.m.i = 2;
243 }
244 
245 /**************************************************
246     Bug 4389
247 **************************************************/
248 
bug4389()249 int bug4389()
250 {
251     string s;
252     dchar c = '\u2348';
253     s ~= c;
254     assert(s.length == 3);
255     dchar d = 'D';
256     s ~= d;
257     assert(s.length == 4);
258     s = "";
259     s ~= c;
260     assert(s.length == 3);
261     s ~= d;
262     assert(s.length == 4);
263     string z;
264     wchar w = '\u0300';
265     z ~= w;
266     assert(z.length == 2);
267     z = "";
268     z ~= w;
269     assert(z.length == 2);
270     return 1;
271 }
272 
273 static assert(bug4389());
274 
275 // ICE(constfold.c)
ice4389()276 int ice4389()
277 {
278     string s;
279     dchar c = '\u2348';
280     s ~= c;
281     s = s ~ "xxx";
282    return 1;
283 }
284 
285 static assert(ice4389());
286 
287 // ICE(expression.c)
ice4390()288 string ice4390()
289 {
290     string s;
291     dchar c = '`';
292     s ~= c;
293     s ~= c;
294    return s;
295 }
296 
297 static assert(mixin(ice4390()) == ``);
298 
299 // bug 5248 (D1 + D2)
300 struct Leaf5248
301 {
Compile_not_ovloadedLeaf5248302     string Compile_not_ovloaded()
303     {
304         return "expression";
305     }
306 }
307 struct Matrix5248
308 {
309     Leaf5248 Right;
310 
Compile()311     string Compile()
312     {
313         return Right.Compile_not_ovloaded();
314     }
315 };
316 
317 static assert(Matrix5248().Compile());
318 
319 /**************************************************
320     4837   >>>=
321 **************************************************/
322 
bug4837()323 bool bug4837()
324 {
325     ushort x = 0x89AB;
326     x >>>= 4;
327     assert(x == 0x89A);
328     byte y = 0x7C;
329     y >>>= 2;
330     assert(y == 0x1F);
331     return true;
332 }
333 
334 static assert(bug4837());
335 
336 /**************************************************
337   10252 shift out of range
338 **************************************************/
339 
lshr10252(int shift)340 int lshr10252(int shift)
341 {
342      int a = 5;
343      return a << shift;
344 }
345 
rshr10252(int shift)346 int rshr10252(int shift)
347 {
348      int a = 5;
349      return a >> shift;
350 }
351 
ushr10252(int shift)352 int ushr10252(int shift)
353 {
354      int a = 5;
355      return a >>> shift;
356 }
357 
358 static assert( is(typeof(compiles!(lshr10252( 4)))));
359 static assert(!is(typeof(compiles!(lshr10252(60)))));
360 static assert( is(typeof(compiles!(rshr10252( 4)))));
361 static assert(!is(typeof(compiles!(rshr10252(80)))));
362 static assert( is(typeof(compiles!(ushr10252( 2)))));
363 static assert(!is(typeof(compiles!(ushr10252(60)))));
364 
365 /**************************************************
366   1982 CTFE null problems
367 **************************************************/
368 
369 enum a1982 = [1, 2, 3];
370 static assert(a1982 !is null);
371 
foo1982()372 string foo1982() { return null; }
373 static assert(foo1982() is null);
374 static assert(!foo1982().length);
375 
376 static assert(null is null);
377 
378 /**************************************************
379   7988 CTFE return values should be allowed in compile-time expressions
380 **************************************************/
381 
this()382 class X7988 { int y; this() { y = 2; } }
383 static assert((new X7988).y == 2);
384 
385 /**************************************************
386   8253 ICE: calling of member function of non-CTFE class variable
387 **************************************************/
388 
389 class Bug8253
390 {
j()391     bool j()
392     {
393         return true;
394     }
395 }
396 Bug8253 m8253;
397 static assert(!is(typeof(compiles!(m8253.j()))));
398 
399 /**************************************************
400   8285 Issue with slice returned from CTFE function
401 **************************************************/
402 
foo8285()403 string foo8285()
404 {
405     string s = "ab";
406     return s[0 .. $];
407 }
408 
T8285b(string s)409 template T8285b(string s) { }
410 
T8285a()411 template T8285a()
412 {
413     enum s = foo8285();
414     alias T8285b!(s) t2;
415 }
416 
bar8285()417 int bar8285()
418 {
419     alias T8285a!() t1;
420     return 0;
421 }
422 
baz8285(int x)423 int baz8285(int x)
424 {
425     return 0;
426 }
427 
428 static assert(baz8285(bar8285()) == 0);
429 
430 // test case 2
431 
xbar8285()432 string xbar8285()
433 {
434     string s = "ab";
435     return s[0 .. $];
436 }
437 
xT8285a()438 template xT8285a()
439 {
440     enum xT8285a = xbar8285()[0 .. $];
441 }
442 
xbaz8285()443 string xbaz8285()
444 {
445     return xT8285a!();
446 }
447 
xfoo8285(string s)448 string xfoo8285(string s)
449 {
450     return s;
451 }
452 
453 static assert(xfoo8285(xbaz8285()) == "ab");
454 
455 /**************************************************
456   'this' parameter bug revealed during refactoring
457 **************************************************/
458 
thisbug1(int x)459 int thisbug1(int x) { return x; }
460 
461 struct ThisBug1
462 {
463     int m = 1;
wutThisBug1464     int wut()
465     {
466         return thisbug1(m);
467     }
468 }
469 
thisbug2()470 int thisbug2()
471 {
472     ThisBug1 spec;
473     return spec.wut();
474 }
475 
476 static assert(thisbug2());
477 
478 /**************************************************
479    6972 ICE with cast()cast()assign
480 **************************************************/
481 
bug6972()482 int bug6972()
483 {
484     ubyte n = 6;
485     n /= 2u;
486     return n;
487 }
488 static assert(bug6972() == 3);
489 
490 /**************************************************
491     Bug 6164
492 **************************************************/
493 
bug6164()494 size_t bug6164()
495 {
496     int[] ctfe2(int n)
497     {
498         int[] r = [];
499         if (n != 0)
500             r ~= [1] ~ ctfe2(n - 1);
501         return r;
502     }
503     return ctfe2(2).length;
504 }
505 static assert(bug6164() == 2);
506 
507 /**************************************************
508     Interpreter code coverage tests
509 **************************************************/
510 
cov1(int a)511 int cov1(int a)
512 {
513     a %= 15382;
514     a /= 5;
515     a = ~ a;
516     bool c = (a == 0);
517     bool b = true && c;
518     assert(b == 0);
519     b = false && c;
520     assert(b == 0);
521     b = false || c;
522     assert(b == 0);
523     a ^= 0x45349;
524     a = ~ a;
525     a &= 0xFF3F;
526     a >>>= 1;
527     a = a ^ 0x7393;
528     a = a >> 1;
529     a = a >>> 1;
530     a = a | 0x010101;
531     return a;
532 }
533 static assert(cov1(534564) == 71589);
534 
cov2()535 int cov2()
536 {
537     int i = 0;
538     do
539     {
540         goto DOLABEL;
541     DOLABEL:
542         if (i != 0)
543         {
544             goto IFLABEL;
545     IFLABEL:
546             switch(i)
547             {
548             case 3:
549                 break;
550             case 6:
551                 goto SWITCHLABEL;
552     SWITCHLABEL:
553                 i = 27;
554                 goto case 3;
555             default:
556                 assert(0);
557             }
558             return i;
559         }
560         i = 6;
561     } while(true);
562     return 88; // unreachable
563 }
564 static assert(cov2() == 27);
565 
CovTuple(T...)566 template CovTuple(T...)
567 {
568     alias T CovTuple;
569 }
570 
571 alias CovTuple!(int, long) TCov3;
572 
cov3(TCov3 t)573 int cov3(TCov3 t)
574 {
575     TCov3 s;
576     s = t;
577     assert(s[0] == 1);
578     assert(s[1] == 2);
579     return 7;
580 }
581 static assert(cov3(1, 2) == 7);
582 
badassert1(int z)583 int badassert1(int z)
584 {
585     assert(z == 5, "xyz");
586     return 1;
587 }
588 
badslice1(int[]z)589 size_t badslice1(int[] z)
590 {
591     return z[0 .. 3].length;
592 }
593 
badslice2(int[]z)594 size_t badslice2(int[] z)
595 {
596     return z[0 .. badassert1(1)].length;
597 }
598 
badslice3(int[]z)599 size_t badslice3(int[] z)
600 {
601     return z[badassert1(1) .. 2].length;
602 }
603 
604 static assert(!is(typeof(compiles!(badassert1(67)))));
605 static assert( is(typeof(compiles!(badassert1(5)))));
606 static assert(!is(typeof(compiles!(badslice1([1,2])))));
607 static assert(!is(typeof(compiles!(badslice2([1,2])))));
608 static assert(!is(typeof(compiles!(badslice3([1,2,3])))));
609 
610 /*******************************************/
611 
bug7894()612 int bug7894()
613 {
614     for (int k = 0; k < 2; ++k)
615     {
616         goto Lagain;
617 Lagain:
618         ;
619     }
620     int m = 1;
621     do
622     {
623         ++m;
624         goto Ldo;
625 Ldo: ;
626     } while (m < 3);
627     assert(m == 3);
628 
629     return 1;
630 }
631 static assert(bug7894());
632 
633 /*******************************************/
634 
bug5524(int x,int[]more...)635 size_t bug5524(int x, int[] more...)
636 {
637     int[0] zz;
638     assert(zz.length == 0);
639     return 7 + more.length + x;
640 }
641 static assert(bug5524(3) == 10);
642 
643 
644 // 5722
645 
646 static assert(("" ~ "\&copy;"[0]).length == 1);
647 const char[] null5722 = null;
648 static assert((null5722 ~ "\&copy;"[0]).length == 1);
649 static assert(("\&copy;"[0] ~ null5722).length == 1);
650 
651 /*******************************************
652  * Tests for CTFE Array support.
653  * Including bugs 1330, 3801, 3835, 4050,
654  * 4051, 5147, and major functionality
655  *******************************************/
656 
bug1330StringIndex()657 char[] bug1330StringIndex()
658 {
659     char[] blah = "foo".dup;
660     assert(blah == "foo");
661     char[] s = blah[0 .. 2];
662     blah[0] = 'h';
663     assert(s == "ho");
664     s[0] = 'm';
665     return blah;
666 }
667 static assert(bug1330StringIndex() == "moo");
668 static assert(bug1330StringIndex() == "moo"); // check we haven't clobbered any string literals
669 
bug1330ArrayIndex()670 int[] bug1330ArrayIndex()
671 {
672     int[] blah = [1,2,3];
673     int[] s = blah;
674     s = blah[0 .. 2];
675     int z = blah[0] = 6;
676     assert(z == 6);
677     assert(blah[0] == 6);
678     assert(s[0] == 6);
679     assert(s == [6, 2]);
680     s[1] = 4;
681     assert(z == 6);
682     return blah;
683 }
684 static assert(bug1330ArrayIndex() == [6, 4, 3]);
685 static assert(bug1330ArrayIndex() == [6, 4, 3]); // check we haven't clobbered any literals
686 
bug1330StringSliceAssign()687 char[] bug1330StringSliceAssign()
688 {
689     char[] blah = "food".dup;
690     assert(blah == "food");
691     char[] s = blah[1 .. 4];
692     blah[0 .. 2] = "hc";
693     assert(s == "cod");
694     s[0 .. 2] = ['a', 'b'];   // Mix string + array literal
695     assert(blah == "habd");
696     s[0 .. 2] = "mq";
697     return blah;
698 }
699 static assert(bug1330StringSliceAssign() == "hmqd");
700 static assert(bug1330StringSliceAssign() == "hmqd");
701 
bug1330ArraySliceAssign()702 int[] bug1330ArraySliceAssign()
703 {
704     int[] blah = [1, 2, 3, 4];
705     int[] s = blah[1 .. 4];
706     blah[0 .. 2] = [7, 9];
707     assert(s == [9, 3, 4]);
708     s[0 .. 2] = [8, 15];
709     return blah;
710 }
711 static assert(bug1330ArraySliceAssign() == [7, 8, 15, 4]);
712 
bug1330ArrayBlockAssign()713 int[] bug1330ArrayBlockAssign()
714 {
715     int[] blah = [1, 2, 3, 4, 5];
716     int[] s = blah[1 .. 4];
717     blah[0 .. 2] = 17;
718     assert(s == [17, 3, 4]);
719     s[0 .. 2] = 9;
720     return blah;
721 }
722 static assert(bug1330ArrayBlockAssign() == [17, 9, 9, 4, 5]);
723 
bug1330StringBlockAssign()724 char[] bug1330StringBlockAssign()
725 {
726     char[] blah = "abcde".dup;
727     char[] s = blah[1 .. 4];
728     blah[0 .. 2] = 'x';
729     assert(s == "xcd");
730     s[0 .. 2] = 'y';
731     return blah;
732 }
733 static assert(bug1330StringBlockAssign() == "xyyde");
734 
assignAA(int x)735 int assignAA(int x)
736 {
737     int[int] aa;
738     int[int] cc = aa;
739     assert(cc.values.length == 0);
740     assert(cc.keys.length == 0);
741     aa[1] = 2;
742     aa[x] = 6;
743     int[int] bb = aa;
744     assert(bb.keys.length == 2);
745     assert(cc.keys.length == 0); // cc is not affected to aa, because it is null
746     aa[500] = 65;
747     assert(bb.keys.length == 3); // but bb is affected by changes to aa
748     return aa[1] + aa[x];
749 }
750 static assert(assignAA(12) == 8);
751 
Compileable(int z)752 template Compileable(int z) { bool OK; }
753 
arraybounds(int j,int k)754 int arraybounds(int j, int k)
755 {
756     int[] xxx = [1, 2, 3, 4, 5];
757     int[] s = xxx[1 .. $];
758     s = s[j .. k]; // slice of slice
759     return s[$ - 1];
760 }
761 static assert(!is(typeof(Compileable!(arraybounds(1, 14)))));
762 static assert(!is(typeof(Compileable!(arraybounds(15, 3)))));
763 static assert(arraybounds(2, 4) == 5);
764 
arraybounds2(int j,int k)765 int arraybounds2(int j, int k)
766 {
767     int[] xxx = [1, 2, 3, 4, 5];
768     int[] s = xxx[j .. k]; // direct slice
769     return 1;
770 }
771 static assert(!is(typeof(Compileable!(arraybounds2(1, 14)))));
772 static assert(!is(typeof(Compileable!(arraybounds2(15, 3)))));
773 static assert(arraybounds2(2, 4) == 1);
774 
bug5147a()775 int bug5147a()
776 {
777     int[1][2] a = 37;
778     return a[0][0];
779 }
780 static assert(bug5147a() == 37);
781 
bug5147b()782 int bug5147b()
783 {
784     int[4][2][3][17] a = 37;
785     return a[0][0][0][0];
786 }
787 static assert(bug5147b() == 37);
788 
setlen()789 int setlen()
790 {
791     int[][] zzz;
792     zzz.length = 2;
793     zzz[0].length = 10;
794     assert(zzz.length == 2);
795     assert(zzz[0].length == 10);
796     assert(zzz[1].length == 0);
797     return 2;
798 }
799 static assert(setlen() == 2);
800 
bug5147()801 int[1][1] bug5147()
802 {
803     int[1][1] a = 1;
804     return a;
805 }
806 static assert(bug5147() == [[1]]);
807 
808 enum int[1][1] enum5147 = bug5147();
809 static assert(enum5147 == [[1]]);
810 
811 immutable int[1][1] bug5147imm = bug5147();
812 
813 // Index referencing
indexref1()814 int[2][2] indexref1()
815 {
816     int[2][2] a = 2;
817     a[0] = 7;
818 
819     int[][] b = [null, null];
820     b[0 .. $] = a[0][0 .. 2];
821     assert(b[0][0] == 7);
822     assert(b[0][1] == 7);
823     int[] w;
824     w = a[0];
825     assert(w[0] == 7);
826     w[0 .. $] = 5;
827     assert(a[0] != [7, 7]);
828     assert(a[0] == [5, 5]);
829     assert(b[0] == [5, 5]);
830     return a;
831 }
indexref2()832 int[2][2] indexref2()
833 {
834     int[2][2] a = 2;
835     a[0] = 7;
836 
837     int[][2] b = null;
838     b[0 .. $] = a[0];
839     assert(b[0][0] == 7);
840     assert(b[0][1] == 7);
841     assert(b == [[7, 7], [7, 7]]);
842     int[] w;
843     w = a[0];
844     assert(w[0] == 7);
845     w[0 .. $] = 5;
846     assert(a[0] != [7, 7]);
847     assert(a[0] == [5, 5]);
848     assert(b[0] == [5, 5]);
849     return a;
850 }
indexref3()851 int[2][2] indexref3()
852 {
853     int[2][2] a = 2;
854     a[0]=7;
855 
856     int[][2] b = [null, null];
857     b[0 .. $] = a[0];
858     assert(b[0][0] == 7);
859     assert(b[0][1] == 7);
860     int[] w;
861     w = a[0];
862     assert(w[0] == 7);
863     w[0 .. $] = 5;
864     assert(a[0] != [7, 7]);
865     assert(a[0] == [5, 5]);
866     assert(b[0] == [5, 5]);
867     return a;
868 }
indexref4()869 int[2][2] indexref4()
870 {
871     int[2][2] a = 2;
872     a[0] = 7;
873 
874     int[][2] b =[[1, 2, 3], [1, 2, 3]]; // wrong code
875     b[0] = a[0];
876     assert(b[0][0] == 7);
877     assert(b[0][1] == 7);
878     int[] w;
879     w = a[0]; //[0 .. $];
880     assert(w[0] == 7);
881     w[0 .. $] = 5;
882     assert(a[0] != [7, 7]);
883     assert(a[0] == [5, 5]);
884     assert(b[0] == [5, 5]);
885     return a;
886 }
887 static assert(indexref1() == [[5, 5], [2, 2]]);
888 static assert(indexref2() == [[5, 5], [2, 2]]);
889 static assert(indexref3() == [[5, 5], [2, 2]]);
890 static assert(indexref4() == [[5, 5], [2, 2]]);
891 
staticdynamic()892 int staticdynamic()
893 {
894     int[2][1] a = 2;
895     assert(a == [[2, 2]]);
896 
897     int[][1] b = a[0][0 .. 1];
898     assert(b[0] == [2]);
899     auto k = b[0];
900     auto m = a[0][0 .. 1];
901     assert(k == [2]);
902     assert(m == k);
903     return 0;
904 }
905 static assert(staticdynamic() == 0);
906 
chainassign()907 int chainassign()
908 {
909     int[4] x = 6;
910     int[] y = new int[4];
911     auto k = (y[] = (x[] = 2));
912     return k[0];
913 }
914 static assert(chainassign() == 2);
915 
916 // index assignment
917 struct S3801
918 {
919     char c;
920     int[3] arr;
921 
thisS3801922     this(int x, int y)
923     {
924         c = 'x';
925         arr[0] = x;
926         arr[1] = y;
927     }
928 }
929 
bug3801()930 int bug3801()
931 {
932     S3801 xxx = S3801(17, 67);
933     int[] w = xxx.arr;
934     xxx.arr[1] = 89;
935     assert(xxx.arr[0] == 17);
936     assert(w[1] == 89);
937     assert(w == [17, 89, 0]);
938     return xxx.arr[1];
939 }
940 
941 enum : S3801 { bug3801e = S3801(17, 18) }
942 static assert(bug3801e.arr == [17, 18, 0]);
943 
944 immutable S3801 bug3801u = S3801(17, 18);
945 static assert(bug3801u.arr == [17, 18, 0]);
946 static assert(bug3801() == 89);
947 
bug3835()948 int bug3835()
949 {
950     int[4] arr;
951     arr[] = 19;
952     arr[0] = 4;
953     int kk;
954     foreach (ref el; arr)
955     {
956         el += 10;
957         kk = el;
958     }
959     assert(arr[2] == 29);
960     arr[0] += 3;
961     return arr[0];
962 }
963 static assert(bug3835() == 17);
964 
bug5852(const (string)s)965 auto bug5852(const(string) s)
966 {
967     string[] r;
968     r ~= s;
969     assert(r.length == 1);
970     return r[0].length;
971 }
972 static assert(bug5852("abc") == 3);
973 
974 // 7217
975 
976 struct S7217 { int[] arr; }
977 
f7217()978 bool f7217()
979 {
980     auto s = S7217();
981     auto t = s.arr;
982     return true;
983 }
984 static assert(f7217());
985 
986 /*******************************************
987     Set array length
988 *******************************************/
989 
990 static assert(
991 {
992     struct W { int[] z; }
993     W w;
994     w.z.length = 2;
995     assert(w.z.length == 2);
996     w.z.length = 6;
997     assert(w.z.length == 6);
998     return true;
999 }());
1000 
1001 // 7185 char[].length = n
1002 
bug7185()1003 bool bug7185()
1004 {
1005     auto arr = new char[2];
1006     auto arr2 = new char[2];
1007     arr2[] = "ab";
1008     arr.length = 1;
1009     arr2.length = 7;
1010     assert(arr.length == 1);
1011     assert(arr2.length == 7);
1012     assert(arr2[0 .. 2] == "ab");
1013     return true;
1014 }
1015 static assert(bug7185());
1016 
bug9908()1017 bool bug9908()
1018 {
1019     static const int[3] sa = 1;
1020     return sa == [1, 1, 1];
1021 }
1022 static assert(bug9908());
1023 
1024 /*******************************************
1025     6934
1026 *******************************************/
1027 
1028 struct Struct6934
1029 {
1030     int[] x = [1, 2];
1031 }
1032 
bar6934(ref int[]p)1033 void bar6934(ref int[] p)
1034 {
1035     p[0] = 12;
1036     assert(p[0] == 12);
1037     p[0 .. 1] = 17;
1038     assert(p[0] == 17);
1039     p = p[1 .. $];
1040 }
1041 
bug6934()1042 int bug6934()
1043 {
1044     Struct6934 q;
1045     bar6934(q.x);
1046     int[][] y = [[2, 5], [3, 6, 8]];
1047     bar6934(y[0]);
1048     return 1;
1049 }
1050 static assert(bug6934());
1051 
1052 /*******************************************
1053              Bug 5671
1054 *******************************************/
1055 
1056 static assert(['a', 'b'] ~ "c" == "abc");
1057 
1058 /*******************************************
1059       8624
1060 *******************************************/
1061 
evil8624()1062 int evil8624()
1063 {
1064     long  m =  0x1_0000_0000L;
1065     assert(m != 0);
1066     long[] a = [0x1_0000_0000L];
1067     long[] b = [0x4_0000_0000L];
1068     assert(a[] != b[]);
1069     return 1;
1070 }
1071 static assert(evil8624());
1072 
1073 /*******************************************
1074         8644 array literal >,<
1075 *******************************************/
1076 
bug8644()1077 int bug8644()
1078 {
1079     auto m = "a";
1080     auto z = ['b'];
1081     auto c = "b7";
1082     auto d = ['b', '6'];
1083     assert(m < z);
1084     assert(z > m);
1085     assert(z <= c);
1086     assert(c > z);
1087     assert(c > d);
1088     assert(d >= d);
1089     return true;
1090 }
1091 static assert(bug8644());
1092 
1093 /*******************************************
1094         Bug 6159
1095 *******************************************/
1096 
1097 struct A6159 {}
1098 
1099 static assert({ return A6159.init is A6159.init; }());
1100 static assert({ return [1] is [1]; }());
1101 
1102 /*******************************************
1103         Bug 5685
1104 *******************************************/
1105 
bug5685()1106 string bug5685()
1107 {
1108     return "xxx";
1109 }
1110 struct Bug5865
1111 {
test1Bug58651112     void test1()
1113     {
1114         enum file2 = (bug5685())[0 .. $];
1115     }
1116 }
1117 
1118 /*******************************************
1119     6235 - Regression ICE on $ in template
1120 *******************************************/
1121 
Bug6235(R)1122 struct Bug6235(R)
1123 {
1124     enum XXX = is(typeof(R.init[0 .. $]) : const ubyte[]);
1125 }
1126 
1127 Bug6235!(ubyte[]) bug6235;
1128 
1129 /*******************************************
1130     8673 ICE
1131 *******************************************/
1132 
1133 enum dollar8673 = [0][(() => $ - 1)()];
1134 
1135 /*******************************************
1136         Bug 5840
1137 *******************************************/
1138 
1139 struct Bug5840
1140 {
1141     string g;
1142     int w;
1143 }
1144 
bug5840(int u)1145 int bug5840(int u)
1146 {
1147     // check for clobbering
1148     Bug5840 x = void;
1149     x.w = 4;
1150     x.g = "3gs";
1151     if (u == 1)
1152         bug5840(2);
1153     if (u == 2)
1154     {
1155         x.g = "abc";
1156         x.w = 3465;
1157     }
1158     else
1159     {
1160         assert(x.g == "3gs");
1161         assert(x.w == 4);
1162     }
1163     return 56;
1164 }
1165 static assert(bug5840(1) == 56);
1166 
1167 /*******************************************
1168         7810
1169 *******************************************/
1170 
bug7810()1171 int bug7810()
1172 {
1173     int[1][3] x = void;
1174     x[0] = [2];
1175     x[1] = [7];
1176     assert(x[0][0] == 2);
1177 
1178     char[1][3] y = void;
1179     y[0] = "a";
1180     y[1] = "b";
1181     assert(y[0][0] == 'a');
1182 
1183     return 1;
1184 }
1185 static assert(bug7810());
1186 
1187 struct Bug7810
1188 {
1189     int w;
1190 }
bug7810b(T)1191 int bug7810b(T)(T[] items...)
1192 {
1193     assert(items[0] == Bug7810(20));
1194     return 42;
1195 }
1196 static assert(bug7810b(Bug7810(20), Bug7810(10)) == 42);
1197 
1198 /*******************************************
1199     std.datetime ICE (30 April 2011)
1200 *******************************************/
1201 
1202 struct TimeOfDayZ
1203 {
1204 public:
thisTimeOfDayZ1205     this(int hour) { }
invariantTimeOfDayZ1206     invariant() { }
1207 }
1208 const testTODsThrownZ = TimeOfDayZ(0);
1209 
1210 /*******************************************
1211         Bug 5954
1212 *******************************************/
1213 
1214 struct Bug5954
1215 {
1216     int x;
thisBug59541217     this(int xx)
1218     {
1219         this.x = xx;
1220     }
1221 }
bug5954()1222 void bug5954()
1223 {
1224     enum f = Bug5954(10);
1225     static assert(f.x == 10);
1226 }
1227 
1228 /*******************************************
1229         Bug 5972
1230 *******************************************/
1231 
bug5972()1232 int bug5972()
1233 {
1234     char[] z = "abc".dup;
1235     char[][] a = [null, null];
1236     a[0]  = z[0 .. 2];
1237     char[] b = a[0];
1238     assert(b == "ab");
1239     a[0][1] = 'q';
1240     assert(a[0] == "aq");
1241     assert(b == "aq");
1242     assert(b[1] == 'q');
1243     //a[0][0 .. $ - 1][0 .. $] = a[0][0 .. $ - 1][0 .. $];  // overlap
1244     return 56;
1245 }
1246 static assert(bug5972() == 56);
1247 
1248 /*******************************************
1249     2.053beta [CTFE]ICE 'global errors'
1250 *******************************************/
1251 
wconcat(wstring replace)1252 int wconcat(wstring replace)
1253 {
1254     wstring value;
1255     value  = "A"w;
1256     value = value ~ replace;
1257     return 1;
1258 }
1259 static assert(wconcat("X"w));
1260 
1261 /*******************************************
1262     10397 string concat
1263 *******************************************/
1264 
1265 static assert(!is(typeof(compiles!("abc" ~ undefined))));
1266 static assert(!is(typeof(compiles!(otherundefined ~ "abc"))));
1267 
1268 /*******************************************
1269     9634 struct concat
1270 *******************************************/
1271 
1272 struct Bug9634
1273 {
1274     int raw;
1275 }
1276 
bug9634()1277 bool bug9634()
1278 {
1279     Bug9634[] jr = [Bug9634(42)];
1280 
1281     Bug9634[] ir = null ~ jr;
1282     Bug9634[] kr = jr ~ null;
1283     Bug9634[] mr = jr ~ jr;
1284 
1285     jr[0].raw = 6;
1286     assert(ir[0].raw == 42);
1287     assert(kr[0].raw == 42);
1288     assert(jr[0].raw == 6);
1289     assert(&mr[0] != &mr[1]);
1290     return true;
1291 }
1292 
1293 static assert(bug9634());
1294 
1295 /*******************************************
1296     Bug 4001: A Space Oddity
1297 *******************************************/
1298 
space()1299 int space() { return 4001; }
1300 
oddity4001(int q)1301 void oddity4001(int q)
1302 {
1303     const int bowie = space();
1304     static assert(space() == 4001);
1305     static assert(bowie == 4001);
1306 }
1307 
1308 /*******************************************
1309     Bug 3779
1310 *******************************************/
1311 
1312 static const bug3779 = ["123"][0][$ - 1];
1313 
1314 /*******************************************
1315     Bug 8893 ICE with bad struct literal
1316 *******************************************/
1317 
1318 struct Foo8893
1319 {
1320     char[3] data;
1321 }
bar8893(Foo8893 f)1322 int bar8893(Foo8893 f)
1323 {
1324     return f.data[0];
1325 }
1326 static assert(!is(typeof(compiles!(bar8893(Foo8893(['a','b']))))));
1327 
1328 /*******************************************
1329     non-Cow struct literals
1330 *******************************************/
1331 
1332 struct Zadok
1333 {
1334     int[3] z;
1335     char[4] s = void;
fogZadok1336     ref int[] fog(ref int[] q) { return q; }
bfgZadok1337     int bfg()
1338     {
1339         z[0] = 56;
1340         auto zs = z[];
1341         fog(zs) = [56, 6, 8];
1342 
1343         assert(z[0] == 56);
1344         assert(z[1] == 61);
1345         assert(z[2] == 61);
1346 
1347         assert(zs[0] == 56);
1348         assert(zs[1] == 6);
1349         return zs[2];
1350     }
1351 }
1352 
1353 struct Vug
1354 {
1355     Zadok p;
1356     int[] other;
1357 }
1358 
quop()1359 int quop()
1360 {
1361     int[] heap = new int[5];
1362     heap[] = 738;
1363     Zadok pong;
1364     pong.z = 3;
1365     int[] w = pong.z;
1366     assert(w[0] == 3);
1367     Zadok phong;
1368     phong.z = 61;
1369     pong = phong;
1370     assert(w[0] == 61);
1371     Vug b = Vug(Zadok(17, "abcd"));
1372     b = Vug(Zadok(17, "abcd"), heap);
1373     b.other[2] = 78;
1374     assert(heap[2] == 78);
1375     char[] y = b.p.s;
1376     assert(y[2] == 'c');
1377     phong.s = ['z','x','f', 'g'];
1378     w = b.p.z;
1379     assert(y[2] == 'c');
1380     assert(w[0] == 17);
1381     b.p = phong;
1382     assert(y[2] == 'f');
1383 
1384     Zadok wok = Zadok(6, "xyzw");
1385     b.p = wok;
1386     assert(y[2] == 'z');
1387     b.p = phong;
1388     assert(w[0] == 61);
1389     Vug q;
1390     q.p = pong;
1391     return pong.bfg();
1392 }
1393 
1394 static assert(quop() == 8);
1395 static assert(quop() == 8); // check for clobbering
1396 
1397 /**************************************************
1398    Bug 5676 tuple assign of struct that has void opAssign
1399 **************************************************/
1400 
1401 struct S5676
1402 {
1403     int x;
opAssignS56761404     void opAssign(S5676 rhs) { x = rhs.x; }
1405 }
1406 
Tup5676(E...)1407 struct Tup5676(E...)
1408 {
1409     E g;
1410     void foo(E values) { g = values; }
1411 }
1412 
ice5676()1413 bool ice5676()
1414 {
1415     Tup5676!(S5676) q;
1416     q.foo(S5676(3));
1417     assert(q.g[0].x == 3);
1418     return true;
1419 }
1420 
1421 static assert(ice5676());
1422 
1423 /**************************************************
1424    Bug 5682 Wrong CTFE with operator overloading
1425 **************************************************/
1426 
1427 struct A
1428 {
1429     int n;
1430     auto opBinary(string op : "*")(A rhs)
1431     {
1432         return A(n * rhs.n);
1433     }
1434 }
1435 
foo(A[]lhs,A[]rhs)1436 A foo(A[] lhs, A[] rhs)
1437 {
1438     A current;
1439     for (size_t k = 0; k < rhs.length; ++k)
1440     {
1441         current = lhs[k] * rhs[k];
1442     }
1443     return current;
1444 }
1445 
test()1446 auto test()
1447 {
1448     return foo([A(1), A(2)], [A(3), A(4)]);
1449 }
1450 
1451 static assert(test().n == 8);
1452 
1453 /**************************************************
1454    Attempt to modify a read-only string literal
1455 **************************************************/
1456 struct Xarg
1457 {
1458     char[] s;
1459 }
1460 
zfs(int n)1461 int zfs(int n)
1462 {
1463     char[] m = "exy".dup;
1464     if (n == 1)
1465     {
1466         // it's OK to cast to const, then cast back
1467         string ss = cast(string)m;
1468         m = cast(char[])ss;
1469         m[2]='q';
1470         return 56;
1471     }
1472     auto q = Xarg(cast(char[])"abc");
1473     assert(q.s[1] == 'b');
1474     if (n == 2)
1475         q.s[1] = 'p';
1476     else if (n == 3)
1477         q.s[0 .. $] = 'p';
1478     char* w = &q.s[2];
1479     if (n == 4)
1480         *w = 'z';
1481     return 76;
1482 }
1483 
1484 static assert(!is(typeof(compiles!(zfs(2)))));
1485 static assert(!is(typeof(compiles!(zfs(3)))));
1486 static assert(!is(typeof(compiles!(zfs(4)))));
1487 static assert( is(typeof(compiles!(zfs(1)))));
1488 static assert( is(typeof(compiles!(zfs(5)))));
1489 
1490 /**************************************************
1491    .dup must protect string literals
1492 **************************************************/
1493 
mutateTheImmutable(immutable string _s)1494 string mutateTheImmutable(immutable string _s)
1495 {
1496     char[] s = _s.dup;
1497     foreach (ref c; s)
1498         c = 'x';
1499     return s.idup;
1500 }
1501 
doharm(immutable string _name)1502 string doharm(immutable string _name)
1503 {
1504     return mutateTheImmutable(_name[2 .. $].idup);
1505 }
1506 
1507 enum victimLiteral = "CL_INVALID_CONTEXT";
1508 
1509 enum thug = doharm(victimLiteral);
1510 static assert(victimLiteral == "CL_INVALID_CONTEXT");
1511 
1512 /**************************************************
1513         Use $ in a slice of a dotvar slice
1514 **************************************************/
1515 
sliceDollar()1516 int sliceDollar()
1517 {
1518     Xarg z;
1519     z.s = new char[20];
1520     z.s[] = 'b';
1521     z.s = z.s[2 .. $ - 2];
1522     z.s[$ - 2] = 'c';
1523     return z.s[$ - 2];
1524 }
1525 static assert(sliceDollar() == 'c');
1526 
1527 /**************************************************
1528    Variation of 5972 which caused segfault
1529 **************************************************/
1530 
bug5972crash()1531 int bug5972crash()
1532 {
1533     char[] z = "abc".dup;
1534     char[][] a = [null, null];
1535     a[0] = z[0 .. 2];
1536     a[0][1] = 'q';
1537     return 56;
1538 }
1539 static assert(bug5972crash() == 56);
1540 
1541 /**************************************************
1542    String slice assignment through ref parameter
1543 **************************************************/
1544 
popft(A)1545 void popft(A)(ref A a)
1546 {
1547     a = a[1 .. $];
1548 }
1549 
sdfgasf()1550 int sdfgasf()
1551 {
1552     auto scp = "abc".dup;
1553     popft(scp);
1554     return 1;
1555 }
1556 static assert(sdfgasf() == 1);
1557 
1558 /**************************************************
1559    8830 slice of slice.ptr
1560 **************************************************/
1561 
bug8830(string s)1562 string bug8830(string s)
1563 {
1564     auto ss = s[1 .. $];
1565     return ss.ptr[0 .. 2];
1566 }
1567 static assert(bug8830("hello") == "el");
1568 
1569 /**************************************************
1570    8608 ICE
1571 **************************************************/
1572 
bug8608(ref int m)1573 void bug8608(ref int m) {}
test8608()1574 void test8608()
1575 {
1576     int z;
1577     int foo(bool b)
1578     {
1579         if (b)
1580             bug8608(z);
1581         return 1;
1582     }
1583     static assert( is(typeof(compiles!(foo(false)))));
1584     static assert(!is(typeof(compiles!(foo(true) ))));
1585 }
1586 
1587 /**************************************************
1588    Bug 7770
1589 **************************************************/
1590 
1591 immutable char[] foo7770 = "abcde";
1592 
bug7770a(string a)1593 int bug7770a(string a)
1594 {
1595     return 1;
1596 }
1597 
bug7770b(char c)1598 bool bug7770b(char c)
1599 {
1600     return true;
1601 }
1602 
1603 static assert(bug7770a(foo7770[0 .. $]));
1604 static assert(bug7770b(foo7770[$ - 2]));
1605 
baz7770()1606 void baz7770()
1607 {
1608     static assert(bug7770a(foo7770[0 .. $]));
1609     static assert(bug7770b(foo7770[$ - 2]));
1610 }
1611 
1612 /**************************************************
1613    8601 ICE
1614 **************************************************/
1615 
bug8601(dstring s)1616 dchar bug8601(dstring s)
1617 {
1618     dstring w = s[1 .. $];
1619     return w[0];
1620 }
1621 
1622 enum dstring e8601 = [cast(dchar)'o', 'n'];
1623 static assert(bug8601(e8601) == 'n');
1624 
1625 /**************************************************
1626    Bug 6015
1627 **************************************************/
1628 
1629 struct Foo6015
1630 {
1631     string field;
1632 }
1633 
func6015(string input)1634 bool func6015(string input)
1635 {
1636     Foo6015 foo;
1637     foo.field = input[0 .. $];
1638     assert(foo.field == "test");
1639     foo.field = "test2";
1640     assert(foo.field != "test");
1641     assert(foo.field == "test2");
1642     return true;
1643 }
1644 
1645 static assert(func6015("test"));
1646 
1647 /**************************************************
1648    Bug 6001
1649 **************************************************/
1650 
bug6001e(ref int[]s)1651 void bug6001e(ref int[] s)
1652 {
1653     int[] r = s;
1654     s ~= 0;
1655 }
bug6001f()1656 bool bug6001f()
1657 {
1658     int[] s;
1659     bug6001e(s);
1660     return true;
1661 }
1662 static assert(bug6001f());
1663 
1664 // Assignment to AAs
1665 
blah(int[char]as)1666 void blah(int[char] as)
1667 {
1668     auto k = [6: as];
1669     as = k[6];
1670 }
blaz()1671 int blaz()
1672 {
1673     int[char] q;
1674     blah(q);
1675     return 67;
1676 }
1677 static assert(blaz() == 67);
1678 
bug6001g(ref int[]w)1679 void bug6001g(ref int[] w)
1680 {
1681     w = [88];
1682     bug6001e(w);
1683     w[0] = 23;
1684 }
1685 
bug6001h()1686 bool bug6001h()
1687 {
1688     int[] s;
1689     bug6001g(s);
1690     assert(s.length == 2);
1691     assert(s[1] == 0);
1692     assert(s[0] == 23);
1693     return true;
1694 }
1695 static assert(bug6001h());
1696 
1697 /**************************************************
1698    10243 wrong code *&arr as ref parameter
1699    10551 wrong code (&arr)[0] as ref parameter
1700 **************************************************/
1701 
bug10243(ref int n)1702 void bug10243(ref int n)
1703 {
1704     n = 3;
1705 }
1706 
bug10551(int * p)1707 void bug10551(int* p)
1708 {
1709     bug10243(p[0]);
1710 }
1711 
test10243()1712 bool test10243()
1713 {
1714     int[1] arr;
1715     bug10243(*arr.ptr);
1716     assert(arr[0] == 3);
1717     int[1] arr2;
1718     bug10551(arr2.ptr);
1719     assert(arr2[0] == 3);
1720     int v;
1721     bug10551(&v);
1722     assert(v == 3);
1723     return true;
1724 }
1725 
1726 static assert(test10243());
1727 
1728 /**************************************************
1729    Bug 4910
1730 **************************************************/
1731 
bug4910(int a)1732 int bug4910(int a)
1733 {
1734     return a;
1735 }
1736 
1737 static int var4910;
1738 static assert(!is(typeof(Compiles!(bug4910(var4910)))));
1739 
1740 static assert(bug4910(123));
1741 
1742 /**************************************************
1743     Bug 5845 - Regression(2.041)
1744 **************************************************/
1745 
test5845(ulong cols)1746 void test5845(ulong cols) {}
1747 
solve(bool niv,ref ulong cols)1748 uint solve(bool niv, ref ulong cols)
1749 {
1750     if (niv)
1751         solve(false, cols);
1752     else
1753         test5845(cols);
1754     return 65;
1755 }
1756 
nqueen(int n)1757 ulong nqueen(int n)
1758 {
1759     ulong cols = 0;
1760     return solve(true, cols);
1761 }
1762 
1763 static assert(nqueen(2) == 65);
1764 
1765 /**************************************************
1766     Bug 5258
1767 **************************************************/
1768 
1769 struct Foo5258 { int x; }
bar5258(int n,ref Foo5258 fong)1770 void bar5258(int n, ref Foo5258 fong)
1771 {
1772     if (n)
1773         bar5258(n - 1, fong);
1774     else
1775         fong.x++;
1776 }
bug5258()1777 int bug5258()
1778 {
1779     Foo5258 foo5258 = Foo5258();
1780     bar5258(1, foo5258);
1781     return 45;
1782 }
1783 static assert(bug5258() == 45);
1784 
1785 struct Foo5258b { int[2] r; }
baqopY(int n,ref int[2]fongo)1786 void baqopY(int n, ref int[2] fongo)
1787 {
1788     if (n)
1789         baqopY(n - 1, fongo);
1790     else
1791         fongo[0]++;
1792 }
bug5258b()1793 int bug5258b()
1794 {
1795     Foo5258b qq;
1796     baqopY(1, qq.r);
1797     return 618;
1798 }
1799 static assert(bug5258b() == 618);
1800 
1801 // Notice that this case involving reassigning the dynamic array
1802 struct Foo5258c { int[] r; }
baqop(int n,ref int[]fongo)1803 void baqop(int n, ref int[] fongo)
1804 {
1805     if (n)
1806         baqop(n - 1, fongo);
1807     else
1808     {
1809         fongo = new int[20];
1810         fongo[0]++;
1811     }
1812 }
bug5258c()1813 size_t bug5258c()
1814 {
1815     Foo5258c qq;
1816     qq.r = new int[30];
1817     baqop(1, qq.r);
1818     return qq.r.length;
1819 }
1820 static assert(bug5258c() == 20);
1821 
1822 /**************************************************
1823     Bug 6049
1824 **************************************************/
1825 
1826 struct Bug6049
1827 {
1828     int m;
thisBug60491829     this(int x) {  m = x; }
invariantBug60491830     invariant() { }
1831 }
1832 
1833 const Bug6049[] foo6049 = [Bug6049(6),  Bug6049(17)];
1834 
1835 static assert(foo6049[0].m == 6);
1836 
1837 /**************************************************
1838     Bug 6052
1839 **************************************************/
1840 
1841 struct Bug6052
1842 {
1843     int a;
1844 }
1845 
bug6052()1846 bool bug6052()
1847 {
1848     Bug6052[2] arr;
1849     for (int i = 0; i < 2; ++ i)
1850     {
1851         Bug6052 el = {i};
1852         Bug6052 ek = el;
1853         arr[i] = el;
1854         el.a = i + 2;
1855         assert(ek.a == i);      // ok
1856         assert(arr[i].a == i);  // fail
1857     }
1858     assert(arr[1].a == 1);  // ok
1859     assert(arr[0].a == 0);  // fail
1860     return true;
1861 }
1862 
1863 static assert(bug6052());
1864 
bug6052b()1865 bool bug6052b()
1866 {
1867     int[][1] arr;
1868     int[1] z = [7];
1869     arr[0] = z;
1870     assert(arr[0][0] == 7);
1871     arr[0] = z;
1872     z[0] = 3;
1873     assert(arr[0][0] == 3);
1874     return true;
1875 }
1876 
1877 static assert(bug6052b());
1878 
1879 struct Bug6052c
1880 {
1881     int x;
thisBug6052c1882     this(int a) { x = a; }
1883 }
1884 
bug6052c()1885 int bug6052c()
1886 {
1887     Bug6052c[] pieces = [];
1888     for (int c = 0; c < 2; ++ c)
1889         pieces ~= Bug6052c(c);
1890     assert(pieces[1].x == 1);
1891     assert(pieces[0].x == 0);
1892     return 1;
1893 }
1894 static assert(bug6052c() == 1);
1895 static assert(bug6052c() == 1);
1896 
1897 
1898 static assert({
1899     Bug6052c[] pieces = [];
1900     pieces.length = 2;
1901     int c = 0;
1902     pieces[0] = Bug6052c(c);
1903     ++c;
1904     pieces[1] = Bug6052c(c);
1905     assert(pieces[0].x == 0);
1906     return true;
1907 }());
1908 
1909 static assert({
1910     int[1][] pieces = [];
1911     pieces.length = 2;
1912     for (int c = 0; c < 2; ++ c)
1913         pieces[c][0] = c;
1914     assert(pieces[1][0] == 1);
1915     assert(pieces[0][0] == 0);
1916     return true;
1917 }());
1918 
1919 static assert({
1920     Bug6052c[] pieces = [];
1921     for (int c = 0; c < 2; ++ c)
1922         pieces ~= Bug6052c(c);
1923     assert(pieces[1].x == 1);
1924     assert(pieces[0].x == 0);
1925     return true;
1926 }());
1927 
1928 static assert({
1929     int[1] z = 7;
1930     int[1][] pieces = [z,z];
1931     pieces[1][0]=3;
1932     assert(pieces[0][0] == 7);
1933     pieces = pieces ~ [z,z];
1934     pieces[3][0] = 16;
1935     assert(pieces[2][0] == 7);
1936     pieces = [z,z] ~ pieces;
1937     pieces[5][0] = 16;
1938     assert(pieces[4][0] == 7);
1939     return true;
1940 }());
1941 
1942 /**************************************************
1943     Bug 6749
1944 **************************************************/
1945 
1946 struct CtState
1947 {
1948     string code;
1949 }
1950 
bug6749()1951 CtState bug6749()
1952 {
1953     CtState[] pieces;
1954     CtState r = CtState("correct");
1955     pieces ~= r;
1956     r = CtState("clobbered");
1957     return pieces[0];
1958 }
1959 static assert(bug6749().code == "correct");
1960 
1961 /**************************************************
1962     Index + slice assign to function returns
1963 **************************************************/
1964 
funcRetArr(int[]a)1965 int[] funcRetArr(int[] a)
1966 {
1967     return a;
1968 }
1969 
testFuncRetAssign()1970 int testFuncRetAssign()
1971 {
1972     int[] x = new int[20];
1973     funcRetArr(x)[2] = 4;
1974     assert(x[2] == 4);
1975     funcRetArr(x)[] = 27;
1976     assert(x[15] == 27);
1977     return 5;
1978 }
1979 static assert(testFuncRetAssign() == 5);
1980 
keyAssign()1981 int keyAssign()
1982 {
1983     int[int] pieces;
1984     pieces[3] = 1;
1985     pieces.keys[0] = 4;
1986     pieces.values[0] = 27;
1987     assert(pieces[3] == 1);
1988     return 5;
1989 }
1990 static assert(keyAssign() == 5);
1991 
1992 /**************************************************
1993     Bug 6054 -- AA literals
1994 **************************************************/
1995 
1996 enum x6054 = {
1997     auto p = {
1998         int[string] pieces;
1999         pieces[['a'].idup] = 1;
2000         return pieces;
2001     }();
2002     return p;
2003 }();
2004 
2005 /**************************************************
2006     Bug 6077
2007 **************************************************/
2008 
2009 enum bug6077 = {
2010     string s;
2011     string t;
2012     return s ~ t;
2013 }();
2014 
2015 /**************************************************
2016     Bug 6078 -- Pass null array by ref
2017 **************************************************/
2018 
2019 struct Foo6078
2020 {
2021     int[] bar;
2022 }
2023 
2024 static assert({
2025     Foo6078 f;
2026     int i;
2027     foreach (ref e; f.bar)
2028     {
2029         i += e;
2030     }
2031     return i;
2032 }() == 0);
2033 
bug6078(ref int[]z)2034 int bug6078(ref int[] z)
2035 {
2036     int[] q = z;
2037     return 2;
2038 }
2039 
2040 static assert({
2041     Foo6078 f;
2042     return bug6078(f.bar);
2043 }() == 2);
2044 
2045 /**************************************************
2046     Bug 6079 -- Array bounds checking
2047 **************************************************/
2048 
2049 static assert(!is(typeof(compiles!({
2050     int[] x = [1, 2, 3, 4];
2051     x[4] = 1;
2052     return true;
2053 }()
2054 ))));
2055 
2056 /**************************************************
2057     Bug 6100
2058 **************************************************/
2059 
2060 struct S6100
2061 {
2062     int a;
2063 }
2064 
init6100(int x)2065 S6100 init6100(int x)
2066 {
2067     S6100 s = S6100(x);
2068     return s;
2069 }
2070 
2071 static const S6100[2] s6100a = [init6100(1), init6100(2)];
2072 static assert(s6100a[0].a == 1);
2073 
2074 /**************************************************
2075     Bug 4825 -- failed with -inline
2076 **************************************************/
2077 
a4825()2078 int a4825()
2079 {
2080     int r;
2081     return r;
2082 }
2083 
b4825()2084 int b4825()
2085 {
2086     return a4825();
2087 }
2088 
c4825()2089 void c4825()
2090 {
2091     void d()
2092     {
2093         auto e = b4825();
2094     }
2095     static const int f = b4825();
2096 }
2097 
2098 /**************************************************
2099     Bug 5708 -- failed with -inline
2100 **************************************************/
2101 
b5708(string s)2102 string b5708(string s) { return s; }
a5708(string s)2103 string a5708(string s) { return b5708(s); }
2104 
bug5708()2105 void bug5708()
2106 {
2107     void m() { a5708("lit"); }
2108     static assert(a5708("foo") == "foo");
2109     static assert(a5708("bar") == "bar");
2110 }
2111 
2112 /**************************************************
2113     Bug 6120 -- failed with -inline
2114 **************************************************/
2115 
Bug6120(T)2116 struct Bug6120(T)
2117 {
2118     this(int x) { }
2119 }
2120 static assert({
2121     auto s = Bug6120!int(0);
2122     return true;
2123 }());
2124 
2125 /**************************************************
2126     Bug 6123 -- failed with -inline
2127 **************************************************/
2128 
Bug6123(T)2129 struct Bug6123(T)
2130 {
2131     void f() {}
2132     // can also trigger if the struct is normal but f is template
2133 }
2134 static assert({
2135     auto piece = Bug6123!int();
2136     piece.f();
2137     return true;
2138 }());
2139 
2140 /**************************************************
2141     Bug 6053 -- ICE involving pointers
2142 **************************************************/
2143 
2144 static assert({
2145     int* a = null;
2146     assert(a is null);
2147     assert(a == null);
2148     return true;
2149 }());
2150 
2151 static assert({
2152     int b;
2153     int* a = &b;
2154     assert(a !is null);
2155     *a = 7;
2156     assert(b == 7);
2157     assert(*a == 7);
2158     return true;
2159 }());
2160 
dontbreak6053()2161 int dontbreak6053()
2162 {
2163     auto q = &dontbreak6053;
2164     void caz() {}
2165     auto tr = &caz;
2166     return 5;
2167 }
2168 static assert(dontbreak6053());
2169 
2170 static assert({
2171     int a;
2172     *(&a) = 15;
2173     assert(a == 15);
2174     assert(*(&a) == 15);
2175     return true;
2176 }());
2177 
2178 static assert({
2179     int a = 5, b = 6, c = 2;
2180     assert(*(c ? &a : &b) == 5);
2181     assert(*(!c ? &a : &b) == 6);
2182     return true;
2183 }());
2184 
2185 static assert({
2186     int a, b, c;
2187     (c ? a : b) = 1;
2188     return true;
2189 }());
2190 
2191 static assert({
2192     int a, b, c = 1;
2193     int* p = &a;
2194     (c ? *p : b) = 51;
2195     assert(a == 51);
2196     return true;
2197 }());
2198 
2199 /**************************************************
2200   Pointer arithmetic, dereference, and comparison
2201 **************************************************/
2202 
2203 // dereference null pointer
2204 static assert(!is(typeof(compiles!({
2205     int a, b, c = 1;
2206     int* p;
2207     (c ? *p : b) = 51;
2208     return 6;
2209 }()
2210 ))));
2211 static assert(!is(typeof(compiles!({
2212     int* a = null;
2213     assert(*a != 6);
2214     return 72;
2215 }()
2216 ))));
2217 
2218 // cannot <, > compare pointers to different arrays
2219 static assert(!is(typeof(compiles!({
2220     int[5] a, b;
2221     bool c = (&a[0] > &b[0]);
2222     return 72;
2223 }()
2224 ))));
2225 
2226 // can ==, is, !is, != compare pointers for different arrays
2227 static assert({
2228     int[5] a;
2229     int[5] b;
2230     assert(!(&a[0] == &b[0]));
2231     assert(&a[0] != &b[0]);
2232     assert(!(&a[0] is &b[0]));
2233     assert(&a[0] !is &b[0]);
2234     return 72;
2235 }());
2236 
2237 static assert({
2238     int[5] a;
2239     a[0] = 25;
2240     a[1] = 5;
2241     int* b = &a[1];
2242     assert(*b == 5);
2243     *b = 34;
2244     int c = *b;
2245     *b += 6;
2246     assert(b == &a[1]);
2247     assert(b != &a[0]);
2248     assert(&a[0] < &a[1]);
2249     assert(&a[0] <= &a[1]);
2250     assert(!(&a[0] >= &a[1]));
2251     assert(&a[4] > &a[0]);
2252     assert(c == 34);
2253     assert(*b == 40);
2254     assert(a[1] == 40);
2255     return true;
2256 }());
2257 
2258 static assert({
2259     int[12] x;
2260     int* p = &x[10];
2261     int* q = &x[4];
2262     return p - q;
2263 }() == 6);
2264 
2265 static assert({
2266     int[12] x;
2267     int* p = &x[10];
2268     int* q = &x[4];
2269     q = p;
2270     assert(p == q);
2271     q = &x[4];
2272     assert(p != q);
2273     q = q + 6;
2274     assert(q is p);
2275     return 6;
2276 }() == 6);
2277 
2278 static assert({
2279     int[12] x;
2280     int[] y = x[2 .. 8];
2281     int* p = &y[4];
2282     int* q = &x[6];
2283     assert(p == q);
2284     p = &y[5];
2285     assert(p > q);
2286     p = p + 5; // OK, as long as we don't dereference
2287     assert(p > q);
2288     return 6;
2289 }() == 6);
2290 
2291 static assert({
2292     char[12] x;
2293     const(char)* p = "abcdef";
2294     const (char)* q = p;
2295     q = q + 2;
2296     assert(*q == 'c');
2297     assert(q > p);
2298     assert(q - p == 2);
2299     assert(p - q == -2);
2300     q = &x[7];
2301     p = &x[1];
2302     assert(q>p);
2303     return 6;
2304 }() == 6);
2305 
2306 // Relations involving null pointers
nullptrcmp()2307 bool nullptrcmp()
2308 {
2309     // null tests
2310     void* null1 = null, null2 = null;
2311     int x = 2;
2312     void* p = &x;
2313     assert(null1 == null2);
2314     assert(null1 is null2);
2315     assert(null1 <= null2);
2316     assert(null1 >= null2);
2317     assert(!(null1 > null2));
2318     assert(!(null2 > null1));
2319     assert(null1 != p);
2320     assert(null1 !is p);
2321     assert(p != null1);
2322     assert(p !is null1);
2323     assert(null1 <= p);
2324     assert(p >= null2);
2325     assert(p > null1);
2326     assert(!(null1 > p));
2327     return true;
2328 }
2329 static assert(nullptrcmp());
2330 
2331 /**************************************************
2332  10840 null pointer in dotvar
2333 **************************************************/
2334 
2335 struct Data10840
2336 {
2337     bool xxx;
2338 }
2339 
2340 struct Bug10840
2341 {
2342     Data10840* _data;
2343 }
2344 
bug10840(int n)2345 bool bug10840(int n)
2346 {
2347     Bug10840 stack;
2348     if (n == 1)
2349     {
2350         // detect deref through null pointer
2351         return stack._data.xxx;
2352     }
2353     // Wrong-code for ?:
2354     return stack._data ? false : true;
2355 }
2356 
2357 static assert(bug10840(0));
2358 static assert(!is(typeof(Compileable!(bug10840(1)))));
2359 
2360 /**************************************************
2361   8216 ptr inside a pointer range
2362 **************************************************/
2363 
2364 // Four-pointer relations. Return true if [p1 .. p2] points inside [q1 .. q2]
2365 // (where the end points don't coincide).
ptr4cmp(void * p1,void * p2,void * q1,void * q2)2366 bool ptr4cmp(void* p1, void* p2, void* q1, void* q2)
2367 {
2368 // Each compare can be written with <, <=, >, or >=.
2369 // Either && or || can be used, giving 32 permutations.
2370 // Additionally each compare can be negated with !, yielding 128 in total.
2371     bool b1 = (p1 > q1 && p2 <= q2);
2372     bool b2 = (p1 > q1 && p2 < q2);
2373     bool b3 = (p1 >= q1 && p2 <= q2);
2374     bool b4 = (p1 >= q1 && p2 < q2);
2375 
2376     bool b5 = (q1 <= p1 && q2 > p2);
2377     bool b6 = (q1 <= p1 && q2 >= p2);
2378     bool b7 = (p2 <= q2 && p1 > q1);
2379     bool b8 = (!(p1 <= q1) && p2 <= q2);
2380     bool b9 = (!(p1 <= q1) && !(p2 > q2));
2381     bool b10 = (!!!(p1 <= q1) && !(p2 > q2));
2382 
2383     assert(b1 == b2 && b1 == b3 && b1 == b4 && b1 == b5 && b1 == b6);
2384     assert(b1 == b7 && b1 == b8 && b1 == b9 && b1 == b10);
2385 
2386     bool c1 = (p1 <= q1 || p2 > q2);
2387     assert(c1 == !b1);
2388     bool c2 = (p1 < q1 || p2 >= q2);
2389     bool c3 = (!(q1 <= p1) || !(q2 >= p2));
2390     assert(c1 == c2 && c1 == c3);
2391     return b1;
2392 }
2393 
bug8216()2394 bool bug8216()
2395 {
2396     int[4] a;
2397     int[13] b;
2398     int v;
2399     int* p = &v;
2400     assert(!ptr4cmp(&a[0], &a[3], p, p));
2401     assert(!ptr4cmp(&b[2], &b[9], &a[1], &a[2]));
2402     assert(!ptr4cmp(&b[1], &b[9], &b[2], &b[8]));
2403     assert( ptr4cmp(&b[2], &b[8], &b[1], &b[9]));
2404     return 1;
2405 }
2406 static assert(bug8216());
2407 
2408 /**************************************************
2409   6517 ptr++, ptr--
2410 **************************************************/
2411 
bug6517()2412 int bug6517()
2413 {
2414     int[] arr = [1, 2, 3];
2415     auto startp = arr.ptr;
2416     auto endp = arr.ptr + arr.length;
2417 
2418     for (; startp < endp; startp++) {}
2419     startp = arr.ptr;
2420     assert(startp++ == arr.ptr);
2421     assert(startp != arr.ptr);
2422     assert(startp-- != arr.ptr);
2423     assert(startp == arr.ptr);
2424 
2425     return 84;
2426 }
2427 static assert(bug6517() == 84);
2428 
2429 /**************************************************
2430   Out-of-bounds pointer assignment and deference
2431 **************************************************/
2432 
ptrDeref(int ofs,bool wantDeref)2433 int ptrDeref(int ofs, bool wantDeref)
2434 {
2435     int[5] a;
2436     int* b = &a[0];
2437     b = b + ofs; // OK
2438     if (wantDeref)
2439         return *b; // out of bounds
2440     return 72;
2441 }
2442 
2443 static assert(!is(typeof(compiles!(ptrDeref(-1, true)))));
2444 static assert( is(typeof(compiles!(ptrDeref(4, true)))));
2445 static assert( is(typeof(compiles!(ptrDeref(5, false)))));
2446 static assert(!is(typeof(compiles!(ptrDeref(5, true)))));
2447 static assert(!is(typeof(compiles!(ptrDeref(6, false)))));
2448 static assert(!is(typeof(compiles!(ptrDeref(6, true)))));
2449 
2450 /**************************************************
2451   Pointer +=
2452 **************************************************/
2453 static assert({
2454     int[12] x;
2455     int zzz;
2456     assert(&zzz);
2457     int* p = &x[10];
2458     int* q = &x[4];
2459     q = p;
2460     assert(p == q);
2461     q = &x[4];
2462     assert(p != q);
2463     q += 4;
2464     assert(q == &x[8]);
2465     q = q - 2;
2466     q = q + 4;
2467     assert(q is p);
2468     return 6;
2469 }() == 6);
2470 
2471 /**************************************************
2472   Reduced version of bug 5615
2473 **************************************************/
2474 
passthrough(const (char)[]x)2475 const(char)[] passthrough(const(char)[] x)
2476 {
2477     return x;
2478 }
2479 
checkPass(Char1)2480 sizediff_t checkPass(Char1)(const(Char1)[] s)
2481 {
2482     const(Char1)[] balance = s[1 .. $];
2483     return passthrough(balance).ptr - s.ptr;
2484 }
2485 static assert(checkPass("foobar") == 1);
2486 
2487 /**************************************************
2488   Pointers must not escape from CTFE
2489 **************************************************/
2490 
2491 struct Toq
2492 {
2493     const(char)* m;
2494 }
2495 
ptrRet(bool b)2496 Toq ptrRet(bool b)
2497 {
2498     string x = "abc";
2499     return Toq(b ? x[0 .. 1].ptr : null);
2500 }
2501 
2502 static assert(is(typeof(compiles!({
2503     enum Toq boz = ptrRet(false); // OK - ptr is null
2504     Toq z = ptrRet(true); // OK -- ptr doesn't escape
2505     return 4;
2506 }()
2507 ))));
2508 
2509 static assert(!is(typeof(compiles!({
2510     enum Toq boz = ptrRet(true); // fail - ptr escapes
2511     return 4;
2512 }()
2513 ))));
2514 
2515 /**************************************************
2516     Pointers to struct members
2517 **************************************************/
2518 
2519 struct Qoz
2520 {
2521     int w;
2522     int[3] yof;
2523 }
2524 
2525 static assert({
2526     int[3] gaz;
2527     gaz[2] = 3156;
2528     Toq z = ptrRet(true);
2529     auto p = z.m;
2530     assert(*z.m == 'a');
2531     assert(*p == 'a');
2532     auto q = &z.m;
2533     assert(*q == p);
2534     assert(**q == 'a');
2535     Qoz g = Qoz(2, [5, 6, 7]);
2536     auto r = &g.w;
2537     assert(*r == 2);
2538     r = &g.yof[1];
2539     assert(*r == 6);
2540     g.yof[0] = 15;
2541     ++r;
2542     assert(*r == 7);
2543     r -= 2;
2544     assert(*r == 15);
2545     r = &gaz[0];
2546     r += 2;
2547     assert(*r == 3156);
2548     return *p;
2549 }() == 'a');
2550 
2551 struct AList
2552 {
2553     AList* next;
2554     int value;
newListAList2555     static AList* newList()
2556     {
2557         AList[] z = new AList[1];
2558         return &z[0];
2559     }
makeAList2560     static AList* make(int i, int j)
2561     {
2562         auto r = newList();
2563         r.next = (new AList[1]).ptr;
2564         r.value = 1;
2565         AList* z = r.next;
2566         (*z).value = 2;
2567         r.next.value = j;
2568         assert(r.value == 1);
2569         assert(r.next.value == 2);
2570         r.next.next = &(new AList[1])[0];
2571         assert(r.next.next != null);
2572         assert(r.next.next);
2573         r.next.next.value = 3;
2574         assert(r.next.next.value == 3);
2575         r.next.next = newList();
2576         r.next.next.value = 9;
2577         return r;
2578     }
checkListAList2579     static int checkList()
2580     {
2581         auto r = make(1,2);
2582         assert(r.value == 1);
2583         assert(r.next.value == 2);
2584         assert(r.next.next.value == 9);
2585         return 2;
2586     }
2587 }
2588 
2589 static assert(AList.checkList() == 2);
2590 
2591 /**************************************************
2592     7194 pointers as struct members
2593 **************************************************/
2594 
2595 struct S7194 { int* p, p2; }
2596 
f7194()2597 int f7194()
2598 {
2599     assert(S7194().p == null);
2600     assert(!S7194().p);
2601     assert(S7194().p == S7194().p2);
2602     S7194 s = S7194();
2603     assert(!s.p);
2604     assert(s.p == null);
2605     assert(s.p == s.p2);
2606     int x;
2607     s.p = &x;
2608     s.p2 = s.p;
2609     assert(s.p == &x);
2610     return 0;
2611 }
2612 
g7194()2613 int g7194()
2614 {
2615     auto s = S7194();
2616     assert(s.p);  // should fail
2617     return 0;
2618 }
2619 
2620 static assert(f7194() == 0);
2621 static assert(!is(typeof(compiles!(g7194()))));
2622 
2623 /**************************************************
2624     7248 recursive struct pointers in array
2625 **************************************************/
2626 
2627 struct S7248 { S7248* ptr; }
2628 
bug7248()2629 bool bug7248()
2630 {
2631     S7248[2] sarr;
2632     sarr[0].ptr = &sarr[1];
2633     sarr[0].ptr = null;
2634     S7248* t = sarr[0].ptr;
2635     return true;
2636 }
2637 static assert(bug7248());
2638 
2639 /**************************************************
2640     7216 calling a struct pointer member
2641 **************************************************/
2642 
2643 struct S7216
2644 {
2645     S7216* p;
2646     int t;
2647 
fS72162648     void f() { }
gS72162649     void g() { ++t; }
2650 }
2651 
bug7216()2652 bool bug7216()
2653 {
2654     S7216 s0, s1;
2655     s1.t = 6;
2656     s0.p = &s1;
2657     s0.p.f();
2658     s0.p.g();
2659     assert(s1.t == 7);
2660     return true;
2661 }
2662 
2663 static assert(bug7216());
2664 
2665 /**************************************************
2666     10858 Wrong code with array of pointers
2667 **************************************************/
2668 
bug10858()2669 bool bug10858()
2670 {
2671     int*[4] x;
2672     x[0] = null;
2673     assert(x[0] == null);
2674     return true;
2675 }
2676 static assert(bug10858());
2677 
2678 /**************************************************
2679     12528 - painting inout type for value type literals
2680 **************************************************/
2681 
inout(T)2682 inout(T)[] dup12528(T)(inout(T)[] a)
2683 {
2684     inout(T)[] res;
2685     foreach (ref e; a)
2686         res ~= e;
2687     return res;
2688 }
2689 
2690 enum arr12528V1 = dup12528([0]);
2691 enum arr12528V2 = dup12528([0, 1]);
2692 static assert(arr12528V1 == [0]);
2693 static assert(arr12528V2 == [0, 1]);
2694 
2695 /**************************************************
2696     9745 Allow pointers to static variables
2697 **************************************************/
2698 
2699 shared int x9745;
2700 shared int[5] y9745;
2701 
shared(int)2702 shared(int)* bug9745(int m)
2703 {
2704     auto k = &x9745;
2705     auto j = &x9745;
2706     auto p = &y9745[0];
2707     auto q = &y9745[3];
2708     assert(j - k == 0);
2709     assert(j == k);
2710     assert(q - p == 3);
2711     --q;
2712     int a = 0;
2713     assert(p + 2 == q);
2714     if (m == 7)
2715     {
2716         auto z1 = y9745[0 .. 2]; // slice global pointer
2717     }
2718     if (m == 8)
2719         p[1] = 7; // modify through a pointer
2720     if (m == 9)
2721          a = p[1]; // read from a pointer
2722     if (m == 0)
2723         return &x9745;
2724     return &y9745[1];
2725 }
2726 
test9745(int m)2727 int test9745(int m)
2728 {
2729     bug9745(m);
2730     // type painting
2731     shared int* w = bug9745(0);
2732     return 1;
2733 }
2734 
2735 shared int* w9745a = bug9745(0);
2736 shared int* w9745b = bug9745(1);
2737 static assert( is(typeof(compiles!(test9745(6)))));
2738 static assert(!is(typeof(compiles!(test9745(7)))));
2739 static assert(!is(typeof(compiles!(test9745(8)))));
2740 static assert(!is(typeof(compiles!(test9745(9)))));
2741 
2742 // pointers cast from an absolute address
2743 // (mostly applies to fake pointers, eg Windows HANDLES)
test9745b()2744 bool test9745b()
2745 {
2746     void* b6 = cast(void*)0xFEFEFEFE;
2747     void* b7 = cast(void*)0xFEFEFEFF;
2748     assert(b6 is b6);
2749     assert(b7 != b6);
2750     return true;
2751 }
2752 static assert(test9745b());
2753 
2754 /**************************************************
2755     9364 ICE with pointer to local struct
2756 **************************************************/
2757 
2758 struct S9364
2759 {
2760     int i;
2761 }
2762 
bug9364()2763 bool bug9364()
2764 {
2765     S9364 s;
2766     auto k = (&s).i;
2767     return 1;
2768 }
2769 
2770 static assert(bug9364());
2771 
2772 /**************************************************
2773     10251 Pointers to const globals
2774 **************************************************/
2775 
2776 static const int glob10251 = 7;
2777 
bug10251()2778 const(int)* bug10251()
2779 {
2780    return &glob10251;
2781 }
2782 
2783 static a10251 = &glob10251; //  OK
2784 static b10251 = bug10251();
2785 
2786 /**************************************************
2787     4065 [CTFE] AA "in" operator doesn't work
2788 **************************************************/
2789 
bug4065(string s)2790 bool bug4065(string s)
2791 {
2792     enum int[string] aa = ["aa":14, "bb":2];
2793     int* p = s in aa;
2794     if (s == "aa")
2795         assert(*p == 14);
2796     else if (s == "bb")
2797         assert(*p == 2);
2798     else
2799         assert(!p);
2800     int[string] zz;
2801     assert(!("xx" in zz));
2802     bool c = !p;
2803     return cast(bool)(s in aa);
2804 }
2805 
2806 static assert(!bug4065("xx"));
2807 static assert( bug4065("aa"));
2808 static assert( bug4065("bb"));
2809 
2810 /**************************************************
2811     12689 - assigning via pointer from 'in' expression
2812 **************************************************/
2813 
g12689()2814 int g12689()
2815 {
2816     int[int] aa;
2817     aa[1] = 13;
2818     assert(*(1 in aa) == 13);
2819     *(1 in aa) = 42;
2820     return aa[1];
2821 }
2822 static assert(g12689() == 42);
2823 
2824 /**************************************************
2825     Pointers in ? :
2826 **************************************************/
2827 
2828 static assert({
2829     int[2] x;
2830     int* p = &x[1];
2831     return p ? true: false;
2832 }());
2833 
2834 /**************************************************
2835     Pointer slicing
2836 **************************************************/
2837 
ptrSlice()2838 int ptrSlice()
2839 {
2840     auto arr = new int[5];
2841     int* x = &arr[0];
2842     int[] y = x[0 .. 5];
2843     x[1 .. 3] = 6;
2844     ++x;
2845     x[1 .. 3] = 14;
2846     assert(arr[1] == 6);
2847     assert(arr[2] == 14);
2848     //x[-1 .. 4] = 5;   // problematic because negative lower boundary will throw RangeError in runtime
2849     (x - 1)[0 .. 3] = 5;
2850     int[] z = arr[1 .. 2];
2851     z.length = 4;
2852     z[$ - 1] = 17;
2853     assert(arr.length == 5);
2854     return 2;
2855 }
2856 static assert(ptrSlice() == 2);
2857 
2858 /**************************************************
2859     6344 - create empty slice from null pointer
2860 **************************************************/
2861 
2862 static assert({
2863     char* c = null;
2864     auto m = c[0 .. 0];
2865     return true;
2866 }());
2867 
2868 /**************************************************
2869     8365 - block assignment of enum arrays
2870 **************************************************/
2871 
2872 enum E8365 { first = 7, second, third, fourth }
2873 static assert({ E8365[2]       x; return x[0];       }() == E8365.first);
2874 static assert({ E8365[2][2]    x; return x[0][0];    }() == E8365.first);
2875 static assert({ E8365[2][2][2] x; return x[0][0][0]; }() == E8365.first);
2876 
2877 /**************************************************
2878     4448 - labelled break + continue
2879 **************************************************/
2880 
bug4448()2881 int bug4448()
2882 {
2883     int n = 2;
2884 L1:
2885     do
2886     {
2887         switch(n)
2888         {
2889         case 5:
2890             return 7;
2891         default:
2892             n = 5;
2893             break L1;
2894         }
2895         int w = 7;
2896     } while (0);
2897     return 3;
2898 }
2899 static assert(bug4448() == 3);
2900 
bug4448b()2901 int bug4448b()
2902 {
2903     int n = 2;
2904 L1:
2905     for (n = 2; n < 5; ++n)
2906     {
2907         for (int m = 1; m < 6; ++m)
2908         {
2909             if (n < 3)
2910             {
2911                 assert(m == 1);
2912                 continue L1;
2913             }
2914         }
2915         break;
2916     }
2917     return 3;
2918 }
2919 static assert(bug4448b() == 3);
2920 
2921 /**************************************************
2922     6985 - non-constant case
2923 **************************************************/
2924 
bug6985(int z)2925 int bug6985(int z)
2926 {
2927     int q = z * 2 - 6;
2928     switch(z)
2929     {
2930     case q:
2931         q = 87;
2932         break;
2933     default:
2934     }
2935     return q;
2936 }
2937 static assert(bug6985(6) == 87);
2938 
2939 /**************************************************
2940     6281 - [CTFE] A null pointer '!is null' returns 'true'
2941 **************************************************/
2942 
2943 static assert(!{
2944     auto p = null;
2945     return p !is null;
2946 }());
2947 
2948 static assert(!{
2949     auto p = null;
2950     return p != null;
2951 }());
2952 
2953 /**************************************************
2954     6331 - evaluate SliceExp on if condition
2955 **************************************************/
2956 
bug6331(string s)2957 bool bug6331(string s)
2958 {
2959     if (s[0 .. 1])
2960         return true;
2961     return false;
2962 }
2963 static assert(bug6331("str"));
2964 
2965 /**************************************************
2966     6283 - assign to AA with slice as index
2967 **************************************************/
2968 
2969 static assert({
2970     immutable p = "pp";
2971     int[string] pieces = [p: 0];
2972     pieces["qq"] = 1;
2973     return true;
2974 }());
2975 
2976 static assert({
2977     immutable renames = [0: "pp"];
2978     int[string] pieces;
2979     pieces[true ? renames[0] : "qq"] = 1;
2980     pieces["anything"] = 1;
2981     return true;
2982 }());
2983 
2984 static assert({
2985     immutable qq = "qq";
2986     string q = qq;
2987     int[string] pieces = ["a":1];
2988     pieces[q] = 0;
2989     string w = "ab";
2990     int z = pieces[w[0 .. 1]];
2991     assert(z == 1);
2992     return true;
2993 }());
2994 
2995 /**************************************************
2996     6282 - dereference 'in' of an AA
2997 **************************************************/
2998 
2999 static assert({
3000     int[] w = new int[4];
3001     w[2] = 6;
3002     auto c = [5: w];
3003     auto kk  = (*(5 in c))[2];
3004     (*(5 in c))[2] = 8;
3005     (*(5 in c))[1 .. $ - 2] = 4;
3006     auto a = [4:"1"];
3007     auto n = *(4 in a);
3008     return n;
3009 }() == "1");
3010 
3011 /**************************************************
3012     6337 - member function call on struct literal
3013 **************************************************/
3014 
3015 struct Bug6337
3016 {
3017     int k;
sixBug63373018     void six()
3019     {
3020         k = 6;
3021     }
ctfeBug63373022     int ctfe()
3023     {
3024         six();
3025         return k;
3026     }
3027 }
3028 static assert(Bug6337().ctfe() == 6);
3029 
3030 /**************************************************
3031     6603 call manifest function pointer
3032 **************************************************/
3033 
f6603(int a)3034 int f6603(int a) { return a + 5; }
3035 enum bug6603 = &f6603;
3036 static assert(bug6603(6) == 11);
3037 
3038 /**************************************************
3039     6375
3040 **************************************************/
3041 
3042 struct D6375
3043 {
3044     int[] arr;
3045 }
a6375(int[]array)3046 A6375 a6375(int[] array)
3047 {
3048     return A6375(array);
3049 }
3050 struct A6375
3051 {
3052     D6375* _data;
thisA63753053     this(int[] arr)
3054     {
3055         _data = new D6375;
3056         _data.arr = arr;
3057     }
dataA63753058     int[] data()
3059     {
3060         return _data.arr;
3061     }
3062 }
3063 static assert({
3064     int[] a = [1, 2];
3065     auto app2 = a6375(a);
3066     auto data = app2.data();
3067     return true;
3068 }());
3069 
3070 /**************************************************
3071     6280 Converting pointers to bool
3072 **************************************************/
3073 
3074 static assert({
3075     if ((0 in [0:0])) {}
3076     if ((0 in [0:0]) && (0 in [0:0])) {}
3077     return true;
3078 }());
3079 
3080 /**************************************************
3081     6276 ~=
3082 **************************************************/
3083 
3084 struct Bug6276
3085 {
3086     int[] i;
3087 }
3088 static assert({
3089     Bug6276 foo;
3090     foo.i ~= 1;
3091     foo.i ~= 2;
3092     return true;
3093 }());
3094 
3095 /**************************************************
3096     6374   ptr[n] = x, x = ptr[n]
3097 **************************************************/
3098 
3099 static assert({
3100     int[] arr = [1];
3101     arr.ptr[0] = 2;
3102     auto k = arr.ptr[0];
3103     assert(k == 2);
3104     return arr[0];
3105 }() == 2);
3106 
3107 /**************************************************
3108     6306  recursion and local variables
3109 **************************************************/
3110 
recurse6306()3111 void recurse6306()
3112 {
3113     bug6306(false);
3114 }
3115 
bug6306(bool b)3116 bool bug6306(bool b)
3117 {
3118     int x = 0;
3119     if (b)
3120         recurse6306();
3121     assert(x == 0);
3122     x = 1;
3123     return true;
3124 }
3125 static assert(bug6306(true));
3126 
3127 /**************************************************
3128     6386  ICE on unsafe pointer cast
3129 **************************************************/
3130 
3131 static assert(!is(typeof(compiles!({
3132     int x = 123;
3133     int* p = &x;
3134     float z;
3135     float* q = cast(float*)p;
3136     return true;
3137 }()
3138 ))));
3139 
3140 static assert({
3141     int[] x = [123, 456];
3142     int* p = &x[0];
3143     auto m = cast(const(int)*)p;
3144     auto q = p;
3145     return *q;
3146 }());
3147 
3148 /**************************************************
3149     6420  ICE on dereference of invalid pointer
3150 **************************************************/
3151 
3152 static assert({
3153     // Should compile, but pointer can't be dereferenced
3154     int x = 123;
3155     int* p = cast(int*)x;
3156     auto q = cast(char*)x;
3157     auto r = cast(char*)323;
3158     // Valid const-changing cast
3159     const float *m = cast(immutable float*)[1.2f,2.4f,3f];
3160     return true;
3161 }()
3162 );
3163 
3164 static assert(!is(typeof(compiles!({
3165     int x = 123;
3166     int* p = cast(int*)x;
3167     int a = *p;
3168     return true;
3169 }()
3170 ))));
3171 
3172 static assert(!is(typeof(compiles!({
3173     int* p = cast(int*)123;
3174     int a = *p;
3175     return true;
3176 }()
3177 ))));
3178 
3179 static assert(!is(typeof(compiles!({
3180     auto k = cast(int*)45;
3181     *k = 1;
3182     return true;
3183 }()
3184 ))));
3185 
3186 static assert(!is(typeof(compiles!({
3187     *cast(float*)"a" = 4.0;
3188     return true;
3189 }()
3190 ))));
3191 
3192 static assert(!is(typeof(compiles!({
3193     float f = 2.8;
3194     long *p = &f;
3195     return true;
3196 }()
3197 ))));
3198 
3199 static assert(!is(typeof(compiles!({
3200     long *p = cast(long*)[1.2f, 2.4f, 3f];
3201     return true;
3202 }()
3203 ))));
3204 
3205 /**************************************************
3206     6250  deref pointers to array
3207 **************************************************/
3208 
simple6250(int[]* x)3209 int[]* simple6250(int[]* x) { return x; }
3210 
swap6250(int[]* lhs,int[]* rhs)3211 void swap6250(int[]* lhs, int[]* rhs)
3212 {
3213     int[] kk = *lhs;
3214     assert(simple6250(lhs) == lhs);
3215     lhs = simple6250(lhs);
3216     assert(kk[0] == 18);
3217     assert((*lhs)[0] == 18);
3218     assert((*rhs)[0] == 19);
3219     *lhs = *rhs;
3220     assert((*lhs)[0] == 19);
3221     *rhs = kk;
3222     assert(*rhs == kk);
3223     assert(kk[0] == 18);
3224     assert((*rhs)[0] == 18);
3225 }
3226 
ctfeSort6250()3227 int ctfeSort6250()
3228 {
3229      int[][2] x;
3230      int[3] a = [17, 18, 19];
3231      x[0] = a[1 .. 2];
3232      x[1] = a[2 .. $];
3233      assert(x[0][0] == 18);
3234      assert(x[0][1] == 19);
3235      swap6250(&x[0], &x[1]);
3236      assert(x[0][0] == 19);
3237      assert(x[1][0] == 18);
3238      a[1] = 57;
3239      assert(x[0][0] == 19);
3240      return x[1][0];
3241 }
3242 
3243 static assert(ctfeSort6250() == 57);
3244 
3245 /**************************************************/
3246 
simple6250b(long[]* x)3247 long[]* simple6250b(long[]* x) { return x; }
3248 
swap6250b(long[]* lhs,long[]* rhs)3249 void swap6250b(long[]* lhs, long[]* rhs)
3250 {
3251     long[] kk = *lhs;
3252     assert(simple6250b(lhs) == lhs);
3253     lhs = simple6250b(lhs);
3254     assert(kk[0] == 18);
3255     assert((*lhs)[0] == 18);
3256     assert((*rhs)[0] == 19);
3257     *lhs = *rhs;
3258     assert((*lhs)[0] == 19);
3259     *rhs = kk;
3260     assert(*rhs == kk);
3261     assert(kk[0] == 18);
3262     assert((*rhs)[0] == 18);
3263 }
3264 
ctfeSort6250b()3265 long ctfeSort6250b()
3266 {
3267      long[][2] x;
3268      long[3] a = [17, 18, 19];
3269      x[0] = a[1 .. 2];
3270      x[1] = a[2 .. $];
3271      assert(x[0][0] == 18);
3272      assert(x[0][1] == 19);
3273      swap6250b(&x[0], &x[1]);
3274      assert(x[0][0] == 19);
3275      assert(x[1][0] == 18);
3276      a[1] = 57;
3277      assert(x[0][0] == 19);
3278      return x[1][0];
3279 }
3280 
3281 static assert(ctfeSort6250b() == 57);
3282 
3283 /**************************************************
3284     6672 circular references in array
3285 **************************************************/
3286 
bug6672(ref string lhs,ref string rhs)3287 void bug6672(ref string lhs, ref string rhs)
3288 {
3289     auto tmp = lhs;
3290     lhs = rhs;
3291     rhs = tmp;
3292 }
3293 
3294 static assert({
3295     auto kw = ["a"];
3296     bug6672(kw[0], kw[0]);
3297     return true;
3298 }());
3299 
slice6672(ref string[2]agg,ref string lhs)3300 void slice6672(ref string[2] agg, ref string lhs)
3301 {
3302     agg[0 .. $] = lhs;
3303 }
3304 
3305 static assert({
3306     string[2] kw = ["a", "b"];
3307     slice6672(kw, kw[0]);
3308     assert(kw[0] == "a");
3309     assert(kw[1] == "a");
3310     return true;
3311 }());
3312 
3313 // an unrelated rejects-valid bug
3314 static assert({
3315     string[2] kw = ["a", "b"];
3316     kw[0 .. 2] = "x";
3317     return true;
3318 }());
3319 
bug6672b(ref string lhs,ref string rhs)3320 void bug6672b(ref string lhs, ref string rhs)
3321 {
3322     auto tmp = lhs;
3323     assert(tmp == "a");
3324     lhs = rhs;
3325     assert(tmp == "a");
3326     rhs = tmp;
3327 }
3328 
3329 static assert({
3330     auto kw=["a", "b"];
3331     bug6672b(kw[0], kw[1]);
3332     assert(kw[0] == "b");
3333     assert(kw[1] == "a");
3334     return true;
3335 }());
3336 
3337 /**************************************************
3338     6399 (*p).length = n
3339 **************************************************/
3340 
3341 struct A6399
3342 {
3343     int[] arr;
subLenA63993344     int subLen()
3345     {
3346         arr = [1, 2, 3, 4, 5];
3347         arr.length -= 1;
3348         return cast(int)arr.length;
3349     }
3350 }
3351 
3352 static assert({
3353     A6399 a;
3354     return a.subLen();
3355 }() == 4);
3356 
3357 /**************************************************
3358     7789 (*p).length++ where *p is null
3359 **************************************************/
3360 
3361 struct S7789
3362 {
fooS77893363     size_t foo()
3364     {
3365         _ary.length += 1;
3366         return _ary.length;
3367     }
3368 
3369     int[] _ary;
3370 }
3371 
3372 static assert(S7789().foo());
3373 
3374 /**************************************************
3375     6418 member named 'length'
3376 **************************************************/
3377 
3378 struct Bug6418
3379 {
lengthBug64183380     size_t length() { return 189; }
3381 }
3382 static assert(Bug6418.init.length == 189);
3383 
3384 /**************************************************
3385     4021 rehash
3386 **************************************************/
3387 
bug4021()3388 bool bug4021()
3389 {
3390     int[int] aa = [1: 1];
3391     aa.rehash;
3392     return true;
3393 }
3394 static assert(bug4021());
3395 
3396 /**************************************************
3397     11629 crash on AA.rehash
3398 **************************************************/
3399 
3400 struct Base11629
3401 {
3402     alias T = ubyte, Char = char;
3403     alias String = immutable(Char)[];
3404 
3405     const Char[T] toChar;
3406 
thisBase116293407     this(int _dummy)
3408     {
3409         Char[T] toCharTmp = [0:'A'];
3410 
3411         toChar = toCharTmp.rehash;
3412     }
3413 }
3414 enum ct11629 = Base11629(4);
3415 
3416 /**************************************************
3417     3512 foreach (dchar; string)
3418     6558 foreach (int, dchar; string)
3419 **************************************************/
3420 
test3512()3421 bool test3512()
3422 {
3423     string s = "öhai";
3424     int q = 0;
3425 
3426     foreach (wchar c; s)
3427     {
3428         if (q == 2)
3429             assert(c == 'a');
3430         ++q;
3431     }
3432     assert(q == 4);
3433 
3434     // _aApplycd1
3435     foreach (dchar c; s)
3436     {
3437         ++q;
3438         if (c == 'h')
3439             break;
3440     }
3441     assert(q == 6);
3442 
3443     // _aApplycw2
3444     foreach (ptrdiff_t i, wchar c; s)
3445     {
3446         assert(i >= 0 && i < s.length);
3447     }
3448 
3449     // _aApplycd2
3450     foreach (ptrdiff_t i, dchar c; s)
3451     {
3452         assert(i >= 0 && i < s.length);
3453     }
3454 
3455     wstring w = "xüm";
3456 
3457     // _aApplywc1
3458     foreach (char c; w)
3459     {
3460         ++q;
3461     }
3462     assert(q == 10);
3463 
3464     // _aApplywd1
3465     foreach (dchar c; w)
3466     {
3467         ++q;
3468     }
3469     assert(q == 13);
3470 
3471     // _aApplywc2
3472     foreach (ptrdiff_t i, char c; w)
3473     {
3474         assert(i >= 0 && i < w.length);
3475     }
3476 
3477     // _aApplywd2
3478     foreach (ptrdiff_t i, dchar c; w)
3479     {
3480         assert(i >= 0 && i < w.length);
3481     }
3482 
3483     dstring d = "yäq";
3484 
3485     // _aApplydc1
3486     q = 0;
3487     foreach (char c; d)
3488     {
3489         ++q;
3490     }
3491     assert(q == 4);
3492 
3493     // _aApplydw1
3494     q = 0;
3495     foreach (wchar c; d)
3496     {
3497         ++q;
3498     }
3499     assert(q == 3);
3500 
3501     // _aApplydc2
3502     foreach (ptrdiff_t i, char c; d)
3503     {
3504         assert(i >= 0 && i < d.length);
3505     }
3506     // _aApplydw2
3507     foreach (ptrdiff_t i, wchar c; d)
3508     {
3509         assert(i >= 0 && i < d.length);
3510     }
3511 
3512     dchar[] dr = "squop"d.dup;
3513 
3514     foreach (ptrdiff_t n, char c; dr)
3515     {
3516         if (n == 2)
3517             break;
3518         assert(c != 'o');
3519     }
3520 
3521     // _aApplyRdc1
3522     foreach_reverse (char c; dr)
3523     {}
3524 
3525     // _aApplyRdw1
3526     foreach_reverse (wchar c; dr)
3527     {}
3528 
3529     // _aApplyRdc2
3530     foreach_reverse (ptrdiff_t n, char c; dr)
3531     {
3532         if (n == 4)
3533             break;
3534         assert(c != 'o');
3535     }
3536 
3537     // _aApplyRdw2
3538     foreach_reverse (ptrdiff_t i, wchar c; dr)
3539     {
3540         assert(i >= 0 && i < dr.length);
3541     }
3542 
3543     q = 0;
3544     wstring w2 = ['x', 'ü', 'm']; // foreach over array literals
3545     foreach_reverse (ptrdiff_t n, char c; w2)
3546     {
3547         ++q;
3548         if (c == 'm') assert(n == 2 && q == 1);
3549         if (c == 'x') assert(n == 0 && q == 4);
3550     }
3551     return true;
3552 }
3553 static assert(test3512());
3554 
3555 /**************************************************
3556     6510 ICE only with -inline
3557 **************************************************/
3558 
3559 struct Stack6510
3560 {
3561     struct Proxy
3562     {
shrinkStack6510::Proxy3563         void shrink() {}
3564     }
3565     Proxy stack;
popStack65103566     void pop()
3567     {
3568         stack.shrink();
3569     }
3570 }
3571 
bug6510()3572 int bug6510()
3573 {
3574     static int used()
3575     {
3576         Stack6510 junk;
3577         junk.pop();
3578         return 3;
3579     }
3580     return used();
3581 }
3582 
test6510()3583 void test6510()
3584 {
3585     static assert(bug6510() == 3);
3586 }
3587 
3588 /**************************************************
3589     6511   arr[] shouldn't make a copy
3590 **************************************************/
3591 
bug6511(T)3592 T bug6511(T)()
3593 {
3594     T[1] a = [1];
3595     a[] += a[];
3596     return a[0];
3597 }
3598 static assert(bug6511!ulong() == 2);
3599 static assert(bug6511!long() == 2);
3600 
3601 /**************************************************
3602     6512   new T[][]
3603 **************************************************/
3604 
bug6512(int m)3605 bool bug6512(int m)
3606 {
3607     auto x = new int[2][][](m, 5);
3608     assert(x.length == m);
3609     assert(x[0].length == 5);
3610     assert(x[0][0].length == 2);
3611     foreach (i; 0.. m)
3612         foreach (j; 0 .. 5)
3613             foreach (k; 0 .. 2)
3614                 x[i][j][k] = k + j*10 + i*100;
3615     foreach (i; 0.. m)
3616         foreach (j; 0 .. 5)
3617             foreach (k; 0 .. 2)
3618                 assert(x[i][j][k] == k + j*10 + i*100);
3619     return true;
3620 }
3621 static assert(bug6512(3));
3622 
3623 /**************************************************
3624     6516   ICE(constfold.c)
3625 **************************************************/
3626 
bug6516()3627 dstring bug6516()
3628 {
3629     return cast(dstring)new dchar[](0);
3630 }
3631 
3632 static assert(bug6516() == ""d);
3633 
3634 /**************************************************
3635     6727   ICE(interpret.c)
3636 **************************************************/
3637 
ice6727(const (char)* z)3638 const(char)* ice6727(const(char)* z) { return z; }
3639 static assert({
3640     auto q = ice6727("a".dup.ptr);
3641     return true;
3642 }());
3643 
3644 /**************************************************
3645     6721   Cannot get pointer to start of char[]
3646 **************************************************/
3647 
3648 static assert({
3649     char[] c1 = "".dup;
3650     auto p = c1.ptr;
3651     string c2 = "";
3652     auto p2 = c2.ptr;
3653     return 6;
3654 }() == 6);
3655 
3656 /**************************************************
3657     6693   Assign to null AA
3658 **************************************************/
3659 
3660 struct S6693
3661 {
3662     int[int] m;
3663 }
3664 
3665 static assert({
3666     int[int][int] aaa;
3667     aaa[3][1] = 4;
3668     int[int][3] aab;
3669     aab[2][1] = 4;
3670     S6693 s;
3671     s.m[2] = 4;
3672     return 6693;
3673 }() == 6693);
3674 
3675 /**************************************************
3676     7602   Segfault AA.keys on null AA
3677 **************************************************/
3678 
test7602()3679 string[] test7602()
3680 {
3681     int[string] array;
3682     return array.keys;
3683 }
3684 
3685 enum bug7602 = test7602();
3686 
3687 /**************************************************
3688     6739   Nested AA assignment
3689 **************************************************/
3690 
3691 static assert({
3692     int[int][int][int] aaa;
3693     aaa[3][1][6] = 14;
3694     return aaa[3][1][6];
3695 }() == 14);
3696 
3697 static assert({
3698     int[int][int] aaa;
3699     aaa[3][1] = 4;
3700     aaa[3][3] = 3;
3701     aaa[1][5] = 9;
3702     auto kk = aaa[1][5];
3703     return kk;
3704 }() == 9);
3705 
3706 /**************************************************
3707     6751   ref AA assignment
3708 **************************************************/
3709 
bug6751(ref int[int]aa)3710 void bug6751(ref int[int] aa)
3711 {
3712     aa[1] = 2;
3713 }
3714 
3715 static assert({
3716     int[int] aa;
3717     bug6751(aa);
3718     assert(aa[1] == 2);
3719     return true;
3720 }());
3721 
bug6751b(ref int[int][int]aa)3722 void bug6751b(ref int[int][int] aa)
3723 {
3724     aa[1][17] = 2;
3725 }
3726 
3727 struct S6751
3728 {
3729     int[int][int] aa;
3730     int[int] bb;
3731 }
3732 
3733 static assert({
3734     S6751 s;
3735     bug6751b(s.aa);
3736     assert(s.aa[1][17] == 2);
3737     return true;
3738 }());
3739 
3740 static assert({
3741     S6751 s;
3742     s.aa[7][56] = 57;
3743     bug6751b(s.aa);
3744     assert(s.aa[1][17] == 2);
3745     assert(s.aa[7][56] == 57);
3746     bug6751c(s.aa);
3747     assert(s.aa.keys.length == 1);
3748     assert(s.aa.values.length == 1);
3749     return true;
3750 }());
3751 
3752 static assert({
3753     S6751 s;
3754     s.bb[19] = 97;
3755     bug6751(s.bb);
3756     assert(s.bb[1] == 2);
3757     assert(s.bb[19] == 97);
3758     return true;
3759 }());
3760 
bug6751c(ref int[int][int]aa)3761 void bug6751c(ref int[int][int] aa)
3762 {
3763     aa = [38: [56 : 77]];
3764 }
3765 
3766 /**************************************************
3767    7790   AA foreach ref
3768 **************************************************/
3769 
3770 struct S7790
3771 {
3772     size_t id;
3773 }
3774 
bug7790(S7790[string]tree)3775 size_t bug7790(S7790[string] tree)
3776 {
3777     foreach (k, ref v; tree)
3778         v.id = 1;
3779     return tree["a"].id;
3780 }
3781 
3782 static assert(bug7790(["a":S7790(0)]) == 1);
3783 
3784 /**************************************************
3785     6765   null AA.length
3786 **************************************************/
3787 
3788 static assert({
3789     int[int] w;
3790     return w.length;
3791 }() == 0);
3792 
3793 /**************************************************
3794     6769   AA.keys, AA.values with -inline
3795 **************************************************/
3796 
3797 static assert({
3798     double[char[3]] w = ["abc" : 2.3];
3799     double[] z = w.values;
3800     return w.keys.length;
3801 }() == 1);
3802 
3803 /**************************************************
3804     4022   AA.get
3805 **************************************************/
3806 
3807 static assert({
3808     int[int] aa = [58: 13];
3809     int r = aa.get(58, 1000);
3810     assert(r == 13);
3811     r = aa.get(59, 1000);
3812     return r;
3813 }() == 1000);
3814 
3815 /**************************************************
3816     6775 AA.opApply
3817 **************************************************/
3818 
3819 static assert({
3820     int[int] aa = [58: 17, 45:6];
3821     int valsum = 0;
3822     int keysum = 0;
3823     foreach (m; aa)  // aaApply
3824     {
3825         valsum += m;
3826     }
3827     assert(valsum == 17 + 6);
3828     valsum = 0;
3829     foreach (n, m; aa)  // aaApply2
3830     {
3831         valsum += m;
3832         keysum += n;
3833     }
3834     assert(valsum == 17 + 6);
3835     assert(keysum == 58 + 45);
3836     // Check empty AA
3837     valsum = 0;
3838     int[int] bb;
3839     foreach (m; bb)
3840     {
3841         ++valsum;
3842     }
3843     assert(valsum == 0);
3844     return true;
3845 }());
3846 
3847 /**************************************************
3848     7890   segfault struct with AA field
3849 **************************************************/
3850 
3851 struct S7890
3852 {
3853     int[int] tab;
3854 }
3855 
bug7890()3856 S7890 bug7890()
3857 {
3858     S7890 foo;
3859     foo.tab[0] = 0;
3860     return foo;
3861 }
3862 
3863 enum e7890 = bug7890();
3864 
3865 /**************************************************
3866     AA.remove
3867 **************************************************/
3868 
3869 static assert({
3870     int[int] aa = [58: 17, 45:6];
3871     aa.remove(45);
3872     assert(aa.length == 1);
3873     aa.remove(7);
3874     assert(aa.length == 1);
3875     aa.remove(58);
3876     assert(aa.length == 0);
3877     return true;
3878 }());
3879 
3880 /**************************************************
3881     try, finally
3882 **************************************************/
3883 
3884 static assert({
3885     int n = 0;
3886 
3887     try
3888     {
3889         n = 1;
3890     }
3891     catch (Exception e)
3892     {}
3893     assert(n == 1);
3894 
3895     try
3896     {
3897         n = 2;
3898     }
3899     catch (Exception e)
3900     {}
3901     finally
3902     {
3903         assert(n == 2);
3904         n = 3;
3905     }
3906     assert(n == 3);
3907     return true;
3908 }());
3909 
3910 /**************************************************
3911     6800 bad pointer casts
3912 **************************************************/
3913 
badpointer(int k)3914 bool badpointer(int k)
3915 {
3916     int m = 6;
3917     int* w =  &m;
3918     assert(*w == 6);
3919     int[3] a = [17, 2, 21];
3920     int* w2 = &a[2];
3921     assert(*w2 == 21);
3922 
3923     // cast int* to uint* is OK
3924     uint* u1 = cast(uint*)w;
3925     assert(*u1 == 6);
3926     uint* u2 = cast(uint*)w2;
3927     assert(*u2 == 21);
3928     uint* u3 = cast(uint*)&m;
3929     assert(*u3 == 6);
3930     // cast int* to void* is OK
3931     void* v1 = cast(void*)w;
3932     void* v3 = &m;
3933     void* v4 = &a[0];
3934     // cast from void* back to int* is OK
3935     int* t3 = cast(int*)v3;
3936     assert(*t3 == 6);
3937     int* t4 = cast(int*)v4;
3938     assert(*t4 == 17);
3939     // cast from void* to uint* is OK
3940     uint* t1 = cast(uint*)v1;
3941     assert(*t1 == 6);
3942     // and check that they're real pointers
3943     m = 18;
3944     assert(*t1 == 18);
3945     assert(*u3 == 18);
3946 
3947     int** p = &w;
3948 
3949     if (k == 1) // bad reinterpret
3950         double *d1 = cast(double*)w;
3951     if (k == 3) // bad reinterpret
3952         char* d3 = cast(char*)w2;
3953     if (k == 4) {
3954         void* q1 = cast(void*)p;    // OK-void is int*
3955         void* *q = cast(void**)p;   // OK-void is int
3956     }
3957     if (k == 5)
3958         void*** q = cast(void***)p;  // bad: too many *
3959     if (k == 6) // bad reinterpret through void*
3960         double* d1 = cast(double*)v1;
3961     if (k == 7)
3962         double* d7 = cast(double*)v4;
3963     if (k == 8)
3964         ++v4; // can't do pointer arithmetic on void*
3965     return true;
3966 }
3967 static assert(badpointer(4));
3968 static assert(!is(typeof(compiles!(badpointer(1)))));
3969 static assert( is(typeof(compiles!(badpointer(2)))));
3970 static assert(!is(typeof(compiles!(badpointer(3)))));
3971 static assert( is(typeof(compiles!(badpointer(4)))));
3972 static assert(!is(typeof(compiles!(badpointer(5)))));
3973 static assert(!is(typeof(compiles!(badpointer(6)))));
3974 static assert(!is(typeof(compiles!(badpointer(7)))));
3975 static assert(!is(typeof(compiles!(badpointer(8)))));
3976 
3977 /**************************************************
3978     10211 Allow casts S**->D**, when S*->D* is OK
3979 **************************************************/
3980 
bug10211()3981 int bug10211()
3982 {
3983     int m = 7;
3984     int* x = &m;
3985     int** y = &x;
3986     assert(**y == 7);
3987     uint* p = cast(uint*)x;
3988     uint** q = cast(uint**)y;
3989     return 1;
3990 }
3991 
3992 static assert(bug10211());
3993 
3994 /**************************************************
3995     10568 CTFE rejects function pointer safety casts
3996 **************************************************/
3997 
safetyDance()3998 @safe void safetyDance() {}
3999 
isItSafeToDance()4000 int isItSafeToDance()
4001 {
4002     void function() @trusted yourfriends = &safetyDance;
4003     void function() @safe nofriendsOfMine = yourfriends;
4004     return 1;
4005 }
4006 
4007 static assert(isItSafeToDance());
4008 
4009 /**************************************************
4010     12296 CTFE rejects const compatible AA pointer cast
4011 **************************************************/
4012 
test12296()4013 int test12296()
4014 {
4015     immutable x = [5 : 4];
4016     auto aa = &x;
4017     const(int[int])* y = aa;
4018     return 1;
4019 }
4020 static assert(test12296());
4021 
4022 /**************************************************
4023     9170 Allow reinterpret casts float<->int
4024 **************************************************/
4025 
f9170(float x)4026 int f9170(float x)
4027 {
4028     return *(cast(int*)&x);
4029 }
4030 
i9170(int x)4031 float i9170(int x)
4032 {
4033     return *(cast(float*)&x);
4034 }
4035 
u9170(uint x)4036 float u9170(uint x)
4037 {
4038     return *(cast(float*)&x);
4039 }
4040 
f9170arr(float[]x)4041 int f9170arr(float[] x)
4042 {
4043     return *(cast(int*)&(x[1]));
4044 }
4045 
d9170(double x)4046 long d9170(double x)
4047 {
4048     return *(cast(long*)&x);
4049 }
4050 
fref9170(ref float x)4051 int fref9170(ref float x)
4052 {
4053     return *(cast(int*)&x);
4054 }
4055 
dref9170(ref double x)4056 long dref9170(ref double x)
4057 {
4058     return *(cast(long*)&x);
4059 }
4060 
bug9170()4061 bool bug9170()
4062 {
4063     float f = 1.25;
4064     double d = 1.25;
4065     assert(f9170(f) == 0x3FA0_0000);
4066     assert(fref9170(f) == 0x3FA0_0000);
4067     assert(d9170(d) == 0x3FF4_0000_0000_0000L);
4068     assert(dref9170(d) == 0x3FF4_0000_0000_0000L);
4069     float [3] farr = [0, 1.25, 0];
4070     assert(f9170arr(farr) == 0x3FA0_0000);
4071     int i = 0x3FA0_0000;
4072     assert(i9170(i) == 1.25);
4073     uint u = 0x3FA0_0000;
4074     assert(u9170(u) == 1.25);
4075     return true;
4076 }
4077 
4078 static assert(bug9170());
4079 
4080 /**************************************************
4081     6792 ICE with pointer cast of indexed array
4082 **************************************************/
4083 
4084 struct S6792
4085 {
4086     int i;
4087 }
4088 
4089 static assert({
4090     {
4091         void* p;
4092         p = [S6792(1)].ptr;
4093         S6792 s = *(cast(S6792*)p);
4094         assert(s.i == 1);
4095     }
4096     {
4097         void*[] ary;
4098         ary ~= [S6792(2)].ptr;
4099         S6792 s = *(cast(S6792*)ary[0]);
4100         assert(s.i == 2);
4101     }
4102     {
4103         void*[7] ary;
4104         ary[6]= [S6792(2)].ptr;
4105         S6792 s = *(cast(S6792*)ary[6]);
4106         assert(s.i == 2);
4107     }
4108     {
4109         void* p;
4110         p = [S6792(1)].ptr;
4111         void*[7] ary;
4112         ary[5]= p;
4113         S6792 s = *(cast(S6792*)ary[5]);
4114         assert(s.i == 1);
4115     }
4116     {
4117         S6792*[string] aa;
4118         aa["key"] = [S6792(3)].ptr;
4119         const(S6792) s = *(cast(const(S6792)*)aa["key"]);
4120         assert(s.i == 3);
4121     }
4122     {
4123         S6792[string] blah;
4124         blah["abc"] = S6792(6);
4125         S6792*[string] aa;
4126         aa["kuy"] = &blah["abc"];
4127         const(S6792) s = *(cast(const(S6792)*)aa["kuy"]);
4128         assert(s.i == 6);
4129 
4130         void*[7] ary;
4131         ary[5]= &blah["abc"];
4132         S6792 t = *(cast(S6792*)ary[5]);
4133         assert(t.i == 6);
4134 
4135         int q = 6;
4136         ary[3]= &q;
4137         int gg = *(cast(int*)(ary[3]));
4138     }
4139     return true;
4140 }());
4141 
4142 /**************************************************
4143    7780 array cast
4144 **************************************************/
4145 
bug7780(int testnum)4146 int bug7780(int testnum)
4147 {
4148     int[] y = new int[2];
4149     y[0] = 2000000;
4150     if (testnum == 1)
4151     {
4152         void[] x = y;
4153         return (cast(byte[])x)[1];
4154     }
4155     if (testnum == 2)
4156     {
4157         int[] x = y[0 .. 1];
4158         return (cast(byte[])x)[1];
4159     }
4160     return 1;
4161 }
4162 
4163 static assert( is(typeof(compiles!(bug7780(0)))));
4164 static assert(!is(typeof(compiles!(bug7780(1)))));
4165 static assert(!is(typeof(compiles!(bug7780(2)))));
4166 
4167 /**************************************************
4168     14028 - static array pointer that refers existing array elements.
4169 **************************************************/
4170 
test14028a(size_t ofs)4171 int test14028a(size_t ofs)(bool ct)
4172 {
4173     int[4] a;
4174     int[2]* p;
4175     int num = ofs;
4176 
4177     if (ct)
4178         p = cast(int[2]*)&a[ofs];    // SymOffExp
4179     else
4180         p = cast(int[2]*)&a[num];    // CastExp + AddrExp
4181 
4182     // pointers comparison
4183     assert(cast(void*)a.ptr <= cast(void*)p);
4184     assert(cast(void*)a.ptr <= cast(void*)&(*p)[0]);
4185     assert(cast(void*)&a[0] <= cast(void*)p);
4186 
4187     return 1;
4188 }
4189 static assert(test14028a!0(true));
4190 static assert(test14028a!0(false));
4191 static assert(test14028a!3(true));
4192 static assert(test14028a!3(false));
4193 static assert(!is(typeof(compiles!(test14028a!4(true)))));
4194 static assert(!is(typeof(compiles!(test14028a!4(false)))));
4195 
test14028b(int num)4196 int test14028b(int num)
4197 {
4198     int[4] a;
4199     int[2]* p;
4200 
4201     if (num == 1)
4202     {
4203         p = cast(int[2]*)&a[0]; // &a[0..2];
4204         (*p)[0] = 1;            // a[0] = 1
4205         (*p)[1] = 2;            // a[1] = 2
4206         assert(a == [1,2,0,0]);
4207         p = p + 1;              // &a[0] -> &a[2]
4208         (*p)[0] = 3;            // a[2] = 3
4209         (*p)[1] = 4;            // a[3] = 4
4210         assert(a == [1,2,3,4]);
4211     }
4212     if (num == 2)
4213     {
4214         p = cast(int[2]*)&a[1]; // &a[1..3];
4215         (*p)[0] = 1;            // a[1] = 1
4216         p = p + 1;              // &a[1..3] -> &a[3..5]
4217         (*p)[0] = 2;            // a[3] = 2
4218         assert(a == [0,1,0,2]);
4219     }
4220     if (num == 3)
4221     {
4222         p = cast(int[2]*)&a[1]; // &a[1..3];
4223         (*p)[0] = 1;            // a[1] = 1
4224         p = p + 1;              // &a[1..3] -> &a[3..5]
4225         (*p)[0] = 2;            // a[3] = 2
4226         (*p)[1] = 3;            // a[4] = 3 (CTFE error)
4227     }
4228     if (num == 4)
4229     {
4230         p = cast(int[2]*)&a[0]; // &a[0..2];
4231         p = p + 1;              // &a[0..2] -> &a[2..4]
4232         p = p + 1;              // &a[2..4] -> &a[4..6] (ok)
4233     }
4234     if (num == 5)
4235     {
4236         p = cast(int[2]*)&a[1]; // &a[1..3];
4237         p = p + 2;              // &a[1..3] -> &a[5..7] (CTFE error)
4238     }
4239     return 1;
4240 }
4241 static assert(test14028b(1));
4242 static assert(test14028b(2));
4243 static assert(!is(typeof(compiles!(test14028b(3)))));
4244 static assert(test14028b(4));
4245 static assert(!is(typeof(compiles!(test14028b(5)))));
4246 
4247 /**************************************************
4248     10275 cast struct literals to immutable
4249 **************************************************/
4250 
4251 struct Bug10275
4252 {
4253     uint[] ivals;
4254 }
4255 
bug10275()4256 Bug10275 bug10275()
4257 {
4258     return Bug10275([1, 2, 3]);
4259 }
4260 
test10275()4261 int test10275()
4262 {
4263     immutable(Bug10275) xxx = cast(immutable(Bug10275))bug10275();
4264     return 1;
4265 }
4266 
4267 static assert(test10275());
4268 
4269 /**************************************************
4270     6851 passing pointer by argument
4271 **************************************************/
4272 
set6851(int * pn)4273 void set6851(int* pn)
4274 {
4275     *pn = 20;
4276 }
bug6851()4277 void bug6851()
4278 {
4279     int n = 0;
4280     auto pn = &n;
4281     *pn = 10;
4282     assert(n == 10);
4283     set6851(&n);
4284 }
4285 static assert({ bug6851(); return true; }());
4286 
4287 /**************************************************
4288     7876
4289 **************************************************/
4290 
bug7876(int n)4291 int* bug7876(int n) @system
4292 {
4293     int x;
4294     auto ptr = &x;
4295     if (n == 2)
4296         ptr = null;
4297     return ptr;
4298 }
4299 
4300 struct S7876
4301 {
4302     int* p;
4303 }
4304 
bug7876b(int n)4305 S7876 bug7876b(int n) @system
4306 {
4307     int x;
4308     S7876 s;
4309     s.p = &x;
4310     if (n == 11)
4311         s.p = null;
4312     return s;
4313 }
4314 
test7876(int n)4315 int test7876(int n)
4316 {
4317     if (n >= 10)
4318     {
4319         S7876 m = bug7876b(n);
4320         return 1;
4321     }
4322     int* p = bug7876(n);
4323     return 1;
4324 }
4325 
4326 static assert( is(typeof(compiles!(test7876(2)))));
4327 static assert(!is(typeof(compiles!(test7876(0)))));
4328 static assert( is(typeof(compiles!(test7876(11)))));
4329 static assert(!is(typeof(compiles!(test7876(10)))));
4330 
4331 /**************************************************
4332     11824
4333 **************************************************/
4334 
f11824(T)4335 int f11824(T)()
4336 {
4337     T[] arr = new T[](1);
4338     T* getAddr(ref T a)
4339     {
4340         return &a;
4341     }
4342     getAddr(arr[0]);
4343     return 1;
4344 }
4345 static assert(f11824!int());        // OK
4346 static assert(f11824!(int[])());    // OK <- NG
4347 
4348 /**************************************************
4349     6817 if converted to &&, only with -inline
4350 **************************************************/
4351 
4352 static assert({
4353     void toggle()
4354     {
4355         bool b;
4356         if (b)
4357             b = false;
4358     }
4359     toggle();
4360     return true;
4361 }());
4362 
4363 /**************************************************
4364     cast to void
4365 **************************************************/
4366 
4367 static assert({
4368     cast(void)(71);
4369     return true;
4370 }());
4371 
4372 /**************************************************
4373     6816 nested function can't access this
4374 **************************************************/
4375 
4376 struct S6816
4377 {
fooS68164378     size_t foo()
4379     {
4380         return (){ return value +1 ; }();
4381     }
4382     size_t value;
4383 }
4384 
4385 enum s6816 = S6816().foo();
4386 
4387 /**************************************************
4388     7277 ICE nestedstruct.init.tupleof
4389 **************************************************/
4390 
4391 struct Foo7277
4392 {
4393     int a;
funcFoo72774394     int func()
4395     {
4396         int b;
4397         void nested()
4398         {
4399             b = 7;
4400             a = 10;
4401         }
4402         nested();
4403         return a+b;
4404     }
4405 }
4406 
4407 static assert(Foo7277().func() == 17);
4408 
4409 /**************************************************
4410     10217 ICE. CTFE version of 9315
4411 **************************************************/
4412 
bug10217()4413 bool bug10217()
4414 {
4415     struct S
4416     {
4417         int i;
4418         void bar() {}
4419     }
4420     auto yyy = S.init.tupleof[$ - 1];
4421     assert(!yyy);
4422     return 1;
4423 }
4424 
4425 static assert(bug10217());
4426 
4427 /**************************************************
4428     8276 ICE
4429 **************************************************/
4430 
bug8676(int n)4431 void bug8676(int n)
4432 {
4433     const int X1 = 4 + n;
4434     const int X2 = 4;
4435     int X3 = 4;
4436     int bar1() { return X1; }
4437     int bar2() { return X2; }
4438     int bar3() { return X3; }
4439     static assert(!is(typeof(compiles!(bar1()))));
4440     static assert( is(typeof(compiles!(bar2()))));
4441     static assert(!is(typeof(compiles!(bar3()))));
4442 }
4443 
4444 /**************************************************
4445     classes and interfaces
4446 **************************************************/
4447 
4448 interface SomeInterface
4449 {
4450     int daz();
4451     float bar(char);
4452     int baz();
4453 }
4454 
4455 interface SomeOtherInterface
4456 {
4457     int xxx();
4458 }
4459 
4460 class TheBase : SomeInterface, SomeOtherInterface
4461 {
4462     int q = 88;
4463     int rad = 61;
4464     int a = 14;
somebaseclassfunc()4465     int somebaseclassfunc() { return 28; }
daz()4466     int daz() { return 0; }
baz()4467     int baz() { return 0; }
xxx()4468     int xxx() { return 762; }
foo()4469     int foo() { return q; }
bar(char c)4470     float bar(char c) { return 3.6; }
4471 }
4472 
4473 class SomeClass : TheBase, SomeInterface
4474 {
4475     int gab = 9;
4476     int fab;
4477     int a = 17;
4478     int b = 23;
foo()4479     override int foo() { return gab + a; }
bar(char c)4480     override float bar(char c) { return 2.6; }
something()4481     int something() { return 0; }
daz()4482     override int daz() { return 0; }
baz()4483     override int baz() { return 0; }
4484 }
4485 
4486 class Unrelated : TheBase
4487 {
this(int x)4488     this(int x) { a = x; }
4489 }
4490 
classtest1(int n)4491 auto classtest1(int n)
4492 {
4493     SomeClass c = new SomeClass;
4494     assert(c.a == 17);
4495     assert(c.q == 88);
4496     TheBase d = c;
4497     assert(d.a == 14);
4498     assert(d.q == 88);
4499     if (n == 7)
4500     {
4501         // bad cast -- should fail
4502         Unrelated u = cast(Unrelated)d;
4503         assert(u is null);
4504     }
4505     SomeClass e = cast(SomeClass)d;
4506     d.q = 35;
4507     assert(c.q == 35);
4508     assert(c.foo() == 9 + 17);
4509     ++c.a;
4510     assert(c.foo() == 9 + 18);
4511     assert(d.foo() == 9 + 18);
4512     d = new TheBase;
4513     SomeInterface fc = c;
4514     SomeOtherInterface ot = c;
4515     assert(fc.bar('x') == 2.6);
4516     assert(ot.xxx() == 762);
4517     fc = d;
4518     ot = d;
4519     assert(fc.bar('x') == 3.6);
4520     assert(ot.xxx() == 762);
4521 
4522     Unrelated u2 = new Unrelated(7);
4523     assert(u2.a == 7);
4524     return 6;
4525 }
4526 static assert(classtest1(1));
4527 static assert(classtest1(2));
4528 static assert(classtest1(7)); // bug 7154
4529 
4530 // can't initialize enum with not null class
classtest2(int n)4531 SomeClass classtest2(int n)
4532 {
4533     return n == 5 ? (new SomeClass) : null;
4534 }
4535 static assert( is(typeof((){ enum const(SomeClass) xx = classtest2(2);}())));
4536 static assert(!is(typeof((){ enum const(SomeClass) xx = classtest2(5);}())));
4537 
4538 class RecursiveClass
4539 {
4540    int x;
this(int n)4541    this(int n) { x = n; }
4542    RecursiveClass b;
doit()4543    void doit() { b = new RecursiveClass(7); b.x = 2;}
4544 }
4545 
classtest3()4546 int classtest3()
4547 {
4548     RecursiveClass x = new RecursiveClass(17);
4549     x.doit();
4550     RecursiveClass y = x.b;
4551     assert(y.x == 2);
4552     assert(x.x == 17);
4553     return 1;
4554 }
4555 
4556 static assert(classtest3());
4557 
4558 /**************************************************
4559     12016 class cast and qualifier reinterpret
4560 **************************************************/
4561 
4562 class B12016 { }
4563 
4564 class C12016 : B12016 { }
4565 
f12016(immutable B12016 b)4566 bool f12016(immutable B12016 b)
4567 {
4568     assert(b);
4569     return true;
4570 }
4571 
4572 static assert(f12016(new immutable C12016));
4573 
4574 /**************************************************
4575     10610 ice immutable implicit conversion
4576 **************************************************/
4577 
Bug10610(T)4578 class Bug10610(T)
4579 {
4580     int baz() immutable
4581     {
4582         return 1;
4583     }
4584     static immutable(Bug10610!T) min = new Bug10610!T();
4585 }
4586 
ice10610()4587 void ice10610()
4588 {
4589    alias T10610 = Bug10610!(int);
4590    static assert (T10610.min.baz());
4591 }
4592 
4593 /**************************************************
4594     13141 regression fix caused by 10610
4595 **************************************************/
4596 
MapResult13141(alias pred)4597 struct MapResult13141(alias pred)
4598 {
4599     int[] range;
4600     @property empty() { return range.length == 0; }
4601     @property front() { return pred(range[0]); }
4602     void popFront() { range = range[1 .. $]; }
4603 }
4604 
array13141(R)4605 string[] array13141(R)(R r)
4606 {
4607     typeof(return) result;
4608     foreach (e; r)
4609         result ~= e;
4610     return result;
4611 }
4612 
4613 //immutable string[] splitterNames = [4].map!(e => "4").array();
4614 immutable string[] splitterNames13141 = MapResult13141!(e => "4")([4]).array13141();
4615 
4616 /**************************************************
4617     11587 AA compare
4618 **************************************************/
4619 
4620 static assert([1:2, 3:4] == [3:4, 1:2]);
4621 
4622 /**************************************************
4623     14325 more AA comparisons
4624 **************************************************/
4625 
4626 static assert([1:1] != [1:2, 2:1]);      // OK
4627 static assert([1:1] != [1:2]);           // OK
4628 static assert([1:1] != [2:1]);           // OK <- Error
4629 static assert([1:1, 2:2] != [3:3, 4:4]); // OK <- Error
4630 
4631 /**************************************************
4632     7147 typeid()
4633 **************************************************/
4634 
4635 static assert({
4636     TypeInfo xxx = typeid(Object);
4637     TypeInfo yyy = typeid(new Error("xxx"));
4638     return true;
4639 }());
4640 
bug7147(int n)4641 int bug7147(int n)
4642 {
4643     Error err = n ? new Error("xxx") : null;
4644     TypeInfo qqq = typeid(err);
4645     return 1;
4646 }
4647 
4648 // Must not segfault if class is null
4649 static assert(!is(typeof(compiles!(bug7147(0)))));
4650 static assert( is(typeof(compiles!(bug7147(1)))));
4651 
4652 
4653 /**************************************************
4654     14123 - identity TypeInfo objects
4655 **************************************************/
4656 
4657 static assert({
4658     bool eq(TypeInfo t1, TypeInfo t2)
4659     {
4660         return t1 is t2;
4661     }
4662 
4663     class C {}
4664     struct S {}
4665 
4666     assert( eq(typeid(C), typeid(C)));
4667     assert(!eq(typeid(C), typeid(Object)));
4668     assert( eq(typeid(S), typeid(S)));
4669     assert(!eq(typeid(S), typeid(int)));
4670     assert( eq(typeid(int), typeid(int)));
4671     assert(!eq(typeid(int), typeid(long)));
4672 
4673     Object o = new Object;
4674     Object c = new C;
4675     assert( eq(typeid(o), typeid(o)));
4676     assert(!eq(typeid(c), typeid(o)));
4677     assert(!eq(typeid(o), typeid(S)));
4678 
4679     return 1;
4680 }());
4681 
4682 /**************************************************
4683     6885 wrong code with new array
4684 **************************************************/
4685 
4686 struct S6885
4687 {
4688     int p;
4689 }
4690 
bug6885()4691 int bug6885()
4692 {
4693     auto array = new double[1][2];
4694     array[1][0] = 6;
4695     array[0][0] = 1;
4696     assert(array[1][0] == 6);
4697 
4698     auto barray = new S6885[2];
4699     barray[1].p = 5;
4700     barray[0].p = 2;
4701     assert(barray[1].p == 5);
4702     return 1;
4703 }
4704 
4705 static assert(bug6885());
4706 
4707 /**************************************************
4708     6886 ICE with new array of dynamic arrays
4709 **************************************************/
4710 
bug6886()4711 int bug6886()
4712 {
4713     auto carray = new int[][2];
4714     carray[1] = [6];
4715     carray[0] = [4];
4716     assert(carray[1][0] == 6);
4717     return 1;
4718 }
4719 
4720 static assert(bug6886());
4721 
4722 /**************************************************
4723     10198 Multidimensional struct block initializer
4724 **************************************************/
4725 
4726 struct Block10198
4727 {
4728     int val[3][4];
4729 }
4730 
bug10198()4731 int bug10198()
4732 {
4733     Block10198 pp = Block10198(67);
4734     assert(pp.val[2][3] == 67);
4735     assert(pp.val[1][3] == 67);
4736     return 1;
4737 }
4738 static assert(bug10198());
4739 
4740 /**************************************************
4741     14440 Multidimensional block initialization should create distinct arrays for each elements
4742 **************************************************/
4743 
Matrix14440(E,size_t row,size_t col)4744 struct Matrix14440(E, size_t row, size_t col)
4745 {
4746     E[col][row] array2D;
4747 
4748     @safe pure nothrow
4749     this(E[row * col] numbers...)
4750     {
4751         foreach (r; 0 .. row)
4752         {
4753             foreach (c; 0 .. col)
4754             {
4755                 array2D[r][c] = numbers[r * col + c];
4756             }
4757         }
4758     }
4759 }
4760 
test14440()4761 void test14440()
4762 {
4763     // Replace 'enum' with 'auto' here and it will work fine.
4764     enum matrix = Matrix14440!(int, 3, 3)(
4765         1, 2, 3,
4766         4, 5, 6,
4767         7, 8, 9
4768     );
4769 
4770     static assert(matrix.array2D[0][0] == 1);
4771     static assert(matrix.array2D[0][1] == 2);
4772     static assert(matrix.array2D[0][2] == 3);
4773     static assert(matrix.array2D[1][0] == 4);
4774     static assert(matrix.array2D[1][1] == 5);
4775     static assert(matrix.array2D[1][2] == 6);
4776     static assert(matrix.array2D[2][0] == 7);
4777     static assert(matrix.array2D[2][1] == 8);
4778     static assert(matrix.array2D[2][2] == 9);
4779 }
4780 
4781 /****************************************************
4782  * Exception chaining tests from xtest46.d
4783  ****************************************************/
4784 
4785 class A75
4786 {
raise(string s)4787     pure static void raise(string s)
4788     {
4789         throw new Exception(s);
4790     }
4791 }
4792 
test75()4793 int test75()
4794 {
4795     int x = 0;
4796     try
4797     {
4798         A75.raise("a");
4799     }
4800     catch (Exception e)
4801     {
4802         x = 1;
4803     }
4804     assert(x == 1);
4805     return 1;
4806 }
4807 static assert(test75());
4808 
4809 /****************************************************
4810  * Exception chaining tests from test4.d
4811  ****************************************************/
4812 
test4_test54()4813 int test4_test54()
4814 {
4815     int status = 0;
4816 
4817     try
4818     {
4819         try
4820         {
4821             status++;
4822             assert(status == 1);
4823             throw new Exception("first");
4824         }
4825         finally
4826         {
4827             status++;
4828             assert(status == 2);
4829             status++;
4830             throw new Exception("second");
4831         }
4832     }
4833     catch (Exception e)
4834     {
4835         assert(e.msg == "first");
4836         assert(e.next.msg == "second");
4837     }
4838     return true;
4839 }
4840 
4841 static assert(test4_test54());
4842 
foo55()4843 void foo55()
4844 {
4845     try
4846     {
4847         Exception x = new Exception("second");
4848         throw x;
4849     }
4850     catch (Exception e)
4851     {
4852         assert(e.msg == "second");
4853     }
4854 }
4855 
test4_test55()4856 int test4_test55()
4857 {
4858     int status = 0;
4859     try
4860     {
4861         try
4862         {
4863             status++;
4864             assert(status == 1);
4865             Exception x = new Exception("first");
4866             throw x;
4867         }
4868         finally
4869         {
4870             status++;
4871             assert(status == 2);
4872             status++;
4873             foo55();
4874         }
4875     }
4876     catch (Exception e)
4877     {
4878         assert(e.msg == "first");
4879         assert(status == 3);
4880     }
4881     return 1;
4882 }
4883 
4884 static assert(test4_test55());
4885 
4886 /****************************************************
4887  * Exception chaining tests from eh.d
4888  ****************************************************/
4889 
bug1513outer()4890 void bug1513outer()
4891 {
4892     int result1513;
4893 
4894     void bug1513a()
4895     {
4896          throw new Exception("d");
4897     }
4898 
4899     void bug1513b()
4900     {
4901         try
4902         {
4903             try
4904             {
4905                 bug1513a();
4906             }
4907             finally
4908             {
4909                 result1513 |= 4;
4910                 throw new Exception("f");
4911             }
4912         }
4913         catch (Exception e)
4914         {
4915             assert(e.msg == "d");
4916             assert(e.next.msg == "f");
4917             assert(!e.next.next);
4918         }
4919     }
4920 
4921     void bug1513c()
4922     {
4923         try
4924         {
4925             try
4926             {
4927                 throw new Exception("a");
4928             }
4929             finally
4930             {
4931                 result1513 |= 1;
4932                 throw new Exception("b");
4933             }
4934         }
4935         finally
4936         {
4937             bug1513b();
4938             result1513 |= 2;
4939             throw new Exception("c");
4940         }
4941     }
4942 
4943     void bug1513()
4944     {
4945         result1513 = 0;
4946         try
4947         {
4948             bug1513c();
4949         }
4950         catch (Exception e)
4951         {
4952             assert(result1513 == 7);
4953             assert(e.msg == "a");
4954             assert(e.next.msg == "b");
4955             assert(e.next.next.msg == "c");
4956         }
4957     }
4958 
4959     bug1513();
4960 }
4961 
collideone()4962 void collideone()
4963 {
4964     try
4965     {
4966         throw new Exception("x");
4967     }
4968     finally
4969     {
4970         throw new Exception("y");
4971     }
4972 }
4973 
doublecollide()4974 void doublecollide()
4975 {
4976     try
4977     {
4978         try
4979         {
4980             try
4981             {
4982                 throw new Exception("p");
4983             }
4984             finally
4985             {
4986                 throw new Exception("q");
4987             }
4988         }
4989         finally
4990         {
4991             collideone();
4992         }
4993     }
4994     catch (Exception e)
4995     {
4996         assert(e.msg == "p");
4997         assert(e.next.msg == "q");
4998         assert(e.next.next.msg == "x");
4999         assert(e.next.next.next.msg == "y");
5000         assert(!e.next.next.next.next);
5001     }
5002 }
5003 
collidetwo()5004 void collidetwo()
5005 {
5006     try
5007     {
5008         try
5009         {
5010             throw new Exception("p2");
5011         }
5012         finally
5013         {
5014             throw new Exception("q2");
5015         }
5016     }
5017     finally
5018     {
5019         collideone();
5020     }
5021 }
5022 
collideMixed()5023 void collideMixed()
5024 {
5025     int works = 6;
5026     try
5027     {
5028         try
5029         {
5030             try
5031             {
5032                 throw new Exception("e");
5033             }
5034             finally
5035             {
5036                 throw new Error("t");
5037             }
5038         }
5039         catch (Exception f)
5040         {
5041             // Doesn't catch, because Error is chained to it.
5042             works += 2;
5043         }
5044     }
5045     catch (Error z)
5046     {
5047         works += 4;
5048         assert(z.msg == "t"); // Error comes first
5049         assert(z.next is null);
5050         assert(z.bypassedException.msg == "e");
5051     }
5052     assert(works == 10);
5053 }
5054 
5055 class AnotherException : Exception
5056 {
this(string s)5057     this(string s)
5058     {
5059         super(s);
5060     }
5061 }
5062 
multicollide()5063 void multicollide()
5064 {
5065     try
5066     {
5067         try
5068         {
5069             try
5070             {
5071                 try
5072                 {
5073                     throw new Exception("m2");
5074                 }
5075                 finally
5076                 {
5077                     throw new AnotherException("n2");
5078                 }
5079             }
5080             catch (AnotherException s)
5081             {
5082                 // Not caught -- we needed to catch the root cause "m2", not
5083                 // just the collateral "n2" (which would leave m2 uncaught).
5084                 assert(0);
5085             }
5086         }
5087         finally
5088         {
5089             collidetwo();
5090         }
5091     }
5092     catch (Exception f)
5093     {
5094         assert(f.msg == "m2");
5095         assert(f.next.msg == "n2");
5096         Throwable e = f.next.next;
5097         assert(e.msg == "p2");
5098         assert(e.next.msg == "q2");
5099         assert(e.next.next.msg == "x");
5100         assert(e.next.next.next.msg == "y");
5101         assert(!e.next.next.next.next);
5102     }
5103 }
5104 
testsFromEH()5105 int testsFromEH()
5106 {
5107     bug1513outer();
5108     doublecollide();
5109     collideMixed();
5110     multicollide();
5111     return 1;
5112 }
5113 static assert(testsFromEH());
5114 
5115 /**************************************************
5116     With + synchronized statements + bug 6901
5117 **************************************************/
5118 
5119 struct With1
5120 {
5121     int a;
5122     int b;
5123 }
5124 
5125 class Foo6
5126 {
5127 }
5128 
5129 class Foo32
5130 {
5131     struct Bar
5132     {
5133         int x;
5134     }
5135 }
5136 
5137 class Base56
5138 {
5139     private string myfoo;
5140     private string mybar;
5141 
5142     // Get/set properties that will be overridden.
5143     void foo(string s) { myfoo = s; }
5144     string foo() { return myfoo; }
5145 
5146     // Get/set properties that will not be overridden.
5147     void bar(string s) { mybar = s; }
5148     string bar() { return mybar; }
5149 }
5150 
5151 class Derived56 : Base56
5152 {
5153     alias Base56.foo foo; // Bring in Base56's foo getter.
5154     override void foo(string s) { super.foo = s; } // Override foo setter.
5155 }
5156 
5157 int testwith()
5158 {
5159     With1 x = With1(7);
5160     with (x)
5161     {
5162         a = 2;
5163     }
5164     assert(x.a == 2);
5165 
5166     // from test11.d
5167     Foo6 foo6 = new Foo6();
5168 
5169     with (foo6)
5170     {
5171         int xx;
5172         xx = 4;
5173     }
5174     with (new Foo32)
5175     {
5176         Bar z;
5177         z.x = 5;
5178     }
5179     Derived56 d = new Derived56;
5180     with (d)
5181     {
5182         foo = "hi";
5183         d.foo = "hi";
5184         bar = "hi";
5185         assert(foo == "hi");
5186         assert(d.foo == "hi");
5187         assert(bar == "hi");
5188     }
5189     int w = 7;
5190     synchronized
5191     {
5192         ++w;
5193     }
5194     assert(w == 8);
5195     return 1;
5196 }
5197 
5198 static assert(testwith());
5199 
5200 /**************************************************
5201     9236 ICE  switch with(EnumType)
5202 **************************************************/
5203 
5204 enum Command9236
5205 {
5206     Char,
5207     Any,
5208 };
5209 
5210 bool bug9236(Command9236 cmd)
5211 {
5212     int n = 0;
5213     with (Command9236) switch (cmd)
5214     {
5215     case Any:
5216         n = 1;
5217         break;
5218     default:
5219         n = 2;
5220     }
5221     assert(n == 1);
5222 
5223     switch (cmd) with (Command9236)
5224     {
5225     case Any:
5226         return true;
5227     default:
5228         return false;
5229     }
5230 }
5231 
5232 static assert(bug9236(Command9236.Any));
5233 
5234 /**************************************************
5235     6416 static struct declaration
5236 **************************************************/
5237 
5238 static assert({
5239     static struct S { int y = 7; }
5240     S a;
5241     a.y += 6;
5242     assert(a.y == 13);
5243     return true;
5244 }());
5245 
5246 /**************************************************
5247     10499 static template struct declaration
5248 **************************************************/
5249 
5250 static assert({
5251     static struct Result() {}
5252     return true;
5253 }());
5254 
5255 /**************************************************
5256     13757 extern(C) alias declaration
5257 **************************************************/
5258 
5259 static assert({
5260     alias FP1 = extern(C) int function();
5261     alias extern(C) int function() FP2;
5262     return true;
5263 }());
5264 
5265 /**************************************************
5266     6522 opAssign + foreach ref
5267 **************************************************/
5268 
5269 struct Foo6522
5270 {
5271     bool b = false;
5272     void opAssign(int x)
5273     {
5274         this.b = true;
5275     }
5276 }
5277 
5278 bool foo6522()
5279 {
5280     Foo6522[1] array;
5281     foreach (ref item; array)
5282         item = 1;
5283     return true;
5284 }
5285 
5286 static assert(foo6522());
5287 
5288 /**************************************************
5289     7245 pointers + foreach ref
5290 **************************************************/
5291 
5292 int bug7245(int testnum)
5293 {
5294     int[3] arr;
5295     arr[0] = 4;
5296     arr[1] = 6;
5297     arr[2] = 8;
5298     int* ptr;
5299 
5300     foreach (i, ref p; arr)
5301     {
5302         if (i == 1)
5303             ptr = &p;
5304         if (testnum == 1)
5305             p = 5;
5306     }
5307 
5308     return *ptr;
5309 }
5310 
5311 static assert(bug7245(0) == 6);
5312 static assert(bug7245(1) == 5);
5313 
5314 /**************************************************
5315     8498 modifying foreach
5316     7658 foreach ref
5317     8539 nested funcs, ref param, -inline
5318 **************************************************/
5319 
5320 int bug8498()
5321 {
5322     foreach (ref i; 0 .. 5)
5323     {
5324         assert(i == 0);
5325         i = 100;
5326     }
5327     return 1;
5328 }
5329 static assert(bug8498());
5330 
5331 string bug7658()
5332 {
5333     string[] children = ["0"];
5334     foreach (ref child; children)
5335         child = "1";
5336     return children[0];
5337 }
5338 
5339 static assert(bug7658() == "1");
5340 
5341 int bug8539()
5342 {
5343     static void one(ref int x)
5344     {
5345         x = 1;
5346     }
5347     static void go()
5348     {
5349         int y;
5350         one(y);
5351         assert(y == 1); // fails with -inline
5352     }
5353     go();
5354     return 1;
5355 }
5356 
5357 static assert(bug8539());
5358 
5359 /**************************************************
5360     7874, 13297, 13740 - better lvalue handling
5361 **************************************************/
5362 
5363 int bug7874(int x){ return ++x = 1; }
5364 static assert(bug7874(0) == 1);
5365 
5366 // ----
5367 
5368 struct S13297
5369 {
5370     int* p;
5371 }
5372 void f13297(ref int* p)
5373 {
5374     p = cast(int*) 1;
5375     assert(p); // passes
5376 }
5377 static assert(
5378 {
5379     S13297 s;
5380     f13297(s.p);
5381     return s.p != null; // false
5382 }());
5383 
5384 // ----
5385 
5386 class R13740
5387 {
5388     int e;
5389     bool empty = false;
5390     @property ref front() { return e; }
5391     void popFront() { empty = true; }
5392 }
5393 static assert({
5394     auto r = new R13740();
5395     foreach (ref e; r)
5396         e = 42;
5397     assert(r.e == 42); /* fails in CTFE */
5398 
5399     return true;
5400 }());
5401 
5402 /**************************************************
5403     6919
5404 **************************************************/
5405 
5406 void bug6919(int* val)
5407 {
5408     *val = 1;
5409 }
5410 void test6919()
5411 {
5412     int n;
5413     bug6919(&n);
5414     assert(n == 1);
5415 }
5416 static assert({ test6919(); return true; }());
5417 
5418 void bug6919b(string* val)
5419 {
5420     *val = "1";
5421 }
5422 
5423 void test6919b()
5424 {
5425     string val;
5426     bug6919b(&val);
5427     assert(val == "1");
5428 }
5429 static assert({ test6919b(); return true; }());
5430 
5431 /**************************************************
5432     6995
5433 **************************************************/
5434 
5435 struct Foo6995
5436 {
5437     static size_t index(size_t v)()
5438     {
5439         return v;
5440     }
5441 }
5442 
5443 static assert(Foo6995.index!(27)() == 27);
5444 
5445 /**************************************************
5446     7043 ref with -inline
5447 **************************************************/
5448 
5449 int bug7043(S)(ref int x)
5450 {
5451     return x;
5452 }
5453 
5454 static assert({
5455     int i = 416;
5456     return bug7043!(char)(i);
5457 }() == 416);
5458 
5459 /**************************************************
5460     6037 recursive ref
5461 **************************************************/
5462 
5463 void bug6037(ref int x, bool b)
5464 {
5465     int w = 3;
5466     if (b)
5467     {
5468         bug6037(w, false);
5469         assert(w == 6);
5470     }
5471     else
5472     {
5473         x = 6;
5474         assert(w == 3); // fails
5475     }
5476 }
5477 
5478 int bug6037outer()
5479 {
5480     int q;
5481     bug6037(q, true);
5482     return 401;
5483 }
5484 
5485 static assert(bug6037outer() == 401);
5486 
5487 /**************************************************
5488     14299 - [REG2.067a], more than one depth of recursive call with ref
5489 **************************************************/
5490 
5491 string gen14299(int max, int idx, ref string name)
5492 {
5493     string ret;
5494     name = [cast(char)(idx + '0')];
5495     ret ~= name;
5496     if (idx < max)
5497     {
5498         string subname;
5499         ret ~= gen14299(max, idx + 1, subname);
5500     }
5501     ret ~= name;
5502     return ret;
5503 }
5504 string test14299(int max)
5505 {
5506     string n;
5507     return gen14299(max, 0, n);
5508 }
5509 static assert(test14299(1) ==     "0110");      // OK <- fail
5510 static assert(test14299(2) ==    "012210");     // OK <- ICE
5511 static assert(test14299(3) ==   "01233210");
5512 static assert(test14299(4) ==  "0123443210");
5513 static assert(test14299(5) == "012345543210");
5514 
5515 /**************************************************
5516     7940 wrong code for complicated assign
5517 **************************************************/
5518 
5519 struct Bug7940
5520 {
5521     int m;
5522 }
5523 
5524 struct App7940
5525 {
5526     Bug7940[] x;
5527 }
5528 
5529 int bug7940()
5530 {
5531     Bug7940[2] y;
5532     App7940 app;
5533     app.x = y[0 .. 1];
5534     app.x[0].m = 12;
5535     assert(y[0].m == 12);
5536     assert(app.x[0].m == 12);
5537     return 1;
5538 }
5539 
5540 static assert(bug7940());
5541 
5542 /**************************************************
5543     10298 wrong code for struct array literal init
5544 **************************************************/
5545 
5546 struct Bug10298
5547 {
5548     int m;
5549 }
5550 
5551 int bug10298()
5552 {
5553     Bug10298[1] y = [Bug10298(78)];
5554     y[0].m = 6;
5555     assert(y[0].m == 6);
5556 
5557     // Root cause
5558     Bug10298[1] x;
5559     x[] = [cast(const Bug10298)(Bug10298(78))];
5560     assert(x[0].m == 78);
5561     return 1;
5562 }
5563 
5564 static assert(bug10298());
5565 
5566 /**************************************************
5567     7266 dotvar ref parameters
5568 **************************************************/
5569 
5570 struct S7266 { int a; }
5571 
5572 bool bug7266()
5573 {
5574     S7266 s;
5575     s.a = 4;
5576     bar7266(s.a);
5577     assert(s.a == 5);
5578     out7266(s.a);
5579     assert(s.a == 7);
5580     return true;
5581 }
5582 
5583 void bar7266(ref int b)
5584 {
5585     b = 5;
5586     assert(b == 5);
5587 }
5588 
5589 void out7266(out int b)
5590 {
5591     b = 7;
5592     assert(b == 7);
5593 }
5594 
5595 static assert(bug7266());
5596 
5597 /**************************************************
5598     9982 dotvar assign through pointer
5599 **************************************************/
5600 
5601 struct Bug9982
5602 {
5603     int a;
5604 }
5605 
5606 int test9982()
5607 {
5608     Bug9982 x;
5609     int*q = &x.a;
5610     *q = 99;
5611     assert(x.a == 99);
5612     return 1;
5613 }
5614 
5615 static assert(test9982());
5616 
5617 // 9982, rejects-valid case
5618 
5619 struct SS9982
5620 {
5621     Bug9982 s2;
5622     this(Bug9982 s1)
5623     {
5624         s2.a = 6;
5625         emplace9982(&s2, s1);
5626         assert(s2.a == 3);
5627     }
5628 }
5629 
5630 void emplace9982(Bug9982* chunk, Bug9982 arg)
5631 {
5632     *chunk = arg;
5633 }
5634 
5635 enum s9982 = Bug9982(3);
5636 enum p9982 = SS9982(s9982);
5637 
5638 /**************************************************
5639     11618 dotvar assign through casted pointer
5640 **************************************************/
5641 
5642 struct Tuple11618(T...)
5643 {
5644     T field;
5645     alias field this;
5646 }
5647 
5648 static assert({
5649     Tuple11618!(immutable dchar) result = void;
5650     auto addr = cast(dchar*)&result[0];
5651     *addr = dchar.init;
5652     return (result[0] == dchar.init);
5653 }());
5654 
5655 /**************************************************
5656     7143 'is' for classes
5657 **************************************************/
5658 
5659 class C7143
5660 {
5661     int x;
5662 }
5663 
5664 int bug7143(int test)
5665 {
5666     C7143 c = new C7143;
5667     C7143 d = new C7143;
5668     if (test == 1)
5669     {
5670         if (c)
5671             return c.x + 8;
5672         return -1;
5673     }
5674     if (test == 2)
5675     {
5676         if (c is null)
5677             return -1;
5678         return c.x + 45;
5679     }
5680     if (test == 3)
5681     {
5682         if (c is c)
5683             return 58;
5684     }
5685     if (test == 4)
5686     {
5687         if (c !is c)
5688             return -1;
5689         else
5690             return 48;
5691     }
5692     if (test == 6)
5693         d = c;
5694     if (test == 5 || test == 6)
5695     {
5696         if (c is d)
5697             return 188;
5698         else
5699             return 48;
5700     }
5701     return -1;
5702 }
5703 
5704 static assert(bug7143(1) == 8);
5705 static assert(bug7143(2) == 45);
5706 static assert(bug7143(3) == 58);
5707 static assert(bug7143(4) == 48);
5708 static assert(bug7143(5) == 48);
5709 static assert(bug7143(6) == 188);
5710 
5711 /**************************************************
5712     7147 virtual function calls from base class
5713 **************************************************/
5714 
5715 class A7147
5716 {
5717     int foo() { return 0; }
5718 
5719     int callfoo()
5720     {
5721         return foo();
5722     }
5723 }
5724 
5725 class B7147 : A7147
5726 {
5727     override int foo() { return 1; }
5728 }
5729 
5730 int test7147()
5731 {
5732     A7147 a = new B7147;
5733     return a.callfoo();
5734 }
5735 
5736 static assert(test7147() == 1);
5737 
5738 /**************************************************
5739     7158
5740 **************************************************/
5741 
5742 class C7158
5743 {
5744     bool b() { return true; }
5745 }
5746 struct S7158
5747 {
5748     C7158 c;
5749 }
5750 
5751 bool test7158()
5752 {
5753     S7158 s = S7158(new C7158);
5754     return s.c.b;
5755 }
5756 static assert(test7158());
5757 
5758 /**************************************************
5759     8484
5760 **************************************************/
5761 
5762 class C8484
5763 {
5764     int n;
5765     int b() { return n + 3; }
5766 }
5767 
5768 struct S
5769 {
5770     C8484 c;
5771 }
5772 
5773 int t8484(ref C8484 c)
5774 {
5775     return c.b();
5776 }
5777 
5778 int test8484()
5779 {
5780     auto s = S(new C8484);
5781     s.c.n = 4;
5782     return t8484(s.c);
5783 }
5784 static assert(test8484() == 7);
5785 
5786 /**************************************************
5787     7419
5788 **************************************************/
5789 
5790 struct X7419
5791 {
5792     double x;
5793     this(double x)
5794     {
5795         this.x = x;
5796     }
5797 }
5798 
5799 void bug7419()
5800 {
5801     enum x = {
5802         auto p = X7419(3);
5803         return p.x;
5804     }();
5805     static assert(x == 3);
5806 }
5807 
5808 /**************************************************
5809     9445 ice
5810 **************************************************/
5811 
5812 template c9445(T...) { }
5813 
5814 void ice9445(void delegate() expr, void function() f2)
5815 {
5816     static assert(!is(typeof(c9445!(f2()))));
5817     static assert(!is(typeof(c9445!(expr()))));
5818 }
5819 
5820 /**************************************************
5821     10452 delegate ==
5822 **************************************************/
5823 
5824 struct S10452
5825 {
5826     bool func() { return true; }
5827 }
5828 
5829 struct Outer10452
5830 {
5831     S10452 inner;
5832 }
5833 
5834 class C10452
5835 {
5836     bool func() { return true; }
5837 }
5838 
5839 bool delegate() ref10452(ref S10452 s)
5840 {
5841     return &s.func;
5842 }
5843 
5844 bool test10452()
5845 {
5846     bool delegate() bar = () { return true; };
5847 
5848     assert(bar !is null);
5849     assert(bar is bar);
5850 
5851     S10452 bag;
5852     S10452[6] bad;
5853     Outer10452 outer;
5854     C10452 tag = new C10452;
5855 
5856     auto rat = &outer.inner.func;
5857     assert(rat == rat);
5858     auto tat = &tag.func;
5859     assert(tat == tat);
5860 
5861     auto bat = &outer.inner.func;
5862     auto mat = &bad[2].func;
5863     assert(mat is mat);
5864     assert(rat == bat);
5865 
5866     auto zat = &bag.func;
5867     auto cat = &bag.func;
5868     assert(zat == zat);
5869     assert(zat == cat);
5870 
5871     auto drat = ref10452(bag);
5872     assert(cat == drat);
5873     assert(drat == drat);
5874     drat = ref10452(bad[2]);
5875     assert( drat == mat);
5876     assert(tat != rat);
5877     assert(zat != rat);
5878     assert(rat != cat);
5879     assert(zat != bar);
5880     assert(tat != cat);
5881     cat = bar;
5882     assert(cat == bar);
5883     return true;
5884 }
5885 static assert(test10452());
5886 
5887 /**************************************************
5888     7162 and 4711
5889 **************************************************/
5890 
5891 void f7162() { }
5892 
5893 bool ice7162()
5894 {
5895     false && f7162();
5896     false || f7162();
5897     false && f7162();  // bug 4711
5898     true && f7162();
5899     return true;
5900 }
5901 
5902 static assert(ice7162());
5903 
5904 /**************************************************
5905     8857, only with -inline (creates an &&)
5906 **************************************************/
5907 
5908 struct Result8857 { char[] next; }
5909 
5910 void bug8857()()
5911 {
5912     Result8857 r;
5913     r.next = null;
5914     if (true)
5915     {
5916        auto next = r.next;
5917     }
5918 }
5919 static assert({
5920     bug8857();
5921     return true;
5922 }());
5923 
5924 /**************************************************
5925     7527
5926 **************************************************/
5927 
5928 struct Bug7527
5929 {
5930     char[] data;
5931 }
5932 
5933 int bug7527()
5934 {
5935     auto app = Bug7527();
5936 
5937     app.data.ptr[0 .. 1] = "x";
5938     return 1;
5939 }
5940 
5941 static assert(!is(typeof(compiles!(bug7527()))));
5942 
5943 /**************************************************
5944     7527
5945 **************************************************/
5946 
5947 int bug7380;
5948 
5949 static assert(!is(typeof( compiles!(
5950     (){
5951         return &bug7380;
5952     }()
5953 ))));
5954 
5955 /**************************************************
5956     7165
5957 **************************************************/
5958 
5959 struct S7165
5960 {
5961     int* ptr;
5962     bool f() const { return !!ptr; }
5963 }
5964 
5965 static assert(!S7165().f());
5966 
5967 /**************************************************
5968     7187
5969 **************************************************/
5970 
5971 int[] f7187() { return [0]; }
5972 int[] f7187b(int n) { return [0]; }
5973 
5974 int g7187(int[] r)
5975 {
5976     auto t = r[0 .. 0];
5977     return 1;
5978 }
5979 
5980 static assert(g7187(f7187()));
5981 static assert(g7187(f7187b(7)));
5982 
5983 struct S7187 { const(int)[] field; }
5984 
5985 const(int)[] f7187c()
5986 {
5987     auto s = S7187([0]);
5988     return s.field;
5989 }
5990 
5991 bool g7187c(const(int)[] r)
5992 {
5993     auto t = r[0 .. 0];
5994     return true;
5995 }
5996 
5997 static assert(g7187c(f7187c()));
5998 
5999 
6000 /**************************************************
6001     6933 struct destructors
6002 **************************************************/
6003 
6004 struct Bug6933
6005 {
6006     int x = 3;
6007     ~this() { }
6008 }
6009 
6010 int test6933()
6011 {
6012     Bug6933 q;
6013     assert(q.x == 3);
6014     return 3;
6015 }
6016 
6017 static assert(test6933());
6018 
6019 /**************************************************
6020     7197
6021 **************************************************/
6022 
6023 int foo7197(int[] x...)
6024 {
6025     return 1;
6026 }
6027 template bar7197(y...)
6028 {
6029     enum int bar7197 = foo7197(y);
6030 }
6031 enum int bug7197 = 7;
6032 static assert(bar7197!(bug7197));
6033 
6034 /**************************************************
6035     Enum string compare
6036 **************************************************/
6037 
6038 enum EScmp : string { a = "aaa" }
6039 
6040 bool testEScmp()
6041 {
6042     EScmp x = EScmp.a;
6043     assert(x < "abc");
6044     return true;
6045 }
6046 
6047 static assert(testEScmp());
6048 
6049 /**************************************************
6050     7667
6051 **************************************************/
6052 
6053 bool baz7667(int[] vars...)
6054 {
6055      return true;
6056 }
6057 
6058 struct S7667
6059 {
6060     static void his(int n)
6061     {
6062         static assert(baz7667(2));
6063     }
6064 }
6065 
6066 bool bug7667()
6067 {
6068     S7667 unused;
6069     unused.his(7);
6070     return true;
6071 }
6072 enum e7667 = bug7667();
6073 
6074 /**************************************************
6075     7536
6076 **************************************************/
6077 
6078 bool bug7536(string expr)
6079 {
6080     return true;
6081 }
6082 
6083 void vop()
6084 {
6085     const string x7536 = "x";
6086     static assert(bug7536(x7536));
6087 }
6088 
6089 /**************************************************
6090     6681 unions
6091 **************************************************/
6092 
6093 struct S6681
6094 {
6095     this(int a, int b) { this.a = b; this.b = a; }
6096     union
6097     {
6098         ulong g;
6099         struct { int a, b; };
6100     }
6101 }
6102 
6103 static immutable S6681 s6681 = S6681(0, 1);
6104 
6105 bool bug6681(int test)
6106 {
6107     S6681 x = S6681(0, 1);
6108     x.g = 5;
6109     auto u = &x.g;
6110     auto v = &x.a;
6111     long w = *u;
6112     int  z;
6113     assert(w == 5);
6114     if (test == 4)
6115         z = *v; // error
6116     x.a = 2; // invalidate g, and hence u.
6117     if (test == 1)
6118         w = *u; // error
6119     z = *v;
6120     assert(z == 2);
6121     x.g = 6;
6122     w = *u;
6123     assert(w == 6);
6124     if (test == 3)
6125         z = *v;
6126     return true;
6127 }
6128 static assert(bug6681(2));
6129 static assert(!is(typeof(compiles!(bug6681(1)))));
6130 static assert(!is(typeof(compiles!(bug6681(3)))));
6131 static assert(!is(typeof(compiles!(bug6681(4)))));
6132 
6133 /**************************************************
6134     9113 ICE with struct in union
6135 **************************************************/
6136 
6137 union U9113
6138 {
6139     struct M
6140     {
6141         int y;
6142     }
6143     int xx;
6144 }
6145 
6146 int bug9113(T)()
6147 {
6148     U9113 x;
6149     x.M.y = 10; // error, need 'this'
6150     return 1;
6151 }
6152 
6153 static assert(!is(typeof(compiles!(bug9113!(int)()))));
6154 
6155 /**************************************************
6156     Creation of unions
6157 **************************************************/
6158 
6159 union UnionTest1
6160 {
6161     int x;
6162     float y;
6163 }
6164 
6165 int uniontest1()
6166 {
6167     UnionTest1 u = UnionTest1(1);
6168     return 1;
6169 }
6170 
6171 static assert(uniontest1());
6172 
6173 /**************************************************
6174     6438 void
6175 **************************************************/
6176 
6177 struct S6438
6178 {
6179     int a;
6180     int b = void;
6181 }
6182 
6183 void fill6438(int[] arr, int testnum)
6184 {
6185     if (testnum == 2)
6186     {
6187         auto u = arr[0];
6188     }
6189     foreach (ref x; arr)
6190         x = 7;
6191     auto r = arr[0];
6192     S6438[2] s;
6193     auto p = &s[0].b;
6194     if (testnum == 3)
6195     {
6196         auto v = *p;
6197     }
6198 }
6199 
6200 bool bug6438(int testnum)
6201 {
6202     int[4] stackSpace = void;
6203     fill6438(stackSpace[], testnum);
6204     assert(stackSpace == [7, 7, 7, 7]);
6205     return true;
6206 }
6207 
6208 static assert( is(typeof(compiles!(bug6438(1)))));
6209 static assert(!is(typeof(compiles!(bug6438(2)))));
6210 static assert(!is(typeof(compiles!(bug6438(3)))));
6211 
6212 /**************************************************
6213     10994 void static array members
6214 **************************************************/
6215 
6216 struct Bug10994
6217 {
6218     ubyte[2] buf = void;
6219 }
6220 
6221 static bug10994 = Bug10994.init;
6222 
6223 /**************************************************
6224     10937 struct inside union
6225 **************************************************/
6226 
6227 struct S10937
6228 {
6229     union
6230     {
6231         ubyte[1] a;
6232         struct
6233         {
6234             ubyte b;
6235         }
6236     }
6237 
6238     this(ubyte B)
6239     {
6240         if (B > 6)
6241             this.b = B;
6242         else
6243             this.a[0] = B;
6244     }
6245 }
6246 
6247 enum test10937 = S10937(7);
6248 enum west10937 = S10937(2);
6249 
6250 /**************************************************
6251     13831
6252 **************************************************/
6253 
6254 struct Vector13831()
6255 {
6256 }
6257 
6258 struct Coord13831
6259 {
6260     union
6261     {
6262         struct { short x; }
6263         Vector13831!() vector;
6264     }
6265 }
6266 
6267 struct Chunk13831
6268 {
6269     this(Coord13831)
6270     {
6271         coord = coord;
6272     }
6273 
6274     Coord13831 coord;
6275 
6276     static const Chunk13831* unknownChunk = new Chunk13831(Coord13831());
6277 }
6278 
6279 /**************************************************
6280     7732
6281 **************************************************/
6282 
6283 struct AssociativeArray
6284 {
6285     int* impl;
6286     int f()
6287     {
6288         if (impl !is null)
6289             auto x = *impl;
6290         return 1;
6291     }
6292 }
6293 
6294 int test7732()
6295 {
6296     AssociativeArray aa;
6297     return aa.f;
6298 }
6299 
6300 static assert(test7732());
6301 
6302 /**************************************************
6303     7784
6304 **************************************************/
6305 struct Foo7784
6306 {
6307     void bug()
6308     {
6309         tab["A"] = Bar7784(&this);
6310         auto pbar = "A" in tab;
6311         auto bar = *pbar;
6312     }
6313 
6314     Bar7784[string] tab;
6315 }
6316 
6317 struct Bar7784
6318 {
6319     Foo7784* foo;
6320     int val;
6321 }
6322 
6323 bool ctfe7784()
6324 {
6325     auto foo = Foo7784();
6326     foo.bug();
6327     return true;
6328 }
6329 
6330 static assert(ctfe7784());
6331 
6332 /**************************************************
6333     7781
6334 **************************************************/
6335 
6336 static assert(({ return; }(), true));
6337 
6338 /**************************************************
6339     7785
6340 **************************************************/
6341 
6342 bool bug7785(int n)
6343 {
6344     int val = 7;
6345     auto p = &val;
6346     if (n == 2)
6347     {
6348         auto ary = p[0 .. 1];
6349     }
6350     auto x = p[0];
6351     val = 6;
6352     assert(x == 7);
6353     if (n == 3)
6354         p[0 .. 1] = 1;
6355     return true;
6356 }
6357 
6358 static assert(bug7785(1));
6359 static assert(!is(typeof(compiles!(bug7785(2)))));
6360 static assert(!is(typeof(compiles!(bug7785(3)))));
6361 
6362 /**************************************************
6363     7987
6364 **************************************************/
6365 
6366 class C7987
6367 {
6368     int m;
6369 }
6370 
6371 struct S7987
6372 {
6373     int* p;
6374     C7987 c;
6375 }
6376 
6377 bool bug7987()
6378 {
6379     int[7] q;
6380     int[][2] b = q[0 .. 5];
6381     assert(b == b);
6382     assert(b is b);
6383     C7987 c1 = new C7987;
6384     C7987 c2 = new C7987;
6385     S7987 s, t;
6386     s.p = &q[0];
6387     t.p = &q[1];
6388     assert(s != t);
6389     s.p = &q[1];
6390     /*assert(s == t);*/     assert(s.p == t.p);
6391     s.c = c1;
6392     t.c = c2;
6393     /*assert(s != t);*/     assert(s.c !is t.c);
6394     assert(s !is t);
6395     s.c = c2;
6396     /*assert(s == t);*/     assert(s.p == t.p && s.c is t.c);
6397     assert(s is t);
6398     return true;
6399 }
6400 
6401 static assert(bug7987());
6402 
6403 /**************************************************
6404     10579 typeinfo.func() must not segfault
6405 **************************************************/
6406 
6407 static assert(!is(typeof(compiles!(typeid(int).toString.length))));
6408 
6409 class Bug10579
6410 {
6411     int foo() { return 1; }
6412 }
6413 Bug10579 uninitialized10579;
6414 
6415 static assert(!is(typeof(compiles!(uninitialized10579.foo()))));
6416 
6417 /**************************************************
6418     10804 mixin ArrayLiteralExp typed string
6419 **************************************************/
6420 
6421 void test10804()
6422 {
6423     String identity(String)(String a) { return a; }
6424 
6425     string cfun()
6426     {
6427         char[] s;
6428         s.length = 8 + 2 + (2) + 1 + 2;
6429         s[] = "identity(`Ω`c)"c[];
6430         return cast(string)s;   // Return ArrayLiteralExp as the CTFE result
6431     }
6432     {
6433         enum a1 = "identity(`Ω`c)"c;
6434         enum a2 = cfun();
6435         static assert(cast(ubyte[])mixin(a1) == [0xCE, 0xA9]);
6436         static assert(cast(ubyte[])mixin(a2) == [0xCE, 0xA9]);  // should pass
6437     }
6438 
6439     wstring wfun()
6440     {
6441         wchar[] s;
6442         s.length = 8 + 2 + (2) + 1 + 2;
6443         s[] = "identity(`\U0002083A`w)"w[];
6444         return cast(wstring)s;  // Return ArrayLiteralExp as the CTFE result
6445     }
6446     {
6447         enum a1 = "identity(`\U0002083A`w)"w;
6448         enum a2 = wfun();
6449         static assert(cast(ushort[])mixin(a1) == [0xD842, 0xDC3A]);
6450         static assert(cast(ushort[])mixin(a2) == [0xD842, 0xDC3A]);
6451     }
6452 
6453     dstring dfun()
6454     {
6455         dchar[] s;
6456         s.length = 8 + 2 + (1) + 1 + 2;
6457         s[] = "identity(`\U00101000`d)"d[];
6458         return cast(dstring)s;  // Return ArrayLiteralExp as the CTFE result
6459     }
6460     {
6461         enum a1 = "identity(`\U00101000`d)"d;
6462         enum a2 = dfun();
6463         static assert(cast(uint[])mixin(a1) == [0x00101000]);
6464         static assert(cast(uint[])mixin(a2) == [0x00101000]);
6465     }
6466 }
6467 
6468 /******************************************************/
6469 
6470 struct B73 {}
6471 struct C73 { B73 b; }
6472 C73 func73() { C73 b = void; b.b = B73(); return b; }
6473 C73 test73 = func73();
6474 
6475 /******************************************************/
6476 
6477 struct S74
6478 {
6479     int n[1];
6480     static S74 test(){ S74 ret = void; ret.n[0] = 0; return ret; }
6481 }
6482 
6483 enum Test74 = S74.test();
6484 
6485 /******************************************************/
6486 
6487 static bool bug8865()
6488 in
6489 {
6490     int x = 0;
6491 label:
6492     foreach (i; (++x) .. 3)
6493     {
6494         if (i == 1)
6495             continue label;     // doesn't work.
6496         else
6497             break label;        // doesn't work.
6498     }
6499 }
6500 out
6501 {
6502     int x = 0;
6503 label:
6504     foreach (i; (++x) .. 3)
6505     {
6506         if (i == 1)
6507             continue label;     // doesn't work.
6508         else
6509             break label;        // doesn't work.
6510     }
6511 }
6512 body
6513 {
6514     int x = 0;
6515 label:
6516     foreach (i; (++x) .. 3)
6517     {
6518         if (i == 1)
6519             continue label;     // works.
6520         else
6521             break label;        // works.
6522     }
6523 
6524     return true;
6525 }
6526 static assert(bug8865());
6527 
6528 /******************************************************/
6529 // 15450 labeled foreach + continue/break
6530 
6531 static assert({
6532   L1:
6533     foreach (l; [0])
6534         continue L1;
6535 
6536   L2:
6537     foreach (l; [0])
6538         break L2;
6539 
6540     return true;
6541 }());
6542 
6543 struct Test75
6544 {
6545     this(int) pure {}
6546 }
6547 
6548 /******************************************************/
6549 
6550 static assert( __traits(compiles, { static shared(Test75*)   t75 = new shared(Test75)(0);    return t75; }));
6551 static assert( __traits(compiles, { static shared(Test75)*   t75 = new shared(Test75)(0);    return t75; }));
6552 static assert( __traits(compiles, { static __gshared Test75* t75 = new Test75(0);            return t75; }));
6553 static assert( __traits(compiles, { static const(Test75*)    t75 = new const(Test75)(0);     return t75; }));
6554 static assert( __traits(compiles, { static immutable Test75* t75 = new immutable(Test75)(0); return t75; }));
6555 static assert(!__traits(compiles, { static Test75*           t75 = new Test75(0);            return t75; }));
6556 /+
6557 static assert(!__traits(compiles, { enum                 t75 = new shared(Test75)(0); return t75; }));
6558 static assert(!__traits(compiles, { enum                 t75 = new Test75(0);         return t75; }));
6559 static assert(!__traits(compiles, { enum shared(Test75)* t75 = new shared(Test75)(0); return t75; }));
6560 static assert(!__traits(compiles, { enum Test75*         t75 = new Test75(0);         return t75; }));
6561 
6562 static assert( __traits(compiles, { enum                    t75 = new const(Test75)(0);     return t75;}));
6563 static assert( __traits(compiles, { enum                    t75 = new immutable(Test75)(0); return t75;}));
6564 static assert( __traits(compiles, { enum const(Test75)*     t75 = new const(Test75)(0);     return t75;}));
6565 static assert( __traits(compiles, { enum immutable(Test75)* t75 = new immutable(Test75)(0); return t75;}));
6566 +/
6567 /******************************************************/
6568 
6569 class Test76
6570 {
6571     this(int) pure {}
6572 }
6573 /+
6574 static assert(!__traits(compiles, { enum                   t76 = new shared(Test76)(0); return t76;}));
6575 static assert(!__traits(compiles, { enum                   t76 = new Test76(0);         return t76;}));
6576 static assert(!__traits(compiles, { enum shared(Test76)    t76 = new shared(Test76)(0); return t76;}));
6577 static assert(!__traits(compiles, { enum Test76            t76 = new Test76(0);         return t76;}));
6578 
6579 static assert( __traits(compiles, { enum                   t76 = new const(Test76)(0);     return t76;}));
6580 static assert( __traits(compiles, { enum                   t76 = new immutable(Test76)(0); return t76;}));
6581 static assert( __traits(compiles, { enum const(Test76)     t76 = new const(Test76)(0);     return t76;}));
6582 static assert( __traits(compiles, { enum immutable(Test76) t76 = new immutable(Test76)(0); return t76;}));
6583 +/
6584 /******************************************************/
6585 
6586 static assert( __traits(compiles, { static shared Test76    t76 = new shared(Test76)(0);   return t76; }));
6587 static assert( __traits(compiles, { static shared(Test76)   t76 = new shared(Test76)(0);   return t76; }));
6588 static assert( __traits(compiles, { static __gshared Test76 t76 = new Test76(0);           return t76; }));
6589 static assert( __traits(compiles, { static const Test76     t76 = new const(Test76)(0);    return t76; }));
6590 static assert( __traits(compiles, { static immutable Test76 t76 = new immutable Test76(0); return t76; }));
6591 static assert(!__traits(compiles, { static Test76           t76 = new Test76(0);           return t76; }));
6592 
6593 /***** Bug 5678 *********************************/
6594 
6595 struct Bug5678
6596 {
6597     this(int) {}
6598 }
6599 
6600 static assert(!__traits(compiles, { enum const(Bug5678)* b5678 = new const(Bug5678)(0); return b5678; }));
6601 
6602 /**************************************************
6603     10782 run semantic2 for class field
6604 **************************************************/
6605 
6606 enum e10782 = 0;
6607 class C10782 { int x = e10782; }
6608 string f10782()
6609 {
6610     auto c = new C10782();
6611     return "";
6612 }
6613 mixin(f10782());
6614 
6615 /**************************************************
6616     10929 NRVO support in CTFE
6617 **************************************************/
6618 
6619 struct S10929
6620 {
6621     this(this)
6622     {
6623         postblitCount++;
6624     }
6625     ~this()
6626     {
6627         dtorCount++;
6628     }
6629     int payload;
6630     int dtorCount;
6631     int postblitCount;
6632 }
6633 
6634 auto makeS10929()
6635 {
6636     auto s = S10929(42, 0, 0);
6637     return s;
6638 }
6639 
6640 bool test10929()
6641 {
6642     auto s = makeS10929();
6643     assert(s.postblitCount == 0);
6644     assert(s.dtorCount == 0);
6645     return true;
6646 };
6647 static assert(test10929());
6648 
6649 /**************************************************
6650     9245 - support postblit call on array assignments
6651 **************************************************/
6652 
6653 bool test9245()
6654 {
6655     int postblits = 0;
6656     struct S
6657     {
6658         this(this)
6659         {
6660             ++postblits;
6661         }
6662     }
6663 
6664     S s;
6665     S[2] a;
6666     assert(postblits == 0);
6667 
6668     {
6669         S[2] arr = s;
6670         assert(postblits == 2);
6671         arr[] = s;
6672         assert(postblits == 4);
6673         postblits = 0;
6674 
6675         S[2] arr2 = arr;
6676         assert(postblits == 2);
6677         arr2 = arr;
6678         assert(postblits == 4);
6679         postblits = 0;
6680 
6681         const S[2] constArr = s;
6682         assert(postblits == 2);
6683         postblits = 0;
6684 
6685         const S[2] constArr2 = arr;
6686         assert(postblits == 2);
6687         postblits = 0;
6688     }
6689     {
6690         S[2][2] arr = s;
6691         assert(postblits == 4);
6692         arr[] = a;
6693         assert(postblits == 8);
6694         postblits = 0;
6695 
6696         S[2][2] arr2 = arr;
6697         assert(postblits == 4);
6698         arr2 = arr;
6699         assert(postblits == 8);
6700         postblits = 0;
6701 
6702         const S[2][2] constArr = s;
6703         assert(postblits == 4);
6704         postblits = 0;
6705 
6706         const S[2][2] constArr2 = arr;
6707         assert(postblits == 4);
6708         postblits = 0;
6709     }
6710 
6711     return true;
6712 }
6713 static assert(test9245());
6714 
6715 /**************************************************
6716     12906 don't call postblit on blit initializing
6717 **************************************************/
6718 
6719 struct S12906 { this(this) { assert(0); } }
6720 
6721 static assert({
6722     S12906[1] arr;
6723     return true;
6724 }());
6725 
6726 /**************************************************
6727     11510 support overlapped field access in CTFE
6728 **************************************************/
6729 
6730 struct S11510
6731 {
6732     union
6733     {
6734         size_t x;
6735         int* y; // pointer field
6736     }
6737 }
6738 bool test11510()
6739 {
6740     S11510 s;
6741 
6742     s.y = [1,2,3].ptr;            // writing overlapped pointer field is OK
6743     assert(s.y[0 .. 3] == [1,2,3]); // reading valid field is OK
6744 
6745     s.x = 10;
6746     assert(s.x == 10);
6747 
6748     // There's no reinterpretation between S.x and S.y
6749     return true;
6750 }
6751 static assert(test11510());
6752 
6753 /**************************************************
6754     11534 - subtitude inout
6755 **************************************************/
6756 
6757 struct MultiArray11534
6758 {
6759     void set(size_t[] sizes...)
6760     {
6761         storage = new size_t[5];
6762     }
6763 
6764     @property auto raw_ptr() inout
6765     {
6766         return storage.ptr + 1;
6767     }
6768     size_t[] storage;
6769 }
6770 
6771 enum test11534 = () {
6772     auto m = MultiArray11534();
6773     m.set(3,2,1);
6774     auto start = m.raw_ptr;   //this trigger the bug
6775     //auto start = m.storage.ptr + 1; //this obviously works
6776     return 0;
6777 }();
6778 
6779 /**************************************************
6780     11941 - Regression of 11534 fix
6781 **************************************************/
6782 
6783 void takeConst11941(const string[]) {}
6784 string[] identity11941(string[] x) { return x; }
6785 
6786 bool test11941a()
6787 {
6788     struct S { string[] a; }
6789     S s;
6790 
6791     takeConst11941(identity11941(s.a));
6792     s.a ~= [];
6793 
6794     return true;
6795 }
6796 static assert(test11941a());
6797 
6798 bool test11941b()
6799 {
6800     struct S { string[] a; }
6801     S s;
6802 
6803     takeConst11941(identity11941(s.a));
6804     s.a ~= "foo"; /* Error refers to this line (15), */
6805     string[] b = s.a[]; /* but only when this is here. */
6806 
6807     return true;
6808 }
6809 static assert(test11941b());
6810 
6811 /**************************************************
6812     11535 - element-wise assignment from string to ubyte array literal
6813 **************************************************/
6814 
6815 struct Hash11535
6816 {
6817     ubyte[6] _buffer;
6818 
6819     void put(scope const(ubyte)[] data...)
6820     {
6821         uint i = 0, index = 0;
6822         auto inputLen = data.length;
6823 
6824         (&_buffer[index])[0 .. inputLen - i] = (&data[i])[0 .. inputLen - i];
6825     }
6826 }
6827 
6828 auto md5_digest11535(T...)(scope const T data)
6829 {
6830     Hash11535 hash;
6831     hash.put(cast(const(ubyte[]))data[0]);
6832     return hash._buffer;
6833 }
6834 
6835 static assert(md5_digest11535(`TEST`) == [84, 69, 83, 84, 0, 0]);
6836 
6837 /**************************************************
6838     11540 - goto label + try-catch-finally / with statement
6839 **************************************************/
6840 
6841 static assert(()
6842 {
6843     // enter to TryCatchStatement.body
6844     {
6845         bool c = false;
6846         try
6847         {
6848             if (c)  // need to bypass front-end optimization
6849                 throw new Exception("");
6850             else
6851             {
6852                 goto Lx;
6853               L1:
6854                 c = true;
6855             }
6856         }
6857         catch (Exception e) {}
6858 
6859       Lx:
6860         if (!c)
6861             goto L1;
6862     }
6863 
6864     // jump inside TryCatchStatement.body
6865     {
6866         bool c = false;
6867         try
6868         {
6869             if (c)  // need to bypass front-end optimization
6870                 throw new Exception("");
6871             else
6872                 goto L2;
6873           L2:
6874             ;
6875         }
6876         catch (Exception e) {}
6877     }
6878 
6879     // exit from TryCatchStatement.body
6880     {
6881         bool c = false;
6882         try
6883         {
6884             if (c)  // need to bypass front-end optimization
6885                 throw new Exception("");
6886             else
6887                 goto L3;
6888         }
6889         catch (Exception e) {}
6890 
6891         c = true;
6892       L3:
6893         assert(!c);
6894     }
6895 
6896     return 1;
6897 }());
6898 
6899 static assert(()
6900 {
6901     // enter to TryCatchStatement.catches which has no exception variable
6902     {
6903         bool c = false;
6904         goto L1;
6905         try
6906         {
6907             c = true;
6908         }
6909         catch (Exception/* e*/)
6910         {
6911           L1:
6912             ;
6913         }
6914         assert(c == false);
6915     }
6916 
6917     // jump inside TryCatchStatement.catches
6918     {
6919         bool c = false;
6920         try
6921         {
6922             throw new Exception("");
6923         }
6924         catch (Exception e)
6925         {
6926             goto L2;
6927             c = true;
6928           L2:
6929             ;
6930         }
6931         assert(c == false);
6932     }
6933 
6934     // exit from TryCatchStatement.catches
6935     {
6936         bool c = false;
6937         try
6938         {
6939             throw new Exception("");
6940         }
6941         catch (Exception e)
6942         {
6943             goto L3;
6944             c = true;
6945         }
6946       L3:
6947         assert(c == false);
6948     }
6949 
6950     return 1;
6951 }());
6952 
6953 static assert(()
6954 {
6955     // enter forward to TryFinallyStatement.body
6956     {
6957         bool c = false;
6958         goto L0;
6959         c = true;
6960         try
6961         {
6962           L0:
6963             ;
6964         }
6965         finally {}
6966         assert(!c);
6967     }
6968 
6969     // enter back to TryFinallyStatement.body
6970     {
6971         bool c = false;
6972         try
6973         {
6974             goto Lx;
6975           L1:
6976             c = true;
6977         }
6978         finally {
6979         }
6980 
6981       Lx:
6982         if (!c)
6983             goto L1;
6984     }
6985 
6986     // jump inside TryFinallyStatement.body
6987     {
6988         try
6989         {
6990             goto L2;
6991           L2: ;
6992         }
6993         finally {}
6994     }
6995 
6996     // exit from TryFinallyStatement.body
6997     {
6998         bool c = false;
6999         try
7000         {
7001             goto L3;
7002         }
7003         finally {}
7004 
7005         c = true;
7006       L3:
7007         assert(!c);
7008     }
7009 
7010     // enter in / exit out from finally block is rejected in semantic analysis
7011 
7012     // jump inside TryFinallyStatement.finalbody
7013     {
7014         bool c = false;
7015         try
7016         {
7017         }
7018         finally
7019         {
7020             goto L4;
7021             c = true;
7022           L4:
7023             assert(c == false);
7024         }
7025     }
7026 
7027     return 1;
7028 }());
7029 
7030 static assert(()
7031 {
7032     {
7033         bool c = false;
7034         with (Object.init)
7035         {
7036             goto L2;
7037             c = true;
7038           L2:
7039             ;
7040         }
7041         assert(c == false);
7042     }
7043 
7044     {
7045         bool c = false;
7046         with (Object.init)
7047         {
7048             goto L3;
7049             c = true;
7050         }
7051       L3:
7052         assert(c == false);
7053     }
7054 
7055     return 1;
7056 }());
7057 
7058 /**************************************************
7059     11627 -  cast dchar to char at compile time on AA assignment
7060 **************************************************/
7061 
7062 bool test11627()
7063 {
7064     char[ubyte] toCharTmp;
7065     dchar letter = 'A';
7066 
7067     //char c = cast(char)letter;    // OK
7068     toCharTmp[0] = cast(char)letter;    // NG
7069 
7070     return true;
7071 }
7072 static assert(test11627());
7073 
7074 /**************************************************
7075     11664 - ignore function local static variables
7076 **************************************************/
7077 
7078 bool test11664()
7079 {
7080     static int x;
7081     static int y = 1;
7082     return true;
7083 }
7084 static assert(test11664());
7085 
7086 /**************************************************
7087     12110 - operand of dereferencing does not need to be an lvalue
7088 **************************************************/
7089 
7090 struct SliceOverIndexed12110
7091 {
7092     Uint24Array12110* arr;
7093 
7094     @property front(uint val)
7095     {
7096         arr.dupThisReference();
7097     }
7098 }
7099 
7100 struct Uint24Array12110
7101 {
7102     ubyte[] data;
7103 
7104     this(ubyte[] range)
7105     {
7106         data = range;
7107         SliceOverIndexed12110(&this).front = 0;
7108         assert(data.length == range.length * 2);
7109     }
7110 
7111     void dupThisReference()
7112     {
7113         auto new_data = new ubyte[data.length * 2];
7114         data = new_data;
7115     }
7116 }
7117 
7118 static m12110 = Uint24Array12110([0x80]);
7119 
7120 /**************************************************
7121     12310 - heap allocation for built-in sclar types
7122 **************************************************/
7123 
7124 bool test12310()
7125 {
7126     auto p1 = new int, p2 = p1;
7127     assert(*p1 == 0);
7128     assert(*p2 == 0);
7129     *p1 = 10;
7130     assert(*p1 == 10);
7131     assert(*p2 == 10);
7132 
7133     auto q1 = new int(3), q2 = q1;
7134     assert(*q1 == 3);
7135     assert(*q2 == 3);
7136     *q1 = 20;
7137     assert(*q1 == 20);
7138     assert(*q2 == 20);
7139 
7140     return true;
7141 }
7142 static assert(test12310());
7143 
7144 /**************************************************
7145     12499 - initialize TupleDeclaraion in CTFE
7146 **************************************************/
7147 
7148 auto f12499()
7149 {
7150     //Initialize 3 ints to 5.
7151     TypeTuple!(int, int, int) a = 5;
7152     return a[0]; //Error: variable _a_field_0 cannot be read at compile time
7153 }
7154 static assert(f12499() == 5);
7155 
7156 /**************************************************
7157     12602 - slice in struct literal members
7158 **************************************************/
7159 
7160 struct Result12602
7161 {
7162     uint[] source;
7163 }
7164 
7165 auto wrap12602a(uint[] r)
7166 {
7167     return Result12602(r);
7168 }
7169 
7170 auto wrap12602b(uint[] r)
7171 {
7172     Result12602 x;
7173     x.source = r;
7174     return x;
7175 }
7176 
7177 auto testWrap12602a()
7178 {
7179     uint[] dest = [1, 2, 3, 4];
7180 
7181     auto ra = wrap12602a(dest[0 .. 2]);
7182     auto rb = wrap12602a(dest[2 .. 4]);
7183 
7184     foreach (i; 0 .. 2)
7185         rb.source[i] = ra.source[i];
7186 
7187     assert(ra.source == [1,2]);
7188     assert(rb.source == [1,2]);
7189     assert(&ra.source[0] == &dest[0]);
7190     assert(&rb.source[0] == &dest[2]);
7191     assert(dest == [1,2,1,2]);
7192     return dest;
7193 }
7194 
7195 auto testWrap12602b()
7196 {
7197     uint[] dest = [1, 2, 3, 4];
7198 
7199     auto ra = wrap12602b(dest[0 .. 2]);
7200     auto rb = wrap12602b(dest[2 .. 4]);
7201 
7202     foreach (i; 0 .. 2)
7203         rb.source[i] = ra.source[i];
7204 
7205     assert(ra.source == [1,2]);
7206     assert(rb.source == [1,2]);
7207     assert(&ra.source[0] == &dest[0]);
7208     assert(&rb.source[0] == &dest[2]);
7209     assert(dest == [1,2,1,2]);
7210     return dest;
7211 }
7212 
7213 auto testWrap12602c()
7214 {
7215     uint[] dest = [1, 2, 3, 4];
7216 
7217     auto ra = Result12602(dest[0 .. 2]);
7218     auto rb = Result12602(dest[2 .. 4]);
7219 
7220     foreach (i; 0 .. 2)
7221         rb.source[i] = ra.source[i];
7222 
7223     assert(ra.source == [1,2]);
7224     assert(rb.source == [1,2]);
7225     assert(&ra.source[0] == &dest[0]);
7226     assert(&rb.source[0] == &dest[2]);
7227     assert(dest == [1,2,1,2]);
7228     return dest;
7229 }
7230 
7231 auto testWrap12602d()
7232 {
7233     uint[] dest = [1, 2, 3, 4];
7234 
7235     Result12602 ra; ra.source = dest[0 .. 2];
7236     Result12602 rb; rb.source = dest[2 .. 4];
7237 
7238     foreach (i; 0 .. 2)
7239         rb.source[i] = ra.source[i];
7240 
7241     assert(ra.source == [1,2]);
7242     assert(rb.source == [1,2]);
7243     assert(&ra.source[0] == &dest[0]);
7244     assert(&rb.source[0] == &dest[2]);
7245     assert(dest == [1,2,1,2]);
7246     return dest;
7247 }
7248 
7249 static assert(testWrap12602a() == [1,2,1,2]);
7250 static assert(testWrap12602b() == [1,2,1,2]);
7251 static assert(testWrap12602c() == [1,2,1,2]);
7252 static assert(testWrap12602d() == [1,2,1,2]);
7253 
7254 /**************************************************
7255     12677 - class type initializing from DotVarExp
7256 **************************************************/
7257 
7258 final class C12677
7259 {
7260     TypeTuple!(Object, int[]) _test;
7261     this()
7262     {
7263         auto t0 = _test[0]; //
7264         auto t1 = _test[1]; //
7265         assert(t0 is null);
7266         assert(t1 is null);
7267     }
7268 }
7269 
7270 struct S12677
7271 {
7272     auto f = new C12677();
7273 }
7274 
7275 /**************************************************
7276     12851 - interpret function local const static array
7277 **************************************************/
7278 
7279 void test12851()
7280 {
7281     const int[5] arr;
7282     alias staticZip = TypeTuple!(arr[0]);
7283 }
7284 
7285 /**************************************************
7286     13630 - indexing and setting array element via pointer
7287 **************************************************/
7288 
7289 struct S13630(T)
7290 {
7291     T[3] arr;
7292 
7293     this(A...)(auto ref in A args)
7294     {
7295         auto p = arr.ptr;
7296 
7297         foreach (ref v; args)
7298         {
7299             *p = 0;
7300         }
7301     }
7302 }
7303 
7304 enum s13630 = S13630!float(1);
7305 
7306 /**************************************************
7307     13827
7308 **************************************************/
7309 
7310 struct Matrix13827(T, uint N)
7311 {
7312     private static defaultMatrix()
7313     {
7314         T arr[N];
7315         return arr;
7316     }
7317 
7318     union
7319     {
7320         T[N] A = defaultMatrix;
7321         T[N] flat;
7322     }
7323 
7324     this(A...)(auto ref in A args)
7325     {
7326         uint k;
7327 
7328         foreach (ref v; args)
7329             flat[k++] = cast(T)v;
7330     }
7331 }
7332 enum m13827 = Matrix13827!(int, 3)(1, 2, 3);
7333 
7334 /**************************************************
7335     13847 - support DotTypeExp
7336 **************************************************/
7337 
7338 class B13847
7339 {
7340     int foo() { return 1; }
7341 }
7342 
7343 class C13847 : B13847
7344 {
7345     override int foo() { return 2; }
7346 
7347     final void test(int n)
7348     {
7349         assert(foo() == n);
7350         assert(B13847.foo() == 1);
7351         assert(C13847.foo() == 2);
7352         assert(this.B13847.foo() == 1);
7353         assert(this.C13847.foo() == 2);
7354     }
7355 }
7356 
7357 class D13847 : C13847
7358 {
7359     override int foo() { return 3; }
7360 }
7361 
7362 static assert({
7363     C13847 c = new C13847();
7364     c.test(2);
7365     assert(c.B13847.foo() == 1);
7366     assert(c.C13847.foo() == 2);
7367 
7368     D13847 d = new D13847();
7369     d.test(3);
7370     assert(d.B13847.foo() == 1);
7371     assert(d.C13847.foo() == 2);
7372     assert(d.D13847.foo() == 3);
7373 
7374     c = d;
7375     c.test(3);
7376     assert(c.B13847.foo() == 1);
7377     assert(c.C13847.foo() == 2);
7378     return true;
7379 }());
7380 
7381 /**************************************************
7382     12495 - cast from string to immutable(ubyte)[]
7383 **************************************************/
7384 
7385 string getStr12495()
7386 {
7387     char[1] buf = void;                     // dummy starting point.
7388     string s = cast(string)buf[0..0];       // empty slice, .ptr points mutable.
7389     assert(buf.ptr == s.ptr);
7390     s ~= 'a';                               // this should allocate.
7391     assert(buf.ptr != s.ptr);
7392     return s.idup;                          // this should allocate again, and
7393                                             // definitely point immutable memory.
7394 }
7395 auto indexOf12495(string s)
7396 {
7397     auto p1 = s.ptr;
7398     auto p2 = (cast(immutable(ubyte)[])s).ptr;
7399     assert(cast(void*)p1 == cast(void*)p2); // OK <- fails
7400     return cast(void*)p2 - cast(void*)p1;   // OK <- "cannot subtract pointers ..."
7401 }
7402 static assert(indexOf12495(getStr12495()) == 0);
7403 
7404 /**************************************************
7405     13992 - Repainting pointer arithmetic result
7406 **************************************************/
7407 
7408 enum hash13992 = hashOf13992("abcd".ptr);
7409 
7410 @trusted hashOf13992(const void* buf)
7411 {
7412     auto data = cast(const(ubyte)*) buf;
7413     size_t hash;
7414     data += 2;      // problematic pointer arithmetic
7415     hash += *data;  // CTFE internal issue was shown by the dereference
7416     return hash;
7417 }
7418 
7419 /**************************************************
7420     13739 - Precise copy for ArrayLiteralExp elements
7421 **************************************************/
7422 
7423 static assert(
7424 {
7425     int[] a1 = [13];
7426     int[][] a2 = [a1];
7427     assert(a2[0] is a1);            // OK
7428     assert(a2[0].ptr is a1.ptr);    // OK <- NG
7429 
7430     a1[0] = 1;
7431     assert(a2[0][0] == 1);  // OK <- NG
7432 
7433     a2[0][0] = 2;
7434     assert(a1[0] == 2);     // OK <- NG
7435 
7436     return 1;
7437 }());
7438 
7439 /**************************************************
7440     14463 - ICE on slice assignment without postblit
7441 **************************************************/
7442 
7443 struct Boo14463
7444 {
7445     private int[1] c;
7446     this(int[] x)
7447     {
7448         c = x;
7449     }
7450 }
7451 immutable Boo14463 a14463 = Boo14463([1]);
7452 
7453 /**************************************************
7454     13295 - Don't copy struct literal in VarExp::interpret()
7455 **************************************************/
7456 
7457 struct S13295
7458 {
7459     int n;
7460 }
7461 
7462 void f13295(ref const S13295 s)
7463 {
7464     *cast(int*) &s.n = 1;
7465     assert(s.n == 1);     // OK <- fail
7466 }
7467 
7468 static assert(
7469 {
7470     S13295 s;
7471     f13295(s);
7472     return s.n == 1; // true <- false
7473 }());
7474 
7475 int foo14061(int[] a)
7476 {
7477     foreach (immutable x; a)
7478     {
7479         auto b = a ~ x;
7480         return b == [1, 1];
7481     }
7482     return 0;
7483 }
7484 static assert(foo14061([1]));
7485 
7486 /**************************************************
7487     14024 - CTFE version
7488 **************************************************/
7489 
7490 bool test14024()
7491 {
7492     string op;
7493 
7494     struct S
7495     {
7496         char x = 'x';
7497         this(this) { op ~= x-0x20; }    // upper case
7498         ~this()    { op ~= x; }         // lower case
7499     }
7500 
7501     S[4] mem;
7502     ref S[2] slice(int a, int b) { return mem[a .. b][0 .. 2]; }
7503 
7504     op = null;
7505     mem[0].x = 'a';
7506     mem[1].x = 'b';
7507     mem[2].x = 'x';
7508     mem[3].x = 'y';
7509     slice(0, 2) = slice(2, 4);  // [ab] = [xy]
7510     assert(op == "XaYb", op);
7511 
7512     op = null;
7513     mem[0].x = 'x';
7514     mem[1].x = 'y';
7515     mem[2].x = 'a';
7516     mem[3].x = 'b';
7517     slice(2, 4) = slice(0, 2);  // [ab] = [xy]
7518     assert(op == "XaYb", op);
7519 
7520     op = null;
7521     mem[0].x = 'a';
7522     mem[1].x = 'b';
7523     mem[2].x = 'c';
7524     slice(0, 2) = slice(1, 3);  // [ab] = [bc]
7525     assert(op == "BaCb", op);
7526 
7527     op = null;
7528     mem[0].x = 'x';
7529     mem[1].x = 'y';
7530     mem[2].x = 'z';
7531     slice(1, 3) = slice(0, 2);  // [yz] = [xy]
7532     assert(op == "YzXy", op);
7533 
7534     return true;
7535 }
7536 static assert(test14024());
7537 
7538 /**************************************************
7539     14304 - cache of static immutable value
7540 **************************************************/
7541 
7542 immutable struct Bug14304
7543 {
7544     string s_name;
7545     alias s_name this;
7546 
7547     string fun()()
7548     {
7549         return "fun";
7550     }
7551 }
7552 class Buggy14304
7553 {
7554     static string fun(string str)()
7555     {
7556         return str;
7557     }
7558     static immutable val = immutable Bug14304("val");
7559 }
7560 void test14304()
7561 {
7562     enum kt = Buggy14304.fun!(Buggy14304.val);
7563     static assert(kt == "val");
7564     enum bt = Buggy14304.val.fun();
7565     static assert(bt == "fun");
7566 }
7567 
7568 /**************************************************
7569     14371 - evaluate BinAssignExp as lvalue
7570 **************************************************/
7571 
7572 int test14371()
7573 {
7574     int x;
7575     ++(x += 1);
7576     return x;
7577 }
7578 static assert(test14371() == 2);
7579 
7580 /**************************************************
7581     7151 - [CTFE] cannot compare classes with ==
7582 **************************************************/
7583 
7584 bool test7151()
7585 {
7586     auto a = new Object;
7587     return a == a && a != new Object;
7588 }
7589 static assert(test7151());
7590 
7591 
7592 /**************************************************
7593     12603 - [CTFE] goto does not correctly call dtors
7594 **************************************************/
7595 
7596 struct S12603
7597 {
7598     this(uint* dtorCalled)
7599     {
7600         *dtorCalled = 0;
7601         this.dtorCalled = dtorCalled;
7602     }
7603 
7604     @disable this();
7605 
7606     ~this()
7607     {
7608         ++*dtorCalled;
7609     }
7610 
7611     uint* dtorCalled;
7612 }
7613 
7614 
7615 auto numDtorCallsByGotoWithinScope()
7616 {
7617     uint dtorCalled;
7618     {
7619         S12603 s = S12603(&dtorCalled);
7620         assert(dtorCalled == 0);
7621         goto L_abc;
7622         L_abc:
7623         assert(dtorCalled == 0);
7624     }
7625     assert(dtorCalled == 1);
7626     return dtorCalled;
7627 }
7628 static assert(numDtorCallsByGotoWithinScope() == 1);
7629 
7630 
7631 auto numDtorCallsByGotoOutOfScope()
7632 {
7633     uint dtorCalled;
7634     {
7635         S12603 s = S12603(&dtorCalled);
7636         assert(dtorCalled == 0);
7637         goto L_abc;
7638     }
7639     L_abc:
7640     assert(dtorCalled == 1);
7641     return dtorCalled;
7642 }
7643 static assert(numDtorCallsByGotoOutOfScope() == 1);
7644 
7645 
7646 uint numDtorCallsByGotoDifferentScopeAfter()
7647 {
7648     uint dtorCalled;
7649     {
7650         S12603 s = S12603(&dtorCalled);
7651         assert(dtorCalled == 0);
7652     }
7653     assert(dtorCalled == 1);
7654     goto L_abc;
7655     L_abc:
7656     assert(dtorCalled == 1);
7657     return dtorCalled;
7658 }
7659 static assert(numDtorCallsByGotoDifferentScopeAfter() == 1);
7660 
7661 
7662 auto numDtorCallsByGotoDifferentScopeBefore()
7663 {
7664     uint dtorCalled;
7665     assert(dtorCalled == 0);
7666     goto L_abc;
7667     L_abc:
7668     assert(dtorCalled == 0);
7669     {
7670         S12603 s = S12603(&dtorCalled);
7671         assert(dtorCalled == 0);
7672     }
7673     assert(dtorCalled == 1);
7674     return dtorCalled;
7675 }
7676 static assert(numDtorCallsByGotoDifferentScopeBefore() == 1);
7677 
7678 
7679 struct S12603_2
7680 {
7681     ~this()
7682     {
7683         dtorCalled = true;
7684     }
7685 
7686     bool dtorCalled = false;
7687 }
7688 
7689 auto structInCaseScope()
7690 {
7691     auto charsets = S12603_2();
7692     switch(1)
7693     {
7694     case 0:
7695         auto set = charsets;
7696         break;
7697     default:
7698         break;
7699     }
7700     return charsets.dtorCalled;
7701 }
7702 
7703 static assert(!structInCaseScope());
7704 
7705 /**************************************************
7706     15233 - ICE in TupleExp, Copy On Write bug
7707 **************************************************/
7708 
7709 alias TT15233(stuff ...) = stuff;
7710 
7711 struct Tok15233 {}
7712 enum tup15233 = TT15233!(Tok15233(), "foo");
7713 static assert(tup15233[0] == Tok15233());
7714 static assert(tup15233[1] == "foo");
7715 
7716 /**************************************************
7717     15251 - void cast in ForStatement.increment
7718 **************************************************/
7719 
7720 int test15251()
7721 {
7722     for (ubyte lwr = 19;
7723         lwr != 20;
7724         cast(void)++lwr)    // have to to be evaluated with ctfeNeedNothing
7725     {}
7726     return 1;
7727 }
7728 static assert(test15251());
7729 
7730 /**************************************************
7731     15998 - Sagfault caused by memory corruption
7732 **************************************************/
7733 
7734 immutable string[2] foo15998 = ["",""];
7735 immutable string[2][] bar15998a = foo15998 ~ baz15998;
7736 immutable string[2][] bar15998b = baz15998 ~ foo15998;
7737 
7738 auto baz15998()
7739 {
7740     immutable(string[2])[] r;
7741     return r;
7742 }
7743 
7744 static assert(bar15998a == [["", ""]]);
7745 static assert(bar15998b == [["", ""]]);
7746 
7747 /**************************************************
7748     16094 - Non-overlapped slice assignment on an aggregate
7749 **************************************************/
7750 
7751 char[] f16094a()
7752 {
7753     char[] x = new char[](6);
7754     x[3..6] = x[0..3];
7755     return x;
7756 }
7757 
7758 int[] f16094b()
7759 {
7760     int[] x = new int[](6);
7761     x[3..6] = x[0..3];
7762     return x;
7763 }
7764 
7765 enum copy16094a = f16094a();
7766 enum copy16094b = f16094b();
7767 
7768 /**************************************************/
7769 // https://issues.dlang.org/show_bug.cgi?id=17407
7770 
7771 bool foo17407()
7772 {
7773     void delegate ( ) longest_convert;
7774     return __traits(compiles, longest_convert = &doesNotExists);
7775 }
7776 
7777 static assert(!foo17407);
7778 
7779 /**************************************************/
7780 // https://issues.dlang.org/show_bug.cgi?id=18057
7781 // Recursive field initializer causes segfault.
7782 
7783 struct RBNode(T)
7784 {
7785     RBNode!T *copy = new RBNode!T;
7786 }
7787 
7788 static assert(!__traits(compiles, { alias bug18057 = RBNode!int; }));
7789 
7790 /************************************************/
7791 // https://issues.dlang.org/show_bug.cgi?id=9937
7792 
7793 int test9937()
7794 {
7795     import core.math;
7796 
7797     float x = float.max;
7798     x *= 2;
7799     x = toPrec!float(x);
7800     x /= 2;
7801     assert(x == float.infinity);
7802     return 1;
7803 }
7804 
7805 static assert(test9937());
7806