Debian Codegraph @

gcc/testsuite/gdc.test/runnable/sdtor.d

raw
1 // PERMUTE_ARGS: -unittest -O -release -inline -fPIC -g
2 // REQUIRED_ARGS: -preview=dtorfields
3 /*
4 TEST_OUTPUT:
5 ---
6 S7353
7 ---
8 */
9
10 import core.vararg;
11 // FIXME: Shouldn't tests that use this go in core.memory now that `delete` has been removed?
12 import core.memory : __delete;
13
14 extern (C) int printf(const(char*) fmt, ...) nothrow;
15
16 template TypeTuple(T...) { alias TypeTuple = T; }
17
18 /**********************************/
19
20 int sdtor;
21
22 struct S1
23 {
24 ~this() { printf("~S()\n"); sdtor++; }
25 }
26
27 void test1()
28 {
29 S1* s = new S1();
30 __delete(s);
31 assert(sdtor == 1);
32 }
33
34 /**********************************/
35
36 int sdtor3;
37
38 struct S3
39 { int a;
40 ~this() { printf("~S3()\n"); sdtor3++; assert(a == 3); }
41 }
42
43 struct T3
44 {
45 int i;
46 S3 s;
47 }
48
49 void test3()
50 {
51 T3* s = new T3();
52 s.s.a = 3;
53 __delete(s);
54 assert(sdtor3 == 1);
55 }
56
57 /**********************************/
58
59 int sdtor4;
60
61 struct S4
62 { int a = 3;
63 ~this()
64 { printf("~S4()\n");
65 if (a == 4)
66 assert(sdtor4 == 2);
67 else
68 { assert(a == 3);
69 assert(sdtor4 == 1);
70 }
71 sdtor4++;
72 }
73 }
74
75 struct T4
76 {
77 int i;
78 S4 s;
79 ~this() { printf("~T4()\n"); assert(sdtor4 == 0); sdtor4++; }
80 S4 t;
81 }
82
83 void test4()
84 {
85 T4* s = new T4();
86 s.s.a = 4;
87 __delete(s);
88 assert(sdtor4 == 3);
89 }
90
91 /**********************************/
92
93 int sdtor5;
94
95 template M5()
96 { ~this()
97 {
98 printf("~M5()\n"); assert(sdtor5 == 1); sdtor5++;
99 }
100 }
101
102 struct T5
103 {
104 mixin M5 m;
105 ~this() { printf("~T5()\n"); assert(sdtor5 == 0); sdtor5++; }
106 }
107
108 void test5()
109 {
110 T5* s = new T5();
111 __delete(s);
112 assert(sdtor5 == 2);
113 }
114
115 /**********************************/
116
117 int sdtor6;
118
119 struct S6
120 { int b = 7;
121 ~this()
122 {
123 printf("~S6()\n"); assert(b == 7); assert(sdtor6 == 1); sdtor6++;
124 }
125 }
126
127 class T6
128 {
129 int a = 3;
130 S6 s;
131 ~this() { printf("~T6()\n"); assert(a == 3); assert(sdtor6 == 0); sdtor6++; }
132 }
133
134 void test6()
135 {
136 T6 s = new T6();
137 __delete(s);
138 assert(sdtor6 == 2);
139 }
140
141 /**********************************/
142
143 int sdtor7;
144
145 struct S7
146 { int b = 7;
147 ~this()
148 {
149 printf("~S7()\n");
150 assert(b == 7);
151 assert(sdtor7 >= 1 && sdtor7 <= 3);
152 sdtor7++;
153 }
154 }
155
156 struct T7
157 {
158 int a = 3;
159 S7[3] s;
160 ~this()
161 { printf("~T7() %d\n", sdtor7);
162 assert(a == 3);
163 assert(sdtor7 == 0);
164 sdtor7++;
165 }
166 }
167
168 void test7()
169 {
170 T7* s = new T7();
171 __delete(s);
172 assert(sdtor7 == 4);
173 }
174
175 /**********************************/
176
177 int sdtor8;
178
179 struct S8
180 { int b = 7;
181 int c;
182 ~this()
183 {
184 printf("~S8() %d\n", sdtor8);
185 assert(b == 7);
186 assert(sdtor8 == c);
187 sdtor8++;
188 }
189 }
190
191 void test8()
192 {
193 S8[] s = new S8[3];
194 s[0].c = 2;
195 s[1].c = 1;
196 s[2].c = 0;
197 __delete(s);
198 assert(sdtor8 == 3);
199 }
200
201 /**********************************/
202
203 int sdtor9;
204
205 struct S9
206 { int b = 7;
207 ~this()
208 {
209 printf("~S9() %d\n", sdtor9);
210 assert(b == 7);
211 sdtor9++;
212 }
213 }
214
215 void test9()
216 {
217 {
218 S9 s;
219 }
220 assert(sdtor9 == 1);
221 }
222
223 /**********************************/
224
225 int sdtor10;
226
227 struct S10
228 { int b = 7;
229 int c;
230 ~this()
231 {
232 printf("~S10() %d\n", sdtor10);
233 assert(b == 7);
234 assert(sdtor10 == c);
235 sdtor10++;
236 }
237 }
238
239 void test10()
240 {
241 {
242 S10[3] s;
243 s[0].c = 2;
244 s[1].c = 1;
245 s[2].c = 0;
246 }
247 assert(sdtor10 == 3);
248 }
249
250 /**********************************/
251
252 int sdtor11;
253
254 template M11()
255 { ~this()
256 {
257 printf("~M11()\n"); assert(sdtor11 == 1); sdtor11++;
258 }
259 }
260
261 class T11
262 {
263 mixin M11 m;
264 ~this() { printf("~T11()\n"); assert(sdtor11 == 0); sdtor11++; }
265 }
266
267 void test11()
268 {
269 T11 s = new T11();
270 __delete(s);
271 assert(sdtor11 == 2);
272 }
273
274 /**********************************/
275
276 int sdtor12;
277
278 struct S12
279 { int a = 3;
280 ~this() { printf("~S12() %d\n", sdtor12); sdtor12++; }
281 }
282
283 void foo12(S12 s)
284 {
285 }
286
287 void test12()
288 {
289 {
290 S12 s;
291 foo12(s);
292 s.a = 4;
293 }
294 assert(sdtor12 == 2);
295 }
296
297 /**********************************/
298
299 struct S13
300 { int a = 3;
301 int opAssign(S13 s)
302 {
303 printf("S13.opAssign(%p)\n", &this);
304 a = 4;
305 return s.a + 2;
306 }
307 }
308
309 void test13()
310 {
311 S13 s;
312 S13 t;
313 assert((s = t) == 5);
314 assert(s.a == 4);
315 }
316
317 /**********************************/
318
319 struct S14
320 { int a = 3;
321 int opAssign(ref S14 s)
322 {
323 printf("S14.opAssign(%p)\n", &this);
324 a = 4;
325 return s.a + 2;
326 }
327 }
328
329 void test14()
330 {
331 S14 s;
332 S14 t;
333 assert((s = t) == 5);
334 assert(s.a == 4);
335 }
336
337 /**********************************/
338
339 struct S15
340 { int a = 3;
341 int opAssign(ref const S15 s)
342 {
343 printf("S15.opAssign(%p)\n", &this);
344 a = 4;
345 return s.a + 2;
346 }
347 }
348
349 void test15()
350 {
351 S15 s;
352 S15 t;
353 assert((s = t) == 5);
354 assert(s.a == 4);
355 }
356
357 /**********************************/
358
359 struct S16
360 { int a = 3;
361 int opAssign(S16 s, ...)
362 {
363 printf("S16.opAssign(%p)\n", &this);
364 a = 4;
365 return s.a + 2;
366 }
367 }
368
369 void test16()
370 {
371 S16 s;
372 S16 t;
373 assert((s = t) == 5);
374 assert(s.a == 4);
375 }
376
377 /**********************************/
378
379 struct S17
380 { int a = 3;
381 int opAssign(...)
382 {
383 printf("S17.opAssign(%p)\n", &this);
384 a = 4;
385 return 5;
386 }
387 }
388
389 void test17()
390 {
391 S17 s;
392 S17 t;
393 assert((s = t) == 5);
394 assert(s.a == 4);
395 }
396
397 /**********************************/
398
399 struct S18
400 { int a = 3;
401 int opAssign(S18 s, int x = 7)
402 {
403 printf("S18.opAssign(%p)\n", &this);
404 a = 4;
405 assert(x == 7);
406 return s.a + 2;
407 }
408 }
409
410 void test18()
411 {
412 S18 s;
413 S18 t;
414 assert((s = t) == 5);
415 assert(s.a == 4);
416 }
417
418 /**********************************/
419
420 struct S19
421 {
422 int a,b,c,d;
423 this(this) { printf("this(this) %p\n", &this); }
424 ~this() { printf("~this() %p\n", &this); }
425 }
426
427 S19 foo19()
428 {
429 S19 s;
430 void bar() { s.a++; }
431 bar();
432 return s;
433 }
434
435 void test19()
436 {
437 S19 t = foo19();
438 printf("-main()\n");
439 }
440
441 /**********************************/
442
443 struct S20
444 {
445 static char[] r;
446 int a,b,c=2,d;
447 this(this) { printf("this(this) %p\n", &this); r ~= '='; }
448 ~this() { printf("~this() %p\n", &this); r ~= '~'; }
449 }
450
451 void foo20()
452 {
453 S20 s;
454 S20[3] a;
455 assert(S20.r == "");
456 a = s;
457 assert(S20.r == "=~=~=~");
458 }
459
460 void test20()
461 {
462 foo20();
463 assert(S20.r == "=~=~=~~~~~");
464 printf("-main()\n");
465 }
466
467 /**********************************/
468
469 struct S21
470 {
471 static char[] r;
472 int a,b,c=2,d;
473 this(this) { printf("this(this) %p\n", &this); r ~= '='; }
474 ~this() { printf("~this() %p\n", &this); r ~= '~'; }
475 }
476
477 void foo21()
478 {
479 S21 s;
480 S21[3] a = s;
481 assert(S21.r == "===");
482 S21.r = null;
483 S21[3][2] b = s;
484 assert(S21.r == "======");
485 S21.r = null;
486 }
487
488 void test21()
489 {
490 foo21();
491 assert(S21.r == "~~~~~~~~~~");
492 printf("-main()\n");
493 }
494
495 /**********************************/
496
497 struct S22
498 {
499 static char[] r;
500 int a,b,c=2,d;
501 this(this) { printf("this(this) %p\n", &this); r ~= '='; }
502 ~this() { printf("~this() %p\n", &this); r ~= '~'; }
503 }
504
505 void foo22()
506 {
507 S22[3] s;
508 S22[3][2] a;
509 assert(S22.r == "");
510 a = s;
511 assert(S22.r == "===~~~===~~~");
512 S22.r = null;
513 }
514
515 void test22()
516 {
517 foo22();
518 assert(S22.r == "~~~~~~~~~");
519 printf("-main()\n");
520 }
521
522
523 /************************************************/
524
525 struct S23
526 {
527 int m = 4, n, o, p, q;
528
529 this(int x)
530 {
531 printf("S23(%d)\n", x);
532 assert(x == 3);
533 assert(m == 4 && n == 0 && o == 0 && p == 0 && q == 0);
534 q = 7;
535 }
536 }
537
538 void test23()
539 {
540 {
541 auto s = S23(3);
542 printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
543 assert(s.m == 4 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
544 }
545 {
546 auto s = new S23(3);
547 printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
548 assert(s.m == 4 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
549 }
550 {
551 S23 s;
552 s = S23(3);
553 printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
554 assert(s.m == 4 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
555 }
556 }
557
558 /************************************************/
559
560 struct S24
561 {
562 int m, n, o, p, q;
563
564 this(int x)
565 {
566 printf("S24(%d)\n", x);
567 assert(x == 3);
568 assert(m == 0 && n == 0 && o == 0 && p == 0 && q == 0);
569 q = 7;
570 }
571 }
572
573 void test24()
574 {
575 {
576 auto s = S24(3);
577 printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
578 assert(s.m == 0 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
579 }
580 {
581 auto s = new S24(3);
582 printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
583 assert(s.m == 0 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
584 }
585 {
586 S24 s;
587 s = S24(3);
588 printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
589 assert(s.m == 0 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
590 }
591 }
592
593 /**********************************/
594
595 struct S25
596 {
597 this(int s) {}
598 }
599
600 void test25()
601 {
602 auto a = S25();
603 }
604
605 /**********************************/
606
607 int z26;
608
609 struct A26
610 {
611 int id;
612 this(int x) { id = x; printf("Created A from scratch: %d\n", x); z26++; }
613 this(this) { printf("Copying A: %d\n", id); z26 += 10; }
614 ~this() { printf("Destroying A: %d\n", id); z26 += 100; }
615 }
616
617 struct B26
618 {
619 A26 member;
620 this(this) { printf("Copying B: %d\n", member.id); assert(0); }
621 }
622
623 B26 foo26()
624 {
625 A26 a = A26(45);
626 printf("1\n");
627 assert(z26 == 1);
628 return B26(a);
629 }
630
631 void test26()
632 {
633 {
634 auto b = foo26();
635 assert(z26 == 111);
636 printf("2\n");
637 }
638 assert(z26 == 211);
639 }
640
641 /**********************************/
642
643 int z27;
644
645 struct A27
646 {
647 int id;
648 this(int x) { id = x; printf("Ctor A27: %d\n", x); z27++; }
649 this(this) { printf("Copying A27: %d\n", id); z27 += 10; }
650 ~this() { printf("Destroying A27: %d\n", id); z27 += 100; }
651 }
652
653 struct B27
654 {
655 A27[2][3] member;
656 }
657
658 void test27()
659 {
660 {
661 A27[2][3] a;
662 assert(z27 == 0);
663 B27 b = B27(a);
664 assert(z27 == 60);
665 }
666 assert(z27 == 1260);
667 }
668
669
670 /**********************************/
671
672 string s28;
673
674 struct A28
675 {
676 this(this)
677 {
678 printf("A's copy\n");
679 s28 ~= "A";
680 }
681 }
682
683 struct B28
684 {
685 A28 member;
686 this(this)
687 {
688 printf("B's copy\n");
689 s28 ~= "B";
690 }
691 }
692
693 void test28()
694 {
695 B28 b1;
696 B28 b2 = b1;
697 assert(s28 == "AB");
698 }
699
700
701 /**********************************/
702
703 string s29;
704
705 template Templ29 ()
706 {
707 this(int i) { this(0.0); s29 ~= "i"; }
708 this(double d) { s29 ~= "d"; }
709 }
710
711 class C29 { mixin Templ29; }
712 struct S29 { mixin Templ29; }
713
714 void test29()
715 {
716 auto r = S29(1);
717 assert(s29 == "di");
718
719 r = S29(2.0);
720 assert(s29 == "did");
721
722 auto c = new C29(2);
723 assert(s29 == "diddi");
724
725 auto c2 = new C29(2.0);
726 assert(s29 == "diddid");
727 }
728
729 /**********************************/
730
731 struct S30
732 {
733 int x;
734 this(T)(T args) { x = args + 1; }
735 }
736
737 void test30()
738 {
739 auto s = S30(3);
740 assert(s.x == 4);
741 }
742
743 /**********************************/
744
745 struct S31
746 {
747 int x;
748 this(T...)(T args) { x = args[0] + 1; }
749 }
750
751 void test31()
752 {
753 auto s = S31(3);
754 assert(s.x == 4);
755 }
756
757 /**********************************/
758
759 struct S32
760 {
761 static int x;
762
763 this(int i)
764 {
765 printf("ctor %p(%d)\n", &this, i);
766 x += 1;
767 }
768
769 this(this)
770 {
771 printf("postblit %p\n", &this);
772 x += 0x10;
773 }
774
775 ~this()
776 {
777 printf("dtor %p\n", &this);
778 x += 0x100;
779 }
780 }
781
782 S32 foo32()
783 {
784 printf("test1\n");
785 return S32(1);
786 }
787
788 S32 bar32()
789 {
790 printf("test1\n");
791 S32 f = S32(1);
792 printf("test2\n");
793 return f;
794 }
795
796 void test32()
797 {
798 {
799 S32 s = foo32();
800 }
801 assert(S32.x == 0x101);
802
803 S32.x = 0;
804 {
805 S32 s = bar32();
806 }
807 assert(S32.x == 0x101);
808 }
809
810 /**********************************/
811
812 struct S33
813 {
814 static int x;
815
816 this(int i)
817 {
818 printf("ctor %p(%d)\n", &this, i);
819 x += 1;
820 }
821
822 this(this)
823 {
824 printf("postblit %p\n", &this);
825 x += 0x10;
826 }
827
828 ~this()
829 {
830 printf("dtor %p\n", &this);
831 x += 0x100;
832 }
833 }
834
835 struct T33
836 {
837 S33 foo()
838 {
839 return t;
840 }
841
842 S33 t;
843 }
844
845 void test33()
846 {
847 {
848 T33 t;
849 S33 s = t.foo();
850 }
851 printf("S.x = %x\n", S33.x);
852 assert(S33.x == 0x210);
853 }
854
855 /**********************************/
856
857 struct X34 {
858 int i;
859 this(this) {
860 printf("postblit %p\n", &this);
861 ++i;
862 }
863
864 ~this() {
865 printf("dtor %p\n", &this);
866 }
867 }
868
869 void test34()
870 {
871 X34[2] xs;
872 // xs[0][0] = X34();
873 printf("foreach\n");
874 for (int j = 0; j < xs.length; j++) {
875 j++,j--;
876 auto x = xs[j];
877 //foreach(x; xs)
878 //printf("foreach x.i = %d\n", x[0].i);
879 //assert(x[0].i == 1);
880 printf("foreach x.i = %d\n", x.i);
881 assert(x.i == 1);
882 }
883 printf("loop done\n");
884 }
885
886 /**********************************/
887
888 struct X35 {
889 __gshared int k;
890 int i;
891 this(this) {
892 printf("postblit %p\n", &this);
893 ++i;
894 }
895
896 ~this() {
897 printf("dtor %p\n", &this);
898 k++;
899 }
900 }
901
902 void test35()
903 {
904 {
905 X35[2] xs;
906 printf("foreach\n");
907 foreach(ref x; xs) {
908 printf("foreach x.i = %d\n", x.i);
909 assert(x.i == 0);
910 }
911 printf("loop done\n");
912 }
913 assert(X35.k == 2);
914 }
915
916 /**********************************/
917
918 struct X36 {
919 __gshared int k;
920 int i;
921 this(this) {
922 printf("postblit %p\n", &this);
923 ++i;
924 }
925
926 ~this() {
927 printf("dtor %p\n", &this);
928 k++;
929 }
930 }
931
932 void test36()
933 {
934 {
935 X36[2] xs;
936 printf("foreach\n");
937 foreach(x; xs) {
938 printf("foreach x.i = %d\n", x.i);
939 assert(x.i == 1);
940 }
941 printf("loop done\n");
942 }
943 assert(X36.k == 4);
944 }
945
946 /**********************************/
947
948 struct X37 {
949 __gshared int k;
950 int i;
951 this(this) {
952 printf("postblit %p\n", &this);
953 ++i;
954 }
955
956 ~this() {
957 printf("dtor %p\n", &this);
958 k++;
959 }
960 }
961
962 void test37() {
963 {
964 X37[2][3] xs;
965 printf("foreach\n");
966 foreach(ref x; xs) {
967 printf("foreach x.i = %d\n", x[0].i);
968 assert(x[0].i == 0);
969 }
970 printf("loop done\n");
971 }
972 assert(X37.k == 6);
973 }
974
975 /**********************************/
976
977 struct S38 {
978 __gshared int count;
979 __gshared int postblit;
980
981 this(int x) {
982 printf("this(%d)\n", x);
983 assert(this.x == 0);
984 this.x = x;
985 count++;
986 }
987 this(this) {
988 printf("this(this) with %d\n", x);
989 assert(x == 1 || x == 2);
990 count++;
991 postblit++;
992 }
993 ~this() {
994 printf("~this(%d)\n", x);
995 assert(x == 1 || x == 2);
996 x = 0;
997 count--;
998 }
999 int x;
1000 }
1001
1002 S38 foo38() {
1003 auto s = S38(1);
1004 return s;
1005 }
1006
1007 S38 bar38() {
1008 return S38(2);
1009 }
1010
1011 void test38()
1012 {
1013 {
1014 auto s1 = foo38();
1015 assert(S38.count == 1);
1016 assert(S38.postblit == 0);
1017 }
1018 assert(S38.count == 0);
1019 S38.postblit = 0;
1020
1021 {
1022 auto s2 = bar38();
1023 assert(S38.count == 1);
1024 assert(S38.postblit == 0);
1025 }
1026 assert(S38.count == 0);
1027 }
1028
1029
1030 /**********************************/
1031
1032 struct Foo39
1033 {
1034 int x;
1035 this(int v){ x = v + 1; }
1036 void opAssign(int v){
1037 x = v + 3;
1038 }
1039 }
1040
1041 void test39()
1042 {
1043 int y = 5;
1044 Foo39 f = y;
1045 assert(f.x == 6);
1046 f = y;
1047 assert(f.x == 8);
1048 // f = Foo39(y);
1049
1050 }
1051
1052 /**********************************/
1053
1054 bool approxEqual(float a, float b)
1055 {
1056 return a < b ? b-a < .001 : a-b < .001;
1057 }
1058
1059 struct Point {
1060 float x = 0, y = 0;
1061 const bool opEquals(const ref Point rhs)
1062 {
1063 return approxEqual(x, rhs.x) && approxEqual(y, rhs.y);
1064 }
1065 }
1066
1067 struct Rectangle {
1068 Point leftBottom, rightTop;
1069 }
1070
1071 void test40()
1072 {
1073 Rectangle a, b;
1074 assert(a == b);
1075 a.leftBottom.x = 1e-8;
1076 assert(a == b);
1077 a.rightTop.y = 5;
1078 assert(a != b);
1079 }
1080
1081 /**********************************/
1082
1083 struct S41 {
1084 this(int) immutable { }
1085 }
1086
1087 void test41()
1088 {
1089 auto s = new immutable S41(3);
1090 //writeln(typeid(typeof(s)));
1091 static assert(is(typeof(s) == immutable(S41)*));
1092
1093 auto t = immutable S41(3);
1094 //writeln(typeid(typeof(t)));
1095 static assert(is(typeof(t) == immutable(S41)));
1096 }
1097
1098 /**********************************/
1099
1100 class C42 {
1101 this(int) immutable {
1102 }
1103 }
1104
1105 void test42()
1106 {
1107 static assert(!__traits(compiles, new C42(3)));
1108 //writeln(typeid(typeof(c)));
1109 //static assert(is(typeof(c) == immutable(C42)));
1110
1111 auto d = new immutable(C42)(3);
1112 //writeln(typeid(typeof(d)));
1113 static assert(is(typeof(d) == immutable(C42)));
1114 }
1115
1116 /**********************************/
1117
1118 struct S43 {
1119 int i;
1120 int* p;
1121 // this(int i, int* t) immutable { this.i = i; p = t; }
1122 }
1123
1124 void test43()
1125 {
1126 int i;
1127 assert(!__traits(compiles, immutable(S43)(3, &i)));
1128 immutable int j = 4;
1129 auto s = immutable(S43)(3, &j);
1130 //writeln(typeid(typeof(s)));
1131 static assert(is(typeof(s) == immutable(S43)));
1132 }
1133
1134 /**********************************/
1135
1136 struct S44 {
1137 int i;
1138 immutable(int)* p;
1139 this(int i, immutable(int)* t) immutable { this.i = i; this.p = t; }
1140 }
1141
1142 void test44()
1143 {
1144 int i;
1145 assert(!__traits(compiles, immutable(S44)(3, &i)));
1146 immutable int j = 4;
1147 auto s = immutable(S44)(3, &j);
1148 //writeln(typeid(typeof(s)));
1149 static assert(is(typeof(s) == immutable(S44)));
1150 }
1151
1152 /**********************************/
1153
1154 class C45 {
1155 C45 next;
1156 this(int[] data) immutable {
1157 next = new immutable(C45)(data[1 .. $]);
1158 }
1159 }
1160
1161 void test45()
1162 {
1163 }
1164
1165 /**********************************/
1166 // https://issues.dlang.org/show_bug.cgi?id=3986
1167
1168 struct SiberianHamster
1169 {
1170 int rat = 813;
1171 this(string z) { }
1172 }
1173
1174 void test46()
1175 {
1176 SiberianHamster basil = "cybil";
1177 assert(basil.rat == 813);
1178 }
1179
1180 /**********************************/
1181 // https://issues.dlang.org/show_bug.cgi?id=8741
1182
1183 struct Vec8741
1184 {
1185 this(float x)
1186 {
1187 m[0] = x;
1188 m[1] = 58;
1189 }
1190 float[2] m;
1191 static Vec8741 zzz = Vec8741(7);
1192 }
1193
1194 void test8741()
1195 {
1196 assert(Vec8741.zzz.m[0] == 7);
1197 assert(Vec8741.zzz.m[1] == 58);
1198 }
1199
1200 /**********************************/
1201
1202 struct Segfault3984
1203 {
1204 int a;
1205 this(int x){
1206 a = x;
1207 }
1208 }
1209
1210 void test47()
1211 {
1212 //static
1213 assert(Segfault3984(3).a == 3);
1214 }
1215
1216 /**********************************/
1217
1218 void test48()
1219 {
1220 struct B {
1221 void foo() { }
1222 }
1223 B x = B.init;
1224 }
1225
1226 /**********************************/
1227
1228 struct Foo49 {
1229 int z;
1230 this(int a){z=a;}
1231 }
1232
1233 void bar49(Foo49 a = Foo49(1))
1234 {
1235 assert(a.z == 1);
1236 }
1237
1238 void test49()
1239 {
1240 bar49();
1241 bar49();
1242 }
1243
1244 /**********************************/
1245
1246 struct aStruct{
1247 int m_Int;
1248
1249 this(int a){
1250 m_Int = a;
1251 }
1252 }
1253
1254 class aClass{
1255 void aFunc(aStruct a = aStruct(44))
1256 {
1257 assert(a.m_Int == 44);
1258 }
1259 }
1260
1261 void test50()
1262 {
1263 aClass a = new aClass();
1264 a.aFunc();
1265 a.aFunc();
1266 }
1267
1268 /**********************************/
1269
1270 int A51_a;
1271
1272 struct A51
1273 {
1274 ~this() { ++A51_a; }
1275 }
1276
1277 void test51()
1278 {
1279 A51_a = 0; { while(0) A51 a; } assert(A51_a == 0);
1280 A51_a = 0; { if(0) A51 a; } assert(A51_a == 0);
1281 A51_a = 0; { if(1){} else A51 a; } assert(A51_a == 0);
1282 A51_a = 0; { for(;0;) A51 a; } assert(A51_a == 0);
1283 A51_a = 0; { if (1) { A51 a; } } assert(A51_a == 1);
1284 A51_a = 0; { if (1) A51 a; } assert(A51_a == 1);
1285 A51_a = 0; { if(0) {} else A51 a; } assert(A51_a == 1);
1286 A51_a = 0; { if (0) for(A51 a;;) {} } assert(A51_a == 0);
1287 A51_a = 0; { if (0) for(;;) A51 a; } assert(A51_a == 0);
1288 A51_a = 0; { do A51 a; while(0); } assert(A51_a == 1);
1289 A51_a = 0; { if (0) while(1) A51 a; } assert(A51_a == 0);
1290 A51_a = 0; { try A51 a; catch(Error e) {} } assert(A51_a == 1);
1291 A51_a = 0; { if (0) final switch(1) A51 a; } assert(A51_a == 0); // should fail to build
1292 // A51_a = 0; { if (0) switch(1) { A51 a; default: } } assert(A51_a == 0);
1293 A51_a = 0; { if (0) switch(1) { default: A51 a; } } assert(A51_a == 0);
1294 // A51_a = 0; { if (1) switch(1) { A51 a; default: } } assert(A51_a == 1); // should be 0, right?
1295 A51_a = 0; { if (1) switch(1) { default: A51 a; } } assert(A51_a == 1);
1296 // A51_a = 0; { final switch(0) A51 a; } assert(A51_a == 0);
1297 A51_a = 0; { A51 a; with(a) A51 b; } assert(A51_a == 2);
1298 }
1299
1300 /**********************************/
1301
1302 string s52;
1303
1304 struct A52
1305 {
1306 int m;
1307 this(this)
1308 {
1309 printf("this(this) %p\n", &this);
1310 s52 ~= 'a';
1311 }
1312 ~this()
1313 {
1314 printf("~this() %p\n", &this);
1315 s52 ~= 'b';
1316 }
1317 A52 copy()
1318 {
1319 s52 ~= 'c';
1320 A52 another = this;
1321 return another;
1322 }
1323 }
1324
1325 void test52()
1326 {
1327 {
1328 A52 a;
1329 A52 b = a.copy();
1330 printf("a: %p, b: %p\n", &a, &b);
1331 }
1332 printf("s = '%.*s'\n", cast(int)s52.length, s52.ptr);
1333 assert(s52 == "cabb");
1334 }
1335
1336 /**********************************/
1337 // https://issues.dlang.org/show_bug.cgi?id=4339
1338
1339 struct A53 {
1340 invariant() { }
1341 ~this() { }
1342 void opAssign(A53 a) {}
1343 int blah(A53 a) { return 0; }
1344 }
1345
1346 /**********************************/
1347
1348 struct S54
1349 {
1350 int x = 1;
1351
1352 int bar() { return x; }
1353
1354 this(int i)
1355 {
1356 printf("ctor %p(%d)\n", &this, i);
1357 t ~= "a";
1358 }
1359
1360 this(this)
1361 {
1362 printf("postblit %p\n", &this);
1363 t ~= "b";
1364 }
1365
1366 ~this()
1367 {
1368 printf("dtor %p\n", &this);
1369 t ~= "c";
1370 }
1371
1372 static string t;
1373 }
1374
1375 void bar54(S54 s) { }
1376
1377 S54 abc54() { return S54(1); }
1378
1379 void test54()
1380 {
1381 { S54.t = null;
1382 S54 s = S54(1);
1383 }
1384 assert(S54.t == "ac");
1385
1386 { S54.t = null;
1387 S54 s = S54();
1388 }
1389 assert(S54.t == "c");
1390
1391 { S54.t = null;
1392 int b = 1 && (){ bar54(S54(1)); return 1;}();
1393 }
1394 assert(S54.t == "ac");
1395
1396 { S54.t = null;
1397 int b = 0 && (){ bar54(S54(1)); return 1;}();
1398 }
1399 assert(S54.t == "");
1400
1401 { S54.t = null;
1402 int b = 0 || (){ bar54(S54(1)); return 1;}();
1403 }
1404 assert(S54.t == "ac");
1405
1406 { S54.t = null;
1407 int b = 1 || (){ bar54(S54(1)); return 1;}();
1408 }
1409 assert(S54.t == "");
1410
1411 {
1412 S54.t = null;
1413 { const S54 s = S54(1); }
1414 assert(S54.t == "ac");
1415 }
1416 {
1417 S54.t = null;
1418 abc54();
1419 assert(S54.t == "ac");
1420 }
1421 {
1422 S54.t = null;
1423 abc54().x += 1;
1424 assert(S54.t == "ac");
1425 }
1426 }
1427
1428 /**********************************/
1429
1430 void test55()
1431 {
1432 S55 s;
1433 auto t = s.copy();
1434 assert(t.count == 1); // (5)
1435 }
1436
1437 struct S55
1438 {
1439 int count;
1440 this(this) { ++count; }
1441 S55 copy() { return this; }
1442 }
1443
1444 /**********************************/
1445
1446 struct S56
1447 {
1448 int x = 1;
1449
1450 int bar() { return x; }
1451
1452 this(int i)
1453 {
1454 printf("ctor %p(%d)\n", &this, i);
1455 t ~= "a";
1456 }
1457
1458 this(this)
1459 {
1460 printf("postblit %p\n", &this);
1461 t ~= "b";
1462 }
1463
1464 ~this()
1465 {
1466 printf("dtor %p\n", &this);
1467 t ~= "c";
1468 }
1469
1470 static string t;
1471 }
1472
1473 int foo56()
1474 {
1475 throw new Throwable("hello");
1476 return 5;
1477 }
1478
1479
1480 void test56()
1481 {
1482 int i;
1483 int j;
1484 try
1485 {
1486 j |= 1;
1487 i = S56(1).x + foo56() + 1;
1488 j |= 2;
1489 }
1490 catch (Throwable o)
1491 {
1492 printf("caught\n");
1493 j |= 4;
1494 }
1495 printf("i = %d, j = %d\n", i, j);
1496 assert(i == 0);
1497 assert(j == 5);
1498 }
1499
1500 /**********************************/
1501 // https://issues.dlang.org/show_bug.cgi?id=5859
1502
1503 int dtor_cnt = 0;
1504 struct S57
1505 {
1506 int v;
1507 this(int n){ v = n; printf("S.ctor v=%d\n", v); }
1508 ~this(){ ++dtor_cnt; printf("S.dtor v=%d\n", v); }
1509 bool opCast(T:bool)(){ printf("S.cast v=%d\n", v); return true; }
1510 }
1511 S57 f(int n){ return S57(n); }
1512
1513 void test57()
1514 {
1515 printf("----\n");
1516 dtor_cnt = 0;
1517 if (auto s = S57(10))
1518 {
1519 printf("ifbody\n");
1520 }
1521 else assert(0);
1522 assert(dtor_cnt == 1);
1523
1524 printf("----\n"); //+
1525 dtor_cnt = 0;
1526 S57(1), S57(2);
1527 if (auto s = S57(10))
1528 {
1529 assert(dtor_cnt == 2);
1530 printf("ifbody\n");
1531 }
1532 else assert(0);
1533 assert(dtor_cnt == 3); // +/
1534
1535 printf("----\n");
1536 dtor_cnt = 0;
1537 try{
1538 if (auto s = S57(10))
1539 {
1540 printf("ifbody\n");
1541 throw new Exception("test");
1542 }
1543 else assert(0);
1544 }catch (Exception e){}
1545 assert(dtor_cnt == 1);
1546
1547
1548
1549 printf("----\n");
1550 dtor_cnt = 0;
1551 if (auto s = f(10))
1552 {
1553 printf("ifbody\n");
1554 }
1555 else assert(0);
1556 assert(dtor_cnt == 1);
1557
1558 printf("----\n"); //+
1559 dtor_cnt = 0;
1560 f(1), f(2);
1561 if (auto s = f(10))
1562 {
1563 assert(dtor_cnt == 2);
1564 printf("ifbody\n");
1565 }
1566 else assert(0);
1567 assert(dtor_cnt == 3); // +/
1568
1569 printf("----\n");
1570 dtor_cnt = 0;
1571 try{
1572 if (auto s = f(10))
1573 {
1574 printf("ifbody\n");
1575 throw new Exception("test");
1576 }
1577 else assert(0);
1578 }catch (Exception e){}
1579 assert(dtor_cnt == 1);
1580
1581
1582
1583
1584 printf("----\n");
1585 dtor_cnt = 0;
1586 if (S57(10))
1587 {
1588 assert(dtor_cnt == 1);
1589 printf("ifbody\n");
1590 }
1591 else assert(0);
1592
1593 printf("----\n");
1594 dtor_cnt = 0;
1595 S57(1), S57(2);
1596 if (S57(10))
1597 {
1598 assert(dtor_cnt == 3);
1599 printf("ifbody\n");
1600 }
1601 else assert(0);
1602
1603 printf("----\n");
1604 dtor_cnt = 0;
1605 try{
1606 if (auto s = S57(10))
1607 {
1608 printf("ifbody\n");
1609 throw new Exception("test");
1610 }
1611 else assert(0);
1612 }catch (Exception e){}
1613 assert(dtor_cnt == 1);
1614
1615
1616
1617 printf("----\n");
1618 dtor_cnt = 0;
1619 if (f(10))
1620 {
1621 assert(dtor_cnt == 1);
1622 printf("ifbody\n");
1623 }
1624 else assert(0);
1625
1626 printf("----\n");
1627 dtor_cnt = 0;
1628 f(1), f(2);
1629 if (f(10))
1630 {
1631 assert(dtor_cnt == 3);
1632 printf("ifbody\n");
1633 }
1634 else assert(0);
1635
1636 printf("----\n");
1637 dtor_cnt = 0;
1638 try{
1639 if (auto s = f(10))
1640 {
1641 printf("ifbody\n");
1642 throw new Exception("test");
1643 }
1644 else assert(0);
1645 }catch (Exception e){}
1646 assert(dtor_cnt == 1);
1647 }
1648
1649 /**********************************/
1650 // https://issues.dlang.org/show_bug.cgi?id=5574
1651
1652 struct foo5574a
1653 {
1654 ~this() {}
1655 }
1656 class bar5574a
1657 {
1658 foo5574a[1] frop;
1659 }
1660
1661 struct foo5574b
1662 {
1663 this(this){}
1664 }
1665 struct bar5574b
1666 {
1667 foo5574b[1] frop;
1668 }
1669
1670 /**********************************/
1671 // https://issues.dlang.org/show_bug.cgi?id=5777
1672
1673 int sdtor58 = 0;
1674 S58* ps58;
1675
1676 struct S58
1677 {
1678 @disable this(this);
1679 ~this(){ ++sdtor58; }
1680 }
1681
1682 S58 makeS58()
1683 {
1684 S58 s;
1685 ps58 = &s;
1686 return s;
1687 }
1688
1689 void test58()
1690 {
1691 auto s1 = makeS58();
1692 assert(ps58 == &s1);
1693 assert(sdtor58 == 0);
1694 }
1695
1696 /**********************************/
1697 // https://issues.dlang.org/show_bug.cgi?id=6308
1698
1699 struct C59
1700 {
1701 void oops()
1702 {
1703 throw new Throwable("Oops!");
1704 }
1705
1706 ~this()
1707 {
1708 }
1709 }
1710
1711 void foo59()
1712 {
1713 C59().oops();
1714 // auto c = C(); c.oops();
1715 }
1716
1717
1718 void test59()
1719 {
1720 int i = 0;
1721 try
1722 foo59();
1723 catch (Throwable)
1724 { i = 1;
1725 }
1726 assert(i == 1);
1727 }
1728
1729 /**********************************/
1730 // https://issues.dlang.org/show_bug.cgi?id=5737
1731
1732 void test5737()
1733 {
1734 static struct S
1735 {
1736 static int destroyed;
1737 static int copied;
1738
1739 this(this)
1740 {
1741 copied++;
1742 }
1743
1744 ~this()
1745 {
1746 destroyed++;
1747 }
1748 }
1749
1750 static S s;
1751
1752 ref S foo()
1753 {
1754 return s;
1755 }
1756
1757 {
1758 auto s2 = foo();
1759 }
1760
1761 assert(S.copied == 1); // fail, s2 was not copied;
1762 assert(S.destroyed == 1); // ok, s2 was destroyed
1763 }
1764
1765 /**********************************/
1766 // https://issues.dlang.org/show_bug.cgi?id=6119
1767
1768 void test6119()
1769 {
1770 int postblit = 0;
1771 int dtor = 0;
1772
1773 struct Test
1774 {
1775 this(this) { ++postblit; }
1776 ~this() { ++dtor; }
1777 }
1778
1779 void takesVal(Test x) {}
1780 ref Test returnsRef(ref Test x) { return x; }
1781
1782 void f(ref Test x) { takesVal( x ); }
1783
1784 Test x;
1785
1786 postblit = dtor = 0;
1787 takesVal(returnsRef(x));
1788 assert(postblit == 1);
1789 assert(dtor == 1);
1790
1791 postblit = dtor = 0;
1792 f(x);
1793 assert(postblit == 1);
1794 assert(dtor == 1);
1795 }
1796
1797 /**********************************/
1798 // https://issues.dlang.org/show_bug.cgi?id=6364
1799
1800 struct Foo6364
1801 {
1802 int state = 1;
1803
1804 ~this()
1805 {
1806 state = 0;
1807 }
1808 }
1809
1810 void testfoo6364()
1811 {
1812 static Foo6364 foo;
1813 printf("%d\n", foo.state);
1814 assert(foo.state == 1);
1815 }
1816
1817 void test6364()
1818 {
1819 testfoo6364();
1820 testfoo6364();
1821 }
1822
1823 /**********************************/
1824 // https://issues.dlang.org/show_bug.cgi?id=6499
1825
1826 struct S6499
1827 {
1828 string m = "<not set>";
1829
1830 this(string s)
1831 {
1832 m = s;
1833 printf("Constructor - %.*s\n", cast(int)m.length, m.ptr);
1834 if (m == "foo") { ++sdtor; assert(sdtor == 1); }
1835 if (m == "bar") { ++sdtor; assert(sdtor == 2); }
1836 }
1837 this(this)
1838 {
1839 printf("Postblit - %.*s\n", cast(int)m.length, m.ptr);
1840 assert(0);
1841 }
1842 ~this()
1843 {
1844 printf("Destructor - %.*s\n", cast(int)m.length, m.ptr);
1845 if (m == "bar") { assert(sdtor == 2); --sdtor; }
1846 if (m == "foo") { assert(sdtor == 1); --sdtor; }
1847 }
1848 S6499 bar() { return S6499("bar"); }
1849 S6499 baz()() { return S6499("baz"); }
1850 }
1851
1852 void test6499()
1853 {
1854 S6499 foo() { return S6499("foo"); }
1855
1856 {
1857 sdtor = 0;
1858 scope(exit) assert(sdtor == 0);
1859 foo().bar();
1860 }
1861 {
1862 sdtor = 0;
1863 scope(exit) assert(sdtor == 0);
1864 foo().baz();
1865 }
1866 }
1867
1868 /**********************************/
1869
1870 template isImplicitlyConvertible(From, To)
1871 {
1872 enum bool isImplicitlyConvertible = is(typeof({
1873 void fun(ref From v) {
1874 void gun(To) {}
1875 gun(v);
1876 }
1877 }()));
1878 }
1879
1880 void test60()
1881 {
1882 static struct X1
1883 {
1884 void* ptr;
1885 this(this){}
1886 }
1887 static struct S1
1888 {
1889 X1 x;
1890 }
1891
1892 static struct X2
1893 {
1894 int ptr;
1895 this(this){}
1896 }
1897 static struct S2
1898 {
1899 X2 x;
1900 }
1901
1902 {
1903 S1 ms;
1904 S1 ms2 = ms; // mutable to mutable
1905 const(S1) cs2 = ms; // mutable to const // NG -> OK
1906 }
1907 {
1908 const(S1) cs;
1909 static assert(!__traits(compiles,{ // NG -> OK
1910 S1 ms2 = cs; // field has reference, then const to mutable is invalid
1911 }));
1912 const(S1) cs2 = cs; // const to const // NG -> OK
1913 }
1914 static assert( isImplicitlyConvertible!( S1 , S1 ) );
1915 static assert( isImplicitlyConvertible!( S1 , const(S1)) ); // NG -> OK
1916 static assert(!isImplicitlyConvertible!(const(S1), S1 ) );
1917 static assert( isImplicitlyConvertible!(const(S1), const(S1)) ); // NG -> OK
1918
1919
1920 {
1921 S2 ms;
1922 S2 ms2 = ms; // mutable to mutable
1923 const(S2) cs2 = ms; // mutable to const // NG -> OK
1924 }
1925 {
1926 const(S2) cs;
1927 S2 ms2 = cs; // all fields are value, then const to mutable is OK
1928 const(S2) cs2 = cs; // const to const // NG -> OK
1929 }
1930
1931 static assert( isImplicitlyConvertible!( S2 , S2 ) );
1932 static assert( isImplicitlyConvertible!( S2 , const(S2)) ); // NG -> OK
1933 static assert( isImplicitlyConvertible!(const(S2), S2 ) );
1934 static assert( isImplicitlyConvertible!(const(S2), const(S2)) ); // NG -> OK
1935 }
1936
1937 /**********************************/
1938 // https://issues.dlang.org/show_bug.cgi?id=4316
1939
1940 struct A4316
1941 {
1942 this(this) @safe { }
1943 }
1944
1945 @safe void test4316()
1946 {
1947 A4316 a;
1948 auto b = a; // Error: safe function 'main' cannot call system function'__cpctor'
1949 }
1950
1951 /**********************************/
1952
1953 struct F6177
1954 {
1955 ~this() {}
1956 }
1957
1958 struct G6177
1959 {
1960 this(F6177[] p...) {}
1961 }
1962
1963 void test6177()
1964 {
1965 F6177 c;
1966 auto g = G6177(c);
1967 }
1968
1969
1970 /**********************************/
1971 // https://issues.dlang.org/show_bug.cgi?id=6470
1972
1973 struct S6470
1974 {
1975 static int spblit;
1976
1977 this(this){ ++spblit; }
1978 }
1979
1980 void test6470()
1981 {
1982 S6470[] a1;
1983 S6470[] a2;
1984 a1.length = 3;
1985 a2.length = 3;
1986 a1[] = a2[];
1987 assert(S6470.spblit == 3);
1988
1989 S6470 s;
1990
1991 S6470[] a3;
1992 a3.length = 3;
1993 a3 = [s, s, s];
1994 assert(S6470.spblit == 6);
1995
1996 void func(S6470[] a){}
1997 func([s, s, s]);
1998 assert(S6470.spblit == 9);
1999 }
2000
2001 /**********************************/
2002 // https://issues.dlang.org/show_bug.cgi?id=6636
2003
2004 struct S6636
2005 {
2006 ~this()
2007 {
2008 ++sdtor;
2009 }
2010 }
2011
2012 void func6636(S6636[3] sa) {}
2013
2014 void test6636()
2015 {
2016 sdtor = 0;
2017
2018 S6636[3] sa;
2019 func6636(sa);
2020 assert(sdtor == 3);
2021 }
2022
2023 /**********************************/
2024 // https://issues.dlang.org/show_bug.cgi?id=6637
2025
2026 struct S6637
2027 {
2028 static int spblit;
2029
2030 this(this){ ++spblit; }
2031 }
2032
2033 void test6637()
2034 {
2035 void func(S6637[3] sa){}
2036
2037 S6637[3] sa;
2038 func(sa);
2039 assert(S6637.spblit == 3);
2040 }
2041
2042 /**********************************/
2043 // https://issues.dlang.org/show_bug.cgi?id=7353
2044
2045 struct S7353
2046 {
2047 static uint ci = 0;
2048 uint i;
2049
2050 this(int x) { i = ci++; /*writeln("new: ", i);*/ }
2051 this(this) { i = ci++; /*writeln("copy ", i);*/ }
2052 ~this() { /*writeln("del ", i);*/ }
2053
2054 S7353 save1() // produces 2 copies in total
2055 {
2056 S7353 s = this;
2057 return s;
2058 }
2059 auto save2() // produces 3 copies in total
2060 {
2061 S7353 s = this;
2062 return s;
2063 pragma(msg, typeof(return));
2064 }
2065 }
2066 void test7353()
2067 {
2068 {
2069 auto s = S7353(1), t = S7353(1);
2070 t = s.save1();
2071 assert(S7353.ci == 3);
2072 }
2073 S7353.ci = 0; //writeln("-");
2074 {
2075 auto s = S7353(1), t = S7353(1);
2076 t = s.save2();
2077 assert(S7353.ci == 3);
2078 }
2079 }
2080
2081 /**********************************/
2082 // https://issues.dlang.org/show_bug.cgi?id=8036
2083
2084 struct S8036a
2085 {
2086 ~this() {}
2087 }
2088 struct S8036b // or class
2089 {
2090 S8036a[0] s;
2091 }
2092
2093 /**********************************/
2094
2095 struct S61
2096 {
2097 this(long length)
2098 {
2099 this.length = length;
2100 }
2101
2102 long length;
2103 }
2104
2105
2106 const(S61) copy(const S61 s)
2107 {
2108 return s;
2109 }
2110
2111
2112 void test61()
2113 {
2114 S61 t = S61(42);
2115 const S61 u = t;
2116
2117 assert(t == u);
2118 assert(copy(t) == u);
2119 assert(t == copy(u));
2120 }
2121
2122 /**********************************/
2123 // https://issues.dlang.org/show_bug.cgi?id=7506
2124
2125 void test7506()
2126 {
2127 static struct S
2128 {
2129 static uint ci = 0;
2130 static uint di = 0;
2131 uint i;
2132
2133 this(int x) { i = ci++; /*writeln("new: ", i);*/ }
2134 this(this) { i = ci++; /*writeln("copy ", i);*/ }
2135 ~this() { ++di; /*writeln("del: ", i);*/ }
2136
2137 S save3()
2138 {
2139 return this;
2140 }
2141 }
2142
2143 {
2144 auto s = S(1), t = S(1);
2145 assert(S.ci == 2);
2146 t = s.save3();
2147 assert(S.ci == 3); // line 23
2148 }
2149 assert(S.di == 3);
2150 }
2151
2152 /**********************************/
2153 // https://issues.dlang.org/show_bug.cgi?id=7516
2154
2155 struct S7516
2156 {
2157 int val;
2158
2159 this(int n) { val = n; }
2160 this(this) { val *= 3; }
2161 }
2162
2163 // CondExp on return statement
2164 void test7516a()
2165 {
2166 alias S = S7516;
2167 S s1 = S(1);
2168 S s2 = S(2);
2169
2170 S foo(bool f) { return f ? s1 : s2; }
2171 S hoo(bool f) { return f ? S(1) : S(2); }
2172 S bar(bool f) { return f ? s1 : S(2); }
2173 S baz(bool f) { return f ? S(1) : s2; }
2174
2175 auto r1 = foo(true); assert(r1.val == 3);
2176 auto r2 = foo(false); assert(r2.val == 6);
2177 auto r3 = hoo(true); assert(r3.val == 1);
2178 auto r4 = hoo(false); assert(r4.val == 2);
2179 auto r5 = bar(true); assert(r5.val == 3);
2180 auto r6 = bar(false); assert(r6.val == 2);
2181 auto r7 = baz(true); assert(r7.val == 1);
2182 auto r8 = baz(false); assert(r8.val == 6);
2183 }
2184
2185 // CondExp on function argument
2186 void test7516b()
2187 {
2188 alias S = S7516;
2189 S s1 = S(1);
2190 S s2 = S(2);
2191 S func(S s) { return s; }
2192
2193 S foo(bool f) { return func(f ? s1 : s2 ); }
2194 S hoo(bool f) { return func(f ? S(1) : S(2)); }
2195 S bar(bool f) { return func(f ? s1 : S(2)); }
2196 S baz(bool f) { return func(f ? S(1) : s2 ); }
2197
2198 auto r1 = foo(true); assert(r1.val == 3 * 3);
2199 auto r2 = foo(false); assert(r2.val == 6 * 3);
2200 auto r3 = hoo(true); assert(r3.val == 1 * 3);
2201 auto r4 = hoo(false); assert(r4.val == 2 * 3);
2202 auto r5 = bar(true); assert(r5.val == 3 * 3);
2203 auto r6 = bar(false); assert(r6.val == 2 * 3);
2204 auto r7 = baz(true); assert(r7.val == 1 * 3);
2205 auto r8 = baz(false); assert(r8.val == 6 * 3);
2206 }
2207
2208 // CondExp on array literal
2209 void test7516c()
2210 {
2211 alias S = S7516;
2212 S s1 = S(1);
2213 S s2 = S(2);
2214
2215 S[] foo(bool f) { return [f ? s1 : s2 ]; }
2216 S[] hoo(bool f) { return [f ? S(1) : S(2)]; }
2217 S[] bar(bool f) { return [f ? s1 : S(2)]; }
2218 S[] baz(bool f) { return [f ? S(1) : s2 ]; }
2219
2220 auto r1 = foo(true); assert(r1[0].val == 3);
2221 auto r2 = foo(false); assert(r2[0].val == 6);
2222 auto r3 = hoo(true); assert(r3[0].val == 1);
2223 auto r4 = hoo(false); assert(r4[0].val == 2);
2224 auto r5 = bar(true); assert(r5[0].val == 3);
2225 auto r6 = bar(false); assert(r6[0].val == 2);
2226 auto r7 = baz(true); assert(r7[0].val == 1);
2227 auto r8 = baz(false); assert(r8[0].val == 6);
2228 }
2229
2230 // CondExp on rhs of cat assign
2231 void test7516d()
2232 {
2233 alias S = S7516;
2234 S s1 = S(1);
2235 S s2 = S(2);
2236
2237 S[] foo(bool f) { S[] a; a ~= f ? s1 : s2 ; return a; }
2238 S[] hoo(bool f) { S[] a; a ~= f ? S(1) : S(2); return a; }
2239 S[] bar(bool f) { S[] a; a ~= f ? s1 : S(2); return a; }
2240 S[] baz(bool f) { S[] a; a ~= f ? S(1) : s2 ; return a; }
2241
2242 auto r1 = foo(true); assert(r1[0].val == 3);
2243 auto r2 = foo(false); assert(r2[0].val == 6);
2244 auto r3 = hoo(true); assert(r3[0].val == 1);
2245 auto r4 = hoo(false); assert(r4[0].val == 2);
2246 auto r5 = bar(true); assert(r5[0].val == 3);
2247 auto r6 = bar(false); assert(r6[0].val == 2);
2248 auto r7 = baz(true); assert(r7[0].val == 1);
2249 auto r8 = baz(false); assert(r8[0].val == 6);
2250 }
2251
2252 // CondExp on struct literal element
2253 void test7516e()
2254 {
2255 alias S = S7516;
2256 S s1 = S(1);
2257 S s2 = S(2);
2258 struct X { S s; }
2259
2260 X foo(bool f) { return X(f ? s1 : s2 ); }
2261 X hoo(bool f) { return X(f ? S(1) : S(2)); }
2262 X bar(bool f) { return X(f ? s1 : S(2)); }
2263 X baz(bool f) { return X(f ? S(1) : s2 ); }
2264
2265 auto r1 = foo(true); assert(r1.s.val == 3);
2266 auto r2 = foo(false); assert(r2.s.val == 6);
2267 auto r3 = hoo(true); assert(r3.s.val == 1);
2268 auto r4 = hoo(false); assert(r4.s.val == 2);
2269 auto r5 = bar(true); assert(r5.s.val == 3);
2270 auto r6 = bar(false); assert(r6.s.val == 2);
2271 auto r7 = baz(true); assert(r7.s.val == 1);
2272 auto r8 = baz(false); assert(r8.s.val == 6);
2273 }
2274
2275 /**********************************/
2276 // https://issues.dlang.org/show_bug.cgi?id=7530
2277
2278 void test7530()
2279 {
2280 static struct S
2281 {
2282 int val;
2283
2284 this(int n) { val = n; }
2285 this(this) { val *= 3; }
2286 }
2287
2288 S[] sa = new S[](1);
2289 sa[0].val = 1;
2290 S foo()
2291 {
2292 return sa[0];
2293 }
2294 auto s = foo();
2295 assert(s.val == 3);
2296 }
2297
2298 /**********************************/
2299
2300 struct S62
2301 {
2302 this(int length)
2303 {
2304 _length = length;
2305 }
2306
2307 int opBinary(string op)(in S62 rhs) const
2308 if(op == "-")
2309 {
2310 return this.length - rhs.length;
2311 }
2312
2313 @property int length() const
2314 {
2315 return _length;
2316 }
2317
2318 invariant()
2319 {
2320 assert(_length == 1);
2321 }
2322
2323 int _length = 1;
2324 }
2325
2326
2327 void test62()
2328 {
2329 immutable result = S62.init - S62.init;
2330 }
2331
2332 /**********************************/
2333 // https://issues.dlang.org/show_bug.cgi?id=7579
2334
2335 void test7579a()
2336 {
2337 static struct S
2338 {
2339 // postblit can also have no body because isn't called
2340 @disable this(this) { assert(0); }
2341 }
2342
2343 @property S fs() { return S(); }
2344 @property S[3] fsa() { return [S(), S(), S()]; }
2345
2346 S s0;
2347 S s1 = S();
2348 static assert(!__traits(compiles, { S s2 = s1; })); // OK
2349 S s2 = fs;
2350 static assert(!__traits(compiles, { s2 = s1; })); // OK
2351
2352 // static array
2353 S[3] sa0;
2354 S[3] sa1 = [S(), S(), S()];
2355 static assert(!__traits(compiles, { S[3] sa2 = sa1; })); // fixed
2356 S[3] sa2 = fsa;
2357 static assert(!__traits(compiles, { sa2 = sa1; })); // fixed
2358 sa2 = [S(), S(), S()];
2359 sa2 = fsa;
2360
2361 S[] da1 = new S[3];
2362 S[] da2 = new S[3];
2363 static assert(!__traits(compiles, { da2[] = da1[]; })); // fixed
2364
2365 // concatenation and appending
2366 static assert(!__traits(compiles, { da1 ~= s1; })); // fixed
2367 static assert(!__traits(compiles, { da1 ~= S(); }));
2368 static assert(!__traits(compiles, { da1 ~= fsa; }));
2369 static assert(!__traits(compiles, { da1 ~= fsa[]; }));
2370 static assert(!__traits(compiles, { da1 = da1 ~ s1; })); // fixed
2371 static assert(!__traits(compiles, { da1 = s1 ~ da1; })); // fixed
2372 static assert(!__traits(compiles, { da1 = da1 ~ S(); }));
2373 static assert(!__traits(compiles, { da1 = da1 ~ fsa; }));
2374 static assert(!__traits(compiles, { da1 = da1 ~ da; }));
2375 }
2376
2377 void test7579b()
2378 {
2379 static struct S
2380 {
2381 // postblit asserts in runtime
2382 this(this) { assert(0); }
2383 }
2384
2385 @property S fs() { return S(); }
2386 @property S[3] fsa() { return [S(), S(), S()]; }
2387
2388 S s0;
2389 S s1 = S();
2390 S s2 = fs;
2391
2392 // static array
2393 S[3] sa0;
2394 S[3] sa1 = [S(), S(), S()];
2395 S[3] sa2 = fsa;
2396 sa2 = [S(), S(), S()];
2397 sa2 = fsa;
2398
2399 S[] da1 = new S[3];
2400 S[] da2 = new S[3];
2401
2402 // concatenation and appending always run postblits
2403 }
2404
2405 /**********************************/
2406 // https://issues.dlang.org/show_bug.cgi?id=8335
2407
2408 struct S8335
2409 {
2410 static int postblit;
2411
2412 int i;
2413 this(this) { ++postblit; }
2414 }
2415
2416 void f8335(ref S8335[3] arr)
2417 {
2418 arr[0].i = 7;
2419 }
2420
2421 void g8335(lazy S8335[3] arr)
2422 {
2423 assert(S8335.postblit == 0);
2424 auto x = arr;
2425 assert(S8335.postblit == 3);
2426 }
2427
2428 void h8335(lazy S8335 s)
2429 {
2430 assert(S8335.postblit == 0);
2431 auto x = s;
2432 assert(S8335.postblit == 1);
2433 }
2434
2435 void test8335()
2436 {
2437 S8335[3] arr;
2438 f8335(arr);
2439 assert(S8335.postblit == 0);
2440 assert(arr[0].i == 7);
2441
2442 g8335(arr);
2443 assert(S8335.postblit == 3);
2444
2445 S8335.postblit = 0;
2446 S8335 s;
2447 h8335(s);
2448 assert(S8335.postblit == 1);
2449 }
2450
2451 /**********************************/
2452 // https://issues.dlang.org/show_bug.cgi?id=8356
2453
2454 void test8356()
2455 {
2456 static struct S
2457 {
2458 @disable this(this) { assert(0); }
2459 }
2460
2461 S s;
2462 S[3] sa;
2463
2464 static assert(!__traits(compiles, {
2465 S fs() { return s; }
2466 }));
2467
2468 static assert(!__traits(compiles, {
2469 S[3] fsa() { return sa; }
2470 }));
2471 }
2472
2473 /**********************************/
2474 // https://issues.dlang.org/show_bug.cgi?id=8475
2475
2476 T func8475(T)(T x) @safe pure
2477 {
2478 return T();
2479 }
2480
2481 template X8475(bool something)
2482 {
2483 struct XY
2484 {
2485 this(this) @safe pure {}
2486 void func(XY x) @safe pure
2487 {
2488 XY y = x; //Error: see below
2489 func8475(x);
2490 func8475(y);
2491 }
2492 }
2493 }
2494
2495 alias X8475!(true).XY Xtrue;
2496
2497 /**********************************/
2498
2499 struct Foo9320 {
2500 real x;
2501
2502 this(real x) {
2503 this.x = x;
2504 }
2505
2506 Foo9320 opBinary(string op)(Foo9320 other) {
2507 return Foo9320(mixin("x" ~ op ~ "other.x"));
2508 }
2509 }
2510
2511 Foo9320 test9320(Foo9320 a, Foo9320 b, Foo9320 c) {
2512 return (a + b) / (a * b) - c;
2513 }
2514
2515 /**********************************/
2516 // https://issues.dlang.org/show_bug.cgi?id=9386
2517
2518 struct Test9386
2519 {
2520 string name;
2521 static char[25] op;
2522 static size_t i;
2523
2524 static @property string sop() { return cast(string)op[0..i]; }
2525
2526 this(string name)
2527 {
2528 this.name = name;
2529 printf("Created %.*s...\n", cast(int)name.length, name.ptr);
2530 assert(i + 1 < op.length);
2531 op[i++] = 'a';
2532 }
2533
2534 this(this)
2535 {
2536 printf("Copied %.*s...\n", cast(int)name.length, name.ptr);
2537 assert(i + 1 < op.length);
2538 op[i++] = 'b';
2539 }
2540
2541 ~this()
2542 {
2543 printf("Deleted %.*s\n", cast(int)name.length, name.ptr);
2544 assert(i + 1 < op.length);
2545 op[i++] = 'c';
2546 }
2547
2548 const int opCmp(ref const Test9386 t)
2549 {
2550 return op[0] - t.op[0];
2551 }
2552 }
2553
2554 void test9386()
2555 {
2556 {
2557 Test9386.op[] = 0;
2558 Test9386.i = 0;
2559
2560 Test9386[] tests =
2561 [ Test9386("one"),
2562 Test9386("two"),
2563 Test9386("three"),
2564 Test9386("four") ];
2565
2566 assert(Test9386.sop == "aaaa");
2567 Test9386.op[] = 0;
2568 Test9386.i = 0;
2569
2570 printf("----\n");
2571 foreach (Test9386 test; tests)
2572 {
2573 printf("\tForeach %.*s\n", cast(int)test.name.length, test.name.ptr);
2574 Test9386.op[Test9386.i++] = 'x';
2575 }
2576
2577 assert(Test9386.sop == "bxcbxcbxcbxc");
2578 Test9386.op[] = 0;
2579 Test9386.i = 0;
2580
2581 printf("----\n");
2582 foreach (ref Test9386 test; tests)
2583 {
2584 printf("\tForeach %.*s\n", cast(int)test.name.length, test.name.ptr);
2585 Test9386.op[Test9386.i++] = 'x';
2586 }
2587 assert(Test9386.sop == "xxxx");
2588 }
2589 printf("====\n");
2590 {
2591 Test9386.op[] = 0;
2592 Test9386.i = 0;
2593
2594 Test9386[Test9386] tests =
2595 [ Test9386("1") : Test9386("one"),
2596 Test9386("2") : Test9386("two"),
2597 Test9386("3") : Test9386("three"),
2598 Test9386("4") : Test9386("four") ];
2599
2600 Test9386.op[] = 0;
2601 Test9386.i = 0;
2602
2603 printf("----\n");
2604 foreach (Test9386 k, Test9386 v; tests)
2605 {
2606 printf("\tForeach %.*s : %.*s\n", cast(int)k.name.length, k.name.ptr,
2607 cast(int)v.name.length, v.name.ptr);
2608 Test9386.op[Test9386.i++] = 'x';
2609 }
2610
2611 assert(Test9386.sop == "bbxccbbxccbbxccbbxcc");
2612 Test9386.op[] = 0;
2613 Test9386.i = 0;
2614
2615 printf("----\n");
2616 foreach (Test9386 k, ref Test9386 v; tests)
2617 {
2618 printf("\tForeach %.*s : %.*s\n", cast(int)k.name.length, k.name.ptr,
2619 cast(int)v.name.length, v.name.ptr);
2620 Test9386.op[Test9386.i++] = 'x';
2621 }
2622 assert(Test9386.sop == "bxcbxcbxcbxc");
2623 Test9386.op[] = 0;
2624 Test9386.i = 0;
2625 }
2626 }
2627
2628 /**********************************/
2629 // https://issues.dlang.org/show_bug.cgi?id=9441
2630
2631 auto x9441 = X9441(0.123);
2632
2633 struct X9441
2634 {
2635 int a;
2636 this(double x) { a = cast(int)(x * 100); }
2637 }
2638
2639 void test9441()
2640 {
2641 assert(x9441.a == 12);
2642 }
2643
2644 /**********************************/
2645
2646 struct Payload
2647 {
2648 size_t _capacity; //Comment me
2649 int[] _pay; //Comment me
2650
2651 size_t insertBack(Data d)
2652 {
2653 immutable newLen = _pay.length + 3;
2654 _pay.length = newLen;
2655 _pay = _pay[0 .. newLen]; //Comment me
2656 return 3;
2657 }
2658 }
2659
2660 struct Impl
2661 {
2662 Payload _payload;
2663 size_t _count;
2664 }
2665
2666 struct Data
2667 {
2668 Impl* _store;
2669
2670 this(int i)
2671 {
2672 _store = new Impl;
2673 _store._payload = Payload.init;
2674 }
2675
2676 ~this()
2677 {
2678 printf("%zd\n", _store._count);
2679 --_store._count;
2680 }
2681 }
2682
2683
2684 void test9720()
2685 {
2686 auto a = Data(1);
2687 auto b = Data(1);
2688 a._store._payload.insertBack(b); //Fails
2689 }
2690
2691 /**********************************/
2692 // https://issues.dlang.org/show_bug.cgi?id=9899
2693
2694 struct S9899
2695 {
2696 @safe pure nothrow ~this() {}
2697 }
2698
2699 struct MemberS9899
2700 {
2701 S9899 s;
2702 }
2703
2704 void test9899() @safe pure nothrow
2705 {
2706 MemberS9899 s; // 11
2707 }
2708
2709 /**********************************/
2710 // https://issues.dlang.org/show_bug.cgi?id=9907
2711
2712 void test9907()
2713 {
2714 static struct SX(bool hasCtor, bool hasDtor)
2715 {
2716 int i;
2717 static size_t assign;
2718 static size_t dtor;
2719
2720 static if (hasCtor)
2721 {
2722 this(int i) { this.i = i; }
2723 }
2724
2725 void opAssign(SX rhs)
2726 {
2727 printf("%08zX(%d) from Rvalue %08zX(%d)\n", cast(size_t)&this.i, this.i, cast(size_t)&rhs.i, rhs.i);
2728 ++assign;
2729 }
2730
2731 void opAssign(ref SX rhs)
2732 {
2733 printf("%08zX(%d) from Lvalue %08zX(%d)\n", cast(size_t)&this.i, this.i, cast(size_t)&rhs.i, rhs.i);
2734 assert(0);
2735 }
2736
2737 static if (hasDtor)
2738 {
2739 ~this()
2740 {
2741 printf("destroying %08zX(%d)\n", cast(size_t)&this.i, this.i);
2742 ++dtor;
2743 }
2744 }
2745 }
2746
2747 S test(S)(int i)
2748 {
2749 return S(i);
2750 }
2751
2752 foreach (hasCtor; TypeTuple!(false, true))
2753 foreach (hasDtor; TypeTuple!(false, true))
2754 {
2755 alias S = SX!(hasCtor, hasDtor);
2756 alias test!S foo;
2757
2758 printf("----\n");
2759 auto s = S(1);
2760
2761 // Assignment from two kinds of rvalues
2762 assert(S.assign == 0);
2763 s = foo(2);
2764 static if (hasDtor) assert(S.dtor == 1);
2765 assert(S.assign == 1);
2766 s = S(3);
2767 assert(S.assign == 2);
2768 static if (hasDtor) assert(S.dtor == 2);
2769 }
2770 printf("----\n");
2771 }
2772
2773 /**********************************/
2774 // https://issues.dlang.org/show_bug.cgi?id=9985
2775
2776 struct S9985
2777 {
2778 ubyte* b;
2779 ubyte[128] buf;
2780 this(this) { assert(0); }
2781
2782 static void* ptr;
2783 }
2784 auto ref makeS9985() @system
2785 {
2786 S9985 s;
2787 s.b = s.buf.ptr;
2788 S9985.ptr = &s;
2789 return s;
2790 }
2791 void test9985()
2792 {
2793 S9985 s = makeS9985();
2794 assert(S9985.ptr == &s); // NRVO
2795
2796 static const int n = 1;
2797 static auto ref retN()
2798 {
2799 return n;
2800 }
2801 auto p = &(retN()); // OK
2802 assert(p == &n);
2803 alias pure nothrow @nogc @safe ref const(int) F1();
2804 static assert(is(typeof(retN) == F1));
2805
2806 enum const(int) x = 1;
2807 static auto ref retX()
2808 {
2809 return x;
2810 }
2811 static assert(!__traits(compiles, { auto q = &(retX()); }));
2812 alias pure nothrow @nogc @safe const(int) F2();
2813 static assert(is(typeof(retX) == F2));
2814 }
2815
2816 /**********************************/
2817
2818 // https://issues.dlang.org/show_bug.cgi?id=17457
2819
2820 void delegate() dg17457;
2821
2822 struct S17457 {
2823 ulong[10] data;
2824
2825 this(int seconds) {
2826 dg17457 = &mfunc;
2827 }
2828 @disable this(this);
2829 void mfunc() {}
2830 }
2831
2832 auto foo17457() {
2833 pragma(inline, false);
2834 return S17457(18);
2835 }
2836
2837 void test17457()
2838 {
2839 auto x = foo17457();
2840 //printf("%p vs %p\n", &x, dg17457.ptr);
2841 assert(&x == dg17457.ptr);
2842 }
2843
2844 /**********************************/
2845 // https://issues.dlang.org/show_bug.cgi?id=9994
2846
2847 void test9994()
2848 {
2849 static struct S
2850 {
2851 static int dtor;
2852 ~this() { ++dtor; }
2853 }
2854
2855 S s;
2856 static assert( __traits(compiles, s.opAssign(s)));
2857 static assert(!__traits(compiles, s.__postblit()));
2858
2859 assert(S.dtor == 0);
2860 s = s;
2861 assert(S.dtor == 1);
2862 }
2863
2864 /**********************************/
2865 // https://issues.dlang.org/show_bug.cgi?id=10053
2866
2867 struct S10053A
2868 {
2869 pure ~this() {}
2870 }
2871
2872 struct S10053B
2873 {
2874 S10053A sa;
2875 ~this() {}
2876 }
2877
2878 /**********************************/
2879 // https://issues.dlang.org/show_bug.cgi?id=10055
2880
2881 void test10055a()
2882 {
2883 static struct SX { pure nothrow @safe ~this() {} }
2884 static struct SY { pure nothrow @safe ~this() {} }
2885 static struct SZ { @disable ~this() {} }
2886
2887 // function to check merge result of the dtor attributes
2888 static void check(S)() { S s; }
2889
2890 static struct S1 { }
2891 static struct S2 { ~this() {} }
2892 static struct SA { SX sx; SY sy; }
2893 static struct SB { SX sx; SY sy; pure nothrow @safe ~this() {} }
2894 static struct SC { SX sx; SY sy; nothrow @safe ~this() {} }
2895 static struct SD { SX sx; SY sy; pure @safe ~this() {} }
2896 static struct SE { SX sx; SY sy; pure nothrow ~this() {} }
2897 static struct SF { SX sx; SY sy; @safe ~this() {} }
2898 static struct SG { SX sx; SY sy; nothrow ~this() {} }
2899 static struct SH { SX sx; SY sy; pure ~this() {} }
2900 static struct SI { SX sx; SY sy; ~this() {} }
2901 static assert(is( typeof(&check!S1) == void function() pure nothrow @nogc @safe ));
2902 static assert(is( typeof(&check!S2) == void function() ));
2903 static assert(is( typeof(&check!SA) == void function() pure nothrow @safe ));
2904 static assert(is( typeof(&check!SB) == void function() pure nothrow @safe ));
2905 static assert(is( typeof(&check!SC) == void function() nothrow @safe ));
2906 static assert(is( typeof(&check!SD) == void function() pure @safe ));
2907 static assert(is( typeof(&check!SE) == void function() pure nothrow ));
2908 static assert(is( typeof(&check!SF) == void function() @safe ));
2909 static assert(is( typeof(&check!SG) == void function() nothrow ));
2910 static assert(is( typeof(&check!SH) == void function() pure ));
2911 static assert(is( typeof(&check!SI) == void function() ));
2912
2913 static struct S1x { SZ sz; }
2914 static struct S2x { ~this() {} SZ sz; }
2915 static struct SAx { SX sx; SY sy; SZ sz; }
2916 static struct SBx { SX sx; SY sy; pure nothrow @safe ~this() {} SZ sz; }
2917 static struct SCx { SX sx; SY sy; nothrow @safe ~this() {} SZ sz; }
2918 static struct SDx { SX sx; SY sy; pure @safe ~this() {} SZ sz; }
2919 static struct SEx { SX sx; SY sy; pure nothrow ~this() {} SZ sz; }
2920 static struct SFx { SX sx; SY sy; @safe ~this() {} SZ sz; }
2921 static struct SGx { SX sx; SY sy; nothrow ~this() {} SZ sz; }
2922 static struct SHx { SX sx; SY sy; pure ~this() {} SZ sz; }
2923 static struct SIx { SX sx; SY sy; ~this() {} SZ sz; }
2924 foreach (Sx; TypeTuple!(S1x, S2x, SAx, SBx, SCx, SDx, SEx, SFx, SGx, SHx, SIx))
2925 {
2926 static assert(!__traits(compiles, &check!Sx));
2927 }
2928 }
2929
2930 void test10055b()
2931 {
2932 static struct SX { pure nothrow @safe this(this) {} }
2933 static struct SY { pure nothrow @safe this(this) {} }
2934 static struct SZ { @disable this(this) {} }
2935
2936 // function to check merge result of the postblit attributes
2937 static void check(S)() { S s; S s2 = s; }
2938
2939 static struct S1 { }
2940 static struct S2 { this(this) {} }
2941 static struct SA { SX sx; SY sy; }
2942 static struct SB { SX sx; SY sy; pure nothrow @safe this(this) {} }
2943 static struct SC { SX sx; SY sy; nothrow @safe this(this) {} }
2944 static struct SD { SX sx; SY sy; pure @safe this(this) {} }
2945 static struct SE { SX sx; SY sy; pure nothrow this(this) {} }
2946 static struct SF { SX sx; SY sy; @safe this(this) {} }
2947 static struct SG { SX sx; SY sy; nothrow this(this) {} }
2948 static struct SH { SX sx; SY sy; pure this(this) {} }
2949 static struct SI { SX sx; SY sy; this(this) {} }
2950 static assert(is( typeof(&check!S1) == void function() pure nothrow @nogc @safe ));
2951 static assert(is( typeof(&check!S2) == void function() ));
2952 static assert(is( typeof(&check!SA) == void function() pure nothrow @safe ));
2953 static assert(is( typeof(&check!SB) == void function() pure nothrow @safe ));
2954 static assert(is( typeof(&check!SC) == void function() nothrow @safe ));
2955 static assert(is( typeof(&check!SD) == void function() pure @safe ));
2956 static assert(is( typeof(&check!SE) == void function() pure nothrow ));
2957 static assert(is( typeof(&check!SF) == void function() @safe ));
2958 static assert(is( typeof(&check!SG) == void function() nothrow ));
2959 static assert(is( typeof(&check!SH) == void function() pure ));
2960 static assert(is( typeof(&check!SI) == void function() ));
2961
2962 static struct S1x { SZ sz; }
2963 static struct S2x { this(this) {} SZ sz; }
2964 static struct SAx { SX sx; SY sy; SZ sz; }
2965 static struct SBx { SX sx; SY sy; pure nothrow @safe this(this) {} SZ sz; }
2966 static struct SCx { SX sx; SY sy; nothrow @safe this(this) {} SZ sz; }
2967 static struct SDx { SX sx; SY sy; pure @safe this(this) {} SZ sz; }
2968 static struct SEx { SX sx; SY sy; pure nothrow this(this) {} SZ sz; }
2969 static struct SFx { SX sx; SY sy; @safe this(this) {} SZ sz; }
2970 static struct SGx { SX sx; SY sy; nothrow this(this) {} SZ sz; }
2971 static struct SHx { SX sx; SY sy; pure this(this) {} SZ sz; }
2972 static struct SIx { SX sx; SY sy; this(this) {} SZ sz; }
2973 foreach (Sx; TypeTuple!(S1x, S2x, SAx, SBx, SCx, SDx, SEx, SFx, SGx, SHx, SIx))
2974 {
2975 static assert(!__traits(compiles, &check!Sx));
2976 }
2977 }
2978
2979 /**********************************/
2980 // https://issues.dlang.org/show_bug.cgi?id=10160
2981
2982 struct S10160 { this(this) {} }
2983
2984 struct X10160a { S10160 s; const int x; }
2985 struct X10160b { S10160 s; enum int x = 1; }
2986
2987 void test10160()
2988 {
2989 X10160a xa;
2990 X10160b xb;
2991 }
2992
2993 /**********************************/
2994 // https://issues.dlang.org/show_bug.cgi?id=10094
2995
2996 void test10094()
2997 {
2998 static void* p;
2999 const string[4] i2s = ()
3000 {
3001 string[4] tmp;
3002 p = &tmp[0];
3003 for (int i = 0; i < 4; ++i)
3004 {
3005 char[1] buf = [cast(char)('0' + i)];
3006 string str = buf.idup;
3007 tmp[i] = str;
3008 }
3009 return tmp; // NRVO should work
3010 }();
3011 assert(p == cast(void*)&i2s[0]);
3012 assert(i2s == ["0", "1", "2", "3"]);
3013 }
3014
3015 /**********************************/
3016 // https://issues.dlang.org/show_bug.cgi?id=10079
3017
3018 // dtor || postblit
3019 struct S10079a
3020 {
3021 this(this) pure nothrow @safe {}
3022 }
3023 struct S10079b
3024 {
3025 ~this() pure nothrow @safe {}
3026 }
3027 struct S10079c
3028 {
3029 this(this) pure nothrow @safe {}
3030 ~this() pure nothrow @safe {}
3031 }
3032 struct S10079d
3033 {
3034 this(this) {}
3035 }
3036 struct S10079e
3037 {
3038 this(this) {}
3039 ~this() pure nothrow @safe {}
3040 }
3041
3042 // memberwise
3043 struct S10079f
3044 {
3045 S10079a a;
3046 S10079b b;
3047 S10079c c;
3048 S10079d d;
3049 S10079e e;
3050 }
3051
3052 void check10079(S)(ref S s) pure nothrow @safe { s = S(); }
3053
3054 // Assignment is pure, nothrow, and @safe in all cases.
3055 static assert(__traits(compiles, &check10079!S10079a));
3056 static assert(__traits(compiles, &check10079!S10079b));
3057 static assert(__traits(compiles, &check10079!S10079c));
3058 static assert(__traits(compiles, &check10079!S10079d));
3059 static assert(__traits(compiles, &check10079!S10079e));
3060 static assert(__traits(compiles, &check10079!S10079f));
3061
3062 /**********************************/
3063 // https://issues.dlang.org/show_bug.cgi?id=10244
3064
3065 void test10244()
3066 {
3067 static struct Foo
3068 {
3069 string _str;
3070 long _num;
3071
3072 template DeclareConstructor(string fieldName)
3073 {
3074 enum code =
3075 `this(typeof(_` ~ fieldName ~ `) value)` ~
3076 `{ this._` ~ fieldName ~ ` = value; }`;
3077 mixin(code);
3078 }
3079
3080 mixin DeclareConstructor!"str";
3081 mixin DeclareConstructor!"num";
3082 }
3083
3084 Foo value1 = Foo("D");
3085 Foo value2 = Foo(128);
3086 assert(value1._str == "D");
3087 assert(value2._num == 128);
3088 }
3089
3090 /**********************************/
3091 // https://issues.dlang.org/show_bug.cgi?id=10694
3092
3093 struct Foo10694 { ~this() { } }
3094
3095 void test10694() pure
3096 {
3097 static Foo10694 i1;
3098 __gshared Foo10694 i2;
3099 void foo() pure
3100 {
3101 static Foo10694 j1;
3102 __gshared Foo10694 j2;
3103 }
3104 }
3105
3106 /**********************************/
3107 // https://issues.dlang.org/show_bug.cgi?id=10787
3108
3109 int global10787;
3110
3111 static ~this() nothrow pure @safe
3112 {
3113 int* p;
3114 static assert(!__traits(compiles, ++p));
3115 static assert(!__traits(compiles, ++global10787));
3116 }
3117
3118 shared static ~this() nothrow pure @safe
3119 {
3120 int* p;
3121 static assert(!__traits(compiles, ++p));
3122 static assert(!__traits(compiles, ++global10787));
3123 }
3124
3125 /**********************************/
3126 // https://issues.dlang.org/show_bug.cgi?id=10789
3127
3128 struct S10789
3129 {
3130 static int count;
3131 int value;
3132
3133 this(int) { value = ++count; }
3134 ~this() { --count; }
3135 this(this) { value = ++count; assert(value == 3); }
3136 }
3137
3138 S10789 fun10789a(bool isCondExp)(bool cond)
3139 {
3140 S10789 s1 = S10789(42), s2 = S10789(24);
3141 assert(S10789.count == 2);
3142 static if (isCondExp)
3143 {
3144 return cond ? s1 : s2;
3145 }
3146 else
3147 {
3148 if (cond)
3149 return s1;
3150 else
3151 return s2;
3152 }
3153 }
3154
3155 auto fun10789b(bool isCondExp)(bool cond)
3156 {
3157 S10789 s1 = S10789(42), s2 = S10789(24);
3158 assert(S10789.count == 2);
3159 static if (isCondExp)
3160 {
3161 return cond ? s1 : s2;
3162 }
3163 else
3164 {
3165 if (cond)
3166 return s1;
3167 else
3168 return s2;
3169 }
3170 }
3171
3172 void test10789()
3173 {
3174 foreach (fun; TypeTuple!(fun10789a, fun10789b))
3175 foreach (isCondExp; TypeTuple!(false, true))
3176 {
3177 {
3178 S10789 s = fun!isCondExp(true);
3179 assert(S10789.count == 1);
3180 assert(s.value == 3);
3181 }
3182 assert(S10789.count == 0);
3183 {
3184 S10789 s = fun!isCondExp(false);
3185 assert(S10789.count == 1);
3186 assert(s.value == 3);
3187 }
3188 assert(S10789.count == 0);
3189 }
3190 }
3191
3192 /**********************************/
3193 // https://issues.dlang.org/show_bug.cgi?id=10972
3194
3195 int test10972()
3196 {
3197 string result;
3198
3199 struct A
3200 {
3201 this(this) { result ~= "pA"; version(none) printf("copied A\n"); }
3202 ~this() { result ~= "dA"; version(none) printf("destroy A\n"); }
3203 }
3204 struct B
3205 {
3206 this(this)
3207 {
3208 result ~= "(pB)"; version(none) printf("B says what?\n");
3209 throw new Exception("BOOM!");
3210 }
3211 ~this() { result ~= "dB"; version(none) printf("destroy B\n"); }
3212 }
3213 struct S
3214 {
3215 A a;
3216 B b;
3217 }
3218
3219 result = "{";
3220 {
3221 S s1;
3222 result ~= "[";
3223 try
3224 {
3225 S s3 = s1;
3226 assert(0);
3227 }
3228 catch (Exception e)
3229 {}
3230 result ~= "]";
3231 }
3232 result ~= "}";
3233 assert(result == "{[pA(pB)dA]dBdA}", result);
3234
3235 result = "{";
3236 {
3237 S s1;
3238 S s2;
3239 result ~= "[";
3240 try
3241 {
3242 s2 = s1;
3243 assert(0);
3244 }
3245 catch (Exception e)
3246 {}
3247 result ~= "]";
3248 }
3249 result ~= "}";
3250 assert(result == "{[pA(pB)dA]dBdAdBdA}", result);
3251
3252 return 1;
3253 }
3254 static assert(test10972()); // CTFE
3255
3256 /**********************************/
3257 // https://issues.dlang.org/show_bug.cgi?id=11134
3258
3259 void test11134()
3260 {
3261 void test(S)()
3262 {
3263 S s;
3264 S[2] sa;
3265 S[2][] dsa = [[S(), S()]];
3266 dsa.reserve(dsa.length + 2); // avoid postblit calls by GC
3267
3268 S.count = 0;
3269 dsa ~= sa;
3270 assert(S.count == 2);
3271
3272 S.count = 0;
3273 dsa ~= [s, s];
3274 assert(S.count == 2);
3275 }
3276
3277 static struct SS
3278 {
3279 static int count;
3280 this(this) { ++count; }
3281 }
3282 test!SS();
3283
3284 struct NS
3285 {
3286 static int count;
3287 this(this) { ++count; }
3288 }
3289 test!NS();
3290 }
3291
3292 /**********************************/
3293 // https://issues.dlang.org/show_bug.cgi?id=11197
3294
3295 struct S11197a
3296 {
3297 this(bool) {}
3298 this(this) {}
3299 }
3300
3301 struct S11197b
3302 {
3303 //this(bool) {}
3304 this(this) {}
3305 }
3306
3307 void test11197()
3308 {
3309 S11197a[][string] aa1;
3310 aa1["test"] ~= S11197a.init;
3311
3312 S11197b[][string] aa2;
3313 aa2["test"] ~= S11197b.init;
3314 }
3315
3316 /**********************************/
3317
3318 struct S7474 {
3319 float x;
3320 ~this() {}
3321 }
3322
3323 void fun7474(T...)() { T x; }
3324 void test7474() { fun7474!S7474(); }
3325
3326 /**********************************/
3327 // https://issues.dlang.org/show_bug.cgi?id=11286
3328
3329 struct A11286
3330 {
3331 ~this() {}
3332 }
3333
3334 A11286 getA11286() pure nothrow
3335 {
3336 return A11286();
3337 }
3338
3339 void test11286()
3340 {
3341 A11286 a = getA11286();
3342 }
3343
3344 /**********************************/
3345 // https://issues.dlang.org/show_bug.cgi?id=11505
3346
3347 struct Foo11505
3348 {
3349 Bar11505 b;
3350 }
3351
3352 struct Bar11505
3353 {
3354 ~this() @safe { }
3355 void* p;
3356 }
3357
3358 void test11505()
3359 {
3360 Foo11505 f;
3361 f = Foo11505();
3362 }
3363
3364 /**********************************/
3365 // https://issues.dlang.org/show_bug.cgi?id=12045
3366
3367 bool test12045()
3368 {
3369 string dtor;
3370 void* ptr;
3371
3372 struct S12045
3373 {
3374 string val;
3375
3376 this(this) { assert(0); }
3377 ~this() { dtor ~= val; }
3378 }
3379
3380 auto makeS12045(bool thrown)
3381 {
3382 auto s1 = S12045("1");
3383 auto s2 = S12045("2");
3384 ptr = &s1;
3385
3386 if (thrown)
3387 throw new Exception("");
3388
3389 return s1; // NRVO
3390 }
3391
3392 dtor = null, ptr = null;
3393 try
3394 {
3395 S12045 s = makeS12045(true);
3396 assert(0);
3397 }
3398 catch (Exception e)
3399 {
3400 assert(dtor == "21", dtor);
3401 }
3402
3403 dtor = null, ptr = null;
3404 {
3405 S12045 s = makeS12045(false);
3406 assert(dtor == "2");
3407 if (!__ctfe) assert(ptr is &s); // NRVO
3408 }
3409 assert(dtor == "21");
3410
3411 return true;
3412 }
3413 static assert(test12045());
3414
3415 /**********************************/
3416 // https://issues.dlang.org/show_bug.cgi?id=12591
3417
3418 struct S12591(T)
3419 {
3420 this(this)
3421 {}
3422 }
3423
3424 struct Tuple12591(Types...)
3425 {
3426 Types expand;
3427 this(Types values)
3428 {
3429 expand[] = values[];
3430 }
3431 }
3432
3433 void test12591()
3434 {
3435 alias T1 = Tuple12591!(S12591!int);
3436 }
3437
3438 /**********************************/
3439 // https://issues.dlang.org/show_bug.cgi?id=12660
3440
3441 struct X12660
3442 {
3443 this(this) @nogc {}
3444 ~this() @nogc {}
3445 void opAssign(X12660) @nogc {}
3446 @nogc invariant() {}
3447 }
3448 struct Y12660
3449 {
3450 X12660 x;
3451
3452 this(this) @nogc {}
3453 ~this() @nogc {}
3454 @nogc invariant() {}
3455 }
3456 struct Z12660
3457 {
3458 Y12660 y;
3459 }
3460
3461 class C12660
3462 {
3463 this() @nogc {}
3464 @nogc invariant() {}
3465 }
3466
3467 void test12660() @nogc
3468 {
3469 X12660 x;
3470 x = x;
3471
3472 Y12660 y = { x };
3473 y = y;
3474
3475 Z12660 z = { y };
3476 z = z;
3477 }
3478
3479 /**********************************/
3480 // https://issues.dlang.org/show_bug.cgi?id=12686
3481
3482 struct Foo12686
3483 {
3484 static int count;
3485
3486 invariant() { ++count; }
3487
3488 @disable this(this);
3489
3490 Foo12686 bar()
3491 {
3492 Foo12686 f;
3493 return f;
3494 }
3495 }
3496
3497 void test12686()
3498 {
3499 Foo12686 f;
3500 Foo12686 f2 = f.bar();
3501 version (unittest)
3502 { }
3503 else
3504 assert(Foo12686.count == 2);
3505 }
3506
3507 /**********************************/
3508 // https://issues.dlang.org/show_bug.cgi?id=13089
3509
3510 struct S13089
3511 {
3512 @disable this(this); // non nothrow
3513 int val;
3514 }
3515
3516 void* p13089;
3517
3518 S13089[1000] foo13089() nothrow
3519 {
3520 typeof(return) data;
3521 p13089 = &data;
3522 return data;
3523 }
3524
3525 void test13089() nothrow
3526 {
3527 immutable data = foo13089();
3528 assert(p13089 == &data);
3529 }
3530
3531 /**********************************/
3532
3533 struct NoDtortest11763 {}
3534
3535 struct HasDtortest11763
3536 {
3537 NoDtortest11763 func()
3538 {
3539 return NoDtortest11763();
3540 }
3541 ~this() {}
3542 }
3543
3544 void test11763()
3545 {
3546 HasDtortest11763().func();
3547 }
3548
3549 /**********************************/
3550
3551 struct Buf { }
3552
3553 struct Variant
3554 {
3555 ~this() { }
3556
3557 Buf get() { Buf b; return b; }
3558 }
3559
3560 Variant value() { Variant v; return v; }
3561
3562 void test13303()
3563 {
3564 value.get();
3565 }
3566
3567 /**********************************/
3568
3569 struct S13673
3570 {
3571 string _name;
3572 ~this() {}
3573 }
3574
3575 string name13673;
3576
3577 void test13673()
3578 {
3579 S13673(name13673);
3580 S13673(name13673);
3581 }
3582
3583 /**********************************/
3584
3585 void test13586()
3586 {
3587 static struct S {
3588 __gshared int count;
3589 ~this() { ++count; printf("~S\n"); }
3590 }
3591
3592 static struct T {
3593 __gshared int count;
3594 ~this() { ++count; printf("~T\n"); }
3595 }
3596
3597 static int foo(bool flag)
3598 {
3599 if (flag)
3600 throw new Exception("hello");
3601 return 1;
3602 }
3603
3604 static void func(S s, int f, T t)
3605 {
3606 printf("func()\n");
3607 }
3608
3609 static class C
3610 {
3611 this(S s, int f, T t)
3612 {
3613 printf("C()\n");
3614 }
3615 }
3616
3617 {
3618 bool threw = false;
3619 try
3620 {
3621 func(S(), foo(true), T());
3622 printf("not reach\n");
3623 }
3624 catch (Exception e)
3625 {
3626 threw = true;
3627 }
3628 printf("threw %d S %d T %d\n", threw, S.count, T.count);
3629 assert(threw && S.count == 1 && T.count == 0);
3630 S.count = 0;
3631 T.count = 0;
3632 }
3633 {
3634 bool threw = false;
3635 try
3636 {
3637 func(S(), foo(false), T());
3638 printf("reached\n");
3639 }
3640 catch (Exception e)
3641 {
3642 threw = true;
3643 }
3644 printf("threw %d S %d T %d\n", threw, S.count, T.count);
3645 assert(!threw && S.count == 1 && T.count == 1);
3646 S.count = 0;
3647 T.count = 0;
3648 }
3649 {
3650 bool threw = false;
3651 try
3652 {
3653 new C(S(), foo(true), T());
3654 printf("not reach\n");
3655 }
3656 catch (Exception e)
3657 {
3658 threw = true;
3659 }
3660 printf("threw %d S %d T %d\n", threw, S.count, T.count);
3661 assert(threw && S.count == 1 && T.count == 0);
3662 S.count = 0;
3663 T.count = 0;
3664 }
3665 }
3666
3667 /**********************************/
3668 // https://issues.dlang.org/show_bug.cgi?id=14443
3669
3670 T enforce14443(E : Throwable = Exception, T)(T value)
3671 {
3672 if (!value)
3673 throw new E("Enforcement failed");
3674 return value;
3675 }
3676
3677 struct RefCounted14443(T)
3678 if (!is(T == class) && !(is(T == interface)))
3679 {
3680 struct RefCountedStore
3681 {
3682 private struct Impl
3683 {
3684 T _payload;
3685 size_t _count;
3686 }
3687
3688 private Impl* _store;
3689
3690 private void initialize(A...)(auto ref A args)
3691 {
3692 import core.stdc.stdlib : malloc;
3693
3694 // enforce is necessary
3695 _store = cast(Impl*) enforce14443(malloc(Impl.sizeof));
3696
3697 // emulate 'emplace'
3698 static if (args.length > 0)
3699 _store._payload.tupleof = args;
3700 else
3701 _store._payload = T.init;
3702
3703 _store._count = 1;
3704 }
3705
3706 @property bool isInitialized() const nothrow @safe
3707 {
3708 return _store !is null;
3709 }
3710
3711 void ensureInitialized()
3712 {
3713 if (!isInitialized) initialize();
3714 }
3715
3716 }
3717 RefCountedStore _refCounted;
3718
3719 this(A...)(auto ref A args) if (A.length > 0)
3720 {
3721 _refCounted.initialize(args);
3722 }
3723
3724 this(this)
3725 {
3726 if (!_refCounted.isInitialized)
3727 return;
3728 ++_refCounted._store._count;
3729 //printf("RefCounted count = %d (inc)\n", _refCounted._store._count);
3730 }
3731
3732 ~this()
3733 {
3734 if (!_refCounted.isInitialized)
3735 return;
3736 assert(_refCounted._store._count > 0);
3737 if (--_refCounted._store._count)
3738 {
3739 //printf("RefCounted count = %u\n", _refCounted._store._count);
3740 return;
3741 }
3742
3743 import core.stdc.stdlib : free;
3744 free(_refCounted._store);
3745 _refCounted._store = null;
3746 }
3747
3748 void opAssign(typeof(this) rhs) { assert(0); }
3749 void opAssign(T rhs) { assert(0); }
3750
3751 @property ref T refCountedPayload()
3752 {
3753 _refCounted.ensureInitialized();
3754 return _refCounted._store._payload;
3755 }
3756
3757 alias refCountedPayload this;
3758 }
3759
3760 struct Path14443
3761 {
3762 struct Payload
3763 {
3764 int p;
3765 }
3766 RefCounted14443!Payload data;
3767 }
3768
3769 struct PathRange14443
3770 {
3771 Path14443 path;
3772 size_t i;
3773
3774 @property PathElement14443 front()
3775 {
3776 return PathElement14443(this, path.data.p);
3777 }
3778 }
3779
3780 struct PathElement14443
3781 {
3782 PathRange14443 range;
3783
3784 this(PathRange14443 range, int)
3785 {
3786 this.range = range;
3787 }
3788 }
3789
3790 void test14443()
3791 {
3792 auto path = Path14443(RefCounted14443!(Path14443.Payload)(12));
3793 assert(path.data.p == 12);
3794
3795 @property refCount() { return path.data._refCounted._store._count; }
3796 assert(refCount == 1);
3797
3798 {
3799 auto _r = PathRange14443(path);
3800 assert(refCount == 2);
3801 // foreach
3802 {
3803 auto element = _r.front;
3804 assert(refCount == 3); // fail with 2.067
3805 }
3806 assert(refCount == 2);
3807 }
3808 assert(refCount == 1);
3809 }
3810
3811 /**********************************/
3812 // postblit/dtor call on static array assignment
3813 // https://issues.dlang.org/show_bug.cgi?id=13661
3814 // https://issues.dlang.org/show_bug.cgi?id=14022
3815 // https://issues.dlang.org/show_bug.cgi?id=14023
3816
3817 bool test13661()
3818 {
3819 string op;
3820
3821 struct S
3822 {
3823 char x = 'x';
3824 this(this) { op ~= x-0x20; } // upper case
3825 ~this() { op ~= x; } // lower case
3826
3827 ref auto opAssign(T)(T arg)
3828 {
3829 assert(0);
3830 return this;
3831 }
3832 }
3833
3834 {
3835 S[2] a;
3836
3837 a[0].x = 'a';
3838 a[1].x = 'b';
3839 a = a.init;
3840 assert(op == "ab");
3841 assert(a[0].x == 'x' && a[1].x == 'x');
3842
3843 a[0].x = 'c';
3844 a[1].x = 'd';
3845 a = [S(), S()]; // equivalent a = a.init
3846 assert(op == "abcd");
3847 assert(a[0].x == 'x' && a[1].x == 'x');
3848 }
3849 assert(op == "abcdxx");
3850
3851 return true;
3852 }
3853 bool test13661a()
3854 {
3855 string op;
3856
3857 struct S
3858 {
3859 char x = 'x';
3860 this(this) { op ~= x-0x20; } // upper case
3861 ~this() { op ~= x; } // lower case
3862 }
3863
3864 {
3865 S[3] sa = [S('a'), S('b'), S('c')];
3866 S[2] sb = sa[1..3];
3867 assert(sa == [S('a'), S('b'), S('c')]);
3868 assert(sb == [S('b'), S('c')]);
3869 sb[0].x = 'x';
3870 sb[1].x = 'y';
3871 assert(sa != [S('a'), S('x'), S('y')]); // OK <- incorrectly fails
3872 assert(sa == [S('a'), S('b'), S('c')]); // OK <- incorrectly fails
3873 assert(sb == [S('x'), S('y')]);
3874 }
3875 return true;
3876 }
3877 static assert(test13661()); // CTFE
3878 static assert(test13661a());
3879
3880 bool test14022()
3881 {
3882 string op;
3883
3884 struct S
3885 {
3886 char x = 'x';
3887 this(this) { op ~= x-0x20; } // upper case
3888 ~this() { op ~= x; } // lower case
3889 }
3890
3891 S[2] makeSA() { return [S('p'), S('q')]; }
3892
3893 struct T
3894 {
3895 S[2] sb;
3896
3897 this(ref S[2] sa)
3898 {
3899 assert(op == "");
3900 this.sb = sa; // TOKconstruct
3901 assert(op == "BC", op);
3902 assert(sb == [S('b'), S('c')]);
3903 }
3904 void test(ref S[2] sa)
3905 {
3906 this.sb = sa; // dotvar: resolveSlice(newva)
3907 assert(op == "BxCy");
3908 }
3909 }
3910
3911 op = null;
3912 {
3913 S[2] sa = [S('a'), S('b')];
3914 T t; t.sb[0].x = 'x';
3915 t.sb[1].x = 'y';
3916 assert(op == "");
3917 t.sb = sa;
3918 assert(op == "AxBy");
3919 t.sb = makeSA();
3920 assert(op == "AxByab");
3921 }
3922 assert(op == "AxByabqpba");
3923
3924 op = null;
3925 {
3926 S[3] sa = [S('a'), S('b'), S('c')];
3927 T t = T(sa[1..3]);
3928 t.sb[0].x = 'x';
3929 t.sb[1].x = 'y';
3930 assert(sa == [S('a'), S('b'), S('c')]);
3931 assert(t.sb == [S('x'), S('y')]);
3932 assert(op == "BC");
3933 }
3934 assert(op == "BCyxcba");
3935
3936 op = null;
3937 {
3938 S[3] sx = [S('a'), S('b'), S('c')];
3939 T t; t.sb[0].x = 'x';
3940 t.sb[1].x = 'y';
3941 t.test(sx[1..3]);
3942 assert(op == "BxCy");
3943 assert(t.sb == [S('b'), S('c')]);
3944 }
3945 assert(op == "BxCycbcba");
3946
3947 return true;
3948 }
3949 static assert(test14022());
3950
3951 bool test14023()
3952 {
3953 string op;
3954
3955 struct S
3956 {
3957 char x = 'x';
3958 this(this) { op ~= x-0x20; } // upper case
3959 ~this() { op ~= x; } // lower case
3960 }
3961
3962 S[2] makeSA() { return [S('p'), S('q')]; }
3963
3964 struct T
3965 {
3966 S[2][1] sb;
3967 this(ref S[2] sa)
3968 {
3969 assert(op == "");
3970 this.sb[0] = sa; // TOKconstruct
3971 assert(sa == [S('b'), S('c')]);
3972 assert(sb[0] == [S('b'), S('c')]);
3973 }
3974 }
3975
3976 void test(ref S[2] sa)
3977 {
3978 S[2][] a;
3979 //a.length = 1; // will cause runtine AccessViolation
3980 a ~= (S[2]).init;
3981 assert(op == "");
3982 a[0] = sa; // index <-- resolveSlice(newva)
3983 assert(op == "BxCx");
3984 assert(a[0] == [S('b'), S('c')]);
3985 }
3986
3987 op = null;
3988 {
3989 S[3] sa = [S('a'), S('b'), S('c')];
3990 T t = T(sa[1..3]);
3991 t.sb[0][0].x = 'x';
3992 t.sb[0][1].x = 'y';
3993 assert(sa != [S('a'), S('x'), S('y')]); // OK <- incorrectly fails
3994 assert(sa == [S('a'), S('b'), S('c')]); // OK <- incorrectly fails
3995 assert(t.sb[0] == [S('x'), S('y')]);
3996 }
3997
3998 op = null;
3999 {
4000 S[2] sa = [S('a'), S('b')];
4001 S[2][] a = [[S('x'), S('y')]];
4002 assert(op == "");
4003 a[0] = sa;
4004 assert(op == "AxBy");
4005 a[0] = makeSA();
4006 assert(op == "AxByab");
4007 }
4008 assert(op == "AxByabba");
4009
4010 op = null;
4011 {
4012 S[3] sa = [S('a'), S('b'), S('c')];
4013 test(sa[1..3]);
4014 assert(op == "BxCx");
4015 }
4016 assert(op == "BxCxcba");
4017
4018 return true;
4019 }
4020 static assert(test14023());
4021
4022 /************************************************/
4023 // https://issues.dlang.org/show_bug.cgi?id=13669 - dtor call on static array variable
4024
4025 bool test13669()
4026 {
4027 string dtor;
4028
4029 struct S
4030 {
4031 char x = 'x';
4032 ~this() { dtor ~= x; }
4033 }
4034
4035 { S[2] a; }
4036 assert(dtor == "xx");
4037 dtor = "";
4038
4039 { S[2] a = [S('a'), S('b')]; }
4040 assert(dtor == "ba"); // reverse order. See also: TypeInfo_StaticArray.destroy()
4041
4042 return true;
4043 }
4044 static assert(test13669());
4045
4046 /**********************************/
4047
4048 __gshared bool b13095 = false;
4049
4050 void bar13095() { throw new Exception(""); }
4051
4052 struct S13095
4053 {
4054 this(int) { printf("ctor %p\n", &this); bar13095(); }
4055
4056 ~this() { b13095 = true; printf("dtor %p\n", &this); }
4057 }
4058
4059 void test13095()
4060 {
4061 try {
4062 S13095(0);
4063 } catch(Exception) { printf("catch\n"); }
4064 assert(!b13095);
4065 }
4066
4067 /**********************************/
4068 // https://issues.dlang.org/show_bug.cgi?id=14264
4069
4070 void test14264()
4071 {
4072 static int dtor;
4073 static struct Foo
4074 {
4075 ~this() { ++dtor; }
4076 T opCast(T:bool)() { return true; }
4077 }
4078
4079 Foo makeFoo()
4080 {
4081 return Foo();
4082 }
4083
4084 assert(dtor == 0);
4085
4086 makeFoo();
4087 assert(dtor == 1);
4088
4089 makeFoo;
4090 assert(dtor == 2);
4091
4092 if (makeFoo()) {}
4093 assert(dtor == 3);
4094
4095 if (makeFoo) {}
4096 assert(dtor == 4);
4097 }
4098
4099 /**********************************/
4100 // https://issues.dlang.org/show_bug.cgi?id=14686
4101
4102 int test14686()
4103 {
4104 string r;
4105
4106 struct S
4107 {
4108 int n;
4109 this(this) { r ~= cast(char)('0' + n); }
4110 }
4111
4112 S s1 = S(1);
4113 S s2 = S(2);
4114 S[] a1 = [S(1)];
4115
4116 S[2] sa1 = [s1, s2];
4117 assert(r == "12", r); // OK
4118
4119 r = "";
4120 S[] a2 = a1 ~ s2; // runtime concatenation
4121 assert(r == "12", r); // OK <- NG only in CTFE
4122
4123 r = "";
4124 S[2] sa2a = [s1] ~ s2;
4125 assert(r == "12", r); // OK <- NG, s2 is not copied
4126
4127 r = "";
4128 S[2] sa2b = s2 ~ [s1];
4129 assert(r == "21", r); // OK <- NG, s2 is not copied
4130
4131 r = "";
4132 S[3] sa3a = ([s1] ~ [s1]) ~ s2;
4133 assert(r == "112", r); // OK <- NG, s2 is not copied
4134
4135 r = "";
4136 S[3] sa3b = s2 ~ ([s1] ~ [s1]);
4137 assert(r == "211", r); // OK <- NG, s2 is not copied
4138
4139 return 1;
4140 }
4141 static assert(test14686());
4142
4143 /**********************************/
4144 // https://issues.dlang.org/show_bug.cgi?id=14815
4145
4146 int test14815()
4147 {
4148 uint dtorCount;
4149
4150 struct S
4151 {
4152 uint x;
4153 ~this() { ++dtorCount; }
4154 }
4155
4156 S[2] sa1;
4157 sa1[0].x = 42;
4158 sa1 = (S[2]).init; // S[2] <- rvalue
4159 assert(sa1[0].x == 0);
4160 assert(dtorCount == 2);
4161
4162 S[2] sa2;
4163 sa2[0].x = 42;
4164 S[] da2 = sa2[];
4165 da2[] = (S[2]).init[]; // S[] <- rvalue slice
4166 assert(sa2[0].x == 0);
4167 assert(dtorCount == 4);
4168
4169 S[2] sa3;
4170 S[2] sa4;
4171 sa3[0].x = 42;
4172 sa3 = sa4; // S[2] <- lvalue
4173 assert(sa3[0].x == 0);
4174 assert(dtorCount == 6);
4175
4176 S[2] sa5;
4177 S[] da4 = sa4[];
4178 da4[] = sa5[]; // S[] <- lvalue slice
4179 assert(sa4[0].x == 0);
4180 assert(dtorCount == 8);
4181
4182 return 1;
4183 }
4184 static assert(test14815());
4185
4186 /**********************************/
4187 // https://issues.dlang.org/show_bug.cgi?id=16197
4188
4189 struct Elem {
4190 static string r;
4191 int x = -1;
4192 this(this) { r ~= 'p'; printf("POSTBLIT %d\n", x++); }
4193 ~this() { r ~= 'd'; printf("DTOR %d\n" , x++); }
4194 }
4195
4196 struct Ctr {
4197 Elem[3] arr;
4198 }
4199
4200 void test16197() {
4201 { auto p = Ctr(); }
4202 assert(Elem.r == "ddd");
4203 }
4204
4205 /**********************************/
4206 // https://issues.dlang.org/show_bug.cgi?id=14860
4207
4208 int test14860()
4209 {
4210 uint dtorCount;
4211
4212 struct S
4213 {
4214 uint x;
4215 ~this() { ++dtorCount; }
4216 }
4217
4218 S[] a = [S(42)];
4219 a[] = S();
4220
4221 assert(a[0].x == 0);
4222 assert(dtorCount == 1);
4223
4224 return 1;
4225 }
4226 static assert(test14860());
4227
4228 /**********************************/
4229 // https://issues.dlang.org/show_bug.cgi?id=14246
4230
4231 struct A14246 {
4232 int a = 3;
4233 static string s;
4234 this( int var ) { printf("A()\n"); a += var; s ~= "a"; }
4235
4236 ~this() { printf("~A()\n"); s ~= "b"; }
4237 }
4238
4239 struct B14246 {
4240 int i;
4241 A14246 a;
4242
4243 this( int var ) {
4244 A14246.s ~= "c";
4245 a = A14246(var+1);
4246 throw new Exception("An exception");
4247 }
4248 }
4249
4250 void test14246() {
4251 try {
4252 auto b = B14246(2);
4253 } catch( Exception ex ) {
4254 printf("Caught ex\n");
4255 A14246.s ~= "d";
4256 }
4257 assert(A14246.s == "cabd");
4258 }
4259
4260 /**********************************/
4261 // https://issues.dlang.org/show_bug.cgi?id=14696
4262
4263 void test14696(int len = 2)
4264 {
4265 string result;
4266
4267 struct S
4268 {
4269 int n;
4270
4271 void* get(void* p = null)
4272 {
4273 result ~= "get(" ~ cast(char)(n+'0') ~ ").";
4274 return null;
4275 }
4276
4277 ~this()
4278 {
4279 result ~= "dtor(" ~ cast(char)(n+'0') ~ ").";
4280 }
4281 }
4282
4283 S makeS(int n)
4284 {
4285 result ~= "makeS(" ~ cast(char)(n+'0') ~ ").";
4286 return S(n);
4287 }
4288 void foo(void* x, void* y = null)
4289 {
4290 result ~= "foo.";
4291 }
4292 void fooThrow(void* x, void* y = null)
4293 {
4294 result ~= "fooThrow.";
4295 throw new Exception("fail!");
4296 }
4297
4298 void check(void delegate() dg, string r, string file = __FILE__, size_t line = __LINE__)
4299 {
4300 import core.exception;
4301
4302 result = null;
4303 try { dg(); } catch (Exception e) {}
4304 if (result != r)
4305 throw new AssertError(result, file, line);
4306 }
4307
4308 // temporary in condition
4309 check({ foo(len == 2 ? makeS(1).get() : null); }, "makeS(1).get(1).foo.dtor(1).");
4310 check({ foo(len == 2 ? null : makeS(1).get() ); }, "foo.");
4311 check({ foo(len != 2 ? makeS(1).get() : null); }, "foo.");
4312 check({ foo(len != 2 ? null : makeS(1).get() ); }, "makeS(1).get(1).foo.dtor(1).");
4313
4314 // temporary in nesting conditions
4315 check({ foo(len >= 2 ? (len == 2 ? makeS(1).get() : null) : null); }, "makeS(1).get(1).foo.dtor(1).");
4316 check({ foo(len >= 2 ? (len == 2 ? null : makeS(1).get() ) : null); }, "foo.");
4317 check({ foo(len >= 2 ? (len != 2 ? makeS(1).get() : null) : null); }, "foo.");
4318 check({ foo(len >= 2 ? (len != 2 ? null : makeS(1).get() ) : null); }, "makeS(1).get(1).foo.dtor(1).");
4319 check({ foo(len >= 2 ? null : (len == 2 ? makeS(1).get() : null) ); }, "foo.");
4320 check({ foo(len >= 2 ? null : (len == 2 ? null : makeS(1).get() ) ); }, "foo.");
4321 check({ foo(len >= 2 ? null : (len != 2 ? makeS(1).get() : null) ); }, "foo.");
4322 check({ foo(len >= 2 ? null : (len != 2 ? null : makeS(1).get() ) ); }, "foo.");
4323 check({ foo(len > 2 ? (len == 2 ? makeS(1).get() : null) : null); }, "foo.");
4324 check({ foo(len > 2 ? (len == 2 ? null : makeS(1).get() ) : null); }, "foo.");
4325 check({ foo(len > 2 ? (len != 2 ? makeS(1).get() : null) : null); }, "foo.");
4326 check({ foo(len > 2 ? (len != 2 ? null : makeS(1).get() ) : null); }, "foo.");
4327 check({ foo(len > 2 ? null : (len == 2 ? makeS(1).get() : null) ); }, "makeS(1).get(1).foo.dtor(1).");
4328 check({ foo(len > 2 ? null : (len == 2 ? null : makeS(1).get() ) ); }, "foo.");
4329 check({ foo(len > 2 ? null : (len != 2 ? makeS(1).get() : null) ); }, "foo.");
4330 check({ foo(len > 2 ? null : (len != 2 ? null : makeS(1).get() ) ); }, "makeS(1).get(1).foo.dtor(1).");
4331
4332 // temporary in condition and throwing callee
4333 check({ fooThrow(len == 2 ? makeS(1).get() : null); }, "makeS(1).get(1).fooThrow.dtor(1).");
4334 check({ fooThrow(len == 2 ? null : makeS(1).get() ); }, "fooThrow.");
4335 check({ fooThrow(len != 2 ? makeS(1).get() : null); }, "fooThrow.");
4336 check({ fooThrow(len != 2 ? null : makeS(1).get() ); }, "makeS(1).get(1).fooThrow.dtor(1).");
4337
4338 // temporary in nesting condititions and throwing callee
4339 check({ fooThrow(len >= 2 ? (len == 2 ? makeS(1).get() : null) : null); }, "makeS(1).get(1).fooThrow.dtor(1).");
4340 check({ fooThrow(len >= 2 ? (len == 2 ? null : makeS(1).get() ) : null); }, "fooThrow.");
4341 check({ fooThrow(len >= 2 ? (len != 2 ? makeS(1).get() : null) : null); }, "fooThrow.");
4342 check({ fooThrow(len >= 2 ? (len != 2 ? null : makeS(1).get() ) : null); }, "makeS(1).get(1).fooThrow.dtor(1).");
4343 check({ fooThrow(len >= 2 ? null : (len == 2 ? makeS(1).get() : null) ); }, "fooThrow.");
4344 check({ fooThrow(len >= 2 ? null : (len == 2 ? null : makeS(1).get() ) ); }, "fooThrow.");
4345 check({ fooThrow(len >= 2 ? null : (len != 2 ? makeS(1).get() : null) ); }, "fooThrow.");
4346 check({ fooThrow(len >= 2 ? null : (len != 2 ? null : makeS(1).get() ) ); }, "fooThrow.");
4347 check({ fooThrow(len > 2 ? (len == 2 ? makeS(1).get() : null) : null); }, "fooThrow.");
4348 check({ fooThrow(len > 2 ? (len == 2 ? null : makeS(1).get() ) : null); }, "fooThrow.");
4349 check({ fooThrow(len > 2 ? (len != 2 ? makeS(1).get() : null) : null); }, "fooThrow.");
4350 check({ fooThrow(len > 2 ? (len != 2 ? null : makeS(1).get() ) : null); }, "fooThrow.");
4351 check({ fooThrow(len > 2 ? null : (len == 2 ? makeS(1).get() : null) ); }, "makeS(1).get(1).fooThrow.dtor(1).");
4352 check({ fooThrow(len > 2 ? null : (len == 2 ? null : makeS(1).get() ) ); }, "fooThrow.");
4353 check({ fooThrow(len > 2 ? null : (len != 2 ? makeS(1).get() : null) ); }, "fooThrow.");
4354 check({ fooThrow(len > 2 ? null : (len != 2 ? null : makeS(1).get() ) ); }, "makeS(1).get(1).fooThrow.dtor(1).");
4355
4356 // temporaries in each conditions
4357 check({ foo(len == 2 ? makeS(1).get() : null, len == 2 ? makeS(2).get() : null); }, "makeS(1).get(1).makeS(2).get(2).foo.dtor(2).dtor(1).");
4358 check({ foo(len == 2 ? makeS(1).get() : null, len != 2 ? makeS(2).get() : null); }, "makeS(1).get(1).foo.dtor(1).");
4359 check({ foo(len != 2 ? makeS(1).get() : null, len == 2 ? makeS(2).get() : null); }, "makeS(2).get(2).foo.dtor(2).");
4360 check({ foo(len != 2 ? makeS(1).get() : null, len != 2 ? makeS(2).get() : null); }, "foo.");
4361
4362 // nesting temporaries in conditions
4363 check({ foo(len == 2 ? makeS(1).get(len == 2 ? makeS(2).get() : null) : null); }, "makeS(1).makeS(2).get(2).get(1).foo.dtor(2).dtor(1).");
4364 check({ foo(len == 2 ? makeS(1).get(len != 2 ? makeS(2).get() : null) : null); }, "makeS(1).get(1).foo.dtor(1).");
4365 check({ foo(len != 2 ? makeS(1).get(len == 2 ? makeS(2).get() : null) : null); }, "foo.");
4366 check({ foo(len != 2 ? makeS(1).get(len != 2 ? makeS(2).get() : null) : null); }, "foo.");
4367 check({ foo(len == 2 ? makeS(makeS(2).n - 1).get() : null); }, "makeS(2).makeS(1).get(1).foo.dtor(1).dtor(2).");
4368 check({ foo(len != 2 ? makeS(makeS(2).n - 1).get() : null); }, "foo.");
4369 }
4370
4371 /**********************************/
4372 // https://issues.dlang.org/show_bug.cgi?id=14838
4373
4374 int test14838() pure nothrow @safe
4375 {
4376 int dtor;
4377
4378 struct S14838(T)
4379 {
4380 ~this() { ++dtor; }
4381 }
4382 struct X14838
4383 {
4384 S14838!int ms;
4385 const S14838!int cs;
4386
4387 S14838!int[2] ma;
4388 const S14838!int[2] ca;
4389
4390 S14838!int[2][2] ma2x2;
4391 const S14838!int[2][2] ca2x2;
4392
4393 // number of S14838 = 1*2 + 2*2 + 4*2 = 14
4394 }
4395
4396 void test(Dg)(scope Dg code)
4397 {
4398 dtor = 0;
4399 code();
4400 }
4401
4402 test(delegate{ S14838!int a; }); assert(dtor == 1);
4403 test(delegate{ const S14838!int a; }); assert(dtor == 1);
4404
4405 test(delegate{ S14838!int[2] a; }); assert(dtor == 2);
4406 test(delegate{ const S14838!int[2] a; }); assert(dtor == 2);
4407
4408 test(delegate{ S14838!int[2][2] a; }); assert(dtor == 4);
4409 test(delegate{ const S14838!int[2][2] a; }); assert(dtor == 4);
4410
4411 test(delegate{ X14838 a; }); assert(dtor == 1 * 14);
4412 test(delegate{ const X14838 a; }); assert(dtor == 1 * 14);
4413
4414 test(delegate{ X14838[2] a; }); assert(dtor == 2 * 14);
4415 test(delegate{ const X14838[2] a; }); assert(dtor == 2 * 14);
4416
4417 test(delegate{ X14838[2][2] a; }); assert(dtor == 4 * 14);
4418 test(delegate{ const X14838[2][2] a; }); assert(dtor == 4 * 14);
4419
4420 return 1;
4421 }
4422 static assert(test14838());
4423
4424 /**********************************/
4425
4426 // https://issues.dlang.org/show_bug.cgi?id=14639
4427
4428 struct Biggy {
4429 ulong[50000] a;
4430 @disable this(this);
4431 }
4432
4433 __gshared Biggy biggy;
4434
4435 void test14639() {
4436 biggy = Biggy.init;
4437 }
4438
4439 /**********************************/
4440
4441 struct S63
4442 {
4443 private long p = 87;
4444
4445 this(int x)
4446 {
4447 assert(p == 87);
4448 p += x;
4449 }
4450
4451 ~this() { }
4452
4453 this(this) { }
4454
4455 void funky() { assert(p == 90); }
4456
4457 static void tester()
4458 {
4459 S63(3).funky();
4460 }
4461 }
4462
4463 void test63()
4464 {
4465 S63.tester();
4466 }
4467
4468 /**********************************/
4469
4470 struct X64
4471 {
4472 static int dtor;
4473
4474 ~this() { ++dtor; }
4475 }
4476
4477 struct S64
4478 {
4479 int n;
4480 long[10] dummy; // S64 needs to be passed by stack
4481 }
4482
4483 S64 foo64()
4484 {
4485 X64();
4486 return S64(1);
4487 }
4488
4489 void test64()
4490 {
4491 auto s = foo64();
4492 assert(X64.dtor == 1);
4493 }
4494
4495 /**********************************/
4496
4497 struct S65
4498 {
4499 static string t;
4500
4501 void bar(int a, int b)
4502 {
4503 t ~= "d";
4504 }
4505 }
4506
4507 S65 foo65a()
4508 {
4509 S65.t ~= "a";
4510 return S65();
4511 }
4512
4513 int foo65b()
4514 {
4515 S65.t ~= "b";
4516 return 1;
4517 }
4518
4519 int foo65c()
4520 {
4521 S65.t ~= "c";
4522 return 2;
4523 }
4524
4525 void test65()
4526 {
4527 import core.stdc.stdio;
4528 foo65a().bar(foo65b(), foo65c());
4529 printf("'%.*s'\n", cast(int)S65.t.length, S65.t.ptr);
4530 assert(S65.t == "abcd");
4531 }
4532
4533 /**********************************/
4534 // https://issues.dlang.org/show_bug.cgi?id=15661
4535
4536 struct X15661
4537 {
4538 ~this() {}
4539 }
4540
4541 X15661 createX15661() { return X15661(); }
4542
4543 struct Y15661
4544 {
4545 static int dtor;
4546
4547 @disable this();
4548 @disable this(this);
4549 this(X15661 a1, X15661 a2) {}
4550 ~this() { ++dtor; }
4551 }
4552
4553 struct Z15661
4554 {
4555 this(int)
4556 {
4557 b = Y15661(createX15661(), createX15661());
4558 assert(Y15661.dtor == 0);
4559 }
4560
4561 private Y15661 b;
4562 }
4563
4564 void test15661()
4565 {
4566 {
4567 auto v = Z15661(5);
4568 assert(Y15661.dtor == 0);
4569 }
4570 assert(Y15661.dtor == 1);
4571 }
4572
4573 /**********************************/
4574
4575 // https://issues.dlang.org/show_bug.cgi?id=18045
4576
4577 struct A18045
4578 {
4579 nothrow:
4580 __gshared int r;
4581 int state;
4582 this(this) { printf("postblit: A(%d)\n", state); r += 1; }
4583 ~this() { printf("dtor: A(%d)\n", state); r *= 3; }
4584 }
4585
4586 A18045 fun18045() nothrow
4587 {
4588 __gshared a = A18045(42);
4589 return a;
4590 }
4591
4592 void test18045() nothrow
4593 {
4594 alias A = A18045;
4595
4596 __gshared a = A(-42);
4597 if (fun18045() == a)
4598 assert(0);
4599 else
4600 assert(A.r == 3);
4601
4602 A.r = 0;
4603 if (a == fun18045())
4604 assert(0);
4605 else
4606 assert(A.r == 3);
4607 }
4608
4609 /**********************************/
4610
4611 struct S66
4612 {
4613 ~this() { }
4614 }
4615
4616 nothrow void notthrow() { }
4617
4618 class C66
4619 {
4620 S66 s;
4621
4622 this() nothrow { notthrow(); }
4623 }
4624
4625 /**********************************/
4626 // https://issues.dlang.org/show_bug.cgi?id=16652
4627
4628 struct Vector
4629 {
4630 this(ubyte a)
4631 {
4632 pragma(inline, false);
4633 buf = a;
4634 }
4635
4636 ~this()
4637 {
4638 pragma(inline, false);
4639 buf = 0;
4640 }
4641
4642 ubyte buf;
4643 }
4644
4645 int bar16652(ubyte* v)
4646 {
4647 pragma(inline, true);
4648 assert(*v == 1);
4649 return 0;
4650 }
4651
4652 void test16652()
4653 {
4654 bar16652(&Vector(1).buf);
4655 }
4656
4657
4658 /**********************************/
4659 // https://issues.dlang.org/show_bug.cgi?id=19676
4660
4661 void test19676()
4662 {
4663 static struct S
4664 {
4665 __gshared int count;
4666 ~this() { ++count; }
4667 }
4668
4669 static S foo() { return S(); }
4670
4671 static void test1()
4672 {
4673 cast(void)foo();
4674 }
4675
4676 static void test2()
4677 {
4678 foo();
4679 }
4680
4681 test1();
4682 assert(S.count == 1);
4683 test2();
4684 assert(S.count == 2);
4685 }
4686
4687 /**********************************/
4688
4689 // https://issues.dlang.org/show_bug.cgi?id=14708
4690
4691 __gshared bool dtor14078 = false;
4692
4693 struct S14078
4694 {
4695 int n;
4696
4697 void* get(void* p = null)
4698 {
4699 return null;
4700 }
4701
4702 ~this()
4703 {
4704 //printf("dtor\n");
4705 dtor14078 = true;
4706 }
4707 }
4708
4709 S14078 makeS14078(int n)
4710 {
4711 return S14078(n);
4712 }
4713
4714 void foo14078(void* x)
4715 {
4716 throw new Exception("fail!");
4717 }
4718
4719 void test(int len = 2)
4720 {
4721 foo14078(makeS14078(1).get());
4722 // A temporary is allocated on stack for the
4723 // return value from makeS14078(1).
4724 // When foo14078 throws exception, it's dtor should be called
4725 // during unwinding stack, but it does not happen in Win64.
4726 }
4727
4728 void test14078()
4729 {
4730 try
4731 {
4732 test();
4733 } catch (Exception e) {}
4734 assert(dtor14078); // fails!
4735 }
4736
4737 /**********************************/
4738
4739 void test67()
4740 {
4741 char[] deleted;
4742
4743 struct S
4744 {
4745 char* p;
4746
4747 ~this() { deleted ~= *p; }
4748
4749 void opAssign(S rhs)
4750 {
4751 // swap
4752 char* tmp = p;
4753 this.p = rhs.p;
4754 rhs.p = tmp;
4755 }
4756 }
4757
4758 char a = 'a', b = 'b';
4759 {
4760 S s = S(&a);
4761 s = S(&b);
4762 }
4763 assert(deleted == "ab", deleted);
4764 }
4765
4766 /**********************************/
4767
4768 void test68()
4769 {
4770 static struct S
4771 {
4772 int i;
4773 bool opEquals(S) { return false; }
4774 ~this() {}
4775 }
4776
4777 assert(S(0) != S(1));
4778 }
4779
4780 /**********************************/
4781
4782 // https://github.com/dlang/dmd/pull/12012
4783
4784 extern (C++)
4785 {
4786 struct S12012
4787 {
4788 int* ctr;
4789 ~this() { }
4790 }
4791
4792 void bar12012(int value, S12012 s)
4793 {
4794 }
4795
4796 S12012 abc12012(ref S12012 s)
4797 {
4798 s.ctr = null;
4799 return s;
4800 }
4801
4802 int def12012(ref S12012 s)
4803 {
4804 return *s.ctr; // seg fault is here
4805 }
4806
4807 void testPR12012()
4808 {
4809 int i;
4810 S12012 s = S12012(&i);
4811 // def must be executed before abc else seg fault
4812 bar12012(def12012(s), abc12012(s));
4813 }
4814 }
4815
4816 /**********************************/
4817 // https://issues.dlang.org/show_bug.cgi?id=24010
4818
4819 alias AliasSeq(TList...) = TList;
4820
4821 __gshared int x24010 = 7;
4822
4823 struct A24010 {
4824 int x;
4825 ~this() {
4826 printf("A.~this\n");
4827 x24010 += 1;
4828 }
4829 }
4830
4831 struct B24010 {
4832 ~this() {
4833 printf("B.~this\n");
4834 x24010 *= 10;
4835 }
4836 }
4837
4838 void test24010()
4839 {
4840 {
4841 AliasSeq!(A24010, B24010) params;
4842 printf("statement\n");
4843 params[0].x = 3;
4844 printf(".x = %d\n", params[0].x);
4845 assert(params[0].x == 3);
4846 assert(x24010 == 7);
4847 }
4848 assert(x24010 == 71);
4849 }
4850
4851 /**********************************/
4852
4853 int main()
4854 {
4855 test1();
4856
4857 test3();
4858 test4();
4859 test5();
4860 test6();
4861 test7();
4862 test8();
4863 test9();
4864 test10();
4865 test11();
4866 test12();
4867 test13();
4868 test14();
4869 test15();
4870 test16();
4871 test17();
4872 test18();
4873 test19();
4874 test20();
4875 test21();
4876 test22();
4877 test23();
4878 test24();
4879 test25();
4880 test26();
4881 test27();
4882 test28();
4883 test29();
4884 test30();
4885 test31();
4886 test32();
4887 test33();
4888 test34();
4889 test35();
4890 test36();
4891 test37();
4892 test38();
4893 test39();
4894 test40();
4895 test41();
4896 test42();
4897 test43();
4898 test44();
4899 test45();
4900 test46();
4901 test47();
4902 test48();
4903 test49();
4904 test50();
4905 test51();
4906 test52();
4907
4908 test54();
4909 test55();
4910 test56();
4911 test57();
4912 test58();
4913 test59();
4914 test5737();
4915 test6119();
4916 test8741();
4917 test6364();
4918 test6499();
4919 test60();
4920 test4316();
4921 test6177();
4922 test6470();
4923 test6636();
4924 test6637();
4925 test7353();
4926 test61();
4927 test7506();
4928 test7516a();
4929 test7516b();
4930 test7516c();
4931 test7516d();
4932 test7516e();
4933 test7530();
4934 test62();
4935 test7579a();
4936 test7579b();
4937 test8335();
4938 test8356();
4939 test9386();
4940 test9441();
4941 test9720();
4942 test9899();
4943 test9907();
4944 test9985();
4945 test17457();
4946 test9994();
4947 test10094();
4948 test10244();
4949 test10694();
4950 test10789();
4951 test10972();
4952 test11134();
4953 test11197();
4954 test7474();
4955 test11505();
4956 test12045();
4957 test12591();
4958 test12660();
4959 test12686();
4960 test13089();
4961 test11763();
4962 test13303();
4963 test13673();
4964 test13586();
4965 test14443();
4966 test13661();
4967 test13661a();
4968 test14022();
4969 test14023();
4970 test13669();
4971 test13095();
4972 test14264();
4973 test14686();
4974 test14815();
4975 test16197();
4976 test14860();
4977 test14246();
4978 test14696();
4979 test14838();
4980 test14639();
4981 test63();
4982 test64();
4983 test65();
4984 test15661();
4985 test18045();
4986 test16652();
4987 test19676();
4988 test14078();
4989 test67();
4990 test68();
4991 testPR12012();
4992 test24010();
4993
4994 printf("Success\n");
4995 return 0;
4996 }