Debian Codegraph @

gcc/testsuite/gdc.test/compilable/interpret3.d

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