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