This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
lifetime.d
2194 lines (1960 loc) · 48.8 KB
/
lifetime.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
module core.lifetime;
// emplace
/**
Given a pointer `chunk` to uninitialized memory (but already typed
as `T`), constructs an object of non-`class` type `T` at that
address. If `T` is a class, initializes the class reference to null.
Returns: A pointer to the newly constructed object (which is the same
as `chunk`).
*/
T* emplace(T)(T* chunk) @safe pure nothrow
{
import core.internal.lifetime : emplaceRef;
emplaceRef!T(*chunk);
return chunk;
}
///
@system unittest
{
static struct S
{
int i = 42;
}
S[2] s2 = void;
emplace(&s2);
assert(s2[0].i == 42 && s2[1].i == 42);
}
///
@system unittest
{
interface I {}
class K : I {}
K k = void;
emplace(&k);
assert(k is null);
I i = void;
emplace(&i);
assert(i is null);
}
/**
Given a pointer `chunk` to uninitialized memory (but already typed
as a non-class type `T`), constructs an object of type `T` at
that address from arguments `args`. If `T` is a class, initializes
the class reference to `args[0]`.
This function can be `@trusted` if the corresponding constructor of
`T` is `@safe`.
Returns: A pointer to the newly constructed object (which is the same
as `chunk`).
*/
T* emplace(T, Args...)(T* chunk, auto ref Args args)
if (is(T == struct) || Args.length == 1)
{
import core.internal.lifetime : emplaceRef;
emplaceRef!T(*chunk, forward!args);
return chunk;
}
///
@system unittest
{
int a;
int b = 42;
assert(*emplace!int(&a, b) == 42);
}
@system unittest
{
shared int i;
emplace(&i, 42);
assert(i == 42);
}
private @nogc pure nothrow @safe
void testEmplaceChunk(void[] chunk, size_t typeSize, size_t typeAlignment)
{
assert(chunk.length >= typeSize, "emplace: Chunk size too small.");
assert((cast(size_t) chunk.ptr) % typeAlignment == 0, "emplace: Chunk is not aligned.");
}
/**
Given a raw memory area `chunk` (but already typed as a class type `T`),
constructs an object of `class` type `T` at that address. The constructor
is passed the arguments `Args`.
If `T` is an inner class whose `outer` field can be used to access an instance
of the enclosing class, then `Args` must not be empty, and the first member of it
must be a valid initializer for that `outer` field. Correct initialization of
this field is essential to access members of the outer class inside `T` methods.
Note:
This function is `@safe` if the corresponding constructor of `T` is `@safe`.
Returns: The newly constructed object.
*/
T emplace(T, Args...)(T chunk, auto ref Args args)
if (is(T == class))
{
import core.internal.traits : isInnerClass;
static assert(!__traits(isAbstractClass, T), T.stringof ~
" is abstract and it can't be emplaced");
// Initialize the object in its pre-ctor state
enum classSize = __traits(classInstanceSize, T);
(() @trusted => (cast(void*) chunk)[0 .. classSize] = typeid(T).initializer[])();
static if (isInnerClass!T)
{
static assert(Args.length > 0,
"Initializing an inner class requires a pointer to the outer class");
static assert(is(Args[0] : typeof(T.outer)),
"The first argument must be a pointer to the outer class");
chunk.outer = args[0];
alias args1 = args[1..$];
}
else alias args1 = args;
// Call the ctor if any
static if (is(typeof(chunk.__ctor(args1))))
{
// T defines a genuine constructor accepting args
// Go the classic route: write .init first, then call ctor
chunk.__ctor(args1);
}
else
{
static assert(args1.length == 0 && !is(typeof(&T.__ctor)),
"Don't know how to initialize an object of type "
~ T.stringof ~ " with arguments " ~ typeof(args1).stringof);
}
return chunk;
}
///
@safe unittest
{
() @safe {
class SafeClass
{
int x;
@safe this(int x) { this.x = x; }
}
auto buf = new void[__traits(classInstanceSize, SafeClass)];
auto support = (() @trusted => cast(SafeClass)(buf.ptr))();
auto safeClass = emplace!SafeClass(support, 5);
assert(safeClass.x == 5);
class UnsafeClass
{
int x;
@system this(int x) { this.x = x; }
}
auto buf2 = new void[__traits(classInstanceSize, UnsafeClass)];
auto support2 = (() @trusted => cast(UnsafeClass)(buf2.ptr))();
static assert(!__traits(compiles, emplace!UnsafeClass(support2, 5)));
static assert(!__traits(compiles, emplace!UnsafeClass(buf2, 5)));
}();
}
@safe unittest
{
class Outer
{
int i = 3;
class Inner
{
@safe auto getI() { return i; }
}
}
auto outerBuf = new void[__traits(classInstanceSize, Outer)];
auto outerSupport = (() @trusted => cast(Outer)(outerBuf.ptr))();
auto innerBuf = new void[__traits(classInstanceSize, Outer.Inner)];
auto innerSupport = (() @trusted => cast(Outer.Inner)(innerBuf.ptr))();
auto inner = innerSupport.emplace!(Outer.Inner)(outerSupport.emplace!Outer);
assert(inner.getI == 3);
}
/**
Given a raw memory area `chunk`, constructs an object of `class` type `T` at
that address. The constructor is passed the arguments `Args`.
If `T` is an inner class whose `outer` field can be used to access an instance
of the enclosing class, then `Args` must not be empty, and the first member of it
must be a valid initializer for that `outer` field. Correct initialization of
this field is essential to access members of the outer class inside `T` methods.
Preconditions:
`chunk` must be at least as large as `T` needs and should have an alignment
multiple of `T`'s alignment. (The size of a `class` instance is obtained by using
$(D __traits(classInstanceSize, T))).
Note:
This function can be `@trusted` if the corresponding constructor of `T` is `@safe`.
Returns: The newly constructed object.
*/
T emplace(T, Args...)(void[] chunk, auto ref Args args)
if (is(T == class))
{
import core.internal.traits : maxAlignment;
enum classSize = __traits(classInstanceSize, T);
testEmplaceChunk(chunk, classSize, maxAlignment!(void*, typeof(T.tupleof)));
return emplace!T(cast(T)(chunk.ptr), args);
}
///
@system unittest
{
static class C
{
int i;
this(int i){this.i = i;}
}
auto buf = new void[__traits(classInstanceSize, C)];
auto c = emplace!C(buf, 5);
assert(c.i == 5);
}
@system unittest
{
class Outer
{
int i = 3;
class Inner
{
auto getI() { return i; }
}
}
auto outerBuf = new void[__traits(classInstanceSize, Outer)];
auto innerBuf = new void[__traits(classInstanceSize, Outer.Inner)];
auto inner = innerBuf.emplace!(Outer.Inner)(outerBuf.emplace!Outer);
assert(inner.getI == 3);
}
@nogc pure nothrow @safe unittest
{
static class __conv_EmplaceTestClass
{
int i = 3;
this(int i) @nogc @safe pure nothrow
{
assert(this.i == 3 && i == 5);
this.i = i;
}
this(int i, ref int j) @nogc @safe pure nothrow
{
assert(i == 5 && j == 6);
this.i = i;
++j;
}
}
int var = 6;
align(__conv_EmplaceTestClass.alignof) ubyte[__traits(classInstanceSize, __conv_EmplaceTestClass)] buf;
auto support = (() @trusted => cast(__conv_EmplaceTestClass)(buf.ptr))();
auto k = emplace!__conv_EmplaceTestClass(support, 5, var);
assert(k.i == 5);
assert(var == 7);
}
/**
Given a raw memory area `chunk`, constructs an object of non-$(D
class) type `T` at that address. The constructor is passed the
arguments `args`, if any.
Preconditions:
`chunk` must be at least as large
as `T` needs and should have an alignment multiple of `T`'s
alignment.
Note:
This function can be `@trusted` if the corresponding constructor of
`T` is `@safe`.
Returns: A pointer to the newly constructed object.
*/
T* emplace(T, Args...)(void[] chunk, auto ref Args args)
if (!is(T == class))
{
import core.internal.traits : Unqual;
import core.internal.lifetime : emplaceRef;
testEmplaceChunk(chunk, T.sizeof, T.alignof);
emplaceRef!(T, Unqual!T)(*cast(Unqual!T*) chunk.ptr, args);
return cast(T*) chunk.ptr;
}
///
@system unittest
{
struct S
{
int a, b;
}
auto buf = new void[S.sizeof];
S s;
s.a = 42;
s.b = 43;
auto s1 = emplace!S(buf, s);
assert(s1.a == 42 && s1.b == 43);
}
// Bulk of emplace unittests starts here
@system unittest /* unions */
{
static union U
{
string a;
int b;
struct
{
long c;
int[] d;
}
}
U u1 = void;
U u2 = { "hello" };
emplace(&u1, u2);
assert(u1.a == "hello");
}
@system unittest // bugzilla 15772
{
abstract class Foo {}
class Bar: Foo {}
void[] memory;
// test in emplaceInitializer
static assert(!is(typeof(emplace!Foo(cast(Foo*) memory.ptr))));
static assert( is(typeof(emplace!Bar(cast(Bar*) memory.ptr))));
// test in the emplace overload that takes void[]
static assert(!is(typeof(emplace!Foo(memory))));
static assert( is(typeof(emplace!Bar(memory))));
}
@system unittest
{
struct S { @disable this(); }
S s = void;
static assert(!__traits(compiles, emplace(&s)));
emplace(&s, S.init);
}
@system unittest
{
struct S1
{}
struct S2
{
void opAssign(S2);
}
S1 s1 = void;
S2 s2 = void;
S1[2] as1 = void;
S2[2] as2 = void;
emplace(&s1);
emplace(&s2);
emplace(&as1);
emplace(&as2);
}
@system unittest
{
static struct S1
{
this(this) @disable;
}
static struct S2
{
this() @disable;
}
S1[2] ss1 = void;
S2[2] ss2 = void;
emplace(&ss1);
static assert(!__traits(compiles, emplace(&ss2)));
S1 s1 = S1.init;
S2 s2 = S2.init;
static assert(!__traits(compiles, emplace(&ss1, s1)));
emplace(&ss2, s2);
}
@system unittest
{
struct S
{
immutable int i;
}
S s = void;
S[2] ss1 = void;
S[2] ss2 = void;
emplace(&s, 5);
assert(s.i == 5);
emplace(&ss1, s);
assert(ss1[0].i == 5 && ss1[1].i == 5);
emplace(&ss2, ss1);
assert(ss2 == ss1);
}
//Start testing emplace-args here
@system unittest
{
interface I {}
class K : I {}
K k = null, k2 = new K;
assert(k !is k2);
emplace!K(&k, k2);
assert(k is k2);
I i = null;
assert(i !is k);
emplace!I(&i, k);
assert(i is k);
}
@system unittest
{
static struct S
{
int i = 5;
void opAssign(S){assert(0);}
}
S[2] sa = void;
S[2] sb;
emplace(&sa, sb);
assert(sa[0].i == 5 && sa[1].i == 5);
}
//Start testing emplace-struct here
// Test constructor branch
@system unittest
{
struct S
{
double x = 5, y = 6;
this(int a, int b)
{
assert(x == 5 && y == 6);
x = a;
y = b;
}
}
auto s1 = new void[S.sizeof];
auto s2 = S(42, 43);
assert(*emplace!S(cast(S*) s1.ptr, s2) == s2);
assert(*emplace!S(cast(S*) s1, 44, 45) == S(44, 45));
}
@system unittest
{
static struct __conv_EmplaceTest
{
int i = 3;
this(int i)
{
assert(this.i == 3 && i == 5);
this.i = i;
}
this(int i, ref int j)
{
assert(i == 5 && j == 6);
this.i = i;
++j;
}
@disable:
this();
this(this);
void opAssign();
}
__conv_EmplaceTest k = void;
emplace(&k, 5);
assert(k.i == 5);
int var = 6;
__conv_EmplaceTest x = void;
emplace(&x, 5, var);
assert(x.i == 5);
assert(var == 7);
var = 6;
auto z = emplace!__conv_EmplaceTest(new void[__conv_EmplaceTest.sizeof], 5, var);
assert(z.i == 5);
assert(var == 7);
}
// Test matching fields branch
@system unittest
{
struct S { uint n; }
S s;
emplace!S(&s, 2U);
assert(s.n == 2);
}
@safe unittest
{
struct S { int a, b; this(int){} }
S s;
static assert(!__traits(compiles, emplace!S(&s, 2, 3)));
}
@system unittest
{
struct S { int a, b = 7; }
S s1 = void, s2 = void;
emplace!S(&s1, 2);
assert(s1.a == 2 && s1.b == 7);
emplace!S(&s2, 2, 3);
assert(s2.a == 2 && s2.b == 3);
}
//opAssign
@system unittest
{
static struct S
{
int i = 5;
void opAssign(int){assert(0);}
void opAssign(S){assert(0);}
}
S sa1 = void;
S sa2 = void;
S sb1 = S(1);
emplace(&sa1, sb1);
emplace(&sa2, 2);
assert(sa1.i == 1);
assert(sa2.i == 2);
}
//postblit precedence
@system unittest
{
//Works, but breaks in "-w -O" because of @@@9332@@@.
//Uncomment test when 9332 is fixed.
static struct S
{
int i;
this(S other){assert(false);}
this(int i){this.i = i;}
this(this){}
}
S a = void;
assert(is(typeof({S b = a;}))); //Postblit
assert(is(typeof({S b = S(a);}))); //Constructor
auto b = S(5);
emplace(&a, b);
assert(a.i == 5);
static struct S2
{
int* p;
this(const S2){}
}
static assert(!is(immutable S2 : S2));
S2 s2 = void;
immutable is2 = (immutable S2).init;
emplace(&s2, is2);
}
//nested structs and postblit
@system unittest
{
static struct S
{
int* p;
this(int i){p = [i].ptr;}
this(this)
{
if (p)
p = [*p].ptr;
}
}
static struct SS
{
S s;
void opAssign(const SS)
{
assert(0);
}
}
SS ssa = void;
SS ssb = SS(S(5));
emplace(&ssa, ssb);
assert(*ssa.s.p == 5);
assert(ssa.s.p != ssb.s.p);
}
//disabled postblit
@system unittest
{
static struct S1
{
int i;
@disable this(this);
}
S1 s1 = void;
emplace(&s1, 1);
assert(s1.i == 1);
static assert(!__traits(compiles, emplace(&s1, s1))); // copy disabled
static assert(__traits(compiles, emplace(&s1, move(s1)))); // move not affected
static struct S2
{
int i;
@disable this(this);
this(ref S2){}
}
S2 s2 = void;
static assert(!__traits(compiles, emplace(&s2, 1)));
emplace(&s2, S2.init);
static struct SS1
{
S1 s;
}
SS1 ss1 = void;
emplace(&ss1);
static assert(!__traits(compiles, emplace(&ss1, ss1))); // copying disabled
static assert(__traits(compiles, emplace(&ss1, move(ss1)))); // move unaffected
static struct SS2
{
S2 s;
}
SS2 ss2 = void;
emplace(&ss2);
static assert(!__traits(compiles, emplace(&ss2, ss2))); // copying disabled
static assert(__traits(compiles, emplace(&ss2, SS2.init))); // move is OK
// SS1 sss1 = s1; //This doesn't compile
// SS1 sss1 = SS1(s1); //This doesn't compile
// So emplace shouldn't compile either
static assert(!__traits(compiles, emplace(&sss1, s1)));
static assert(!__traits(compiles, emplace(&sss2, s2)));
}
//Imutability
@system unittest
{
//Castable immutability
{
static struct S1
{
int i;
}
static assert(is( immutable(S1) : S1));
S1 sa = void;
auto sb = immutable(S1)(5);
emplace(&sa, sb);
assert(sa.i == 5);
}
//Un-castable immutability
{
static struct S2
{
int* p;
}
static assert(!is(immutable(S2) : S2));
S2 sa = void;
auto sb = immutable(S2)(null);
assert(!__traits(compiles, emplace(&sa, sb)));
}
}
@system unittest
{
static struct S
{
immutable int i;
immutable(int)* j;
}
S s = void;
emplace(&s, 1, null);
emplace(&s, 2, &s.i);
assert(s is S(2, &s.i));
}
//Context pointer
@system unittest
{
int i = 0;
{
struct S1
{
void foo(){++i;}
}
S1 sa = void;
S1 sb;
emplace(&sa, sb);
sa.foo();
assert(i == 1);
}
{
struct S2
{
void foo(){++i;}
this(this){}
}
S2 sa = void;
S2 sb;
emplace(&sa, sb);
sa.foo();
assert(i == 2);
}
}
//Alias this
@system unittest
{
static struct S
{
int i;
}
//By Ref
{
static struct SS1
{
int j;
S s;
alias s this;
}
S s = void;
SS1 ss = SS1(1, S(2));
emplace(&s, ss);
assert(s.i == 2);
}
//By Value
{
static struct SS2
{
int j;
S s;
S foo() @property{return s;}
alias foo this;
}
S s = void;
SS2 ss = SS2(1, S(2));
emplace(&s, ss);
assert(s.i == 2);
}
}
version (CoreUnittest)
{
//Ambiguity
private struct __std_conv_S
{
int i;
this(__std_conv_SS ss) {assert(0);}
static opCall(__std_conv_SS ss)
{
__std_conv_S s; s.i = ss.j;
return s;
}
}
private struct __std_conv_SS
{
int j;
__std_conv_S s;
ref __std_conv_S foo() return @property {s.i = j; return s;}
alias foo this;
}
}
@system unittest
{
static assert(is(__std_conv_SS : __std_conv_S));
__std_conv_S s = void;
__std_conv_SS ss = __std_conv_SS(1);
__std_conv_S sTest1 = ss; //this calls "SS alias this" (and not "S.this(SS)")
emplace(&s, ss); //"alias this" should take precedence in emplace over "opCall"
assert(s.i == 1);
}
//Nested classes
@system unittest
{
class A{}
static struct S
{
A a;
}
S s1 = void;
S s2 = S(new A);
emplace(&s1, s2);
assert(s1.a is s2.a);
}
//safety & nothrow & CTFE
@system unittest
{
//emplace should be safe for anything with no elaborate opassign
static struct S1
{
int i;
}
static struct S2
{
int i;
this(int j)@safe nothrow{i = j;}
}
int i;
S1 s1 = void;
S2 s2 = void;
auto pi = &i;
auto ps1 = &s1;
auto ps2 = &s2;
void foo() @safe nothrow
{
emplace(pi);
emplace(pi, 5);
emplace(ps1);
emplace(ps1, 5);
emplace(ps1, S1.init);
emplace(ps2);
emplace(ps2, 5);
emplace(ps2, S2.init);
}
foo();
T bar(T)() @property
{
T t/+ = void+/; //CTFE void illegal
emplace(&t, 5);
return t;
}
// CTFE
enum a = bar!int;
static assert(a == 5);
enum b = bar!S1;
static assert(b.i == 5);
enum c = bar!S2;
static assert(c.i == 5);
// runtime
auto aa = bar!int;
assert(aa == 5);
auto bb = bar!S1;
assert(bb.i == 5);
auto cc = bar!S2;
assert(cc.i == 5);
}
@system unittest
{
struct S
{
int[2] get(){return [1, 2];}
alias get this;
}
struct SS
{
int[2] ii;
}
struct ISS
{
int[2] ii;
}
S s;
SS ss = void;
ISS iss = void;
emplace(&ss, s);
emplace(&iss, s);
assert(ss.ii == [1, 2]);
assert(iss.ii == [1, 2]);
}
//disable opAssign
@system unittest
{
static struct S
{
@disable void opAssign(S);
}
S s;
emplace(&s, S.init);
}
//opCall
@system unittest
{
int i;
//Without constructor
{
static struct S1
{
int i;
static S1 opCall(int*){assert(0);}
}
S1 s = void;
static assert(!__traits(compiles, emplace(&s, 1)));
}
//With constructor
{
static struct S2
{
int i = 0;
static S2 opCall(int*){assert(0);}
static S2 opCall(int){assert(0);}
this(int i){this.i = i;}
}
S2 s = void;
emplace(&s, 1);
assert(s.i == 1);
}
//With postblit ambiguity
{
static struct S3
{
int i = 0;
static S3 opCall(ref S3){assert(0);}
}
S3 s = void;
emplace(&s, S3.init);
}
}
/+ these tests can't be performed in druntime, but a mirror still exists in phobos...
@safe unittest //@@@9559@@@
{
import std.algorithm.iteration : map;
import std.array : array;
import std.typecons : Nullable;
alias I = Nullable!int;
auto ints = [0, 1, 2].map!(i => i & 1 ? I.init : I(i))();
auto asArray = array(ints);
}
@system unittest //http://forum.dlang.org/post/nxbdgtdlmwscocbiypjs@forum.dlang.org
{
import std.array : array;
import std.datetime : SysTime, UTC;
import std.math : isNaN;
static struct A
{
double i;
}
static struct B
{
invariant()
{
if (j == 0)
assert(a.i.isNaN(), "why is 'j' zero?? and i is not NaN?");
else
assert(!a.i.isNaN());
}
SysTime when; // comment this line avoid the breakage
int j;
A a;
}
B b1 = B.init;
assert(&b1); // verify that default eyes invariants are ok;
auto b2 = B(SysTime(0, UTC()), 1, A(1));
assert(&b2);
auto b3 = B(SysTime(0, UTC()), 1, A(1));
assert(&b3);
auto arr = [b2, b3];
assert(arr[0].j == 1);
assert(arr[1].j == 1);
auto a2 = arr.array(); // << bang, invariant is raised, also if b2 and b3 are good
}
+/
//static arrays
@system unittest
{
static struct S
{
int[2] ii;
}
static struct IS
{
immutable int[2] ii;
}
int[2] ii;
S s = void;
IS ims = void;
ubyte ub = 2;
emplace(&s, ub);
emplace(&s, ii);
emplace(&ims, ub);
emplace(&ims, ii);
uint[2] uu;
static assert(!__traits(compiles, {S ss = S(uu);}));
static assert(!__traits(compiles, emplace(&s, uu)));