/
sdtor.d
4817 lines (3998 loc) · 92 KB
/
sdtor.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// PERMUTE_ARGS: -unittest -O -release -inline -fPIC -g
// REQUIRED_ARGS: -preview=dtorfields
import core.vararg;
extern (C) int printf(const(char*) fmt, ...) nothrow;
template TypeTuple(T...) { alias TypeTuple = T; }
/**********************************/
int sdtor;
struct S1
{
~this() { printf("~S()\n"); sdtor++; }
}
void test1()
{
S1* s = new S1();
delete s;
assert(sdtor == 1);
}
/**********************************/
int sdtor2;
struct S2
{
~this() { printf("~S2()\n"); sdtor2++; }
delete(void* p) { assert(sdtor2 == 1); printf("S2.delete()\n"); sdtor2++; }
}
void test2()
{
S2* s = new S2();
delete s;
assert(sdtor2 == 2);
}
/**********************************/
int sdtor3;
struct S3
{ int a;
~this() { printf("~S3()\n"); sdtor3++; assert(a == 3); }
}
struct T3
{
int i;
S3 s;
}
void test3()
{
T3* s = new T3();
s.s.a = 3;
delete s;
assert(sdtor3 == 1);
}
/**********************************/
int sdtor4;
struct S4
{ int a = 3;
~this()
{ printf("~S4()\n");
if (a == 4)
assert(sdtor4 == 2);
else
{ assert(a == 3);
assert(sdtor4 == 1);
}
sdtor4++;
}
}
struct T4
{
int i;
S4 s;
~this() { printf("~T4()\n"); assert(sdtor4 == 0); sdtor4++; }
S4 t;
}
void test4()
{
T4* s = new T4();
s.s.a = 4;
delete s;
assert(sdtor4 == 3);
}
/**********************************/
int sdtor5;
template M5()
{ ~this()
{
printf("~M5()\n"); assert(sdtor5 == 1); sdtor5++;
}
}
struct T5
{
mixin M5 m;
~this() { printf("~T5()\n"); assert(sdtor5 == 0); sdtor5++; }
}
void test5()
{
T5* s = new T5();
delete s;
assert(sdtor5 == 2);
}
/**********************************/
int sdtor6;
struct S6
{ int b = 7;
~this()
{
printf("~S6()\n"); assert(b == 7); assert(sdtor6 == 1); sdtor6++;
}
}
class T6
{
int a = 3;
S6 s;
~this() { printf("~T6()\n"); assert(a == 3); assert(sdtor6 == 0); sdtor6++; }
}
void test6()
{
T6 s = new T6();
delete s;
assert(sdtor6 == 2);
}
/**********************************/
int sdtor7;
struct S7
{ int b = 7;
~this()
{
printf("~S7()\n");
assert(b == 7);
assert(sdtor7 >= 1 && sdtor7 <= 3);
sdtor7++;
}
}
struct T7
{
int a = 3;
S7[3] s;
~this()
{ printf("~T7() %d\n", sdtor7);
assert(a == 3);
assert(sdtor7 == 0);
sdtor7++;
}
}
void test7()
{
T7* s = new T7();
delete s;
assert(sdtor7 == 4);
}
/**********************************/
int sdtor8;
struct S8
{ int b = 7;
int c;
~this()
{
printf("~S8() %d\n", sdtor8);
assert(b == 7);
assert(sdtor8 == c);
sdtor8++;
}
}
void test8()
{
S8[] s = new S8[3];
s[0].c = 2;
s[1].c = 1;
s[2].c = 0;
delete s;
assert(sdtor8 == 3);
}
/**********************************/
int sdtor9;
struct S9
{ int b = 7;
~this()
{
printf("~S9() %d\n", sdtor9);
assert(b == 7);
sdtor9++;
}
}
void test9()
{
{
S9 s;
}
assert(sdtor9 == 1);
}
/**********************************/
int sdtor10;
struct S10
{ int b = 7;
int c;
~this()
{
printf("~S10() %d\n", sdtor10);
assert(b == 7);
assert(sdtor10 == c);
sdtor10++;
}
}
void test10()
{
{
S10[3] s;
s[0].c = 2;
s[1].c = 1;
s[2].c = 0;
}
assert(sdtor10 == 3);
}
/**********************************/
int sdtor11;
template M11()
{ ~this()
{
printf("~M11()\n"); assert(sdtor11 == 1); sdtor11++;
}
}
class T11
{
mixin M11 m;
~this() { printf("~T11()\n"); assert(sdtor11 == 0); sdtor11++; }
}
void test11()
{
T11 s = new T11();
delete s;
assert(sdtor11 == 2);
}
/**********************************/
int sdtor12;
struct S12
{ int a = 3;
~this() { printf("~S12() %d\n", sdtor12); sdtor12++; }
}
void foo12(S12 s)
{
}
void test12()
{
{
S12 s;
foo12(s);
s.a = 4;
}
assert(sdtor12 == 2);
}
/**********************************/
struct S13
{ int a = 3;
int opAssign(S13 s)
{
printf("S13.opAssign(%p)\n", &this);
a = 4;
return s.a + 2;
}
}
void test13()
{
S13 s;
S13 t;
assert((s = t) == 5);
assert(s.a == 4);
}
/**********************************/
struct S14
{ int a = 3;
int opAssign(ref S14 s)
{
printf("S14.opAssign(%p)\n", &this);
a = 4;
return s.a + 2;
}
}
void test14()
{
S14 s;
S14 t;
assert((s = t) == 5);
assert(s.a == 4);
}
/**********************************/
struct S15
{ int a = 3;
int opAssign(ref const S15 s)
{
printf("S15.opAssign(%p)\n", &this);
a = 4;
return s.a + 2;
}
}
void test15()
{
S15 s;
S15 t;
assert((s = t) == 5);
assert(s.a == 4);
}
/**********************************/
struct S16
{ int a = 3;
int opAssign(S16 s, ...)
{
printf("S16.opAssign(%p)\n", &this);
a = 4;
return s.a + 2;
}
}
void test16()
{
S16 s;
S16 t;
assert((s = t) == 5);
assert(s.a == 4);
}
/**********************************/
struct S17
{ int a = 3;
int opAssign(...)
{
printf("S17.opAssign(%p)\n", &this);
a = 4;
return 5;
}
}
void test17()
{
S17 s;
S17 t;
assert((s = t) == 5);
assert(s.a == 4);
}
/**********************************/
struct S18
{ int a = 3;
int opAssign(S18 s, int x = 7)
{
printf("S18.opAssign(%p)\n", &this);
a = 4;
assert(x == 7);
return s.a + 2;
}
}
void test18()
{
S18 s;
S18 t;
assert((s = t) == 5);
assert(s.a == 4);
}
/**********************************/
struct S19
{
int a,b,c,d;
this(this) { printf("this(this) %p\n", &this); }
~this() { printf("~this() %p\n", &this); }
}
S19 foo19()
{
S19 s;
void bar() { s.a++; }
bar();
return s;
}
void test19()
{
S19 t = foo19();
printf("-main()\n");
}
/**********************************/
struct S20
{
static char[] r;
int a,b,c=2,d;
this(this) { printf("this(this) %p\n", &this); r ~= '='; }
~this() { printf("~this() %p\n", &this); r ~= '~'; }
}
void foo20()
{
S20 s;
S20[3] a;
assert(S20.r == "");
a = s;
assert(S20.r == "=~=~=~");
}
void test20()
{
foo20();
assert(S20.r == "=~=~=~~~~~");
printf("-main()\n");
}
/**********************************/
struct S21
{
static char[] r;
int a,b,c=2,d;
this(this) { printf("this(this) %p\n", &this); r ~= '='; }
~this() { printf("~this() %p\n", &this); r ~= '~'; }
}
void foo21()
{
S21 s;
S21[3] a = s;
assert(S21.r == "===");
S21.r = null;
S21[3][2] b = s;
assert(S21.r == "======");
S21.r = null;
}
void test21()
{
foo21();
assert(S21.r == "~~~~~~~~~~");
printf("-main()\n");
}
/**********************************/
struct S22
{
static char[] r;
int a,b,c=2,d;
this(this) { printf("this(this) %p\n", &this); r ~= '='; }
~this() { printf("~this() %p\n", &this); r ~= '~'; }
}
void foo22()
{
S22[3] s;
S22[3][2] a;
assert(S22.r == "");
a = s;
assert(S22.r == "===~~~===~~~");
S22.r = null;
}
void test22()
{
foo22();
assert(S22.r == "~~~~~~~~~");
printf("-main()\n");
}
/************************************************/
struct S23
{
int m = 4, n, o, p, q;
this(int x)
{
printf("S23(%d)\n", x);
assert(x == 3);
assert(m == 4 && n == 0 && o == 0 && p == 0 && q == 0);
q = 7;
}
}
void test23()
{
{
auto s = S23(3);
printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
assert(s.m == 4 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
}
{
auto s = new S23(3);
printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
assert(s.m == 4 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
}
{
S23 s;
s = S23(3);
printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
assert(s.m == 4 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
}
}
/************************************************/
struct S24
{
int m, n, o, p, q;
this(int x)
{
printf("S24(%d)\n", x);
assert(x == 3);
assert(m == 0 && n == 0 && o == 0 && p == 0 && q == 0);
q = 7;
}
}
void test24()
{
{
auto s = S24(3);
printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
assert(s.m == 0 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
}
{
auto s = new S24(3);
printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
assert(s.m == 0 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
}
{
S24 s;
s = S24(3);
printf("s.m = %d, s.n = %d, s.q = %d\n", s.m, s.n, s.q);
assert(s.m == 0 && s.n == 0 && s.o == 0 && s.p == 0 && s.q == 7);
}
}
/**********************************/
struct S25
{
this(int s) {}
}
void test25()
{
auto a = S25();
}
/**********************************/
int z26;
struct A26
{
int id;
this(int x) { id = x; printf("Created A from scratch: %d\n", x); z26++; }
this(this) { printf("Copying A: %d\n", id); z26 += 10; }
~this() { printf("Destroying A: %d\n", id); z26 += 100; }
}
struct B26
{
A26 member;
this(this) { printf("Copying B: %d\n", member.id); assert(0); }
}
B26 foo26()
{
A26 a = A26(45);
printf("1\n");
assert(z26 == 1);
return B26(a);
}
void test26()
{
{
auto b = foo26();
assert(z26 == 111);
printf("2\n");
}
assert(z26 == 211);
}
/**********************************/
int z27;
struct A27
{
int id;
this(int x) { id = x; printf("Ctor A27: %d\n", x); z27++; }
this(this) { printf("Copying A27: %d\n", id); z27 += 10; }
~this() { printf("Destroying A27: %d\n", id); z27 += 100; }
}
struct B27
{
A27[2][3] member;
}
void test27()
{
{
A27[2][3] a;
assert(z27 == 0);
B27 b = B27(a);
assert(z27 == 60);
}
assert(z27 == 1260);
}
/**********************************/
string s28;
struct A28
{
this(this)
{
printf("A's copy\n");
s28 ~= "A";
}
}
struct B28
{
A28 member;
this(this)
{
printf("B's copy\n");
s28 ~= "B";
}
}
void test28()
{
B28 b1;
B28 b2 = b1;
assert(s28 == "AB");
}
/**********************************/
string s29;
template Templ29 ()
{
this(int i) { this(0.0); s29 ~= "i"; }
this(double d) { s29 ~= "d"; }
}
class C29 { mixin Templ29; }
struct S29 { mixin Templ29; }
void test29()
{
auto r = S29(1);
assert(s29 == "di");
r = S29(2.0);
assert(s29 == "did");
auto c = new C29(2);
assert(s29 == "diddi");
auto c2 = new C29(2.0);
assert(s29 == "diddid");
}
/**********************************/
struct S30
{
int x;
this(T)(T args) { x = args + 1; }
}
void test30()
{
auto s = S30(3);
assert(s.x == 4);
}
/**********************************/
struct S31
{
int x;
this(T...)(T args) { x = args[0] + 1; }
}
void test31()
{
auto s = S31(3);
assert(s.x == 4);
}
/**********************************/
struct S32
{
static int x;
this(int i)
{
printf("ctor %p(%d)\n", &this, i);
x += 1;
}
this(this)
{
printf("postblit %p\n", &this);
x += 0x10;
}
~this()
{
printf("dtor %p\n", &this);
x += 0x100;
}
}
S32 foo32()
{
printf("test1\n");
return S32(1);
}
S32 bar32()
{
printf("test1\n");
S32 f = S32(1);
printf("test2\n");
return f;
}
void test32()
{
{
S32 s = foo32();
}
assert(S32.x == 0x101);
S32.x = 0;
{
S32 s = bar32();
}
assert(S32.x == 0x101);
}
/**********************************/
struct S33
{
static int x;
this(int i)
{
printf("ctor %p(%d)\n", &this, i);
x += 1;
}
this(this)
{
printf("postblit %p\n", &this);
x += 0x10;
}
~this()
{
printf("dtor %p\n", &this);
x += 0x100;
}
}
struct T33
{
S33 foo()
{
return t;
}
S33 t;
}
void test33()
{
{
T33 t;
S33 s = t.foo();
}
printf("S.x = %x\n", S33.x);
assert(S33.x == 0x210);
}
/**********************************/
struct X34 {
int i;
this(this) {
printf("postblit %p\n", &this);
++i;
}
~this() {
printf("dtor %p\n", &this);
}
}
void test34()
{
X34[2] xs;
// xs[0][0] = X34();
printf("foreach\n");
for (int j = 0; j < xs.length; j++) {
j++,j--;
auto x = xs[j];
//foreach(x; xs)
//printf("foreach x.i = %d\n", x[0].i);
//assert(x[0].i == 1);
printf("foreach x.i = %d\n", x.i);
assert(x.i == 1);
}
printf("loop done\n");
}
/**********************************/
struct X35 {
__gshared int k;
int i;
this(this) {
printf("postblit %p\n", &this);
++i;
}
~this() {
printf("dtor %p\n", &this);
k++;
}
}
void test35()
{
{
X35[2] xs;
printf("foreach\n");
foreach(ref x; xs) {
printf("foreach x.i = %d\n", x.i);
assert(x.i == 0);
}
printf("loop done\n");
}
assert(X35.k == 2);
}
/**********************************/
struct X36 {
__gshared int k;
int i;
this(this) {
printf("postblit %p\n", &this);
++i;
}
~this() {
printf("dtor %p\n", &this);
k++;
}
}
void test36()
{
{
X36[2] xs;
printf("foreach\n");
foreach(x; xs) {
printf("foreach x.i = %d\n", x.i);
assert(x.i == 1);
}
printf("loop done\n");
}
assert(X36.k == 4);
}
/**********************************/
struct X37 {
__gshared int k;
int i;
this(this) {
printf("postblit %p\n", &this);
++i;
}
~this() {
printf("dtor %p\n", &this);
k++;
}
}
void test37() {
{
X37[2][3] xs;
printf("foreach\n");
foreach(ref x; xs) {
printf("foreach x.i = %d\n", x[0].i);
assert(x[0].i == 0);
}
printf("loop done\n");
}
assert(X37.k == 6);
}
/**********************************/
struct S38 {
__gshared int count;
__gshared int postblit;
this(int x) {
printf("this(%d)\n", x);
assert(this.x == 0);
this.x = x;
count++;
}
this(this) {
printf("this(this) with %d\n", x);
assert(x == 1 || x == 2);
count++;
postblit++;