Debian Codegraph @

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

raw
1 /*
2 TEST_OUTPUT:
3 ---
4 false
5 [] = int
6 [] = string
7 [0] = int
8 [1] = string
9 [] = string
10 [] = int
11 [1] = string
12 [0] = int
13 ---
14
15 RUN_OUTPUT:
16 ---
17 1 1.1
18 ctor
19 cpctor
20 dtor
21 cpctor
22 dtor
23 dtor
24 Success
25 ---
26 */
27
28 extern (C) int printf(const(char*) fmt, ...);
29 import core.vararg;
30
31 struct Tup(T...)
32 {
33 T field;
34 alias field this;
35
36 bool opEquals(const Tup rhs) const
37 {
38 foreach (i, _; T)
39 if (field[i] != rhs.field[i])
40 return false;
41 return true;
42 }
43 }
44
45 Tup!T tup(T...)(T fields)
46 {
47 return typeof(return)(fields);
48 }
49
50 template Seq(T...)
51 {
52 alias T Seq;
53 }
54
55 /**********************************************/
56
57 struct S
58 {
59 int x;
60 alias x this;
61 }
62
63 int foo(int i)
64 {
65 return i * 2;
66 }
67
68 void test1()
69 {
70 S s;
71 s.x = 7;
72 int i = -s;
73 assert(i == -7);
74
75 i = s + 8;
76 assert(i == 15);
77
78 i = s + s;
79 assert(i == 14);
80
81 i = 9 + s;
82 assert(i == 16);
83
84 i = foo(s);
85 assert(i == 14);
86 }
87
88 /**********************************************/
89
90 class C
91 {
92 int x;
93 alias x this;
94 }
95
96 void test2()
97 {
98 C s = new C();
99 s.x = 7;
100 int i = -s;
101 assert(i == -7);
102
103 i = s + 8;
104 assert(i == 15);
105
106 i = s + s;
107 assert(i == 14);
108
109 i = 9 + s;
110 assert(i == 16);
111
112 i = foo(s);
113 assert(i == 14);
114 }
115
116 /**********************************************/
117
118 void test3()
119 {
120 Tup!(int, double) t;
121 t[0] = 1;
122 t[1] = 1.1;
123 assert(t[0] == 1);
124 assert(t[1] == 1.1);
125 printf("%d %g\n", t[0], t[1]);
126 }
127
128 /**********************************************/
129
130 struct Iter
131 {
132 bool empty() { return true; }
133 void popFront() { }
134 ref Tup!(int, int) front() { return *new Tup!(int, int); }
135 ref Iter opSlice() return { return this; }
136 }
137
138 void test4()
139 {
140 foreach (a; Iter()) { }
141 }
142
143 /**********************************************/
144
145 void test5()
146 {
147 static struct Double1 {
148 double val = 1;
149 alias val this;
150 }
151 static Double1 x() { return Double1(); }
152 x()++;
153 }
154
155 /**********************************************/
156 // https://issues.dlang.org/show_bug.cgi?id=4617
157
158 struct S4617
159 {
160 struct F
161 {
162 int square(int n) { return n*n; }
163 real square(real n) { return n*n; }
164 }
165 F forward;
166
167 alias forward this;
168
169 alias forward.square sqr; // okay
170
171 int field;
172 void mfunc();
173 template Templ(){}
174 void tfunc()(){}
175 }
176
177 template Id4617(alias k) { alias k Id4617; }
178
179 void test4617a()
180 {
181 alias Id4617!(S4617.square) test1; //NG
182 alias Id4617!(S4617.forward.square) test2; //OK
183
184 alias Id4617!(S4617.sqr) test3; //okay
185
186 static assert(__traits(isSame, S4617.square, S4617.forward.square));
187 }
188
189 void test4617b()
190 {
191 static struct Sub(T)
192 {
193 T value;
194 @property ref inout(T) payload() inout { return value; }
195 alias payload this;
196 }
197
198 alias Id4617!(S4617.field) S_field;
199 alias Id4617!(S4617.mfunc) S_mfunc;
200 alias Id4617!(S4617.Templ) S_Templ;
201 alias Id4617!(S4617.tfunc) S_tfunc;
202
203 alias Sub!S4617 T4617;
204 alias Id4617!(T4617.field) R_field;
205 alias Id4617!(T4617.mfunc) R_mfunc;
206 alias Id4617!(T4617.Templ) R_Templ;
207 alias Id4617!(T4617.tfunc) R_tfunc;
208 static assert(__traits(isSame, R_field, S_field));
209 static assert(__traits(isSame, R_mfunc, S_mfunc));
210 static assert(__traits(isSame, R_Templ, S_Templ));
211 static assert(__traits(isSame, R_tfunc, S_tfunc));
212
213 alias Id4617!(T4617.square) R_sqr;
214 static assert(__traits(isSame, R_sqr, S4617.forward.square));
215 }
216
217 /**********************************************/
218 // https://issues.dlang.org/show_bug.cgi?id=4773
219
220 void test4773()
221 {
222 struct Rebindable
223 {
224 Object obj;
225 @property const(Object) get(){ return obj; }
226 alias get this;
227 }
228
229 Rebindable r;
230 if (r) assert(0);
231 r.obj = new Object;
232 if (!r) assert(0);
233 }
234
235 /**********************************************/
236 // https://issues.dlang.org/show_bug.cgi?id=5188
237
238 void test5188()
239 {
240 struct S
241 {
242 int v = 10;
243 alias v this;
244 }
245
246 S s;
247 assert(s <= 20);
248 assert(s != 14);
249 }
250
251 /***********************************************/
252
253 struct Foo {
254 void opIndexAssign(int x, size_t i) {
255 val = x;
256 }
257 void opSliceAssign(int x, size_t a, size_t b) {
258 val = x;
259 }
260 int val;
261 }
262
263 struct Bar {
264 Foo foo;
265 alias foo this;
266 }
267
268 void test6() {
269 Bar b;
270 b[0] = 1;
271 assert(b.val == 1);
272 b[0 .. 1] = 2;
273 assert(b.val == 2);
274 }
275
276 /**********************************************/
277 // recursive alias this detection
278
279 class C0 {}
280
281 class C1 { C2 c; alias c this; }
282 class C2 { C1 c; alias c this; }
283
284 class C3 { C2 c; alias c this; }
285
286 struct S0 {}
287
288 struct S1 { S2* ps; @property ref get(){return *ps;} alias get this; }
289 struct S2 { S1* ps; @property ref get(){return *ps;} alias get this; }
290
291 struct S3 { S2* ps; @property ref get(){return *ps;} alias get this; }
292
293 struct S4 { S5* ps; @property ref get(){return *ps;} alias get this; }
294 struct S5 { S4* ps; @property ref get(){return *ps;} alias get this; }
295
296 struct S6 { S5* ps; @property ref get(){return *ps;} alias get this; }
297
298 void test7()
299 {
300 // Able to check a type is implicitly convertible within a finite time.
301 static assert(!is(C1 : C0));
302 static assert( is(C2 : C1));
303 static assert( is(C1 : C2));
304 static assert(!is(C3 : C0));
305 static assert( is(C3 : C1));
306 static assert( is(C3 : C2));
307
308 static assert(!is(S1 : S0));
309 static assert( is(S2 : S1));
310 static assert( is(S1 : S2));
311 static assert(!is(S3 : S0));
312 static assert( is(S3 : S1));
313 static assert( is(S3 : S2));
314
315 C0 c0; C1 c1; C3 c3;
316 S0 s0; S1 s1; S3 s3; S4 s4; S6 s6;
317
318 // Allow merging types that contains alias this recursion.
319 static assert( __traits(compiles, c0 is c1)); // typeMerge(c || c) e2->implicitConvTo(t1);
320 static assert( __traits(compiles, c0 is c3)); // typeMerge(c || c) e2->implicitConvTo(t1);
321 static assert( __traits(compiles, c1 is c0)); // typeMerge(c || c) e1->implicitConvTo(t2);
322 static assert( __traits(compiles, c3 is c0)); // typeMerge(c || c) e1->implicitConvTo(t2);
323 static assert(!__traits(compiles, s1 is c0)); // typeMerge(c || c) e1
324 static assert(!__traits(compiles, s3 is c0)); // typeMerge(c || c) e1
325 static assert(!__traits(compiles, c0 is s1)); // typeMerge(c || c) e2
326 static assert(!__traits(compiles, c0 is s3)); // typeMerge(c || c) e2
327
328 static assert(!__traits(compiles, s1 is s0)); // typeMerge(s && s) e1
329 static assert(!__traits(compiles, s3 is s0)); // typeMerge(s && s) e1
330 static assert(!__traits(compiles, s0 is s1)); // typeMerge(s && s) e2
331 static assert(!__traits(compiles, s0 is s3)); // typeMerge(s && s) e2
332 static assert(!__traits(compiles, s1 is s4)); // typeMerge(s && s) e1 + e2
333 static assert(!__traits(compiles, s3 is s6)); // typeMerge(s && s) e1 + e2
334
335 static assert(!__traits(compiles, s1 is 10)); // typeMerge(s || s) e1
336 static assert(!__traits(compiles, s3 is 10)); // typeMerge(s || s) e1
337 static assert(!__traits(compiles, 10 is s1)); // typeMerge(s || s) e2
338 static assert(!__traits(compiles, 10 is s3)); // typeMerge(s || s) e2
339
340 // SliceExp::semantic
341 static assert(!__traits(compiles, c1[]));
342 static assert(!__traits(compiles, c3[]));
343 static assert(!__traits(compiles, s1[]));
344 static assert(!__traits(compiles, s3[]));
345
346 // CallExp::semantic
347 // static assert(!__traits(compiles, c1()));
348 // static assert(!__traits(compiles, c3()));
349 static assert(!__traits(compiles, s1()));
350 static assert(!__traits(compiles, s3()));
351
352 // AssignExp::semantic
353 static assert(!__traits(compiles, { c1[1] = 0; }));
354 static assert(!__traits(compiles, { c3[1] = 0; }));
355 static assert(!__traits(compiles, { s1[1] = 0; }));
356 static assert(!__traits(compiles, { s3[1] = 0; }));
357 static assert(!__traits(compiles, { c1[ ] = 0; }));
358 static assert(!__traits(compiles, { c3[ ] = 0; }));
359 static assert(!__traits(compiles, { s1[ ] = 0; }));
360 static assert(!__traits(compiles, { s3[ ] = 0; }));
361
362 // UnaExp::op_overload
363 static assert(!__traits(compiles, +c1[1]));
364 static assert(!__traits(compiles, +c3[1]));
365 static assert(!__traits(compiles, +s1[1]));
366 static assert(!__traits(compiles, +s3[1]));
367 static assert(!__traits(compiles, +c1[ ]));
368 static assert(!__traits(compiles, +c3[ ]));
369 static assert(!__traits(compiles, +s1[ ]));
370 static assert(!__traits(compiles, +s3[ ]));
371 static assert(!__traits(compiles, +c1));
372 static assert(!__traits(compiles, +c3));
373 static assert(!__traits(compiles, +s1));
374 static assert(!__traits(compiles, +s3));
375
376 // ArrayExp::op_overload
377 static assert(!__traits(compiles, c1[1]));
378 static assert(!__traits(compiles, c3[1]));
379 static assert(!__traits(compiles, s1[1]));
380 static assert(!__traits(compiles, s3[1]));
381
382 // BinExp::op_overload
383 static assert(!__traits(compiles, c1 + 10)); // e1
384 static assert(!__traits(compiles, c3 + 10)); // e1
385 static assert(!__traits(compiles, 10 + c1)); // e2
386 static assert(!__traits(compiles, 10 + c3)); // e2
387 static assert(!__traits(compiles, s1 + 10)); // e1
388 static assert(!__traits(compiles, s3 + 10)); // e1
389 static assert(!__traits(compiles, 10 + s1)); // e2
390 static assert(!__traits(compiles, 10 + s3)); // e2
391
392 // BinExp::compare_overload
393 static assert(!__traits(compiles, c1 < 10)); // (Object.opCmp(int) is invalid)
394 static assert(!__traits(compiles, c3 < 10)); // (Object.opCmp(int) is invalid)
395 static assert(!__traits(compiles, 10 < c1)); // (Object.opCmp(int) is invalid)
396 static assert(!__traits(compiles, 10 < c3)); // (Object.opCmp(int) is invalid)
397 static assert(!__traits(compiles, s1 < 10)); // e1
398 static assert(!__traits(compiles, s3 < 10)); // e1
399 static assert(!__traits(compiles, 10 < s1)); // e2
400 static assert(!__traits(compiles, 10 < s3)); // e2
401
402 // BinAssignExp::op_overload
403 static assert(!__traits(compiles, c1[1] += 1));
404 static assert(!__traits(compiles, c3[1] += 1));
405 static assert(!__traits(compiles, s1[1] += 1));
406 static assert(!__traits(compiles, s3[1] += 1));
407 static assert(!__traits(compiles, c1[ ] += 1));
408 static assert(!__traits(compiles, c3[ ] += 1));
409 static assert(!__traits(compiles, s1[ ] += 1));
410 static assert(!__traits(compiles, s3[ ] += 1));
411 static assert(!__traits(compiles, c1 += c0)); // e1
412 static assert(!__traits(compiles, c3 += c0)); // e1
413 static assert(!__traits(compiles, s1 += s0)); // e1
414 static assert(!__traits(compiles, s3 += s0)); // e1
415 static assert(!__traits(compiles, c0 += c1)); // e2
416 static assert(!__traits(compiles, c0 += c3)); // e2
417 static assert(!__traits(compiles, s0 += s1)); // e2
418 static assert(!__traits(compiles, s0 += s3)); // e2
419 static assert(!__traits(compiles, c1 += s1)); // e1 + e2
420 static assert(!__traits(compiles, c3 += s3)); // e1 + e2
421
422 // ForeachStatement::inferAggregate
423 static assert(!__traits(compiles, { foreach (e; s1){} }));
424 static assert(!__traits(compiles, { foreach (e; s3){} }));
425 static assert(!__traits(compiles, { foreach (e; c1){} }));
426 static assert(!__traits(compiles, { foreach (e; c3){} }));
427
428 // Expression::checkToBoolean
429 static assert(!__traits(compiles, { if (s1){} }));
430 static assert(!__traits(compiles, { if (s3){} }));
431
432 // SwitchStatement::semantic
433 static assert(!__traits(compiles, { switch (c0) { default: } }));
434 static assert(!__traits(compiles, { switch (c1) { default: } }));
435 static assert(!__traits(compiles, { switch (c3) { default: } }));
436
437 // https://issues.dlang.org/show_bug.cgi?id=12537: function arguments with IFTI
438 void eq12537()(Object lhs) {}
439 const C0 cc0;
440 const C1 cc1;
441 const C3 cc3;
442 static assert(!__traits(compiles, eq12537(cc0)));
443 static assert(!__traits(compiles, eq12537(cc1)));
444 static assert(!__traits(compiles, eq12537(cc3)));
445 }
446
447 /***************************************************/
448 // https://issues.dlang.org/show_bug.cgi?id=11875
449 // endless recursion in Type::deduceType
450
451 struct T11875x(C)
452 {
453 C c;
454 }
455 class D11875a { D11875b c; alias c this; }
456 class D11875b { D11875a c; alias c this; }
457 static assert(!is(D11875a == D11875b));
458 static assert( is(T11875x!D11875a == T11875x!D, D) && is(D == D11875a));
459 static assert(!is(D11875a == T11875x!D, D)); // this used to freeze dmd
460
461 // test that types in recursion are still detected
462 struct T11875y(C)
463 {
464 C c;
465 alias c this;
466 }
467 class D11875c { T11875y!D11875b c; alias c this; }
468 static assert(is(D11875c : T11875y!D, D) && is(D == D11875b));
469
470 /***************************************************/
471 // https://issues.dlang.org/show_bug.cgi?id=11930
472
473 class BarObj11930 {}
474
475 struct Bar11930
476 {
477 BarObj11930 _obj;
478 alias _obj this;
479 }
480
481 BarObj11930 getBarObj11930(T)(T t)
482 {
483 static if (is(T unused : BarObj11930))
484 return t;
485 else
486 static assert(false, "Can not get BarObj from " ~ T.stringof);
487 }
488
489 void test11930()
490 {
491 Bar11930 b;
492 getBarObj11930(b);
493 }
494
495 /***************************************************/
496 // https://issues.dlang.org/show_bug.cgi?id=2781
497
498 struct Tuple2781a(T...) {
499 T data;
500 alias data this;
501 }
502
503 struct Tuple2781b(T) {
504 T data;
505 alias data this;
506 }
507
508 void test2781()
509 {
510 Tuple2781a!(uint, float) foo;
511 foreach(elem; foo) {}
512
513 {
514 Tuple2781b!(int[]) bar1;
515 foreach(elem; bar1) {}
516
517 Tuple2781b!(int[int]) bar2;
518 foreach(key, elem; bar2) {}
519
520 Tuple2781b!(string) bar3;
521 foreach(dchar elem; bar3) {}
522 }
523
524 {
525 Tuple2781b!(int[]) bar1;
526 foreach(elem; bar1) goto L1;
527 L1:
528 ;
529
530 Tuple2781b!(int[int]) bar2;
531 foreach(key, elem; bar2) goto L2;
532 L2:
533 ;
534
535 Tuple2781b!(string) bar3;
536 foreach(dchar elem; bar3) goto L3;
537 L3:
538 ;
539 }
540
541
542 int eval;
543
544 auto t1 = tup(10, "str");
545 auto i1 = 0;
546 foreach (e; t1)
547 {
548 pragma(msg, "[] = ", typeof(e));
549 static if (is(typeof(e) == int )) assert(i1 == 0 && e == 10);
550 static if (is(typeof(e) == string)) assert(i1 == 1 && e == "str");
551 ++i1;
552 }
553
554 auto t2 = tup(10, "str");
555 foreach (i2, e; t2)
556 {
557 pragma(msg, "[", cast(int)i2, "] = ", typeof(e));
558 static if (is(typeof(e) == int )) { static assert(i2 == 0); assert(e == 10); }
559 static if (is(typeof(e) == string)) { static assert(i2 == 1); assert(e == "str"); }
560 }
561
562 auto t3 = tup(10, "str");
563 auto i3 = 2;
564 foreach_reverse (e; t3)
565 {
566 --i3;
567 pragma(msg, "[] = ", typeof(e));
568 static if (is(typeof(e) == int )) assert(i3 == 0 && e == 10);
569 static if (is(typeof(e) == string)) assert(i3 == 1 && e == "str");
570 }
571
572 auto t4 = tup(10, "str");
573 foreach_reverse (i4, e; t4)
574 {
575 pragma(msg, "[", cast(int)i4, "] = ", typeof(e));
576 static if (is(typeof(e) == int )) { static assert(i4 == 0); assert(e == 10); }
577 static if (is(typeof(e) == string)) { static assert(i4 == 1); assert(e == "str"); }
578 }
579
580 eval = 0;
581 foreach (i, e; tup(tup((){eval++; return 10;}(), 3.14), tup("str", [1,2])))
582 {
583 static if (i == 0) assert(e == tup(10, 3.14));
584 static if (i == 1) assert(e == tup("str", [1,2]));
585 }
586 assert(eval == 1);
587
588 eval = 0;
589 foreach (i, e; tup((){eval++; return 10;}(), tup(3.14, tup("str", tup([1,2])))))
590 {
591 static if (i == 0) assert(e == 10);
592 static if (i == 1) assert(e == tup(3.14, tup("str", tup([1,2]))));
593 }
594 assert(eval == 1);
595 }
596
597 /**********************************************/
598 // https://issues.dlang.org/show_bug.cgi?id=6546
599
600 void test6546()
601 {
602 class C {}
603 class D : C {}
604
605 struct S { C c; alias c this; } // S : C
606 struct T { S s; alias s this; } // T : S
607 struct U { T t; alias t this; } // U : T
608
609 C c;
610 D d;
611 S s;
612 T t;
613 U u;
614
615 assert(c is c); // OK
616 assert(c is d); // OK
617 assert(c is s); // OK
618 assert(c is t); // OK
619 assert(c is u); // OK
620
621 assert(d is c); // OK
622 assert(d is d); // OK
623 assert(d is s); // doesn't work
624 assert(d is t); // doesn't work
625 assert(d is u); // doesn't work
626
627 assert(s is c); // OK
628 assert(s is d); // doesn't work
629 assert(s is s); // OK
630 assert(s is t); // doesn't work
631 assert(s is u); // doesn't work
632
633 assert(t is c); // OK
634 assert(t is d); // doesn't work
635 assert(t is s); // doesn't work
636 assert(t is t); // OK
637 assert(t is u); // doesn't work
638
639 assert(u is c); // OK
640 assert(u is d); // doesn't work
641 assert(u is s); // doesn't work
642 assert(u is t); // doesn't work
643 assert(u is u); // OK
644 }
645
646 /**********************************************/
647 // https://issues.dlang.org/show_bug.cgi?id=6736
648
649 void test6736()
650 {
651 static struct S1
652 {
653 struct S2 // must be 8 bytes in size
654 {
655 uint a, b;
656 }
657 S2 s2;
658 alias s2 this;
659 }
660 S1 c;
661 static assert(!is(typeof(c + c)));
662 }
663
664 /**********************************************/
665 // https://issues.dlang.org/show_bug.cgi?id=2777
666
667 struct ArrayWrapper(T) {
668 T[] array;
669 alias array this;
670 }
671
672 // alias array this
673 void test2777a()
674 {
675 ArrayWrapper!(uint) foo;
676 foo.length = 5; // Works
677 foo[0] = 1; // Works
678 auto e0 = foo[0]; // Works
679 auto e4 = foo[$ - 1]; // Error: undefined identifier __dollar
680 auto s01 = foo[0..2]; // Error: ArrayWrapper!(uint) cannot be sliced with[]
681 }
682
683 // alias tuple this
684 void test2777b()
685 {
686 auto t = tup(10, 3.14, "str", [1,2]);
687
688 assert(t[$ - 1] == [1,2]);
689
690 auto f1 = t[];
691 assert(f1[0] == 10);
692 assert(f1[1] == 3.14);
693 assert(f1[2] == "str");
694 assert(f1[3] == [1,2]);
695
696 auto f2 = t[1..3];
697 assert(f2[0] == 3.14);
698 assert(f2[1] == "str");
699 }
700
701 /****************************************/
702 // https://issues.dlang.org/show_bug.cgi?id=2787
703
704 struct Base2787
705 {
706 int x;
707 void foo() { auto _ = x; }
708 }
709
710 struct Derived2787
711 {
712 Base2787 _base;
713 alias _base this;
714 int y;
715 void bar() { auto _ = x; }
716 }
717
718 /***********************************/
719 // https://issues.dlang.org/show_bug.cgi?id=5679
720
721 void test5679()
722 {
723 class Foo {}
724
725 class Base
726 {
727 @property Foo getFoo() { return null; }
728 }
729 class Derived : Base
730 {
731 alias getFoo this;
732 }
733
734 Derived[] dl;
735 Derived d = new Derived();
736 dl ~= d; // Error: cannot append type alias_test.Base to type Derived[]
737 }
738
739 /***********************************/
740 // https://issues.dlang.org/show_bug.cgi?id=6508
741
742 void test6508()
743 {
744 int x, y;
745 Seq!(x, y) = tup(10, 20);
746 assert(x == 10);
747 assert(y == 20);
748 }
749
750 void test6508x()
751 {
752 static int ctor, cpctor, dtor;
753
754 static struct Tuple(T...)
755 {
756 T field;
757 alias field this;
758
759 this(int) { ++ctor; printf("ctor\n"); }
760 this(this) { ++cpctor; printf("cpctor\n"); }
761 ~this() { ++dtor; printf("dtor\n"); }
762 }
763
764 {
765 alias Tup = Tuple!(int, string);
766 auto tup = Tup(1);
767 assert(ctor==1 && cpctor==0 && dtor==0);
768
769 auto getVal() { return tup; }
770 ref getRef(ref Tup s = tup) { return s; }
771
772 {
773 auto n1 = tup[0];
774 assert(ctor==1 && cpctor==0 && dtor==0);
775
776 auto n2 = getRef()[0];
777 assert(ctor==1 && cpctor==0 && dtor==0);
778
779 auto n3 = getVal()[0];
780 assert(ctor==1 && cpctor==1 && dtor==1);
781 }
782
783 // bug in DotVarExp::semantic
784 {
785 typeof(tup.field) vars;
786 vars = getVal();
787 assert(ctor==1 && cpctor==2 && dtor==2);
788 }
789 }
790 assert(ctor==1 && cpctor==2 && dtor==3);
791 assert(ctor + cpctor == dtor);
792 }
793
794 /***********************************/
795 // https://issues.dlang.org/show_bug.cgi?id=6369
796
797 void test6369a()
798 {
799 alias Seq!(int, string) Field;
800
801 auto t1 = Tup!(int, string)(10, "str");
802 Field field1 = t1; // NG -> OK
803 assert(field1[0] == 10);
804 assert(field1[1] == "str");
805
806 auto t2 = Tup!(int, string)(10, "str");
807 Field field2 = t2.field; // NG -> OK
808 assert(field2[0] == 10);
809 assert(field2[1] == "str");
810
811 auto t3 = Tup!(int, string)(10, "str");
812 Field field3;
813 field3 = t3.field;
814 assert(field3[0] == 10);
815 assert(field3[1] == "str");
816 }
817
818 void test6369b()
819 {
820 auto t = Tup!(Tup!(int, double), string)(tup(10, 3.14), "str");
821
822 Seq!(int, double, string) fs1 = t;
823 assert(fs1[0] == 10);
824 assert(fs1[1] == 3.14);
825 assert(fs1[2] == "str");
826
827 Seq!(Tup!(int, double), string) fs2 = t;
828 assert(fs2[0][0] == 10);
829 assert(fs2[0][1] == 3.14);
830 assert(fs2[0] == tup(10, 3.14));
831 assert(fs2[1] == "str");
832
833 Tup!(Tup!(int, double), string) fs3 = t;
834 assert(fs3[0][0] == 10);
835 assert(fs3[0][1] == 3.14);
836 assert(fs3[0] == tup(10, 3.14));
837 assert(fs3[1] == "str");
838 }
839
840 void test6369c()
841 {
842 auto t = Tup!(Tup!(int, double), Tup!(string, int[]))(tup(10, 3.14), tup("str", [1,2]));
843
844 Seq!(int, double, string, int[]) fs1 = t;
845 assert(fs1[0] == 10);
846 assert(fs1[1] == 3.14);
847 assert(fs1[2] == "str");
848 assert(fs1[3] == [1,2]);
849
850 Seq!(int, double, Tup!(string, int[])) fs2 = t;
851 assert(fs2[0] == 10);
852 assert(fs2[1] == 3.14);
853 assert(fs2[2] == tup("str", [1,2]));
854
855 Seq!(Tup!(int, double), string, int[]) fs3 = t;
856 assert(fs3[0] == tup(10, 3.14));
857 assert(fs3[0][0] == 10);
858 assert(fs3[0][1] == 3.14);
859 assert(fs3[1] == "str");
860 assert(fs3[2] == [1,2]);
861 }
862
863 void test6369d()
864 {
865 int eval = 0;
866 Seq!(int, string) t = tup((){++eval; return 10;}(), "str");
867 assert(eval == 1);
868 assert(t[0] == 10);
869 assert(t[1] == "str");
870 }
871
872 /**********************************************/
873 // https://issues.dlang.org/show_bug.cgi?id=6434
874
875 struct Variant6434{}
876
877 struct A6434
878 {
879 Variant6434 i;
880 alias i this;
881
882 void opDispatch(string name)()
883 {
884 }
885 }
886
887 void test6434()
888 {
889 A6434 a;
890 a.weird; // no property 'weird' for type 'VariantN!(maxSize)'
891 }
892
893 /**************************************/
894 // https://issues.dlang.org/show_bug.cgi?id=6366
895
896 void test6366()
897 {
898 struct Zip
899 {
900 string str;
901 size_t i;
902 this(string s)
903 {
904 str = s;
905 }
906 @property const bool empty()
907 {
908 return i == str.length;
909 }
910 @property Tup!(size_t, char) front()
911 {
912 return typeof(return)(i, str[i]);
913 }
914 void popFront()
915 {
916 ++i;
917 }
918 }
919
920 foreach (i, c; Zip("hello"))
921 {
922 switch (i)
923 {
924 case 0: assert(c == 'h'); break;
925 case 1: assert(c == 'e'); break;
926 case 2: assert(c == 'l'); break;
927 case 3: assert(c == 'l'); break;
928 case 4: assert(c == 'o'); break;
929 default:assert(0);
930 }
931 }
932
933 auto range(F...)(F field)
934 {
935 static struct Range {
936 F field;
937 bool empty = false;
938 Tup!F front() { return typeof(return)(field); }
939 void popFront(){ empty = true; }
940 }
941 return Range(field);
942 }
943
944 foreach (i, t; range(10, tup("str", [1,2]))){
945 static assert(is(typeof(i) == int));
946 static assert(is(typeof(t) == Tup!(string, int[])));
947 assert(i == 10);
948 assert(t == tup("str", [1,2]));
949 }
950 auto r1 = range(10, "str", [1,2]);
951 auto r2 = range(tup(10, "str"), [1,2]);
952 auto r3 = range(10, tup("str", [1,2]));
953 auto r4 = range(tup(10, "str", [1,2]));
954 alias Seq!(r1, r2, r3, r4) ranges;
955 foreach (n, _; ranges)
956 {
957 foreach (i, s, a; ranges[n]){
958 static assert(is(typeof(i) == int));
959 static assert(is(typeof(s) == string));
960 static assert(is(typeof(a) == int[]));
961 assert(i == 10);
962 assert(s == "str");
963 assert(a == [1,2]);
964 }
965 }
966 }
967
968 /***************************************************/
969 // https://issues.dlang.org/show_bug.cgi?id=6711
970
971 void test6711()
972 {
973 struct A { int i; }
974 struct B { A a; alias a this; }
975 struct C { B b; alias b this; }
976
977 B b;
978 with (b)
979 {
980 i = 42;
981 }
982 assert(b.i == 42);
983
984 C c;
985 with (c)
986 {
987 i = 42;
988 }
989 assert(c.i == 42);
990 }
991
992 /**********************************************/
993 // https://issues.dlang.org/show_bug.cgi?id=12161
994
995 class A12161
996 {
997 void m() {}
998 }
999
1000 class B12161
1001 {
1002 A12161 a;
1003 alias a this;
1004 }
1005
1006 void test12161()
1007 {
1008 B12161 b = new B12161();
1009 b.a = new A12161();
1010 with (b)
1011 m();
1012 }
1013
1014 /**********************************************/
1015 // https://issues.dlang.org/show_bug.cgi?id=6759
1016
1017 struct Range
1018 {
1019 size_t front() { return 0; }
1020 void popFront() { empty = true; }
1021 bool empty;
1022 }
1023
1024 struct ARange
1025 {
1026 Range range;
1027 alias range this;
1028 }
1029
1030 void test6759()
1031 {
1032 ARange arange;
1033 assert(arange.front == 0);
1034 foreach(e; arange)
1035 {
1036 assert(e == 0);
1037 }
1038 }
1039
1040 /**********************************************/
1041 // https://issues.dlang.org/show_bug.cgi?id=6479
1042
1043 struct Memory6479
1044 {
1045 mixin Wrapper6479!();
1046 }
1047 struct Image6479
1048 {
1049 Memory6479 sup;
1050 alias sup this;
1051 }
1052 mixin template Wrapper6479()
1053 {
1054 }
1055
1056 /**********************************************/
1057 // https://issues.dlang.org/show_bug.cgi?id=6832
1058
1059 void test6832()
1060 {
1061 static class Foo { }
1062 static struct Bar { Foo foo; alias foo this; }
1063 Bar bar;
1064 bar = new Foo; // ok
1065 assert(bar !is null); // ng
1066
1067 struct Int { int n; alias n this; }
1068 Int a;
1069 int b;
1070 auto c = (true ? a : b); // TODO
1071 assert(c == a);
1072 }
1073
1074 /**********************************************/
1075 // https://issues.dlang.org/show_bug.cgi?id=6928
1076
1077 void test6928()
1078 {
1079 struct T { int* p; } // p is necessary.
1080 T tx;
1081
1082 struct S {
1083 T get() const { return tx; }
1084 alias get this;
1085 }
1086
1087 immutable(S) s;
1088 immutable(T) t;
1089 static assert(is(typeof(1? s:t))); // ok.
1090 static assert(is(typeof(1? t:s))); // ok.
1091 static assert(is(typeof(1? s:t)==typeof(1? t:s))); // fail.
1092
1093 auto x = 1? t:s; // ok.
1094 auto y = 1? s:t; // compile error.
1095 }
1096
1097 /**********************************************/
1098 // https://issues.dlang.org/show_bug.cgi?id=6929
1099
1100 struct S6929
1101 {
1102 T6929 get() const { return T6929.init; }
1103 alias get this;
1104 }
1105 struct T6929
1106 {
1107 S6929 get() const { return S6929.init; }
1108 alias get this;
1109 }
1110 void test6929()
1111 {
1112 T6929 t;
1113 S6929 s;
1114 static assert(!is(typeof(1? t:s)));
1115 }
1116
1117 /***************************************************/
1118 // https://issues.dlang.org/show_bug.cgi?id=7136
1119
1120 void test7136()
1121 {
1122 struct X
1123 {
1124 Object get() immutable { return null; }
1125 alias get this;
1126 }
1127 immutable(X) x;
1128 Object y;
1129 static assert( is(typeof(1?x:y) == Object)); // fails
1130 static assert(!is(typeof(1?x:y) == const(Object))); // fails
1131
1132 struct A
1133 {
1134 int[] get() immutable { return null; }
1135 alias get this;
1136 }
1137 immutable(A) a;
1138 int[] b;
1139 static assert( is(typeof(1?a:b) == int[])); // fails
1140 static assert(!is(typeof(1?a:b) == const(int[]))); // fails
1141 }
1142
1143 /***************************************************/
1144 // https://issues.dlang.org/show_bug.cgi?id=7731
1145
1146 struct A7731
1147 {
1148 int a;
1149 }
1150 template Inherit7731(alias X)
1151 {
1152 X __super;
1153 alias __super this;
1154 }
1155 struct B7731
1156 {
1157 mixin Inherit7731!A7731;
1158 int b;
1159 }
1160
1161 struct PolyPtr7731(X)
1162 {
1163 X* _payload;
1164 static if (is(typeof(X.init.__super)))
1165 {
1166 alias typeof(X.init.__super) Super;
1167 @property auto getSuper(){ return PolyPtr7731!Super(&_payload.__super); }
1168 alias getSuper this;
1169 }
1170 }
1171 template create7731(X)
1172 {
1173 PolyPtr7731!X create7731(T...)(T args){
1174 return PolyPtr7731!X(args);
1175 }
1176 }
1177
1178 void f7731a(PolyPtr7731!A7731 a) {/*...*/}
1179 void f7731b(PolyPtr7731!B7731 b) {f7731a(b);/*...*/}
1180
1181 void test7731()
1182 {
1183 auto b = create7731!B7731();
1184 }
1185
1186 /***************************************************/
1187 // https://issues.dlang.org/show_bug.cgi?id=7808
1188
1189 struct Nullable7808(T)
1190 {
1191 private T _value;
1192
1193 this()(T value)
1194 {
1195 _value = value;
1196 }
1197
1198 @property ref inout(T) get() inout pure @safe
1199 {
1200 return _value;
1201 }
1202 alias get this;
1203 }
1204
1205 class C7808 {}
1206 struct S7808 { C7808 c; }
1207
1208 void func7808(S7808 s) {}
1209
1210 void test7808()
1211 {
1212 auto s = Nullable7808!S7808(S7808(new C7808));
1213 func7808(s);
1214 }
1215
1216 /***************************************************/
1217 // https://issues.dlang.org/show_bug.cgi?id=7945
1218
1219 struct S7945
1220 {
1221 int v;
1222 alias v this;
1223 }
1224 void foo7945(ref int n){}
1225
1226 void test7945()
1227 {
1228 auto s = S7945(1);
1229 foo7945(s); // 1.NG -> OK
1230 s.foo7945(); // 2.OK, ufcs
1231 foo7945(s.v); // 3.OK
1232 s.v.foo7945(); // 4.OK, ufcs
1233 }
1234
1235 /***************************************************/
1236 // https://issues.dlang.org/show_bug.cgi?id=15674
1237 // alias this on out parameter, consistent with 7945 case
1238
1239 struct S15674
1240 {
1241 int v;
1242 alias v this;
1243 }
1244 void foo15674(out int i){ i = 42; }
1245
1246 void test15674()
1247 {
1248 S15674 s;
1249 s.v = 1; foo15674(s); assert(s.v == 42);
1250 s.v = 1; foo15674(s.v); assert(s.v == 42);
1251 s.v = 1; s.foo15674(); assert(s.v == 42);
1252 s.v = 1; s.v.foo15674(); assert(s.v == 42);
1253 }
1254
1255 /***************************************************/
1256 // https://issues.dlang.org/show_bug.cgi?id=7979
1257
1258 void test7979()
1259 {
1260 static struct N
1261 {
1262 int val;
1263 alias val this;
1264 }
1265 N n = N(1);
1266
1267 switch (n)
1268 {
1269 case 0:
1270 assert(0);
1271 case 1:
1272 break;
1273 default:
1274 assert(0);
1275 }
1276
1277 static struct S
1278 {
1279 string val;
1280 alias val this;
1281 }
1282 S s = S("b");
1283
1284 switch (s)
1285 {
1286 case "a":
1287 assert(0);
1288 case "b":
1289 break;
1290 default:
1291 assert(0);
1292 }
1293 }
1294
1295 /***************************************************/
1296 // https://issues.dlang.org/show_bug.cgi?id=7992
1297
1298 struct S7992
1299 {
1300 int[] arr;
1301 alias arr this;
1302 }
1303 S7992 func7992(...)
1304 {
1305 S7992 ret;
1306 ret.arr.length = _arguments.length;
1307 return ret;
1308 }
1309 void test7992()
1310 {
1311 int[] arr;
1312 assert(arr.length == 0);
1313 arr ~= func7992(1, 2); //NG
1314 //arr = func7992(1, 2); //OK
1315 assert(arr.length == 2);
1316 }
1317
1318 /***************************************************/
1319 // https://issues.dlang.org/show_bug.cgi?id=8169
1320
1321 void test8169()
1322 {
1323 static struct ValueImpl
1324 {
1325 static immutable(int) getValue()
1326 {
1327 return 42;
1328 }
1329 }
1330
1331 static struct ValueUser
1332 {
1333 ValueImpl m_valueImpl;
1334 alias m_valueImpl this;
1335 }
1336
1337 static assert(ValueImpl.getValue() == 42); // #0, OK
1338 static assert(ValueUser.getValue() == 42); // #1, NG -> OK
1339 static assert( ValueUser.m_valueImpl .getValue() == 42); // #2, NG -> OK
1340 static assert(typeof(ValueUser.m_valueImpl).getValue() == 42); // #3, OK
1341 }
1342
1343 /***************************************************/
1344 // https://issues.dlang.org/show_bug.cgi?id=8735
1345
1346 struct S8735(alias Arg)
1347 {
1348 alias Arg Val;
1349 alias Val this;
1350 }
1351
1352 struct Tuple9709(T...)
1353 {
1354 alias T expand;
1355 alias expand this;
1356 }
1357
1358 void test8735()
1359 {
1360 alias S8735!1 S;
1361 S s;
1362 int n = s;
1363 assert(n == 1);
1364
1365 // https://issues.dlang.org/show_bug.cgi?id=11502
1366 static void f(int i);
1367 S8735!f sf;
1368
1369 // https://issues.dlang.org/show_bug.cgi?id=9709
1370 alias A = Tuple9709!(1,int,"foo");
1371 A a;
1372 //static assert(A[0] == 1);
1373 static assert(a[0] == 1);
1374 //static assert(is(A[1] == int));
1375 //static assert(is(a[1] == int));
1376 //static assert(A[2] == "foo");
1377 static assert(a[2] == "foo");
1378 }
1379
1380 /***************************************************/
1381 // https://issues.dlang.org/show_bug.cgi?id=9174
1382
1383 void test9174()
1384 {
1385 static struct Foo
1386 {
1387 char x;
1388 alias x this;
1389 }
1390 static assert(is(typeof(true ? 'A' : Foo()) == char));
1391 static assert(is(typeof(true ? Foo() : 100) == int));
1392 }
1393
1394 /***************************************************/
1395 // https://issues.dlang.org/show_bug.cgi?id=9177
1396
1397 struct S9177
1398 {
1399 int foo(int){ return 0; }
1400 alias foo this;
1401 }
1402 pragma(msg, is(S9177 : int));
1403
1404 /***************************************************/
1405 // https://issues.dlang.org/show_bug.cgi?id=9858
1406
1407 struct S9858()
1408 {
1409 @property int get() const
1410 {
1411 return 42;
1412 }
1413 alias get this;
1414 void opAssign(int) {}
1415 }
1416 void test9858()
1417 {
1418 const S9858!() s;
1419 int i = s;
1420 }
1421
1422 /***************************************************/
1423 // https://issues.dlang.org/show_bug.cgi?id=9873
1424
1425 void test9873()
1426 {
1427 struct Tup(T...) { T field; alias field this; }
1428
1429 auto seq1 = Seq!(1, "hi");
1430 assert(Seq!(1, "hi") == Seq!(1, "hi"));
1431 assert(seq1 == Seq!(1, "hi"));
1432 assert(Seq!(1, "hi") == seq1);
1433 assert(seq1 == seq1);
1434
1435 auto seq2 = Seq!(2, "hi");
1436 assert(Seq!(1, "hi") != Seq!(2, "hi"));
1437 assert(seq2 != Seq!(1, "hi"));
1438 assert(Seq!(1, "hi") != seq2);
1439 assert(seq2 != seq1);
1440
1441 auto tup1 = Tup!(int, string)(1, "hi");
1442 assert(Seq!(1, "hi") == tup1);
1443 assert(seq1 == tup1);
1444 assert(tup1 == Seq!(1, "hi"));
1445 assert(tup1 == seq1);
1446
1447 auto tup2 = Tup!(int, string)(2, "hi");
1448 assert(Seq!(1, "hi") != tup2);
1449 assert(seq1 != tup2);
1450 assert(tup2 != Seq!(1, "hi"));
1451 assert(tup2 != seq1);
1452
1453 static assert(!__traits(compiles, seq1 == Seq!(1, "hi", [1,2])));
1454 static assert(!__traits(compiles, tup1 == Seq!(1, "hi", [1,2])));
1455 }
1456
1457 /***************************************************/
1458 // https://issues.dlang.org/show_bug.cgi?id=10178
1459
1460 void test10178()
1461 {
1462 struct S { static int count; }
1463 S s;
1464 assert((s.tupleof == s.tupleof) == true);
1465 assert((s.tupleof != s.tupleof) == false);
1466
1467 S getS()
1468 {
1469 S s;
1470 ++S.count;
1471 return s;
1472 }
1473 assert(getS().tupleof == getS().tupleof);
1474 assert(S.count == 2);
1475 }
1476
1477 /***************************************************/
1478 // https://issues.dlang.org/show_bug.cgi?id=10179
1479
1480 void test10179()
1481 {
1482 struct S { static int count; }
1483 S s;
1484 static assert(s.tupleof.length == 0);
1485 s.tupleof = s.tupleof; // error -> OK
1486
1487 S getS()
1488 {
1489 S s;
1490 ++S.count;
1491 return s;
1492 }
1493 getS().tupleof = getS().tupleof;
1494 assert(S.count == 2);
1495 }
1496
1497 /***************************************************/
1498 // https://issues.dlang.org/show_bug.cgi?id=9890
1499
1500 void test9890()
1501 {
1502 struct RefCounted(T)
1503 {
1504 T _payload;
1505
1506 ref T refCountedPayload()
1507 {
1508 return _payload;
1509 }
1510
1511 alias refCountedPayload this;
1512 }
1513
1514 struct S(int x_)
1515 {
1516 alias x_ x;
1517 }
1518
1519 alias RefCounted!(S!1) Rs;
1520 static assert(Rs.x == 1);
1521 }
1522
1523 /***************************************************/
1524 // https://issues.dlang.org/show_bug.cgi?id=10004
1525
1526 void test10004()
1527 {
1528 static int count = 0;
1529
1530 static S make(S)()
1531 {
1532 ++count; // necessary to make this function impure
1533 S s;
1534 return s;
1535 }
1536
1537 struct SX(T...) {
1538 T field; alias field this;
1539 }
1540 alias S = SX!(int, long);
1541 assert(make!S.field == make!S.field);
1542 assert(count == 2);
1543 }
1544
1545 /***************************************************/
1546 // https://issues.dlang.org/show_bug.cgi?id=10180
1547
1548 template TypeTuple10180(TL...) { alias TypeTuple10180 = TL; }
1549
1550 template Identity10180(alias T) { alias Identity10180 = T; }
1551
1552 struct Tuple10180(Specs...)
1553 {
1554 static if (is(Specs))
1555 {
1556 alias Types = Specs;
1557 Types expand;
1558 alias expand this;
1559 }
1560 else
1561 {
1562 alias Types = TypeTuple10180!(Specs[0]);
1563 Types expand;
1564 mixin("alias Identity10180!(expand[0]) "~Specs[1]~";");
1565
1566 @property
1567 ref Tuple10180!(Specs[0]) _Tuple_super()
1568 {
1569 return *cast(typeof(return)*) (&expand[0]);
1570 }
1571 alias _Tuple_super this;
1572 }
1573 }
1574
1575 void test10180()
1576 {
1577 Tuple10180!(int, "a") x;
1578 auto o1 = x.a.offsetof; // OK
1579 auto o2 = x[0].offsetof; // NG: no property 'offsetof' for type 'int'
1580 auto o3 = x._Tuple_super[0].offsetof; // same as above
1581 assert(o2 == o3);
1582 }
1583
1584 /***************************************************/
1585 // https://issues.dlang.org/show_bug.cgi?id=10456
1586
1587 void test10456()
1588 {
1589 S10456 s1, s2;
1590 auto x = s1 == s2;
1591 }
1592
1593 struct S10456
1594 {
1595 enum E { e };
1596 alias E this;
1597 int[] x;
1598 }
1599
1600 /***************************************************/
1601 // https://issues.dlang.org/show_bug.cgi?id=11261
1602
1603 template Tuple11261(Specs...)
1604 {
1605 struct Tuple11261
1606 {
1607 static if (Specs.length != 4) // anonymous field version
1608 {
1609 alias Specs Types;
1610 Types expand;
1611 alias expand this;
1612 }
1613 else
1614 {
1615 alias Seq!(Specs[0], Specs[2]) Types;
1616 Types expand;
1617 ref inout(Tuple11261!Types) _Tuple_super() inout @trusted
1618 {
1619 return *cast(typeof(return)*) &(expand[0]);
1620 }
1621 // This is mostly to make t[n] work.
1622 alias _Tuple_super this;
1623 }
1624
1625 this()(Types values)
1626 {
1627 expand[] = values[];
1628 }
1629 }
1630 }
1631
1632 interface InputRange11261(E)
1633 {
1634 @property bool empty();
1635 @property E front();
1636 void popFront();
1637
1638 int opApply(int delegate(E));
1639 int opApply(int delegate(size_t, E));
1640
1641 }
1642 template InputRangeObject11261(R)
1643 {
1644 alias typeof(R.init.front()) E;
1645
1646 class InputRangeObject11261 : InputRange11261!E
1647 {
1648 private R _range;
1649
1650 this(R range) { this._range = range; }
1651
1652 @property bool empty() { return _range.empty; }
1653 @property E front() { return _range.front; }
1654 void popFront() { _range.popFront(); }
1655
1656 int opApply(int delegate(E) dg) { return 0; }
1657 int opApply(int delegate(size_t, E) dg) { return 0; }
1658 }
1659 }
1660
1661 // ------
1662
1663 class Container11261
1664 {
1665 alias Tuple11261!(string, "key", string, "value") Key;
1666
1667 InputRange11261!Key opSlice()
1668 {
1669 Range r;
1670 return new InputRangeObject11261!Range(r);
1671 }
1672 private struct Range
1673 {
1674 enum empty = false;
1675 auto popFront() {}
1676 auto front() { return Key("myKey", "myValue"); }
1677 }
1678 }
1679
1680 void test11261()
1681 {
1682 auto container = new Container11261();
1683 foreach (k, v; container) // map the tuple of container[].front to (k, v)
1684 {
1685 static assert(is(typeof(k) == string) && is(typeof(v) == string));
1686 break;
1687 }
1688 }
1689
1690 /***************************************************/
1691 // https://issues.dlang.org/show_bug.cgi?id=11333
1692
1693 alias id11333(a...) = a;
1694
1695 struct Unit11333
1696 {
1697 enum value = Unit11333.init.tupleof;
1698 alias value this;
1699 }
1700
1701 void test11333()
1702 {
1703 void foo() {}
1704
1705 id11333!() unit;
1706 unit = unit; // ok
1707 foo(unit); // ok
1708
1709 unit = Unit11333.value; // ok
1710 foo(Unit11333.value); // ok
1711
1712 Unit11333 unit2;
1713 unit = unit2; // ok <- segfault
1714 }
1715
1716 /***************************************************/
1717 // https://issues.dlang.org/show_bug.cgi?id=11538
1718
1719 struct NullableRef11538(T)
1720 {
1721 T* _value;
1722 inout(T) get() inout { return *_value; }
1723 alias get this;
1724 }
1725
1726 struct S11538
1727 {
1728 NullableRef11538!S11538 parent;
1729 }
1730
1731 /***************************************************/
1732 // https://issues.dlang.org/show_bug.cgi?id=11800
1733
1734 struct A11800
1735 {
1736 B11800 b;
1737 alias b this;
1738 }
1739
1740 struct B11800
1741 {
1742 static struct Value {}
1743 Value value;
1744 alias value this;
1745
1746 void foo(ref const B11800 rhs)
1747 {
1748 }
1749 }
1750
1751 void test11800()
1752 {
1753 A11800 a;
1754 B11800 b;
1755 b.foo(a);
1756 }
1757
1758 /***************************************************/
1759 // https://issues.dlang.org/show_bug.cgi?id=12008
1760
1761 struct RefCounted12008(T)
1762 {
1763 struct RefCountedStore
1764 {
1765 private struct Impl
1766 {
1767 T _payload;
1768 }
1769
1770 private void initialize(A...)(auto ref A args)
1771 {
1772 import core.memory;
1773 }
1774
1775 void ensureInitialized()
1776 {
1777 initialize();
1778 }
1779
1780 }
1781 RefCountedStore _refCounted;
1782
1783 void opAssign(T rhs)
1784 {
1785 }
1786
1787 int refCountedPayload()
1788 {
1789 _refCounted.ensureInitialized();
1790 return 0;
1791 }
1792
1793 int refCountedPayload() inout
1794 {
1795 return 0;
1796 }
1797
1798 alias refCountedPayload this;
1799 }
1800
1801 struct SharedInput12008
1802 {
1803 Group12008 unused;
1804 }
1805
1806 struct Group12008
1807 {
1808 RefCounted12008!SharedInput12008 _allGroups;
1809 }
1810
1811 /***************************************************/
1812 // https://issues.dlang.org/show_bug.cgi?id=12038
1813
1814 bool f12038(void* p) { return true; }
1815
1816 struct S12038
1817 {
1818 @property p() { f12038(&this); }
1819 alias p this;
1820 }
1821
1822 /***************************************************/
1823 // https://issues.dlang.org/show_bug.cgi?id=13490
1824
1825 struct S13490
1826 {
1827 int i;
1828 alias i this;
1829 }
1830
1831 struct T13490
1832 {
1833 S13490[] a1, a2;
1834 }
1835
1836 void test13490()
1837 {
1838 T13490 t;
1839
1840 (true ? t.a1 : t.a2) ~= S13490(1);
1841 assert(t.a1 == [S13490(1)]);
1842 assert(t.a2 == []);
1843
1844 (false ? t.a1 : t.a2) ~= S13490(2);
1845 assert(t.a1 == [S13490(1)]);
1846 assert(t.a2 == [S13490(2)]);
1847 }
1848
1849 /***************************************************/
1850 // https://issues.dlang.org/show_bug.cgi?id=11355
1851
1852 struct A11355
1853 {
1854 static int postblit;
1855 this(this) { ++postblit; }
1856 }
1857
1858 struct B11355
1859 {
1860 A11355 a;
1861 alias a this;
1862 }
1863
1864 B11355 make11355()
1865 {
1866 return B11355();
1867 }
1868 void test11355()
1869 {
1870 A11355 a1 = make11355();
1871 assert(A11355.postblit == 1);
1872 }
1873
1874 /***************************************************/
1875 // https://issues.dlang.org/show_bug.cgi?id=13009
1876
1877 struct T13009
1878 {
1879 void put(char c) {}
1880 }
1881
1882 struct S13009(bool rev)
1883 {
1884 T13009 t;
1885
1886 static if (!rev)
1887 {
1888 @property T13009 getT() { return t; }
1889 @property inout(T13009) getT() inout { return t; }
1890 }
1891 else
1892 {
1893 @property inout(T13009) getT() inout { return t; }
1894 @property T13009 getT() { return t; }
1895 }
1896
1897 alias getT this;
1898 }
1899
1900 void test13009()
1901 {
1902 foreach (bool rev; Seq!(false, true))
1903 {
1904 alias S = S13009!rev;
1905
1906 alias MS = S;
1907 alias CS = const(S);
1908 alias WS = inout( S);
1909 alias WCS = inout(const S);
1910 alias SMS = shared( S);
1911 alias SCS = shared( const S);
1912 alias SWS = shared(inout S);
1913 alias SWCS = shared(inout const S);
1914 alias IS = immutable(S);
1915
1916 alias MSput = MS .put;
1917 alias CSput = CS .put;
1918 alias WSput = WS .put;
1919 alias WCSput = WCS.put;
1920 static assert(!__traits(compiles, { alias SMSput = SMS .put; }));
1921 static assert(!__traits(compiles, { alias SCSput = SCS .put; }));
1922 static assert(!__traits(compiles, { alias SWSput = SWS .put; }));
1923 static assert(!__traits(compiles, { alias SWCSput = SWCS.put; }));
1924 alias ISput = IS .put;
1925 }
1926 }
1927
1928 /***************************************************/
1929 // https://issues.dlang.org/show_bug.cgi?id=14806
1930
1931 struct Nullable14806
1932 {
1933 float get() { return float.nan; }
1934 alias get this;
1935 }
1936
1937 struct Foo14806(T)
1938 {
1939 T bar;
1940 Nullable14806 baz;
1941 }
1942
1943 void test14806()
1944 {
1945 Foo14806!int a, b;
1946 assert(a == b);
1947 // ==> a.tupleof != b.tupleof
1948 // ==> a.bar != b.bar || a.baz.get() != b.baz.get()
1949
1950 Foo14806!string c, d;
1951 assert(c == d);
1952 // ==> c.tupleof != d.tupleof
1953 // ==> c.bar != d.bar || c.baz.get() != d.baz.get()
1954 }
1955
1956 /***************************************************/
1957 // https://issues.dlang.org/show_bug.cgi?id=14948
1958
1959 struct RefCounted14948(T)
1960 {
1961 struct Impl
1962 {
1963 T data;
1964 }
1965 Impl* impl;
1966
1967 @property ref T payload() { return impl.data; }
1968
1969 alias payload this;
1970 }
1971
1972 struct HTTP14948
1973 {
1974 struct Impl
1975 {
1976 }
1977
1978 RefCounted14948!Impl p;
1979 }
1980
1981 void test14948()
1982 {
1983 int[HTTP14948] aa;
1984 }
1985
1986 /***************************************************/
1987 // https://issues.dlang.org/show_bug.cgi?id=15292
1988
1989 struct NullableRef15292(T)
1990 {
1991 inout(T) get() inout
1992 {
1993 assert(false);
1994 }
1995
1996 alias get this;
1997 }
1998
1999 struct S15292
2000 {
2001 NullableRef15292!S15292 n; // -> no segfault
2002
2003 /* The field 'n' contains alias this, so to use it for the equality,
2004 * following helper function is automatically generated in buildXopEquals().
2005 *
2006 * static bool __xopEquals(ref const S15292 p, ref const S15292 q)
2007 * {
2008 * return p == q;
2009 * }
2010 *
2011 * In its definition, const(S15292) equality is analyzed. It fails, then
2012 * the error is gagged.
2013 */
2014 }
2015
2016 /***************************************************/
2017
2018 struct S19284a { int x; }
2019 struct S19284b
2020 {
2021 S19284a s;
2022 alias s this;
2023 int t;
2024 void f()
2025 {
2026 void wrapped()
2027 {
2028 x = 1;
2029 t = 1;
2030 }
2031 wrapped(); // <-- 'x' not found, whereas 's.x' works fine
2032 }
2033
2034 void f1()
2035 {
2036 x = 2;
2037 }
2038
2039 void f2()
2040 {
2041 int x;
2042 void wrapped()
2043 {
2044 x = 7;
2045 }
2046 wrapped();
2047 assert(x == 7);
2048 }
2049
2050 void f3()
2051 {
2052 void wrapped()
2053 {
2054 void wrapped2()
2055 {
2056 x = 5;
2057 }
2058 wrapped2();
2059 }
2060 wrapped();
2061 }
2062 }
2063
2064 void test19284()
2065 {
2066 S19284b t;
2067
2068 // nested function modifies alias this
2069 t.f();
2070 assert(t.x == 1);
2071 assert(t.t == 1);
2072
2073 // member function modifies alias this
2074 t.f1();
2075 assert(t.x == 2);
2076
2077 // nested function does not modify alias this when it is shadowd by a local variable
2078 t.f2();
2079 assert(t.x == 2);
2080
2081 // multiple levels of nesting
2082 t.f3();
2083 assert(t.x == 5);
2084 }
2085
2086 // 16633
2087
2088 class Item
2089 {
2090 alias children this;
2091 Item[] children;
2092 void populate()
2093 {
2094 children ~= new Item(); // Item is seen as []
2095 assert(children.length == 1);
2096 }
2097 }
2098
2099 void test16633()
2100 {
2101 Item root = new Item();
2102 root.populate;
2103 }
2104
2105 /***************************************************/
2106 // https://issues.dlang.org/show_bug.cgi?id=13009
2107
2108 struct RefCounted13009_2(T)
2109 {
2110 ref T refCountedPayload()
2111 {
2112 assert(false);
2113 }
2114
2115 ref inout(T) refCountedPayload() inout
2116 {
2117 assert(false);
2118 }
2119
2120 alias refCountedPayload this;
2121 }
2122
2123 struct S13009_2
2124 {
2125 struct Payload
2126 {
2127 int[] data;
2128 }
2129
2130 RefCounted13009_2!Payload payload;
2131 alias X = typeof(payload.data[0]);
2132
2133 void foo()
2134 {
2135 payload.data[0] = 0;
2136 }
2137 }
2138
2139 /***************************************************/
2140
2141 int main()
2142 {
2143 test1();
2144 test2();
2145 test3();
2146 test4();
2147 test5();
2148 test4617a();
2149 test4617b();
2150 test4773();
2151 test5188();
2152 test6();
2153 test7();
2154 test2781();
2155 test6546();
2156 test6736();
2157 test2777a();
2158 test2777b();
2159 test5679();
2160 test6508();
2161 test6508x();
2162 test6369a();
2163 test6369b();
2164 test6369c();
2165 test6369d();
2166 test6434();
2167 test6366();
2168 test6711();
2169 test12161();
2170 test6759();
2171 test6832();
2172 test6928();
2173 test6929();
2174 test7136();
2175 test7731();
2176 test7808();
2177 test7945();
2178 test15674();
2179 test7979();
2180 test7992();
2181 test8169();
2182 test8735();
2183 test9174();
2184 test9858();
2185 test9873();
2186 test10178();
2187 test10179();
2188 test9890();
2189 test10004();
2190 test10180();
2191 test10456();
2192 test11333();
2193 test11800();
2194 test13490();
2195 test11355();
2196 test14806();
2197 test19284();
2198 test16633();
2199
2200 printf("Success\n");
2201 return 0;
2202 }