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