Debian Codegraph @

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

raw
1 // PERMUTE_ARGS: -inline -O
2 // REQUIRED_ARGS: -preview=fieldwise
3
4 // Test operator overloading
5
6 extern (C) int printf(const(char*) fmt, ...);
7
8 template Seq(T...){ alias T Seq; }
9
10 bool thrown(E, T)(lazy T val)
11 {
12 try { val(); return false; }
13 catch (E e) { return true; }
14 }
15
16 void stompStack() { int[256] sa = 0xdeadbeef; }
17
18 /**************************************/
19
20 class A
21 {
22 string opUnary(string s)()
23 {
24 printf("A.opUnary!(%.*s)\n", cast(int)s.length, s.ptr);
25 return s;
26 }
27 }
28
29 void test1()
30 {
31 auto a = new A();
32
33 +a;
34 -a;
35 ~a;
36 *a;
37 ++a;
38 --a;
39
40 auto x = a++;
41 assert(x == a);
42 auto y = a--;
43 assert(y == a);
44 }
45
46 /**************************************/
47
48 class A2
49 {
50 T opCast(T)()
51 {
52 auto s = T.stringof;
53 printf("A.opCast!(%.*s)\n", cast(int)s.length, s.ptr);
54 return T.init;
55 }
56 }
57
58
59 void test2()
60 {
61 auto a = new A2();
62
63 auto x = cast(int)a;
64 assert(x == 0);
65
66 auto y = cast(char)a;
67 assert(y == char.init);
68 }
69
70 /**************************************/
71
72 struct A3
73 {
74 int opBinary(string s)(int i)
75 {
76 printf("A.opBinary!(%.*s)\n", cast(int)s.length, s.ptr);
77 return 0;
78 }
79
80 int opBinaryRight(string s)(int i) if (s == "/" || s == "*")
81 {
82 printf("A.opBinaryRight!(%.*s)\n", cast(int)s.length, s.ptr);
83 return 0;
84 }
85
86 T opCast(T)()
87 {
88 auto s = T.stringof;
89 printf("A.opCast!(%.*s)\n", cast(int)s.length, s.ptr);
90 return T.init;
91 }
92 }
93
94
95 void test3()
96 {
97 A3 a;
98
99 a + 3;
100 4 * a;
101 4 / a;
102 a & 5;
103 }
104
105 /**************************************/
106
107 struct A4
108 {
109 int opUnary(string s)()
110 {
111 printf("A.opUnary!(%.*s)\n", cast(int)s.length, s.ptr);
112 return 0;
113 }
114
115 T opCast(T)()
116 {
117 auto s = T.stringof;
118 printf("A.opCast!(%.*s)\n", cast(int)s.length, s.ptr);
119 return T.init;
120 }
121 }
122
123
124 void test4()
125 {
126 A4 a;
127
128 if (a)
129 int x = 3;
130 if (!a)
131 int x = 3;
132 if (!!a)
133 int x = 3;
134 }
135
136 /**************************************/
137
138 class A5
139 {
140 override bool opEquals(Object o)
141 {
142 printf("A.opEquals!(%p)\n", o);
143 return 1;
144 }
145
146 int opUnary(string s)()
147 {
148 printf("A.opUnary!(%.*s)\n", cast(int)s.length, s.ptr);
149 return 0;
150 }
151
152 T opCast(T)()
153 {
154 auto s = T.stringof;
155 printf("A.opCast!(%.*s)\n", cast(int)s.length, s.ptr);
156 return T.init;
157 }
158 }
159
160 class B5 : A5
161 {
162 override bool opEquals(Object o)
163 {
164 printf("B.opEquals!(%p)\n", o);
165 return 1;
166 }
167 }
168
169
170 void test5()
171 {
172 A5 a = new A5();
173 A5 a2 = new A5();
174 B5 b = new B5();
175 A n = null;
176
177 if (a == a)
178 int x = 3;
179 if (a == a2)
180 int x = 3;
181 if (a == b)
182 int x = 3;
183 if (a == n)
184 int x = 3;
185 if (n == a)
186 int x = 3;
187 if (n == n)
188 int x = 3;
189 }
190
191 /**************************************/
192
193 struct S6
194 {
195 const bool opEquals(ref const S6 b)
196 {
197 printf("S.opEquals(S %p)\n", &b);
198 return true;
199 }
200
201 const bool opEquals(ref const T6 b)
202 {
203 printf("S.opEquals(T %p)\n", &b);
204 return true;
205 }
206 }
207
208 struct T6
209 {
210 const bool opEquals(ref const T6 b)
211 {
212 printf("T.opEquals(T %p)\n", &b);
213 return true;
214 }
215 /+
216 const bool opEquals(ref const S6 b)
217 {
218 printf("T.opEquals(S %p)\n", &b);
219 return true;
220 }
221 +/
222 }
223
224
225 void test6()
226 {
227 S6 s1;
228 S6 s2;
229
230 if (s1 == s2)
231 int x = 3;
232
233 T6 t;
234
235 if (s1 == t)
236 int x = 3;
237
238 if (t == s2)
239 int x = 3;
240 }
241
242 /**************************************/
243
244 struct S7
245 {
246 const int opCmp(ref const S7 b)
247 {
248 printf("S.opCmp(S %p)\n", &b);
249 return -1;
250 }
251
252 const int opCmp(ref const T7 b)
253 {
254 printf("S.opCmp(T %p)\n", &b);
255 return -1;
256 }
257 }
258
259 struct T7
260 {
261 const int opCmp(ref const T7 b)
262 {
263 printf("T.opCmp(T %p)\n", &b);
264 return -1;
265 }
266 /+
267 const int opCmp(ref const S7 b)
268 {
269 printf("T.opCmp(S %p)\n", &b);
270 return -1;
271 }
272 +/
273 }
274
275
276 void test7()
277 {
278 S7 s1;
279 S7 s2;
280
281 if (s1 < s2)
282 int x = 3;
283
284 T7 t;
285
286 if (s1 < t)
287 int x = 3;
288
289 if (t < s2)
290 int x = 3;
291 }
292
293 /**************************************/
294
295 struct A8
296 {
297 int opUnary(string s)()
298 {
299 printf("A.opUnary!(%.*s)\n", cast(int)s.length, s.ptr);
300 return 0;
301 }
302
303 int opIndexUnary(string s, T)(T i)
304 {
305 printf("A.opIndexUnary!(%.*s)(%d)\n", cast(int)s.length, s.ptr, i);
306 return 0;
307 }
308
309 int opIndexUnary(string s, T)(T i, T j)
310 {
311 printf("A.opIndexUnary!(%.*s)(%d, %d)\n", cast(int)s.length, s.ptr, i, j);
312 return 0;
313 }
314
315 int opSliceUnary(string s)()
316 {
317 printf("A.opSliceUnary!(%.*s)()\n", cast(int)s.length, s.ptr);
318 return 0;
319 }
320
321 int opSliceUnary(string s, T)(T i, T j)
322 {
323 printf("A.opSliceUnary!(%.*s)(%d, %d)\n", cast(int)s.length, s.ptr, i, j);
324 return 0;
325 }
326 }
327
328
329 void test8()
330 {
331 A8 a;
332
333 -a;
334 -a[3];
335 -a[3, 4];
336 -a[];
337 -a[5 .. 6];
338 --a[3];
339 }
340
341 /**************************************/
342
343 struct A9
344 {
345 int opOpAssign(string s)(int i)
346 {
347 printf("A.opOpAssign!(%.*s)\n", cast(int)s.length, s.ptr);
348 return 0;
349 }
350
351 int opIndexOpAssign(string s, T)(int v, T i)
352 {
353 printf("A.opIndexOpAssign!(%.*s)(%d, %d)\n", cast(int)s.length, s.ptr, v, i);
354 return 0;
355 }
356
357 int opIndexOpAssign(string s, T)(int v, T i, T j)
358 {
359 printf("A.opIndexOpAssign!(%.*s)(%d, %d, %d)\n", cast(int)s.length, s.ptr, v, i, j);
360 return 0;
361 }
362
363 int opSliceOpAssign(string s)(int v)
364 {
365 printf("A.opSliceOpAssign!(%.*s)(%d)\n", cast(int)s.length, s.ptr, v);
366 return 0;
367 }
368
369 int opSliceOpAssign(string s, T)(int v, T i, T j)
370 {
371 printf("A.opSliceOpAssign!(%.*s)(%d, %d, %d)\n", cast(int)s.length, s.ptr, v, i, j);
372 return 0;
373 }
374 }
375
376
377 void test9()
378 {
379 A9 a;
380
381 a += 8;
382 a -= 8;
383 a *= 8;
384 a /= 8;
385 a %= 8;
386 a &= 8;
387 a |= 8;
388 a ^= 8;
389 a <<= 8;
390 a >>= 8;
391 a >>>= 8;
392 a ~= 8;
393 a ^^= 8;
394
395 a[3] += 8;
396 a[3] -= 8;
397 a[3] *= 8;
398 a[3] /= 8;
399 a[3] %= 8;
400 a[3] &= 8;
401 a[3] |= 8;
402 a[3] ^= 8;
403 a[3] <<= 8;
404 a[3] >>= 8;
405 a[3] >>>= 8;
406 a[3] ~= 8;
407 a[3] ^^= 8;
408
409 a[3, 4] += 8;
410 a[] += 8;
411 a[5 .. 6] += 8;
412 }
413
414 /**************************************/
415
416 struct BigInt
417 {
418 int opEquals(T)(T n) const
419 {
420 return 1;
421 }
422
423 int opEquals(T:int)(T n) const
424 {
425 return 1;
426 }
427
428 int opEquals(T:const(BigInt))(T n) const
429 {
430 return 1;
431 }
432
433 }
434
435 int decimal(BigInt b, const BigInt c)
436 {
437 while (b != c) {
438 }
439 return 1;
440 }
441
442 /**************************************/
443
444 struct Foo10
445 {
446 int opUnary(string op)() { return 1; }
447 }
448
449 void test10()
450 {
451 Foo10 foo;
452 foo++;
453 }
454
455 /**************************************/
456
457 struct S4913
458 {
459 bool opCast(T : bool)() { return true; }
460 }
461
462 int bug4913()
463 {
464 if (S4913 s = S4913()) { return 83; }
465 return 9;
466 }
467
468 static assert(bug4913() == 83);
469
470 /**************************************/
471 // https://issues.dlang.org/show_bug.cgi?id=5551
472
473 struct Foo11 {
474 Foo11 opUnary(string op:"++")() {
475 return this;
476 }
477 Foo11 opBinary(string op)(int y) {
478 return this;
479 }
480 }
481
482 void test11()
483 {
484 auto f = Foo11();
485 f++;
486 }
487
488 /**************************************/
489 // https://issues.dlang.org/show_bug.cgi?id=4099
490
491 struct X4099
492 {
493 int x;
494 alias x this;
495
496 typeof(this) opUnary (string operator) ()
497 {
498 printf("operator called\n");
499 return this;
500 }
501 }
502
503 void test4099()
504 {
505 X4099 x;
506 X4099 r1 = ++x; //operator called
507 X4099 r2 = x++; //BUG! (alias this used. returns int)
508 }
509
510 /**************************************/
511
512 void test12()
513 {
514 static int opeq;
515
516 // xopEquals OK
517 static struct S1a { const bool opEquals( const typeof(this) rhs) { ++opeq; return false; } }
518 static struct S1b { const bool opEquals(ref const typeof(this) rhs) { ++opeq; return false; } }
519 static struct S1c { const bool opEquals( typeof(this) rhs) { ++opeq; return false; } }
520
521 // xopEquals NG
522 static struct S2a { bool opEquals( typeof(this) rhs) { ++opeq; return false; } }
523
524 foreach (S; Seq!(S1a, S1b, S1c))
525 {
526 S s;
527 opeq = 0;
528 assert(s != s); // call opEquals directly
529 assert(!typeid(S).equals(&s, &s)); // -> xopEquals (-> __xopEquals) -> opEquals
530 assert(opeq == 2);
531 }
532
533 foreach (S; Seq!(S2a))
534 {
535 S s;
536 opeq = 0;
537 assert(s != s);
538 assert(thrown!Error(!typeid(S).equals(&s, &s)));
539 // Error("notImplemented") thrown
540 assert(opeq == 1);
541 }
542 }
543
544 /**************************************/
545
546 void test13()
547 {
548 static int opeq;
549
550 struct X
551 {
552 const bool opEquals(const X){ ++opeq; return false; }
553 }
554 struct S
555 {
556 X x;
557 }
558
559 S makeS(){ return S(); }
560
561 S s;
562 opeq = 0;
563 assert(s != s);
564 assert(makeS() != s);
565 assert(s != makeS());
566 assert(makeS() != makeS());
567 assert(opeq == 4);
568
569 // built-in opEquals == const bool opEquals(const S rhs);
570 assert(s != s);
571 assert(opeq == 5);
572
573 // xopEquals
574 assert(!typeid(S).equals(&s, &s));
575 assert(opeq == 6);
576 }
577
578 /**************************************/
579
580 void test14()
581 {
582 static int opeq;
583
584 struct S
585 {
586 const bool opEquals(T)(const T rhs) { ++opeq; return false; }
587 }
588
589 S makeS(){ return S(); }
590
591 S s;
592 opeq = 0;
593 assert(s != s);
594 assert(makeS() != s);
595 assert(s != makeS());
596 assert(makeS() != makeS());
597 assert(opeq == 4);
598
599 // xopEquals (-> __xxopEquals) -> template opEquals
600 assert(!typeid(S).equals(&s, &s));
601 assert(opeq == 5);
602 }
603
604 /**************************************/
605
606 void test15()
607 {
608 struct S
609 {
610 const bool opEquals(T)(const(T) rhs)
611 if (!is(T == S))
612 { return false; }
613
614 @disable const bool opEquals(T)(const(T) rhs)
615 if (is(T == S))
616 { return false; }
617 }
618
619 S makeS(){ return S(); }
620
621 S s;
622 static assert(!__traits(compiles, s != s));
623 static assert(!__traits(compiles, makeS() != s));
624 static assert(!__traits(compiles, s != makeS()));
625 static assert(!__traits(compiles, makeS() != makeS()));
626
627 // xopEquals (-> __xxopEquals) -> Error thrown
628 assert(thrown!Error(!typeid(S).equals(&s, &s)));
629 }
630
631 /**************************************/
632
633 void test16()
634 {
635 struct X
636 {
637 int n;
638 const bool opEquals(T)(T t)
639 {
640 return false;
641 }
642 }
643 struct S
644 {
645 X x;
646 }
647
648 S s1, s2;
649 assert(s1 != s2);
650 // field template opEquals should call
651 }
652
653 /**************************************/
654
655 void test17()
656 {
657 static int opeq = 0;
658
659 struct S
660 {
661 bool opEquals(ref S rhs) { ++opeq; return false; }
662 }
663 S[] sa1 = new S[3];
664 S[] sa2 = new S[3];
665 assert(sa1 != sa2); // isn't used TypeInfo.equals
666 assert(opeq == 1);
667
668 const(S)[] csa = new const(S)[3];
669 static assert(!__traits(compiles, csa == sa1));
670 static assert(!__traits(compiles, sa1 == csa));
671 static assert(!__traits(compiles, csa == csa));
672 }
673
674 /**************************************/
675 // https://issues.dlang.org/show_bug.cgi?id=3789
676
677 bool test3789()
678 {
679 static struct Float
680 {
681 double x;
682 }
683 Float f;
684 assert(f.x != f.x); // NaN != NaN
685 assert(f != f);
686
687 static struct Array
688 {
689 int[] x;
690 }
691 Array a1 = Array([1,2,3].dup);
692 Array a2 = Array([1,2,3].dup);
693 if (!__ctfe)
694 { // Currently doesn't work this in CTFE - may or may not a bug.
695 assert(a1.x !is a2.x);
696 }
697 assert(a1.x == a2.x);
698 assert(a1 == a2);
699
700 static struct AA
701 {
702 int[int] x;
703 }
704 AA aa1 = AA([1:1,2:2,3:3]);
705 AA aa2 = AA([1:1,2:2,3:3]);
706 if (!__ctfe)
707 { // Currently doesn't work this in CTFE - may or may not a bug.
708 assert(aa1.x !is aa2.x);
709 }
710 if (!__ctfe)
711 { // This is definitely a bug. Should work in CTFE.
712 assert(aa1.x == aa2.x);
713 assert(aa1 == aa2);
714 }
715
716 if (!__ctfe)
717 { // Currently union operation is not supported in CTFE.
718 union U1
719 {
720 double x;
721 }
722 static struct UnionA
723 {
724 int[] a;
725 U1 u;
726 }
727 auto ua1 = UnionA([1,2,3]);
728 auto ua2 = UnionA([1,2,3]);
729 assert(ua1.u.x is ua2.u.x);
730 assert(ua1.u.x != ua2.u.x);
731 assert(ua1 != ua2);
732 ua1.u.x = 1.0;
733 ua2.u.x = 1.0;
734 assert(ua1.u.x is ua2.u.x);
735 assert(ua1.u.x == ua2.u.x);
736 assert(ua1 == ua2);
737 ua1.u.x = double.nan;
738 assert(ua1.u.x !is ua2.u.x);
739 assert(ua1.u.x != ua2.u.x);
740 assert(ua1 != ua2);
741
742 union U2
743 {
744 int[] a;
745 }
746 static struct UnionB
747 {
748 double x;
749 U2 u;
750 }
751 auto ub1 = UnionB(1.0);
752 auto ub2 = UnionB(1.0);
753 assert(ub1 == ub2);
754 ub1.u.a = [1,2,3].dup;
755 ub2.u.a = [1,2,3].dup;
756 assert(ub1.u.a !is ub2.u.a);
757 assert(ub1.u.a == ub2.u.a);
758 assert(ub1 == ub2);
759 ub2.u.a = ub1.u.a;
760 assert(ub1.u.a is ub2.u.a);
761 assert(ub1.u.a == ub2.u.a);
762 assert(ub1 == ub2);
763 }
764
765 if (!__ctfe)
766 { // This is definitely a bug. Should work in CTFE.
767 static struct Class
768 {
769 Object x;
770 }
771 static class X
772 {
773 override bool opEquals(Object o){ return true; }
774 }
775
776 Class c1a = Class(new Object());
777 Class c2a = Class(new Object());
778 assert(c1a.x !is c2a.x);
779 assert(c1a.x != c2a.x);
780 assert(c1a != c2a); // Pass, Object.opEquals works like bitwise compare
781
782 Class c1b = Class(new X());
783 Class c2b = Class(new X());
784 assert(c1b.x !is c2b.x);
785 assert(c1b.x == c2b.x);
786 assert(c1b == c2b); // Fails, should pass
787 }
788 return true;
789 }
790 static assert(test3789());
791
792 struct S
793 {
794 bool opEquals(ref const S) { return false; }
795 }
796
797 struct T
798 {
799 S s;
800 int value;
801 alias value this;
802 }
803
804 void test11161()
805 {
806 T t1, t2;
807 assert(t1.tupleof != t2.tupleof);
808 assert(t1 != t2); // fails
809 }
810
811 /**************************************/
812 // https://issues.dlang.org/show_bug.cgi?id=5810
813
814 struct Bug5810
815 {
816 void opUnary(string op)() {}
817 }
818
819 struct Foo5810
820 {
821 Bug5810 x;
822 void bar() { x++; }
823 }
824
825 /**************************************/
826 // https://issues.dlang.org/show_bug.cgi?id=6798
827
828 struct Tuple6798(T...)
829 {
830 T field;
831 alias field this;
832
833 bool opEquals(Tuple6798 rhs)
834 {
835 foreach (i, _; T)
836 {
837 if (this[i] != rhs[i])
838 return false;
839 }
840 return true;
841 }
842 }
843 auto tuple6798(T...)(T args)
844 {
845 return Tuple6798!T(args);
846 }
847
848 int test6798a()
849 {
850 //import std.typecons;
851 alias tuple6798 tuple;
852
853 static struct S1
854 {
855 auto opDollar(size_t dim)()
856 {
857 return 99;
858 }
859 auto opSlice(int dim)(int lwr, int upr)
860 {
861 return [dim, lwr, upr];
862 }
863
864 auto opIndex(A...)(A indices)
865 {
866 return tuple(" []", indices);
867 }
868 auto opIndexUnary(string op, A...)(A indices)
869 {
870 return tuple(op~"[]", indices);
871 }
872 auto opIndexAssign(A...)(string s, A indices)
873 {
874 return tuple("[] =", s, indices);
875 }
876 auto opIndexOpAssign(string op, A...)(string s, A indices)
877 {
878 return tuple("[]"~op~"=", s, indices);
879 }
880 }
881 S1 s1;
882 assert( s1[] == tuple(" []"));
883 assert( s1[10] == tuple(" []", 10));
884 assert( s1[10, 20] == tuple(" []", 10, 20));
885 assert( s1[10..20] == tuple(" []", [0, 10, 20]));
886 assert(+s1[] == tuple("+[]"));
887 assert(-s1[10] == tuple("-[]", 10));
888 assert(*s1[10, 20] == tuple("*[]", 10, 20));
889 assert(~s1[10..20] == tuple("~[]", [0, 10, 20]));
890 assert((s1[] ="x") == tuple("[] =", "x"));
891 assert((s1[10] ="x") == tuple("[] =", "x", 10));
892 assert((s1[10, 20] ="x") == tuple("[] =", "x", 10, 20));
893 assert((s1[10..20] ="x") == tuple("[] =", "x", [0, 10, 20]));
894 assert((s1[] +="x") == tuple("[]+=", "x"));
895 assert((s1[10] -="x") == tuple("[]-=", "x", 10));
896 assert((s1[10, 20]*="x") == tuple("[]*=", "x", 10, 20));
897 assert((s1[10..20]~="x") == tuple("[]~=", "x", [0, 10, 20]));
898 assert( s1[20..30, 10] == tuple(" []", [0, 20, 30], 10));
899 assert( s1[10, 10..$, $-4, $..2] == tuple(" []", 10, [1,10,99], 99-4, [3,99,2]));
900 assert(+s1[20..30, 10] == tuple("+[]", [0, 20, 30], 10));
901 assert(-s1[10, 10..$, $-4, $..2] == tuple("-[]", 10, [1,10,99], 99-4, [3,99,2]));
902 assert((s1[20..30, 10] ="x") == tuple("[] =", "x", [0, 20, 30], 10));
903 assert((s1[10, 10..$, $-4, $..2] ="x") == tuple("[] =", "x", 10, [1,10,99], 99-4, [3,99,2]));
904 assert((s1[20..30, 10] +="x") == tuple("[]+=", "x", [0, 20, 30], 10));
905 assert((s1[10, 10..$, $-4, $..2]-="x") == tuple("[]-=", "x", 10, [1,10,99], 99-4, [3,99,2]));
906
907 // opIndex exist, but opSlice for multi-dimensional doesn't.
908 static struct S2
909 {
910 auto opSlice(size_t dim)() { return [dim]; }
911 auto opSlice()(size_t lwr, size_t upr) { return [lwr, upr]; }
912
913 auto opIndex(A...)(A indices){ return [[indices]]; }
914 }
915 S2 s2;
916 assert(s2[] == [[]]);
917 assert(s2[1] == [[1]]);
918 assert(s2[1, 2] == [[1, 2]]);
919 assert(s2[1..2] == [1, 2]);
920 static assert(!__traits(compiles, s2[1, 2..3] ));
921 static assert(!__traits(compiles, s2[1..2, 2..3] ));
922
923 // opSlice for multi-dimensional exists, but opIndex for that doesn't.
924 static struct S3
925 {
926 auto opSlice(size_t dim)(size_t lwr, size_t upr) { return [lwr, upr]; }
927
928 auto opIndex(size_t n){ return [[n]]; }
929 auto opIndex(size_t n, size_t m){ return [[n, m]]; }
930 }
931 S3 s3;
932 static assert(!__traits(compiles, s3[] ));
933 assert(s3[1] == [[1]]);
934 assert(s3[1, 2] == [[1, 2]]);
935 static assert(!__traits(compiles, s3[1..2] ));
936 static assert(!__traits(compiles, s3[1, 2..3] ));
937 static assert(!__traits(compiles, s3[1..2, 2..3] ));
938
939 return 0;
940 }
941
942 int test6798b()
943 {
944 static struct Typedef(T)
945 {
946 private T Typedef_payload = T.init;
947
948 alias a = Typedef_payload;
949
950 auto ref opIndex(this X, D...)(auto ref D i) { return a[i]; }
951 auto ref opSlice(this X )() { return a[]; }
952 auto ref opSlice(this X, B, E)(auto ref B b, auto ref E e)
953 {
954 assert(b == 0 && e == 3);
955 return a[b..e];
956 }
957
958 template opDispatch(string name)
959 {
960 // field or property function
961 @property auto ref opDispatch(this X)() { return mixin("a."~name); }
962 @property auto ref opDispatch(this X, V)(auto ref V v) { return mixin("a."~name~" = v"); }
963 }
964
965 static if (is(typeof(a) : E[], E))
966 {
967 auto opDollar() const { return a.length; }
968 }
969 }
970
971 Typedef!(int[]) dollar2;
972 dollar2.length = 3;
973 assert(dollar2.Typedef_payload.length == 3);
974 assert(dollar2[0 .. $] is dollar2[0 .. 3]);
975
976 return 0;
977 }
978
979 int test6798c()
980 {
981 alias T = Tuple6798!(int, int);
982 auto n = T[].init;
983 static assert(is(typeof(n[0]) == Tuple6798!(int, int)));
984
985 return 0;
986 }
987
988 void test6798()
989 {
990 static assert(test6798a() == 0); // CTFE check
991 test6798a();
992 static assert(test6798b() == 0);
993 test6798b();
994 static assert(test6798c() == 0);
995 test6798c();
996 }
997
998 /**************************************/
999 // https://issues.dlang.org/show_bug.cgi?id=12382
1000
1001 struct S12382
1002 {
1003 size_t opDollar() { return 0; }
1004 size_t opIndex(size_t) { return 0; }
1005 }
1006
1007 S12382 func12382() { return S12382(); }
1008
1009 static assert(S12382.init[$] == 0);
1010 static assert(func12382()[$] == 0);
1011 enum e12382a = S12382.init[$];
1012 enum e12382b = func12382()[$];
1013 static v12382a = S12382.init[$];
1014 static v12382b = func12382()[$];
1015
1016 void test12382()
1017 {
1018 static assert(S12382.init[$] == 0);
1019 static assert(func12382()[$] == 0);
1020 enum e12382a = S12382.init[$];
1021 enum e12382b = func12382()[$];
1022 static v12382a = S12382.init[$];
1023 static v12382b = func12382()[$];
1024 }
1025
1026 /**************************************/
1027 // https://issues.dlang.org/show_bug.cgi?id=12904
1028
1029 struct S12904
1030 {
1031 void opIndexAssign(U, A...)(U value, A args)
1032 {
1033 static assert(0);
1034 }
1035 void opSliceAssign(int n)
1036 {
1037 assert(n == 10);
1038 }
1039
1040 size_t opDollar(size_t dim)()
1041 {
1042 return 7;
1043 }
1044
1045 int opSlice(size_t dim)(size_t, size_t to)
1046 {
1047 assert(to == 7);
1048 return 1;
1049 }
1050
1051 int opIndex(int i1, int i2)
1052 {
1053 assert(i1 == 1 && i2 == 1);
1054 return 10;
1055 }
1056 }
1057
1058 void test12904()
1059 {
1060 S12904 s;
1061 s[] = s[0..$, 1];
1062 s[] = s[0..$, 0..$];
1063 }
1064
1065 /**************************************/
1066 // https://issues.dlang.org/show_bug.cgi?id=7641
1067
1068 mixin template Proxy7641(alias a)
1069 {
1070 auto ref opBinaryRight(string op, B)(auto ref B b)
1071 {
1072 return mixin("b "~op~" a");
1073 }
1074 }
1075 struct Typedef7641(T)
1076 {
1077 private T Typedef_payload;
1078
1079 this(T init)
1080 {
1081 Typedef_payload = init;
1082 }
1083
1084 mixin Proxy7641!Typedef_payload;
1085 }
1086
1087 void test7641()
1088 {
1089 class C {}
1090 C c1 = new C();
1091 auto a = Typedef7641!C(c1);
1092 static assert(!__traits(compiles, { C c2 = a; }));
1093 }
1094
1095 /**************************************/
1096 // https://issues.dlang.org/show_bug.cgi?id=8434
1097
1098 void test8434()
1099 {
1100 static class Vector2D(T)
1101 {
1102 T x, y;
1103
1104 this(T x, T y) {
1105 this.x = x;
1106 this.y = y;
1107 }
1108
1109 U opCast(U)() const { assert(0); }
1110 }
1111
1112 alias Vector2D!(short) Vector2s;
1113 alias Vector2D!(float) Vector2f;
1114
1115 Vector2s vs1 = new Vector2s(42, 23);
1116 Vector2s vs2 = new Vector2s(42, 23);
1117
1118 assert(vs1 != vs2);
1119 }
1120
1121 /**************************************/
1122
1123 void test18()
1124 {
1125 // one dimensional indexing
1126 static struct IndexExp
1127 {
1128 int[] opIndex(int a)
1129 {
1130 return [a];
1131 }
1132
1133 int[] opIndexUnary(string op)(int a)
1134 {
1135 return [a];
1136 }
1137
1138 int[] opIndexAssign(int val, int a)
1139 {
1140 return [val, a];
1141 }
1142
1143 int[] opIndexOpAssign(string op)(int val, int a)
1144 {
1145 return [val, a];
1146 }
1147
1148 int opDollar()
1149 {
1150 return 8;
1151 }
1152 }
1153
1154 IndexExp index;
1155 // opIndex
1156 assert(index[8] == [8]);
1157 assert(index[$] == [8]);
1158 assert(index[$-1] == [7]);
1159 assert(index[$-$/2] == [4]);
1160 // opIndexUnary
1161 assert(-index[8] == [8]);
1162 assert(-index[$] == [8]);
1163 assert(-index[$-1] == [7]);
1164 assert(-index[$-$/2] == [4]);
1165 // opIndexAssign
1166 assert((index[8] = 2) == [2, 8]);
1167 assert((index[$] = 2) == [2, 8]);
1168 assert((index[$-1] = 2) == [2, 7]);
1169 assert((index[$-$/2] = 2) == [2, 4]);
1170 // opIndexOpAssign
1171 assert((index[8] += 2) == [2, 8]);
1172 assert((index[$] += 2) == [2, 8]);
1173 assert((index[$-1] += 2) == [2, 7]);
1174 assert((index[$-$/2] += 2) == [2, 4]);
1175
1176 // opDollar is only one-dimensional
1177 static assert(!is(typeof(index[$, $])));
1178 static assert(!is(typeof(-index[$, $])));
1179 static assert(!is(typeof(index[$, $] = 2)));
1180 static assert(!is(typeof(index[$, $] += 2)));
1181
1182 // multi dimensional indexing
1183 static struct ArrayExp
1184 {
1185 int[] opIndex(int a, int b)
1186 {
1187 return [a, b];
1188 }
1189
1190 int[] opIndexUnary(string op)(int a, int b)
1191 {
1192 return [a, b];
1193 }
1194
1195 int[] opIndexAssign(int val, int a, int b)
1196 {
1197 return [val, a, b];
1198 }
1199
1200 int[] opIndexOpAssign(string op)(int val, int a, int b)
1201 {
1202 return [val, a, b];
1203 }
1204
1205 int opDollar(int dim)()
1206 {
1207 return dim;
1208 }
1209 }
1210
1211 ArrayExp array;
1212 // opIndex
1213 assert(array[8, 8] == [8, 8]);
1214 assert(array[$, $] == [0, 1]);
1215 assert(array[$, $-1] == [0, 0]);
1216 assert(array[2, $-$/2] == [2, 1]);
1217 // opIndexUnary
1218 assert(-array[8, 8] == [8, 8]);
1219 assert(-array[$, $] == [0, 1]);
1220 assert(-array[$, $-1] == [0, 0]);
1221 assert(-array[2, $-$/2] == [2, 1]);
1222 // opIndexAssign
1223 assert((array[8, 8] = 2) == [2, 8, 8]);
1224 assert((array[$, $] = 2) == [2, 0, 1]);
1225 assert((array[$, $-1] = 2) == [2, 0, 0]);
1226 assert((array[2, $-$/2] = 2) == [2, 2, 1]);
1227 // opIndexOpAssign
1228 assert((array[8, 8] += 2) == [2, 8, 8]);
1229 assert((array[$, $] += 2) == [2, 0, 1]);
1230 assert((array[$, $-1] += 2) == [2, 0, 0]);
1231 assert((array[2, $-$/2] += 2) == [2, 2, 1]);
1232
1233 // one dimensional slicing
1234 static struct SliceExp
1235 {
1236 int[] opSlice(int a, int b)
1237 {
1238 return [a, b];
1239 }
1240
1241 int[] opSliceUnary(string op)(int a, int b)
1242 {
1243 return [a, b];
1244 }
1245
1246 int[] opSliceAssign(int val, int a, int b)
1247 {
1248 return [val, a, b];
1249 }
1250
1251 int[] opSliceOpAssign(string op)(int val, int a, int b)
1252 {
1253 return [val, a, b];
1254 }
1255
1256 int opDollar()
1257 {
1258 return 8;
1259 }
1260 }
1261
1262 SliceExp slice;
1263 // opSlice
1264 assert(slice[0 .. 8] == [0, 8]);
1265 assert(slice[0 .. $] == [0, 8]);
1266 assert(slice[0 .. $-1] == [0, 7]);
1267 assert(slice[$-3 .. $-1] == [5, 7]);
1268 // opSliceUnary
1269 assert(-slice[0 .. 8] == [0, 8]);
1270 assert(-slice[0 .. $] == [0, 8]);
1271 assert(-slice[0 .. $-1] == [0, 7]);
1272 assert(-slice[$-3 .. $-1] == [5, 7]);
1273 // opSliceAssign
1274 assert((slice[0 .. 8] = 2) == [2, 0, 8]);
1275 assert((slice[0 .. $] = 2) == [2, 0, 8]);
1276 assert((slice[0 .. $-1] = 2) == [2, 0, 7]);
1277 assert((slice[$-3 .. $-1] = 2) == [2, 5, 7]);
1278 // opSliceOpAssign
1279 assert((slice[0 .. 8] += 2) == [2, 0, 8]);
1280 assert((slice[0 .. $] += 2) == [2, 0, 8]);
1281 assert((slice[0 .. $-1] += 2) == [2, 0, 7]);
1282 assert((slice[$-3 .. $-1] += 2) == [2, 5, 7]);
1283
1284 // test different kinds of opDollar
1285 auto dollar(string opDollar)()
1286 {
1287 static struct Dollar
1288 {
1289 size_t opIndex(size_t a) { return a; }
1290 mixin(opDollar);
1291 }
1292 Dollar d;
1293 return d[$];
1294 }
1295 assert(dollar!q{@property size_t opDollar() { return 8; }}() == 8);
1296 assert(dollar!q{template opDollar(size_t dim) { enum opDollar = dim; }}() == 0);
1297 assert(dollar!q{static const size_t opDollar = 8;}() == 8);
1298 assert(dollar!q{enum opDollar = 8;}() == 8);
1299 assert(dollar!q{size_t length() { return 8; } alias length opDollar;}() == 8);
1300 }
1301
1302 /**************************************/
1303
1304 void test19()
1305 {
1306 static struct Foo
1307 {
1308 int[] opSlice(int a, int b)
1309 {
1310 return [a, b];
1311 }
1312
1313 int opDollar(int dim)()
1314 {
1315 return dim;
1316 }
1317 }
1318
1319 Foo foo;
1320 assert(foo[0 .. $] == [0, 0]);
1321 }
1322
1323 /**************************************/
1324 // https://issues.dlang.org/show_bug.cgi?id=9453
1325
1326 struct Foo9453
1327 {
1328 static int ctor = 0;
1329
1330 this(string bar) { ++ctor; }
1331
1332 void opIndex(size_t i) const {}
1333 void opSlice(size_t s, size_t e) const {}
1334
1335 size_t opDollar(int dim)() const if (dim == 0) { return 1; }
1336 }
1337
1338 void test9453()
1339 {
1340 assert(Foo9453.ctor == 0); Foo9453("bar")[$-1];
1341 assert(Foo9453.ctor == 1); Foo9453("bar")[0..$];
1342 assert(Foo9453.ctor == 2);
1343 }
1344
1345 /**************************************/
1346 // https://issues.dlang.org/show_bug.cgi?id=9496
1347
1348 struct S9496
1349 {
1350 static S9496* ptr;
1351
1352 size_t opDollar()
1353 {
1354 assert(ptr is &this);
1355 return 10;
1356 }
1357 void opSlice(size_t , size_t)
1358 {
1359 assert(ptr is &this);
1360 }
1361 void getSlice()
1362 {
1363 assert(ptr is &this);
1364 this[1 .. opDollar()];
1365 this[1 .. $];
1366 }
1367 }
1368
1369 void test9496()
1370 {
1371 S9496 s;
1372 S9496.ptr = &s;
1373 s.getSlice();
1374 s[1 .. $];
1375 }
1376
1377 /**************************************/
1378 // https://issues.dlang.org/show_bug.cgi?id=9689
1379
1380 struct B9689(T)
1381 {
1382 T val;
1383 @disable this(this);
1384
1385 bool opEquals(this X, B)(auto ref B b)
1386 {
1387 //pragma(msg, "+", X, ", B = ", B, ", ref = ", __traits(isRef, b));
1388 return this.val == b.val;
1389 //pragma(msg, "-", X, ", B = ", B, ", ref = ", __traits(isRef, b));
1390 }
1391 }
1392
1393 struct S9689
1394 {
1395 B9689!int num;
1396 }
1397
1398 void test9689()
1399 {
1400 B9689!S9689 b;
1401 }
1402
1403 /**************************************/
1404 // https://issues.dlang.org/show_bug.cgi?id=9694
1405
1406 struct S9694
1407 {
1408 bool opEquals(ref S9694 rhs)
1409 {
1410 assert(0);
1411 }
1412 }
1413 struct T9694
1414 {
1415 S9694 s;
1416 }
1417 void test9694()
1418 {
1419 T9694 t;
1420 assert(thrown!Error(typeid(T9694).equals(&t, &t)));
1421 }
1422
1423 /**************************************/
1424 // https://issues.dlang.org/show_bug.cgi?id=10064
1425
1426 void test10064()
1427 {
1428 static struct S
1429 {
1430 int x = 3;
1431
1432 @disable this();
1433
1434 this(int)
1435 { x = 7; }
1436
1437 int opSlice(size_t, size_t)
1438 { return 0; }
1439
1440 @property size_t opDollar()
1441 {
1442 assert(x == 7 || x == 3); // fails
1443 assert(x == 7);
1444 return 0;
1445 }
1446 }
1447 auto x = S(0)[0 .. $];
1448 }
1449
1450 /**************************************/
1451 // https://issues.dlang.org/show_bug.cgi?id=12585
1452
1453 void test12585()
1454 {
1455 struct Bar
1456 {
1457 int opIndex(size_t index)
1458 {
1459 return 0;
1460 }
1461 }
1462
1463 struct Foo
1464 {
1465 Bar opIndex(size_t index)
1466 {
1467 throw new Exception("Fail");
1468 }
1469 }
1470
1471 Foo foo()
1472 {
1473 return Foo();
1474 }
1475
1476 void catchStuff(E)(lazy E expression)
1477 {
1478 try
1479 expression();
1480 catch (Exception e) {}
1481 }
1482
1483 catchStuff(foo()[0][0]); // OK <- NG
1484 catchStuff(foo().opIndex(0)[0]); // OK
1485 catchStuff(foo()[0].opIndex(0)); // OK
1486 Foo f; catchStuff(f[0][0]); // OK
1487 }
1488
1489 /**************************************/
1490 // https://issues.dlang.org/show_bug.cgi?id=10394
1491
1492 void test10394()
1493 {
1494 alias Seq!(int, int) Pair;
1495 Pair pair;
1496
1497 struct S1
1498 {
1499 int opBinary(string op)(Pair) { return 1; }
1500 bool opEquals(Pair) { return true; }
1501 int opOpAssign(string op)(Pair) { return 1; }
1502 }
1503 S1 s1;
1504 assert((s1 + pair) == 1);
1505 assert((s1 == pair) == true);
1506 assert((s1 *= pair) == 1);
1507
1508 struct S2
1509 {
1510 int opBinaryRight(string op)(Pair lhs) { return 1; }
1511 int opCmp(Pair) { return -1; }
1512 }
1513 S2 s2;
1514 assert((pair in s2) == 1);
1515 assert(s2 < pair);
1516 }
1517
1518 /**************************************/
1519 // https://issues.dlang.org/show_bug.cgi?id=10597
1520
1521 struct R10597
1522 {
1523 void opIndex(int) {}
1524 void opSlice(int, int) {}
1525 int opDollar();
1526 }
1527 R10597 r;
1528
1529 struct S10597
1530 {
1531 static assert(is(typeof(r[0]))); //ok
1532 static assert(is(typeof(r[$]))); //fails
1533
1534 static assert(is(typeof(r[0..0]))); //ok
1535 static assert(is(typeof(r[$..$]))); //fails
1536
1537 void foo()
1538 {
1539 static assert(is(typeof(r[0]))); //ok
1540 static assert(is(typeof(r[$]))); //ok
1541
1542 static assert(is(typeof(r[0..0]))); //ok
1543 static assert(is(typeof(r[$..$]))); //ok
1544 }
1545 }
1546
1547 static assert(is(typeof(r[0]))); //ok
1548 static assert(is(typeof(r[$]))); //fails
1549
1550 static assert(is(typeof(r[0..0]))); //ok
1551 static assert(is(typeof(r[$..$]))); //fails
1552
1553 void test10597()
1554 {
1555 static assert(is(typeof(r[0]))); //ok
1556 static assert(is(typeof(r[$]))); //ok
1557
1558 static assert(is(typeof(r[0..0]))); //ok
1559 static assert(is(typeof(r[$..$]))); //ok
1560 }
1561
1562 /**************************************/
1563 // https://issues.dlang.org/show_bug.cgi?id=10567
1564
1565 // doesn't require thunk
1566 struct S10567x1n { int value; int opCmp(ref const S10567x1n rhs) const { return 0; } }
1567
1568 // requires thunk
1569 struct S10567y1n { int value; int opCmp(const S10567y1n rhs) const { return 0; } }
1570 struct S10567y1t { int value; int opCmp(S)(const S rhs) const { return 0; } }
1571
1572 // doesn't support const comparison
1573 struct S10567z1n { int value; int opCmp(const S10567z1n rhs) { return 0; } }
1574 struct S10567z1t { int value; int opCmp(S)(const S rhs) { return 0; } }
1575
1576 /+
1577 struct S10567x2n { S10567x1n s; this(int n) { s = typeof(s)(n); } alias s this; }
1578
1579 struct S10567y2n { S10567y1n s; this(int n) { s = typeof(s)(n); } alias s this; }
1580 struct S10567y2t { S10567y1t s; this(int n) { s = typeof(s)(n); } alias s this; }
1581
1582 struct S10567z2n { S10567z1n s; this(int n) { s = typeof(s)(n); } alias s this; }
1583 struct S10567z2t { S10567z1t s; this(int n) { s = typeof(s)(n); } alias s this; }
1584
1585 struct S10567d1
1586 {
1587 int value;
1588 int opDispatch(string name, S)(const S rhs) const if (name == "opCmp")
1589 { assert(0); }
1590 }
1591 struct S10567d2
1592 {
1593 int value;
1594 template opDispatch(string name) if (name == "opCmp")
1595 {
1596 int opDispatch(const S rhs) const
1597 { assert(0); }
1598 }
1599 }
1600
1601 // recursive alias this + opCmp searching
1602 struct S10567r1
1603 {
1604 static S10567r2 t;
1605 ref S10567r2 payload() { return t; }
1606 alias payload this;
1607
1608 int opCmp(const S10567r1 s) const { return 0; }
1609 }
1610 struct S10567r2
1611 {
1612 static S10567r1 s;
1613 ref S10567r1 payload() { return s; }
1614 alias payload this;
1615 }
1616 +/
1617
1618 void test10567()
1619 {
1620 foreach (S; Seq!(S10567x1n/+, S10567x2n+/))
1621 {
1622 S sx = S(1);
1623 S sy = S(2);
1624 assert(!(sx < sy) && !(sx > sy));
1625 assert(sx.opCmp(sy) == 0);
1626
1627 assert(typeid(S).compare(&sx, &sy) == 0);
1628 static if (is(S == S10567x1n))
1629 assert(cast(void*)typeid(S).xopCmp == cast(void*)&S.opCmp, S.stringof);
1630 }
1631
1632 foreach (S; Seq!(S10567y1n, S10567y1t/+, S10567y2n, S10567y2t+/))
1633 {
1634 S sx = S(1);
1635 S sy = S(2);
1636 assert(!(sx < sy) && !(sx > sy));
1637 assert(sx.opCmp(sy) == 0);
1638
1639 assert(typeid(S).compare(&sx, &sy) == 0);
1640 }
1641
1642 foreach (S; Seq!(S10567z1n, S10567z1t/+, S10567z2n, S10567z2t+/))
1643 {
1644 S sx = S(1);
1645 S sy = S(2);
1646 assert(!(sx < sy) && !(sx > sy));
1647 assert(sx.opCmp(sy) == 0);
1648 }
1649 /+
1650 foreach (S; Seq!(S10567d1, S10567d2))
1651 {
1652 int[S] aa;
1653 aa[S(1)] = 10; aa[S(1)] = 1;
1654 aa[S(2)] = 20; aa[S(2)] = 2;
1655 assert(aa.length == 2);
1656 foreach (k, v; aa)
1657 assert(k.value == v);
1658
1659 S sx = S(1);
1660 S sy = S(2);
1661
1662 // Don't invoke opDispatch!"opCmp"
1663 assert(typeid(S).compare(&sx, &sy) != 0);
1664 }
1665 +/
1666 }
1667
1668 /**************************************/
1669 // https://issues.dlang.org/show_bug.cgi?id=11062
1670
1671 struct S11062ia
1672 {
1673 struct S1
1674 {
1675 void opIndexAssign(int val, int key) {}
1676 }
1677 struct S2
1678 {
1679 S1 headers;
1680 }
1681
1682 private S2 m_obj;
1683 @property S2 get() { return m_obj; }
1684 alias get this;
1685 }
1686
1687 struct S11062sa
1688 {
1689 struct S1
1690 {
1691 void opSliceAssign(int val, int lwr, int upr) {}
1692 }
1693 struct S2
1694 {
1695 S1 headers;
1696 }
1697
1698 private S2 m_obj;
1699 @property S2 get() { return m_obj; }
1700 alias get this;
1701 }
1702
1703 void test11062()
1704 {
1705 auto sia = S11062ia();
1706 sia.headers[1] = 1; // bug
1707
1708 auto ssa = S11062sa();
1709 ssa.headers[1..2] = 1; // bug
1710 }
1711
1712 /**************************************/
1713 // https://issues.dlang.org/show_bug.cgi?id=11311
1714
1715 void test11311()
1716 {
1717 static int ctor, cpctor, dtor;
1718
1719 static struct S
1720 {
1721 this(int) { ++ctor; }
1722 this(this) { ++cpctor; }
1723 ~this() { ++dtor; }
1724 }
1725 static struct Arr
1726 {
1727 S data;
1728 ref S opIndex(int) return { return data; }
1729 ref S opSlice(int, int) return { return data; }
1730 }
1731
1732 {
1733 Arr a = Arr(S(1));
1734 assert(ctor == 1);
1735 assert(cpctor == 0);
1736 assert(dtor == 0);
1737
1738 auto getA1() { return a; }
1739 //getA1().opIndex(1); // OK
1740 getA1()[1]; // NG
1741
1742 assert(ctor == 1);
1743 assert(cpctor == 1); // getA() returns a copy of a
1744 assert(dtor == 1); // temporary returned by getA() should be destroyed
1745 }
1746 assert(dtor == 2);
1747 assert(ctor + cpctor == dtor);
1748
1749 ctor = cpctor = dtor = 0;
1750
1751 {
1752 Arr a = Arr(S(1));
1753 assert(ctor == 1);
1754 assert(cpctor == 0);
1755 assert(dtor == 0);
1756
1757 auto getA2() { return a; }
1758 //getA2().opSlice(1, 2); // OK
1759 getA2()[1..2]; // NG
1760
1761 assert(ctor == 1);
1762 assert(cpctor == 1); // getA() returns a copy of a
1763 assert(dtor == 1); // temporary returned by getA() should be destroyed
1764 }
1765 assert(dtor == 2);
1766 assert(ctor + cpctor == dtor);
1767 }
1768
1769 /**************************************/
1770 // https://issues.dlang.org/show_bug.cgi?id=12193
1771
1772 void test12193()
1773 {
1774 struct Foo
1775 {
1776 bool bar;
1777 alias bar this;
1778 void opOpAssign(string op)(size_t x)
1779 {
1780 bar = false;
1781 }
1782 }
1783
1784 Foo foo;
1785 foo <<= 1;
1786 }
1787
1788 /**************************************/
1789 // https://issues.dlang.org/show_bug.cgi?id=14057
1790
1791 struct W14057
1792 {
1793 int[] subType;
1794 alias subType this;
1795
1796 W14057 opSlice(size_t, size_t)
1797 {
1798 return this;
1799 }
1800 }
1801
1802 void test14057()
1803 {
1804 auto w = W14057();
1805 W14057 w2 = w[0 .. 1337];
1806 }
1807
1808 /**************************************/
1809
1810 struct Tuple20(T...) { T field; alias field this; }
1811
1812 void test20a()
1813 {
1814 // ae1save in in AssignExp::semantic
1815 int a, b;
1816
1817 struct A1
1818 {
1819 void opIndexAssign(int v, Tuple20!(int, int) ) { a = v; }
1820 Tuple20!(int, int) opSlice(size_t dim)(int, int) { return typeof(return).init; }
1821 }
1822 struct A2
1823 {
1824 A1 a1;
1825 alias a1 this;
1826 int opIndexAssign(int) { return b; }
1827 }
1828
1829 stompStack();
1830 A2 foo() { return A2(); }
1831 foo()[1..2] = 1;
1832 // ref A1 __tmp = foo().a1; __tmp.opIndexAssign(1, __tmp.opSlice!0(1, 2));
1833 assert(a == 1); // should work
1834 assert(b == 0);
1835 }
1836
1837 void test20b()
1838 {
1839 // ae1save in UnaExp::op_overload()
1840 int a, b;
1841
1842 struct A1
1843 {
1844 void opIndexUnary(string op)(Tuple20!(int, int) ) { a = 1; }
1845 Tuple20!(int, int) opSlice(size_t dim)(int, int) { return typeof(return).init; }
1846 void dummy() {} // nessary to make A1 nested struct
1847 }
1848 struct A2
1849 {
1850 A1 a1;
1851 alias a1 this;
1852 int opIndexUnary(string op)(int) { return 0; }
1853 }
1854
1855 stompStack();
1856 A2 foo() { return A2(); }
1857 +foo()[1..2];
1858 // ref A1 __tmp = foo().a1; __tmp.opIndexUnary!"+"(__tmp.opSlice!0(1, 2));
1859 assert(a == 1); // should pass
1860 assert(b == 0);
1861 }
1862
1863 void test20c()
1864 {
1865 // ae1save in ArrayExp::op_overload()
1866 int a, b;
1867
1868 struct A1
1869 {
1870 void opIndex(Tuple20!(int, int) ) { a = 1; }
1871 Tuple20!(int, int) opSlice(size_t dim)(int, int) { return typeof(return).init; }
1872 }
1873 struct A2
1874 {
1875 A1 a1;
1876 alias a1 this;
1877 int opIndex(int) { return 0; }
1878 }
1879
1880 stompStack();
1881 A2 foo() { return A2(); }
1882 foo()[1..2];
1883 // ref A1 __tmp = foo().a1; __tmp.opIndex(__tmp.opSlice!0(1, 2));
1884 assert(a == 1); // should pass
1885 assert(b == 0);
1886 }
1887
1888 void test20d()
1889 {
1890 // ae1save in BinAssignExp::op_overload()
1891 int a, b;
1892
1893 struct A1
1894 {
1895 void opIndexOpAssign(string op)(int v, Tuple20!(int, int) ) { a = v; }
1896 Tuple20!(int, int) opSlice(size_t dim)(int, int) { return typeof(return).init; }
1897 void dummy() {} // nessary to make A1 nested struct
1898 }
1899 struct A2
1900 {
1901 A1 a1;
1902 alias a1 this;
1903 ref int opIndexOpAssign(alias op)(int) { return b; }
1904 }
1905
1906 stompStack();
1907 A2 foo() { return A2(); }
1908 foo()[1..2] += 1;
1909 // ref A1 __tmp = foo().a1; __tmp.opIndexOpAssign!"+"(1, __tmp.opSlice!0(1, 2));
1910 assert(a == 1); // should pass
1911 assert(b == 0);
1912 }
1913
1914 /**************************************/
1915 // https://issues.dlang.org/show_bug.cgi?id=14624
1916
1917 void test14624()
1918 {
1919 struct A1
1920 {
1921 int x;
1922 ref int opIndex() return { return x; }
1923 ref int opSlice() { assert(0); }
1924 }
1925 {
1926 A1 a = A1(1);
1927 auto x = a[]; // a.opIndex()
1928 assert(x == a.x);
1929 auto y = -a[]; // -a.opIndex() <-- not found: a.opIndexUnary!"-"
1930 assert(y == -a.x);
1931 a[] = 1; // a.opIndex() = 1; <-- not found: a.opIndexAssign(int)
1932 assert(a.x == 1);
1933 a[] += 1; // a.opIndex() += 1; <-- not found: a.opIndexOpAssign!"+"(int)
1934 assert(a.x == 2);
1935 }
1936
1937 struct A2
1938 {
1939 int x;
1940 ref int opIndex() return { x = 10; return x; }
1941 ref int opSlice() { assert(0); }
1942 ref int opSliceUnary(alias op)() { x = 11; return x; }
1943 ref int opSliceAssign(int) return { x = 12; return x; }
1944 ref int opSliceOpAssign(alias op)(int) { x = 13; return x; }
1945 }
1946 {
1947 A2 a = A2(1);
1948 auto x = a[]; // a.opIndex()
1949 assert(a.x == 10);
1950 auto y = -a[]; // a.opSliceUnary!"-"() is preferred than: -a.opIndex()
1951 assert(a.x == 11);
1952 a[] = 1; // a.opSliceAssign(1) is preferred than: a.opIndex() = 1;
1953 assert(a.x == 12);
1954 a[] += 1; // a.opSliceOpAssign!"+"(1) is preferred than: a.opIndex() += 1;
1955 assert(a.x == 13);
1956 }
1957 }
1958
1959 /**************************************/
1960 // https://issues.dlang.org/show_bug.cgi?id=14625
1961
1962 void test14625()
1963 {
1964 struct R
1965 {
1966 @property bool empty() { return true; }
1967 @property int front() { return 0; }
1968 void popFront() {}
1969 }
1970
1971 struct C1
1972 {
1973 R opIndex() { return R(); }
1974 R opSlice() { assert(0); }
1975 }
1976 C1 c1;
1977 foreach (e; c1) {} // OK <- asserts in opSlice()
1978 foreach (e; c1[]) {} // OK, opIndex()
1979
1980 struct C2
1981 {
1982 R opIndex() { return R(); }
1983 }
1984 C2 c2;
1985 foreach (e; c2) {} // OK <- rejected
1986 foreach (e; c2[]) {} // OK, opIndex()
1987 }
1988
1989 /**************************************/
1990
1991 int main()
1992 {
1993 test1();
1994 test2();
1995 test3();
1996 test4();
1997 test5();
1998 test6();
1999 test7();
2000 test8();
2001 test9();
2002 test10();
2003 test11();
2004 test4099();
2005 test12();
2006 test13();
2007 test14();
2008 test15();
2009 test16();
2010 test17();
2011 test3789();
2012 test11161();
2013 test6798();
2014 test12904();
2015 test7641();
2016 test8434();
2017 test18();
2018 test19();
2019 test9453();
2020 test9496();
2021 test9689();
2022 test9694();
2023 test10064();
2024 test12585();
2025 test10394();
2026 test10567();
2027 test11062();
2028 test11311();
2029 test14057();
2030 test20a();
2031 test20b();
2032 test20c();
2033 test20d();
2034 test14624();
2035 test14625();
2036
2037 printf("Success\n");
2038 return 0;
2039 }