1 import std.stdio;
2 
3 struct S
4 {
5     int x;
6     int y;
7 }
8 
9 /********************************************/
10 
test1()11 void test1()
12 {
13     S s = S(1,2);
14     assert(s.x == 1);
15     assert(s.y == 2);
16 }
17 
18 /********************************************/
19 
foo2(S s)20 void foo2(S s)
21 {
22     assert(s.x == 1);
23     assert(s.y == 2);
24 }
25 
test2()26 void test2()
27 {
28     foo2( S(1,2) );
29 }
30 
31 /********************************************/
32 
foo3()33 S foo3()
34 {
35     return S(1, 2);
36 }
37 
test3()38 void test3()
39 {
40     S s = foo3();
41     assert(s.x == 1);
42     assert(s.y == 2);
43 }
44 
45 /********************************************/
46 
47 struct S4
48 {
49     long x;
50     long y;
51     long z;
52 }
53 
foo4()54 S4 foo4()
55 {
56     return S4(1, 2, 3);
57 }
58 
test4()59 void test4()
60 {
61     S4 s = foo4();
62     assert(s.x == 1);
63     assert(s.y == 2);
64     assert(s.z == 3);
65 }
66 
67 /********************************************/
68 
69 struct S5
70 {
71     long x;
72     char y;
73     long z;
74 }
75 
foo5()76 S5 foo5()
77 {
78     return S5(1, 2, 3);
79 }
80 
test5()81 void test5()
82 {
83     S5 s = foo5();
84     assert(s.x == 1);
85     assert(s.y == 2);
86     assert(s.z == 3);
87 }
88 
89 /********************************************/
90 
91 struct S6
92 {
93     long x;
94     char y;
95     long z;
96 }
97 
test6()98 void test6()
99 {
100     S6 s1 = S6(1,2,3);
101     S6 s2 = S6(1,2,3);
102     assert(s1 == s2);
103 
104     s1 = S6(4,5,6);
105     s2 = S6(4,5,6);
106     assert(s1 == s2);
107 
108     S6* p1 = &s1;
109     S6* p2 = &s2;
110     *p1 = S6(7,8,9);
111     *p2 = S6(7,8,9);
112     assert(*p1 == *p2);
113 }
114 
115 /********************************************/
116 
117 struct S7
118 {
119     long x;
120     char y;
121     long z;
122 }
123 
test7()124 void test7()
125 {
126     static S7 s1 = S7(1,2,3);
127     static S7 s2 = S7(1,2,3);
128     assert(s1 == s2);
129 }
130 
131 /********************************************/
132 
133 struct S8
134 {
135     int i;
136     string s;
137 }
138 
test8()139 void test8()
140 {
141     S8 s = S8(3, "hello");
142     assert(s.i == 3);
143     assert(s.s == "hello");
144 
145     static S8 t = S8(4, "betty");
146     assert(t.i == 4);
147     assert(t.s == "betty");
148 
149     S8 u = S8(3, ['h','e','l','l','o']);
150     assert(u.i == 3);
151     assert(u.s == "hello");
152 
153     static S8 v = S8(4, ['b','e','t','t','y']);
154     assert(v.i == 4);
155     assert(v.s == "betty");
156 }
157 
158 /********************************************/
159 
160 struct S9
161 {
162     int i;
163     char[5] s;
164 }
165 
test9()166 void test9()
167 {
168     S9 s = S9(3, "hello");
169     assert(s.i == 3);
170     assert(s.s == "hello");
171 
172     static S9 t = S9(4, "betty");
173     assert(t.i == 4);
174     assert(t.s == "betty");
175 
176     S9 u = S9(3, ['h','e','l','l','o']);
177     assert(u.i == 3);
178     assert(u.s == "hello");
179 
180     static S9 v = S9(4, ['b','e','t','t','y']);
181     assert(v.i == 4);
182     assert(v.s == "betty");
183 }
184 
185 /********************************************/
186 
187 alias int myint10;
188 
189 struct S10
190 {
191     int i;
192     union
193     {
194         int x = 2;
195         int y;
196     }
197     int j = 3;
198     myint10 k = 4;
199 }
200 
test10()201 void test10()
202 {
203     S10 s = S10( 1 );
204     assert(s.i == 1);
205     assert(s.x == 2);
206     assert(s.y == 2);
207     assert(s.j == 3);
208     assert(s.k == 4);
209 
210     static S10 t = S10( 1 );
211     assert(t.i == 1);
212     assert(t.x == 2);
213     assert(t.y == 2);
214     assert(t.j == 3);
215     assert(t.k == 4);
216 
217     S10 u = S10( 1, 5 );
218     assert(u.i == 1);
219     assert(u.x == 5);
220     assert(u.y == 5);
221     assert(u.j == 3);
222     assert(u.k == 4);
223 
224     static S10 v = S10( 1, 6 );
225     assert(v.i == 1);
226     assert(v.x == 6);
227     assert(v.y == 6);
228     assert(v.j == 3);
229     assert(v.k == 4);
230 }
231 
232 /********************************************/
233 
234 struct S11
235 {
236     int i;
237     int j = 3;
238 }
239 
240 
test11()241 void test11()
242 {
243     static const s = S11( 1, 5 );
244     static const i = s.i;
245     assert(i == 1);
246     static assert(s.j == 5);
247 }
248 
249 /********************************************/
250 
251 struct S12
252 {
253     int[5] x;
254     int[5] y = 3;
255 }
256 
test12()257 void test12()
258 {
259     S12 s = S12();
260     foreach (v; s.x)
261         assert(v == 0);
262     foreach (v; s.y)
263         assert(v == 3);
264 }
265 
266 /********************************************/
267 
268 struct S13
269 {
270     int[5] x;
271     int[5] y;
272     int[6][3] z;
273 }
274 
test13()275 void test13()
276 {
277     S13 s = S13(0,3,4);
278     foreach (v; s.x)
279         assert(v == 0);
280     foreach (v; s.y)
281         assert(v == 3);
282     for (int i = 0; i < 6; i++)
283     {
284         for (int j = 0; j < 3; j++)
285         {
286             assert(s.z[j][i] == 4);
287         }
288     }
289 }
290 
291 /********************************************/
292 
293 struct S14a { int n; }
this(int n)294 struct S14b { this(int n){} }
295 
foo14(ref S14a s)296 void foo14(ref S14a s) {}
foo14(ref S14b s)297 void foo14(ref S14b s) {}
hoo14()298 void hoo14()(ref S14a s) {}
hoo14()299 void hoo14()(ref S14b s) {}
poo14(S)300 void poo14(S)(ref S s) {}
301 
bar14(S14a s)302 void bar14(S14a s) {}
bar14(S14b s)303 void bar14(S14b s) {}
var14()304 void var14()(S14a s) {}
var14()305 void var14()(S14b s) {}
war14(S)306 void war14(S)(S s) {}
307 
baz14(S14a s)308 int baz14(    S14a s) { return 1; }
baz14(ref S14a s)309 int baz14(ref S14a s) { return 2; }
baz14(S14b s)310 int baz14(    S14b s) { return 1; }
baz14(ref S14b s)311 int baz14(ref S14b s) { return 2; }
vaz14()312 int vaz14()(    S14a s) { return 1; }
vaz14()313 int vaz14()(ref S14a s) { return 2; }
vaz14()314 int vaz14()(    S14b s) { return 1; }
vaz14()315 int vaz14()(ref S14b s) { return 2; }
waz14(S)316 int waz14(S)(    S s) { return 1; }
waz14(S)317 int waz14(S)(ref S s) { return 2; }
318 
test14()319 void test14()
320 {
321     // can not bind rvalue-sl with ref
322     static assert(!__traits(compiles, foo14(S14a(0))));
323     static assert(!__traits(compiles, foo14(S14b(0))));
324     static assert(!__traits(compiles, hoo14(S14a(0))));
325     static assert(!__traits(compiles, hoo14(S14b(0))));
326     static assert(!__traits(compiles, poo14(S14a(0))));
327     static assert(!__traits(compiles, poo14(S14b(0))));
328 
329     // still can bind rvalue-sl with non-ref
330     bar14(S14a(0));
331     bar14(S14b(0));
332     var14(S14a(0));
333     var14(S14b(0));
334     war14(S14a(0));
335     war14(S14b(0));
336 
337     // preferred binding of rvalue-sl in overload resolution
338     assert(baz14(S14a(0)) == 1);
339     assert(baz14(S14b(0)) == 1);
340     assert(vaz14(S14a(0)) == 1);
341     assert(vaz14(S14b(0)) == 1);
342     assert(waz14(S14a(0)) == 1);
343     assert(waz14(S14b(0)) == 1);
344 }
345 
346 /********************************************/
347 
check15(T,ubyte results,A...)348 void check15(T, ubyte results, A...)(A args)
349 {
350                          // m c i s sc
351     enum m  = (results & 0b_1_0_0_0_0) != 0;
352     enum c  = (results & 0b_0_1_0_0_0) != 0;
353     enum i  = (results & 0b_0_0_1_0_0) != 0;
354     enum s  = (results & 0b_0_0_0_1_0) != 0;
355     enum sc = (results & 0b_0_0_0_0_1) != 0;
356 
357     // allocation on stack
358     static assert((is(typeof(                  T(args) ) U) && is(U ==              T  )) == m);
359     static assert((is(typeof(            const T(args) ) U) && is(U ==        const(T) )) == c);
360     static assert((is(typeof(        immutable T(args) ) U) && is(U ==    immutable(T) )) == i);
361     static assert((is(typeof(           shared T(args) ) U) && is(U ==       shared(T) )) == s);
362     static assert((is(typeof(     shared const T(args) ) U) && is(U == shared(const T) )) == sc);
363 
364     // allocation on heap
365     static assert((is(typeof( new              T(args) ) U) && is(U ==              T *)) == m);
366     static assert((is(typeof( new        const T(args) ) U) && is(U ==        const(T)*)) == c);
367     static assert((is(typeof( new    immutable T(args) ) U) && is(U ==    immutable(T)*)) == i);
368     static assert((is(typeof( new       shared T(args) ) U) && is(U ==       shared(T)*)) == s);
369     static assert((is(typeof( new shared const T(args) ) U) && is(U == shared(const T)*)) == sc);
370 }
test15a()371 void test15a()
372 {
373     static struct Foo1 { this(int v) {}                int value; }
374     static struct Boo1 { this(int v) const {}          int[] value; }
375     static struct Bar1 { this(int[] v) {}              int[] value; }
376     static struct Baz1 { this(const int[] v) pure {}   int[] value; }  // unique ctor
377     static struct Coo1 { this(int[] v) immutable {}    int[] value; }
378     static struct Car1 { this(int[] v) immutable {}    immutable(int)[] value; }
379     check15!(Foo1, 0b_1_1_0_0_0)(1);
380     check15!(Boo1, 0b_0_1_0_0_0)(1);
381     check15!(Bar1, 0b_1_1_0_0_0)(null);
382     check15!(Baz1, 0b_1_1_1_1_1)(null);
383     check15!(Coo1, 0b_0_1_1_0_1)(null);
384     check15!(Car1, 0b_0_1_1_0_1)(null);
385                    // m c i s sc
386 
387     // Template constructor should work as same as non-template ones
388     static struct Foo2 { this()(int v) {}              int value; }
389     static struct Boo2 { this()(int v) const {}        int[] value; }
390     static struct Bar2 { this()(int[] v) {}            int[] value; }  // has mutable indieection
391     static struct Baz2 { this()(const int[] v) pure {} int[] value; }  // unique ctor
392     static struct Coo2 { this()(int[] v) immutable {}  int[] value; }
393     static struct Car2 { this()(int[] v) immutable {}  immutable(int)[] value; }
394     check15!(Foo2, 0b_1_1_0_0_0)(1);
395     check15!(Boo2, 0b_0_1_0_0_0)(1);
396     check15!(Bar2, 0b_1_1_0_0_0)(null);
397     check15!(Baz2, 0b_1_1_1_1_1)(null);
398     check15!(Coo2, 0b_0_1_1_0_1)(null);
399     check15!(Car2, 0b_0_1_1_0_1)(null);
400                    // m c i s sc
401 
402     // Except Bar!().__ctor, their constructors are inferred to pure, then they become unique ctors.
403     static struct Foo3() { this(int v) {}              int value; }
404     static struct Boo3() { this(int v) const {}        int[] value; }
405     static struct Bar3() { this(int[] v) {}            int[] value; }  // has mutable indieection
406     static struct Baz3() { this(const int[] v) pure {} int[] value; }  // unique ctor
407     static struct Coo3() { this(int[] v) immutable {}  int[] value; }
408     static struct Car3() { this(int[] v) immutable {}  immutable(int)[] value; }
409     check15!(Foo3!(), 0b_1_1_1_1_1)(1);
410     check15!(Boo3!(), 0b_1_1_1_1_1)(1);
411     check15!(Bar3!(), 0b_1_1_0_0_0)(null);
412     check15!(Baz3!(), 0b_1_1_1_1_1)(null);
413     check15!(Coo3!(), 0b_1_1_1_1_1)(null);
414     check15!(Car3!(), 0b_1_1_1_1_1)(null);
415                       // m c i s sc
416 }
417 
418 // inout constructor works as like unique constructor in many cases
test15b()419 void test15b()
420 {
421     static struct Nullable1
422     {
423         private int[] _value;
424         private bool _isNull = true;
425         this(inout int[] v) inout //pure
426         {
427             _value = v;
428             //static int g; auto x = g; // impure access
429             _isNull = false;
430         }
431     }
432     static assert( __traits(compiles,           Nullable1([1,2,3])));
433     static assert(!__traits(compiles,           Nullable1([1,2,3].idup)));
434     static assert(!__traits(compiles, immutable Nullable1([1,2,3])));
435     static assert( __traits(compiles, immutable Nullable1([1,2,3].idup)));
436     static assert(!__traits(compiles,    shared Nullable1([1,2,3])));
437     static assert(!__traits(compiles,    shared Nullable1([1,2,3].idup)));
438 
439     static struct Nullable2(T)
440     {
441         private T _value;
442         private bool _isNull = true;
443         this(inout T v) inout //pure
444         {
445             _value = v;
446             //static int g; auto x = g; // impure access
447             _isNull = false;
448         }
449     }
450     static assert( __traits(compiles,           Nullable2!(int[])([1,2,3])));
451     static assert(!__traits(compiles,           Nullable2!(int[])([1,2,3].idup)));
452     static assert(!__traits(compiles, immutable Nullable2!(int[])([1,2,3])));
453     static assert( __traits(compiles, immutable Nullable2!(int[])([1,2,3].idup)));
454     static assert(!__traits(compiles,    shared Nullable2!(int[])([1,2,3])));
455     static assert(!__traits(compiles,    shared Nullable2!(int[])([1,2,3].idup)));
456 
457     // ctor is inout pure, but cannot create unique object.
458     struct S
459     {
460         int[] marr;
461         const int[] carr;
462         immutable int[] iarr;
463         this(int[] m, const int[] c, immutable int[] i) inout pure
464         {
465             static assert(!__traits(compiles, marr = m));
466             static assert(!__traits(compiles, carr = c));  // cannot implicitly convertible const(int[]) to inout(const(int[]))
467             iarr = i;
468         }
469     }
470     static assert(!__traits(compiles, { int[] ma; immutable int[] ia; auto m =           S(ma, ma, ia); }));
471     static assert( __traits(compiles, { int[] ma; immutable int[] ia; auto c =     const S(ma, ma, ia); }));
472     static assert(!__traits(compiles, { int[] ma; immutable int[] ia; auto i = immutable S(ma, ma, ia); }));
473 }
474 
475 // TemplateThisParameter with constructor should work
test15c()476 void test15c()
477 {
478     static class C
479     {
480         this(this This)()
481         {
482             static assert(is(This == immutable C));
483         }
484 
485         this(T = void, this This)(int)
486         {
487             static assert(is(This == immutable C));
488         }
489     }
490     auto c1 = new immutable C;
491     auto c2 = new immutable C(1);
492 }
493 
test15d()494 void test15d()  // Bugzilla 9974
495 {
496     class CM { this() {} }
497     auto cm = new CM();
498 
499     const class CC { this() {} }
500     const cc = new const CC();
501 
502     immutable class CI { this() {} }
503     immutable ci = new immutable CI();
504 
505     shared class CS { this() {} }
506     shared cs = new shared CS();
507 
508     shared const class CSC { this() {} }
509     shared const csc = new shared const CSC();
510 
511 
512     struct SM { this(int) {} }
513     auto sm = new SM(1);
514 
515     const struct SC { this(int) {} }
516     const sc = new const SC(1);
517 
518     immutable struct SI { this(int) {} }
519     immutable si = new immutable SI(1);
520 
521     shared struct SS { this(int) {} }
522     shared ss = new shared SS(1);
523 
524     shared const struct SSC { this(int) {} }
525     shared const ssc = new shared const SSC(1);
526 }
527 
test15e()528 void test15e()  // Bugzilla 10005
529 {
530     // struct literal
531     static struct S
532     {
533         int[] a;
534     }
535     int[] marr = [1,2,3];
536     static assert( __traits(compiles, {           S m =           S(marr); }));
537     static assert( __traits(compiles, {     const S c =           S(marr); }));
538     static assert(!__traits(compiles, { immutable S i =           S(marr); }));
539     immutable int[] iarr = [1,2,3];
540     static assert(!__traits(compiles, {           S m = immutable S(iarr); }));
541     static assert( __traits(compiles, {     const S c = immutable S(iarr); }));
542     static assert( __traits(compiles, { immutable S i = immutable S(iarr); }));
543 
544     // mutable constructor
545     static struct MS
546     {
547         int[] a;
548         this(int n) { a = new int[](n); }
549     }
550     static assert( __traits(compiles, {           MS m =           MS(3); }));
551     static assert( __traits(compiles, {     const MS c =           MS(3); }));
552     static assert(!__traits(compiles, { immutable MS i =           MS(3); }));
553     static assert(!__traits(compiles, {           MS m = immutable MS(3); }));
554     static assert(!__traits(compiles, {     const MS c = immutable MS(3); }));
555     static assert(!__traits(compiles, { immutable MS i = immutable MS(3); }));
556 
557     // immutable constructor
558     static struct IS
559     {
560         int[] a;
561         this(int n) immutable { a = new int[](n); }
562     }
563     static assert(!__traits(compiles, {           IS m =           IS(3); }));
564     static assert(!__traits(compiles, {     const IS c =           IS(3); }));
565     static assert(!__traits(compiles, { immutable IS i =           IS(3); }));
566     static assert(!__traits(compiles, {           IS m = immutable IS(3); }));
567     static assert( __traits(compiles, {     const IS c = immutable IS(3); }));
568     static assert( __traits(compiles, { immutable IS i = immutable IS(3); }));
569 }
570 
571 struct Foo9984
572 {
573     int[] p;
574     // Prefix storage class and tempalte constructor
thisFoo9984575     inout this()(inout int[] a) { p = a; }
fooFoo9984576     auto foo() inout { return inout(Foo9984)(p); }
577 }
578 
test9993a()579 void test9993a()
580 {
581     static class A
582     {
583         int x;
584         this()           { x = 13; }
585         this() immutable { x = 42; }
586     }
587               A ma = new           A;   assert(ma.x == 13);
588     immutable A ia = new immutable A;   assert(ia.x == 42);
589     static assert(!__traits(compiles, { immutable A ia = new A; }));
590 
591     static class B
592     {
593         int x;
594         this()       { x = 13; }
595         this() const { x = 42; }
596     }
597     const B mb = new       B;           assert(mb.x == 13);
598     const B cb = new const B;           assert(cb.x == 42);
599     static assert(!__traits(compiles, { immutable B ib = new B; }));
600 
601     static class C
602     {
603         int x;
604         this() const     { x = 13; }
605         this() immutable { x = 42; }
606     }
607         const C cc = new     const C;   assert(cc.x == 13);
608     immutable C ic = new immutable C;   assert(ic.x == 42);
609     static assert(!__traits(compiles, { C mc = new C; }));
610 }
test9993b()611 void test9993b()
612 {
613     static class A
614     {
615         int x;
616         this()()           { x = 13; }
617         this()() immutable { x = 42; }
618     }
619               A ma = new           A;   assert(ma.x == 13);
620     immutable A ia = new immutable A;   assert(ia.x == 42);
621     static assert(__traits(compiles, { immutable A ia = new A; }));
622 
623     static class B
624     {
625         int x;
626         this()()       { x = 13; }
627         this()() const { x = 42; }
628     }
629     const B mb = new       B;           assert(mb.x == 13);
630     const B cb = new const B;           assert(cb.x == 42);
631     static assert(__traits(compiles, { immutable B ib = new B; }));
632 
633     static class C
634     {
635         int x;
636         this()() const     { x = 13; }
637         this()() immutable { x = 42; }
638     }
639         const C cc = new     const C;   assert(cc.x == 13);
640     immutable C ic = new immutable C;   assert(ic.x == 42);
641     static assert(!__traits(compiles, { C mc = new C; }));
642 }
643 
644 /********************************************/
645 // 1914
646 
647 struct Bug1914a
648 {
649     const char[10] i = [1,0,0,0,0,0,0,0,0,0];
650     char[10] x = i;
651     int y = 5;
652 }
653 
654 struct Bug1914b
655 {
656     const char[10] i = [0,0,0,0,0,0,0,0,0,0];
657     char[10] x = i;
658     int y = 5;
659 }
660 
661 struct Bug1914c
662 {
663     const char[2] i = ['a', 'b'];
664     const char[2][3] j = [['x', 'y'], ['p', 'q'], ['r', 's']];
665     const char[2][3] k = ["cd", "ef", "gh"];
666     const char[2][3] l = [['x', 'y'], ['p'], ['h', 'k']];
667     char[2][3] x = i;
668     int y = 5;
669     char[2][3] z = j;
670     char[2][3] w = k;
671     int v = 27;
672     char[2][3] u = l;
673     int t = 718;
674 }
675 
676 struct T3198
677 {
678     int g = 1;
679 }
680 
681 class Foo3198
682 {
683     int[5] x = 6;
684     T3198[5] y = T3198(4);
685 }
686 
test3198and1914()687 void test3198and1914()
688 {
689     Bug1914a a;
690     assert(a.y == 5, "bug 1914, non-zero init");
691     Bug1914b b;
692     assert(b.y == 5, "bug 1914, zero init");
693     Bug1914c c;
694     assert(c.y == 5, "bug 1914, multilevel init");
695     assert(c.v == 27, "bug 1914, multilevel init2");
696     assert(c.x[2][1] == 'b');
697     assert(c.t == 718, "bug 1914, multi3");
698     assert(c.u[1][0] == 'p');
699     assert(c.u[1][1] == char.init);
700     auto f = new Foo3198();
701     assert(f.x[0] == 6);
702     assert(f.y[0].g == 4, "bug 3198");
703 }
704 
705 /********************************************/
706 // 14996
707 
708 enum E14996a : string { confirm = "confirm" }
709 enum E14996b : long[] { confirm = [1,2,3,4] }
710 
711 struct S14996
712 {
713     E14996a[1] data1;
714     E14996b[1] data2;
715 }
716 
717 /********************************************/
718 // 2427
719 
test2427()720 void test2427()
721 {
722     struct S
723     {
724         int x;
725     }
726 
727     int foo(int i)
728     {
729         return i;
730     }
731 
732     int i;
733     S s = { foo(i) };
734 }
735 
736 /********************************************/
737 
738 struct T5885 {
739     uint a, b;
740 }
741 
mulUintToDouble(T5885 t,double m)742 double mulUintToDouble(T5885 t, double m) {
743     return t.a * m;
744 }
745 
test5885()746 void test5885()
747 {
748     enum ae = mulUintToDouble(T5885(10, 0), 10.0);
749     enum be = mulUintToDouble(T5885(10, 20), 10.0);
750     static assert(ae == be);
751 
752     auto a = mulUintToDouble(T5885(10, 0), 10.0);
753     auto b = mulUintToDouble(T5885(10, 20), 10.0);
754     assert(a == b);
755 }
756 
757 /********************************************/
758 // 5889
759 
760 struct S5889a { int n; }
this(int n)761 struct S5889b { this(int n){} }
762 
isLvalue(S)763 bool isLvalue(S)(auto ref S s){ return __traits(isRef, s); }
764 
foo(ref S5889a s)765 int foo(ref S5889a s) { return 1; }
foo(S5889a s)766 int foo(    S5889a s) { return 2; }
foo(ref S5889b s)767 int foo(ref S5889b s) { return 1; }
foo(S5889b s)768 int foo(    S5889b s) { return 2; }
769 
goo(ref const (S5889a)s)770 int goo(ref const(S5889a) s) { return 1; }
goo(const (S5889a)s)771 int goo(    const(S5889a) s) { return 2; }
goo(ref const (S5889b)s)772 int goo(ref const(S5889b) s) { return 1; }
goo(const (S5889b)s)773 int goo(    const(S5889b) s) { return 2; }
774 
too(S)775 int too(S)(ref S s) { return 1; }
too(S)776 int too(S)(    S s) { return 2; }
777 
makeRvalue(S)778 S makeRvalue(S)(){ S s; return s; }
779 
test5889()780 void test5889()
781 {
782     S5889a sa;
783     S5889b sb;
784 
785     assert( isLvalue(sa));
786     assert( isLvalue(sb));
787     assert(!isLvalue(S5889a(0)));
788     assert(!isLvalue(S5889b(0)));
789     assert(!isLvalue(makeRvalue!S5889a()));
790     assert(!isLvalue(makeRvalue!S5889b()));
791 
792     assert(foo(sa) == 1);
793     assert(foo(sb) == 1);
794     assert(foo(S5889a(0)) == 2);
795     assert(foo(S5889b(0)) == 2);
796     assert(foo(makeRvalue!S5889a()) == 2);
797     assert(foo(makeRvalue!S5889b()) == 2);
798 
799     assert(goo(sa) == 1);
800     assert(goo(sb) == 1);
801     assert(goo(S5889a(0)) == 2);
802     assert(goo(S5889b(0)) == 2);
803     assert(goo(makeRvalue!S5889a()) == 2);
804     assert(goo(makeRvalue!S5889b()) == 2);
805 
806     assert(too(sa) == 1);
807     assert(too(sb) == 1);
808     assert(too(S5889a(0)) == 2);
809     assert(too(S5889b(0)) == 2);
810     assert(too(makeRvalue!S5889a()) == 2);
811     assert(too(makeRvalue!S5889b()) == 2);
812 }
813 
814 /********************************************/
815 // 4147
816 
817 struct S4247
818 {
819     int n = 1024;
thisS4247820     this(int x) { n = x; }
821 }
test4247()822 void test4247()
823 {
824     auto p1 = S4247();
825     assert(p1.n == 1024);
826 
827     auto p2 = S4247(1);
828     assert(p2.n == 1);
829 }
830 
831 /********************************************/
832 // 6937
833 
test6937()834 void test6937()
835 {
836     static struct S
837     {
838         int x, y;
839     }
840 
841     auto s1 = S(1, 2);
842     auto ps1 = new S(1, 2);
843     assert(ps1.x == 1);
844     assert(ps1.y == 2);
845     assert(*ps1 == s1);
846 
847     auto ps2 = new S(1);
848     assert(ps2.x == 1);
849     assert(ps2.y == 0);
850     assert(*ps2 == S(1, 0));
851 
852     static assert(!__traits(compiles, new S(1,2,3)));
853 
854     int v = 0;
855     struct NS
856     {
857         int x;
858         void foo() { v = x; }
859     }
860     auto ns = NS(1);
861     ns.foo();
862     assert(ns.x == 1);
863     assert(v == 1);
864     auto pns = new NS(2);
865     assert(pns.x == 2);
866     pns.foo();
867     assert(v == 2);
868     pns.x = 1;
869     assert(*pns == ns);
870 
871     static struct X {
872         int v;
873         this(this) { ++v; }
874     }
875     static struct Y {
876         X x;
877     }
878     Y y = Y(X(1));
879     assert(y.x.v == 1);
880     auto py1 = new Y(X(1));
881     assert(py1.x.v == 1);
882     assert(*py1 == y);
883     auto py2 = new Y(y.x);
884     assert(py2.x.v == 2);
885 }
886 
887 /********************************************/
888 // 12681
889 
890 struct HasUnion12774
891 {
892     union
893     {
894         int a, b;
895     }
896 }
897 
898 bool test12681()
899 {
900     immutable int x = 42;
901 
902     static struct S1
903     {
904         immutable int *p;
905     }
906     immutable s1 = new S1(&x);
907     assert(s1.p == &x);
908 
909     struct S2
910     {
911         immutable int *p;
912         void foo() {}
913     }
914     auto s2 = new S2(&x);
915     assert(s2.p == &x);
916 
917     struct S3
918     {
919         immutable int *p;
920         int foo() { return x; }
921     }
922     auto s3 = new S3(&x);
923     assert(s3.p == &x);
924     assert(s3.foo() == 42);
925 
926     auto x12774 = new HasUnion12774();
927 
928     return true;
929 }
930 static assert(test12681());
931 
932 /********************************************/
933 // 3991
934 
935 union X3991
936 {
937     int   a = void;
938     dchar b = void;
939 }
940 
941 union Y3991
942 {
943     int   a = void;
944     dchar b = 'a';
945 }
946 
947 union Z3991
948 {
949     int   a = 123;
950     dchar b = void;
951 }
952 
953 void test3991()
954 {
955     X3991 x;
956 
957     Y3991 y;
958     assert(y.b == 'a');
959 
960     Z3991 z;
961     assert(z.a == 123);
962 }
963 
964 /********************************************/
965 // 7727
966 
967 union U7727A1 { int i;       double d;       }
968 union U7727A2 { int i = 123; double d;       }
969 //union U7727A3 { int i;       double d = 2.5; }
970 
971 union U7727B1 { double d;       int i;       }
972 union U7727B2 { double d = 2.5; int i;       }
973 //union U7727B3 { double d;       int i = 123; }
974 
975 void test7727()
976 {
977     import core.stdc.math : isnan;
978 
979     { U7727A1 u;                assert(u.i == 0); }
980     { U7727A1 u = { i: 1024 };  assert(u.i == 1024); }
981     { U7727A1 u = { d: 1.225 }; assert(u.d == 1.225); }
982   static assert(!__traits(compiles,
983     { U7727A1 u = { i: 1024, d: 1.225 }; }
984   ));
985 
986     { U7727A2 u;                assert(u.i == 123); }
987     { U7727A2 u = { i: 1024 };  assert(u.i == 1024); }
988     { U7727A2 u = { d: 1.225 }; assert(u.d == 1.225); }
989   static assert(!__traits(compiles,
990     { U7727A2 u = { i: 1024, d: 1.225 }; }
991   ));
992 
993 // Blocked by issue 1432
994 //    { U7727A3 u;                assert(u.d == 2.5); }
995 //    { U7727A3 u = { i: 1024 };  assert(u.i == 1024); }
996 //    { U7727A3 u = { d: 1.225 }; assert(u.d == 1.225); }
997 //  static assert(!__traits(compiles,
998 //    { U7727A3 u = { i: 1024, d: 1.225 }; }
999 //  ));
1000 
1001     { U7727B1 u;                assert(isnan(u.d)); }
1002     { U7727B1 u = { i: 1024 };  assert(u.i == 1024); }
1003     { U7727B1 u = { d: 1.225 }; assert(u.d == 1.225); }
1004   static assert(!__traits(compiles,
1005     { U7727B1 u = { i: 1024, d: 1.225 }; }
1006   ));
1007 
1008     { U7727B2 u;                assert(u.d == 2.5); }
1009     { U7727B2 u = { i: 1024 };  assert(u.i == 1024); }
1010     { U7727B2 u = { d: 1.225 }; assert(u.d == 1.225); }
1011   static assert(!__traits(compiles,
1012     { U7727B2 u = { i: 1024, d: 1.225 }; }
1013   ));
1014 
1015 // Blocked by issue 1432
1016 //    { U7727B3 u;                assert(u.i == 123); }
1017 //    { U7727B3 u = { i: 1024 };  assert(u.i == 1024); }
1018 //    { U7727B3 u = { d: 1.225 }; assert(u.d == 1.225); }
1019 //  static assert(!__traits(compiles,
1020 //    { U7727B3 u = { i: 1024, d: 1.225 }; }
1021 //  ));
1022 
1023 
1024     test7727a();
1025     test7727b();
1026 }
1027 
1028 // --------
1029 
1030 struct Foo7727a
1031 {
1032     ushort bar2;
1033 }
1034 struct Foo7727b
1035 {
1036     union
1037     {
1038         ubyte[2] bar1;
1039         ushort bar2;
1040     }
1041 }
1042 
1043 void test7727a()
1044 {
1045     immutable Foo7727a foo1 = { bar2: 100 }; // OK
1046     immutable Foo7727b foo2 = { bar2: 100 }; // OK <-- error
1047 }
1048 
1049 // --------
1050 
1051 struct S7727 { int i; double d; }
1052 union U7727 { int i; double d; }
1053 
1054 void test7727b()
1055 {
1056     S7727 s = { d: 5 }; // OK
1057     U7727 u = { d: 5 }; // OK <-- Error: is not a static and cannot have static initializer
1058 }
1059 
1060 /********************************************/
1061 // 7929
1062 
1063 void test7929()
1064 {
1065     static struct S
1066     {
1067         int [] numbers;
1068     }
1069 
1070     const int [] numbers = new int[2];
1071     const S si = {numbers};
1072     // Error: cannot implicitly convert expression (numbers) of type const(int[]) to int[]
1073 
1074     const S se = const(S)(numbers);
1075     // OK
1076 }
1077 
1078 /********************************************/
1079 // 7021
1080 
1081 struct S7021
1082 {
1083     @disable this();
1084 }
1085 
1086 void test7021()
1087 {
1088   static assert(!is(typeof({
1089     auto s = S7021();
1090   })));
1091 }
1092 
1093 /********************************************/
1094 // 8738
1095 
1096 void test8738()
1097 {
1098     int[3] a = [1, 2, 3];
1099 
1100     struct S { int a, b, c; }
1101     S s = S(1, 2, 3);
1102 
1103     a = [4, a[0], 6];
1104     s = S(4, s.a, 6);
1105 
1106     assert(a == [4, 1, 6]);
1107     assert(s == S(4, 1, 6));
1108 }
1109 
1110 /********************************************/
1111 // 8763
1112 
1113 void test8763()
1114 {
1115     struct S
1116     {
1117         this(int) {}
1118     }
1119 
1120     void foo(T, Args...)(Args args)
1121     {
1122         T t = T(args);
1123         // Error: constructor main.S.this (int) is not callable using argument types ()
1124     }
1125 
1126     S t = S(); // OK, initialize to S.init
1127     foo!S();
1128 }
1129 
1130 /********************************************/
1131 // 8902
1132 
1133 union U8902 { int a, b; }
1134 
1135 enum U8902 u8902a = U8902.init; // No errors
1136 U8902 u8902b;                   // No errors
1137 U8902 u8902c = U8902.init;      // Error: duplicate union initialization for b
1138 
1139 void test8902()
1140 {
1141     U8902 u8902d = U8902.init;                  // No errors
1142     immutable U8902 u8902e = U8902.init;        // No errors
1143     immutable static U8902 u8902f = U8902.init; // Error: duplicate union...
1144     static U8902 u8902g = u8902e;               // Error: duplicate union...
1145     static U8902 u8902h = U8902.init;           // Error: duplicate union...
1146 }
1147 
1148 /********************************************/
1149 // 9116
1150 
1151 void test9116()
1152 {
1153     static struct X
1154     {
1155         int v;
1156         this(this) { ++v; }
1157     }
1158     static struct Y
1159     {
1160         X x;
1161     }
1162     X x = X(1);
1163     assert(x.v == 1);
1164     Y y = Y(X(1));
1165     //printf("y.x.v = %d\n", y.x.v);  // print 2, but should 1
1166     assert(y.x.v == 1); // fails
1167 }
1168 
1169 /********************************************/
1170 // 9293
1171 
1172 void test9293()
1173 {
1174     static struct A
1175     {
1176     //  enum A zero = A(); // This works as expected
1177         enum A zero = {};  // Note the difference here
1178 
1179         int opCmp(const ref A a) const
1180         {
1181             assert(0);
1182         }
1183 
1184         int opCmp(const A a) const
1185         {
1186             return 0;
1187         }
1188     }
1189 
1190     A a;
1191     auto b = a >= A.zero;  // Error: A() is not an lvalue
1192 }
1193 
1194 /********************************************/
1195 // 9566
1196 
1197 void test9566()
1198 {
1199     static struct ExpandData
1200     {
1201         ubyte[4096] window = 0;
1202     }
1203     ExpandData a;
1204     auto b = ExpandData.init;   // bug
1205 }
1206 
1207 /********************************************/
1208 // 9775
1209 
1210 enum Month9775 : ubyte { jan = 1, }
1211 struct Date9775
1212 {
1213     this(int year, int month, int day) pure
1214     {
1215         _year  = cast(short)year;
1216         _month = cast(Month9775)month;
1217         _day   = cast(ubyte)day;
1218     }
1219     short     _year  = 1;
1220     Month9775 _month = Month9775.jan;
1221     ubyte     _day   = 1;
1222 }
1223 
1224 const Date9775 date9775c1 = Date9775(2012, 12, 21);
1225 const          date9775c2 = Date9775(2012, 12, 21);
1226 enum  Date9775 date9775e1 = Date9775(2012, 12, 21);
1227 enum           date9775e2 = Date9775(2012, 12, 21);
1228 
1229 /********************************************/
1230 // 11105
1231 
1232 struct S11105
1233 {
1234     int[2][1] a21;
1235 }
1236 
1237 void test11105()
1238 {
1239     S11105 s = S11105([1, 2]);
1240 }
1241 
1242 /********************************************/
1243 // 11147
1244 
1245 struct V11147
1246 {
1247     union
1248     {
1249         struct
1250         {
1251             float x = 0;
1252             float y = 0;
1253             float z = 0;
1254         }
1255         struct
1256         {
1257             float r;
1258             float g;
1259             float b;
1260         }
1261     }
1262 }
1263 
1264 void test11147()
1265 {
1266     auto v = V11147.init;
1267     assert(v.x == 0f);
1268     assert(v.y == 0f);
1269     assert(v.z == 0f);
1270     assert(v.r == 0f);
1271     assert(v.g == 0f);
1272     assert(v.b == 0f);
1273 }
1274 
1275 /********************************************/
1276 // 11256
1277 
1278 struct S11256 { @disable this(); }
1279 
1280 struct Z11256a(Ranges...)
1281 {
1282     Ranges ranges;
1283     this(Ranges rs) { ranges = rs; }
1284 }
1285 struct Z11256b(Ranges...)
1286 {
1287     Ranges ranges = Ranges.init;    // Internal error: e2ir.c 5321
1288     this(Ranges rs) { ranges = rs; }
1289 }
1290 struct Z11256c(Ranges...)
1291 {
1292     Ranges ranges = void;           // todt.c(475) v->type->ty == Tsarray && vsz == 0
1293     this(Ranges rs) { ranges = rs; }
1294 }
1295 
1296 struct F11256(alias pred)
1297 {
1298     this(int[] = null) { }
1299 }
1300 
1301 Z!Ranges z11256(alias Z, Ranges...)(Ranges ranges)
1302 {
1303     return Z!Ranges(ranges);
1304 }
1305 
1306 void test11256()
1307 {
1308     z11256!Z11256a(S11256.init, F11256!(gv => true)());
1309     z11256!Z11256b(S11256.init, F11256!(gv => true)());
1310     z11256!Z11256c(S11256.init, F11256!(gv => true)());
1311 }
1312 
1313 /********************************************/
1314 // 11269
1315 
1316 struct Atom
1317 {
1318     union
1319     {
1320         int i;
1321         struct
1322         {
1323             ulong first, rest;
1324         }
1325         struct
1326         {
1327             uint a, b;
1328         }
1329     }
1330 }
1331 
1332 void test11269()
1333 {
1334     Atom a1;
1335     Atom a2 = {i:1, rest:10, b:2};
1336 }
1337 
1338 /********************************************/
1339 // 11427
1340 
1341 struct S11427
1342 {
1343     union
1344     {
1345         ubyte a;
1346         int x;
1347     }
1348     void[] arr;
1349 }
1350 
1351 int foo11427() @safe
1352 {
1353     S11427 s1 = S11427();
1354     S11427 s2;
1355     return 0;
1356 }
1357 
1358 /********************************************/
1359 // 12011
1360 
1361 struct S12011a
1362 {
1363     int f() { return i; }
1364     enum e = this.init.f();
1365     int i = 1, j = 2;
1366 }
1367 
1368 struct S12011b
1369 {
1370     int f() { return i; }
1371     enum e = S12011b().f();
1372     int i = 1, j = 2;
1373 }
1374 
1375 void test12011()
1376 {
1377     static assert(S12011a.e == 1);
1378     static assert(S12011b.e == 1);
1379 }
1380 
1381 /********************************************/
1382 // 13021
1383 
1384 void test13021()
1385 {
1386     static union U1
1387     {
1388         float a;
1389         int b;
1390     }
1391 
1392     static union U2
1393     {
1394         double a;
1395         long b;
1396     }
1397 
1398     static union U3
1399     {
1400         real a;
1401         struct B { long b1, b2; } // ICE happens only if B.sizeof == real.sizeof
1402         B b;
1403     }
1404 
1405     static union U4
1406     {
1407         real a;
1408         long[2] b; // ditto
1409     }
1410 
1411     auto f = U1(1.0);  auto ok = f.b;
1412 
1413     auto fail1 = U1(1.0).b; // OK <- Internal error: e2ir.c 1162
1414     auto fail2 = U2(1.0).b; // OK <- Internal error: e2ir.c 1162
1415     auto fail3 = U3(1.0).b; // OK <- Internal error: e2ir.c 1162
1416     auto fail4 = U4(1.0).b; // OK <- Internal error: backend/el.c 2904
1417 }
1418 
1419 /********************************************/
1420 // 14556
1421 
1422 enum E14556 { a = 1 }
1423 
1424 struct S14556a
1425 {
1426     this(int) {}
1427     E14556[1] data;
1428 }
1429 
1430 struct S14556b
1431 {
1432     this(int) {}
1433     void[1] data;
1434 }
1435 
1436 void test14556()
1437 {
1438     auto sa = S14556a(0);
1439     assert(sa.data == [E14556.a]);
1440 
1441     auto sb = S14556b(0);
1442     assert(sb.data[] == cast(ubyte[1])[0]);
1443 }
1444 
1445 /********************************************/
1446 // https://issues.dlang.org/show_bug.cgi?id=17622
1447 
1448 struct S17622
1449 {
1450     int i;
1451 
1452     this(ubyte)
1453     {
1454         return;
1455     }
1456 
1457     void fun()
1458     {
1459         assert(i == 0);
1460     }
1461 }
1462 
1463 S17622 make()
1464 {
1465     return S17622(0);
1466 }
1467 
1468 void test17622()
1469 {
1470     S17622 s = make();
1471 
1472     auto rdg = (){ s.fun(); };
1473 
1474     s.fun();
1475 }
1476 
1477 /********************************************/
1478 
1479 int main()
1480 {
1481     test1();
1482     test2();
1483     test3();
1484     test4();
1485     test5();
1486     test6();
1487     test7();
1488     test8();
1489     test9();
1490     test10();
1491     test11();
1492     test12();
1493     test13();
1494     test14();
1495     test15a();
1496     test15b();
1497     test15c();
1498     test15d();
1499     test15e();
1500     test9993a();
1501     test9993b();
1502     test3198and1914();
1503     test2427();
1504     test5885();
1505     test5889();
1506     test4247();
1507     test6937();
1508     test12681();
1509     test3991();
1510     test7727();
1511     test7929();
1512     test7021();
1513     test8738();
1514     test8763();
1515     test8902();
1516     test9116();
1517     test9293();
1518     test9566();
1519     test11105();
1520     test11147();
1521     test11256();
1522     test13021();
1523     test14556();
1524     test17622();
1525 
1526     printf("Success\n");
1527     return 0;
1528 }
1529