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
|
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -disable-availability-checking -verify -verify-additional-prefix tns- %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability
// This run validates that for specific test cases around closures, we properly
// emit errors in the type checker before we run sns. This ensures that we know that
// these cases can't happen when SNS is enabled.
//
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -disable-availability-checking -verify -verify-additional-prefix typechecker-only- -DTYPECHECKER_ONLY %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability
// REQUIRES: concurrency
// REQUIRES: asserts
////////////////////////
// MARK: Declarations //
////////////////////////
/// Classes are always non-sendable, so this is non-sendable
class NonSendableKlass { // expected-complete-note 53{{}}
// expected-typechecker-only-note @-1 3{{}}
// expected-tns-note @-2 {{}}
var field: NonSendableKlass? = nil
var boolean: Bool = false
init() {}
init(_ x: NonSendableKlass) {
}
func asyncCall() async {}
func asyncCallWithIsolatedParameter(isolation: isolated (any Actor)? = #isolation) async {
}
}
class SendableKlass : @unchecked Sendable {}
actor MyActor {
var klass = NonSendableKlass()
// expected-typechecker-only-note @-1 7{{property declared here}}
final var finalKlass = NonSendableKlass()
func useKlass(_ x: NonSendableKlass) {}
func useSendableFunction(_: @Sendable () -> Void) {}
func useNonSendableFunction(_: () -> Void) {}
func doSomething() {}
@MainActor func useKlassMainActor(_ x: NonSendableKlass) {}
}
final actor FinalActor {
var klass = NonSendableKlass()
func useKlass(_ x: NonSendableKlass) {}
}
@MainActor
class MainActorIsolatedKlass {
var klass = NonSendableKlass()
let klassLet = NonSendableKlass()
}
@MainActor
final class FinalMainActorIsolatedKlass {
var klass = NonSendableKlass()
}
func useInOut<T>(_ x: inout T) {}
func useValue<T>(_ x: T) {}
func useValueAsync<T>(_ x: T) async {}
@MainActor func transferToMain<T>(_ t: T) async {}
var booleanFlag: Bool { false }
struct SingleFieldKlassBox { // expected-complete-note 2{{consider making struct 'SingleFieldKlassBox' conform to the 'Sendable' protocol}}
var k = NonSendableKlass()
}
struct TwoFieldKlassBox { // expected-typechecker-only-note 2{{}}
var k1 = NonSendableKlass()
var k2 = NonSendableKlass()
}
class TwoFieldKlassClassBox {
var k1 = NonSendableKlass()
var k2 = NonSendableKlass()
var recursive: TwoFieldKlassClassBox? = nil
}
////////////////////////////
// MARK: Actor Self Tests //
////////////////////////////
extension MyActor {
func warningIfCallingGetter() async {
await self.klass.asyncCall() // expected-complete-warning {{passing argument of non-sendable type 'NonSendableKlass' outside of actor-isolated context may introduce data races}}
// expected-tns-warning @-1 {{sending 'self.klass' risks causing data races}}
// expected-tns-note @-2 {{sending 'self'-isolated 'self.klass' to nonisolated instance method 'asyncCall()' risks causing data races between nonisolated and 'self'-isolated uses}}
}
func warningIfCallingAsyncOnFinalField() async {
// Since we are calling finalKlass directly, we emit a warning here.
await self.finalKlass.asyncCall() // expected-complete-warning {{passing argument of non-sendable type 'NonSendableKlass' outside of actor-isolated context may introduce data races}}
// expected-tns-warning @-1 {{sending 'self.finalKlass' risks causing data races}}
// expected-tns-note @-2 {{sending 'self'-isolated 'self.finalKlass' to nonisolated instance method 'asyncCall()' risks causing data races between nonisolated and 'self'-isolated uses}}
}
// We do not warn on this since we warn in the caller of our getter instead.
var klassGetter: NonSendableKlass {
self.finalKlass
}
}
extension FinalActor {
func warningIfCallingAsyncOnFinalField() async {
// Since our whole class is final, we emit the error directly here.
await self.klass.asyncCall() // expected-complete-warning {{passing argument of non-sendable type 'NonSendableKlass' outside of actor-isolated context may introduce data races}}
// expected-tns-warning @-1 {{sending 'self.klass' risks causing data races}}
// expected-tns-note @-2 {{sending 'self'-isolated 'self.klass' to nonisolated instance method 'asyncCall()' risks causing data races between nonisolated and 'self'-isolated uses}}
}
}
/////////////////////////////
// MARK: Closure Formation //
/////////////////////////////
// This test makes sure that we can properly pattern match project_box.
func formClosureWithoutCrashing() {
var c = NonSendableKlass() // expected-warning {{variable 'c' was never mutated; consider changing to 'let' constant}}
let _ = { print(c) }
}
// In this test, closure is not promoted into its box form. As a result, we
// treat assignments into contents as a merge operation rather than an assign.
func closureInOut(_ a: MyActor) async {
var contents = NonSendableKlass()
let ns0 = NonSendableKlass()
let ns1 = NonSendableKlass()
contents = ns0
contents = ns1
var closure = {}
closure = { useInOut(&contents) }
await a.useKlass(ns0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass'}}
// expected-tns-warning @-2 {{sending 'ns0' risks causing data races}}
// expected-tns-note @-3 {{sending 'ns0' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
if await booleanFlag {
await a.useKlass(ns1) // expected-tns-note {{access can happen concurrently}}
} else {
closure() // expected-tns-note {{access can happen concurrently}}
}
}
func closureInOutDifferentActor(_ a: MyActor, _ a2: MyActor) async {
var contents = NonSendableKlass()
let ns0 = NonSendableKlass()
let ns1 = NonSendableKlass()
contents = ns0
contents = ns1
var closure = {}
closure = { useInOut(&contents) }
await a.useKlass(ns0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass'}}
// expected-tns-warning @-2 {{sending 'ns0' risks causing data races}}
// expected-tns-note @-3 {{sending 'ns0' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
// We only emit a warning on the first use we see, so make sure we do both
// the use and the closure.
if await booleanFlag {
await a2.useKlass(ns1) // expected-tns-note {{access can happen concurrently}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass'}}
} else {
closure() // expected-tns-note {{access can happen concurrently}}
}
}
func closureInOut2(_ a: MyActor) async {
var contents = NonSendableKlass()
let ns0 = NonSendableKlass()
let ns1 = NonSendableKlass()
contents = ns0
contents = ns1
var closure = {}
await a.useKlass(ns0) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass'}}
closure = { useInOut(&contents) } // expected-tns-note {{access can happen concurrently}}
await a.useKlass(ns1) // expected-tns-warning {{sending 'ns1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass'}}
closure() // expected-tns-note {{access can happen concurrently}}
}
func closureNonInOut(_ a: MyActor) async {
var contents = NonSendableKlass()
let ns0 = NonSendableKlass()
let ns1 = NonSendableKlass()
contents = ns0
contents = ns1
var closure = {}
await a.useKlass(ns0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass'}}
closure = { useValue(contents) }
await a.useKlass(ns1) // expected-tns-warning {{sending 'ns1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass'}}
closure() // expected-tns-note {{access can happen concurrently}}
}
// TODO: Rework the nonisolated closure so we only treat nonisolated closures
// that capture self as being nonisolated. When we capture from outside of a
// method, we cannot access the inside of the actor without using an await, so
// this is safe.
func transferNonIsolatedNonAsyncClosureTwice() async {
let a = MyActor()
// This is nonisolated and non-async... we can transfer it safely.
var actorCaptureClosure = { print(a) }
await transferToMain(actorCaptureClosure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
actorCaptureClosure = { print(a) }
await transferToMain(actorCaptureClosure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
extension MyActor {
// Simple just capture self and access field.
func simpleClosureCaptureSelfAndTransfer() async {
let closure: () -> () = {
print(self.klass)
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func simpleClosureCaptureSelfAndTransferThroughTuple() async {
let closure: () -> () = {
print(self.klass)
}
let x = (1, closure)
await transferToMain(x) // expected-complete-warning {{passing argument of non-sendable type '(Int, () -> ())' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'self'-isolated value of type '(Int, () -> ())' with later accesses to main actor-isolated context risks causing data races}}
}
func simpleClosureCaptureSelfAndTransferThroughTupleBackwards() async {
let closure: () -> () = {
print(self.klass)
}
let x = (closure, 1)
await transferToMain(x) // expected-tns-warning {{sending 'self'-isolated value of type '(() -> (), Int)' with later accesses to main actor-isolated context risks causing data races}}
// expected-complete-warning @-1 {{passing argument of non-sendable type '(() -> (), Int)' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
func simpleClosureCaptureSelfAndTransferThroughOptional() async {
let closure: () -> () = {
print(self.klass)
}
let x: Any? = (1, closure)
await transferToMain(x) // expected-complete-warning {{passing argument of non-sendable type 'Any?' into main actor-isolated context may introduce data races}}
// expected-tns-warning @-1 {{sending 'x' risks causing data races}}
// expected-tns-note @-2 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func simpleClosureCaptureSelfAndTransferThroughOptionalBackwards() async {
let closure: () -> () = {
print(self.klass)
}
// In contrast to the tuple backwards case, we do error here since the value
// we are forming is an Any? so we actually form the tuple as an object and
// store it all as once.
let x: Any? = (closure, 1)
await transferToMain(x) // expected-complete-warning {{passing argument of non-sendable type 'Any?' into main actor-isolated context may introduce data races}}
// expected-tns-warning @-1 {{sending 'x' risks causing data races}}
// expected-tns-note @-2 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func simpleClosureCaptureSelfWithReinit() async {
var closure: () -> () = {
print(self.klass)
}
// Error here.
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
closure = {}
// But not here.
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
func simpleClosureCaptureSelfWithReinit2() async {
var closure: () -> () = {}
// No error here.
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
closure = {
print(self.klass)
}
// Error here.
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func simpleClosureCaptureSelfWithReinit3() async {
var closure: () -> () = {}
// We get a transfer after use error.
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}}
if await booleanFlag {
closure = {
print(self.klass)
}
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-note @-2 {{access can happen concurrently}}
// expected-tns-warning @-3 {{sending 'closure' risks causing data races}}
// expected-tns-note @-4 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
// In this case, we reinit along both paths, but only one has an actor derived
// thing.
func simpleClosureCaptureSelfWithReinit4() async {
var closure: () -> () = {}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
if await booleanFlag {
closure = {
print(self.klass)
}
} else {
closure = {}
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
#if TYPECHECKER_ONLY
func simpleClosureCaptureSelfThroughTupleWithFieldAccess() async {
// In this case, we erase that we accessed self so we hit a type checker
// error. We could make this a SNS error, but since in the other cases where
// we have a nonisolated non-async we are probably going to change it to be
// async as well making these errors go away.
let x = (self, 1)
let closure: () -> () = {
print(x.0.klass) // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
#endif
func simpleClosureCaptureSelfThroughTuple() async {
let x = (self, self.klass)
let closure: () -> () = {
print(x.1)
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func simpleClosureCaptureSelfThroughTuple2() async {
let x = (2, self.klass)
let closure: () -> () = {
print(x.1)
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
func simpleClosureCaptureSelfThroughOptional() async {
let x: Any? = (2, self.klass)
let closure: () -> () = {
print(x as Any)
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
}
func testSimpleLetClosureCaptureActor() async {
let a = MyActor()
let closure = { print(a) }
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
#if TYPECHECKER_ONLY
func testSimpleLetClosureCaptureActorField() async {
let a = MyActor()
let closure = { print(a.klass) } // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
func testSimpleLetClosureCaptureActorFieldThroughTuple() async {
let a = (MyActor(), 0)
let closure = { print(a.0.klass) } // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
func testSimpleLetClosureCaptureActorFieldThroughOptional() async {
let a: MyActor? = MyActor()
let closure = { print(a!.klass) } // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
#endif
func testSimpleVarClosureCaptureActor() async {
let a = MyActor()
var closure = {}
closure = { print(a) }
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
#if TYPECHECKER_ONLY
func testSimpleVarClosureCaptureActorField() async {
let a = MyActor()
var closure = {}
closure = { print(a.klass) } // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
func testSimpleVarClosureCaptureActorFieldThroughTuple() async {
let a = (MyActor(), 0)
var closure = {}
closure = { print(a.0.klass) } // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
func testSimpleVarClosureCaptureActorFieldThroughOptional() async {
let a: MyActor? = MyActor()
var closure = {}
closure = { print(a!.klass) } // expected-typechecker-only-error {{actor-isolated property 'klass' can not be referenced from a nonisolated context}}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
#endif
extension MyActor {
// Make sure that we properly propagate actor derived from klass into field's
// value.
func simplePropagateNonSendableThroughMultipleProperty() async {
let f = self.klass.field!
let closure: () -> () = {
print(f)
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
// Make sure that we properly propagate actor derived from klass into field's
// value.
func simplePropagateNonSendableThroughMultipleProperty2() async {
let f = self.klass.field!
let closure: () -> () = {
print(f.field!)
}
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
}
}
extension MyActor {
func testVarReassignStopActorDerived() async {
var closure: () -> () = {
print(self)
}
// This should error.
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'self'-isolated 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// This doesnt since we re-assign
closure = {}
await transferToMain(closure)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// This re-assignment shouldn't error.
closure = {}
// But this transfer should.
await transferToMain(closure) // expected-complete-warning {{passing argument of non-sendable type '() -> ()' into main actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'closure' risks causing data races}}
// expected-tns-note @-3 {{sending 'closure' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}}
// But this will error since we race.
closure() // expected-tns-note {{access can happen concurrently}}
}
}
///////////////////////////////////
// MARK: Sendable Function Tests //
///////////////////////////////////
// Make sure that we do not error on function values that are Sendable... even
// if the function is converted to a non-Sendable form by a function conversion.
func testConversionsAndSendable(a: MyActor, f: @Sendable () -> Void, f2: () -> Void) async {
// No function conversion.
await a.useSendableFunction(f)
// Function conversion.
await a.useNonSendableFunction(f)
// Show that we error if we are not sendable.
await a.useNonSendableFunction(f2) // expected-complete-warning {{passing argument of non-sendable type '() -> Void' into actor-isolated context may introduce data races}}
// expected-complete-note @-1 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// expected-tns-warning @-2 {{sending 'f2' risks causing data races}}
// expected-tns-note @-3 {{sending task-isolated 'f2' to actor-isolated instance method 'useNonSendableFunction' risks causing data races between actor-isolated and task-isolated uses}}
}
func testSendableClosureCapturesNonSendable(a: MyActor) {
let klass = NonSendableKlass()
let _ = { @Sendable in
_ = klass // expected-warning {{capture of 'klass' with non-sendable type 'NonSendableKlass' in a `@Sendable` closure}}
}
}
func testSendableClosureCapturesNonSendable2(a: FinalMainActorIsolatedKlass) {
let klass = NonSendableKlass()
let _ = { @Sendable @MainActor in
a.klass = klass // expected-complete-warning {{capture of 'klass' with non-sendable type 'NonSendableKlass' in a `@Sendable` closure}}
}
}
/////////////////////////////////////////////////////
// MARK: Multiple Field Var Assignment Merge Tests //
/////////////////////////////////////////////////////
func singleFieldVarMergeTest() async {
var box = SingleFieldKlassBox()
box = SingleFieldKlassBox()
// This transfers the entire region.
await transferToMain(box.k)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
// But since box has only a single element, this doesn't race.
box.k = NonSendableKlass()
useValue(box.k)
useValue(box)
// We transfer the box back to main.
await transferToMain(box)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'SingleFieldKlassBox' into main actor-isolated context may introduce data races}}
// And reassign over the entire box, so again we can use it again.
box = SingleFieldKlassBox()
useValue(box)
useValue(box.k)
await transferToMain(box) // expected-tns-warning {{sending 'box' risks causing data races}}
// expected-tns-note @-1 {{sending 'box' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'SingleFieldKlassBox' into main actor-isolated context may introduce data races}}
// But if we use box.k here, we emit an error since we didn't reinitialize at
// all.
useValue(box.k) // expected-tns-note {{access can happen concurrently}}
}
func multipleFieldVarMergeTest1() async {
var box = TwoFieldKlassBox()
box = TwoFieldKlassBox()
// This transfers the entire region.
await transferToMain(box.k1) // expected-tns-warning {{sending 'box.k1' risks causing data races}}
// expected-tns-note @-1 {{sending 'box.k1' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
// So even if we reassign over k1, since we did a merge, this should error.
box.k1 = NonSendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(box)
}
func multipleFieldVarMergeTest2() async {
var box = TwoFieldKlassBox()
box = TwoFieldKlassBox()
// This transfers the entire region.
await transferToMain(box.k1)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
// But if we assign over box completely, we can use it again.
box = TwoFieldKlassBox()
useValue(box.k1)
useValue(box.k2)
useValue(box)
await transferToMain(box.k2)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
// But if we assign over box completely, we can use it again.
box = TwoFieldKlassBox()
useValue(box.k1)
useValue(box.k2)
useValue(box)
}
func multipleFieldTupleMergeTest1() async {
var box = (NonSendableKlass(), NonSendableKlass())
box = (NonSendableKlass(), NonSendableKlass())
// This transfers the entire region.
await transferToMain(box.0) // expected-tns-warning {{sending 'box.0' risks causing data races}}
// expected-tns-note @-1 {{sending 'box.0' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
// So even if we reassign over k1, since we did a merge, this should error.
box.0 = NonSendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(box)
}
// TODO: Tuples today do not work since I need to change how we assign into
// tuple addresses to use a single instruction. Today SILGen always uses
// multiple values. I am going to fix this in a subsequent commit.
func multipleFieldTupleMergeTest2() async {
var box = (NonSendableKlass(), NonSendableKlass())
// This transfers the entire region.
await transferToMain(box.0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
let box2 = (NonSendableKlass(), NonSendableKlass())
// But if we assign over box completely, we can use it again.
box = box2
useValue(box.0)
useValue(box.1)
useValue(box)
await transferToMain(box.1)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
// But if we assign over box completely, we can use it again.
box = (NonSendableKlass(), NonSendableKlass())
useValue(box.0)
useValue(box.1)
useValue(box)
}
///////////////////////////
// MARK: ClassFieldTests //
///////////////////////////
class ClassFieldTests { // expected-complete-note 6{{}}
let letSendableTrivial = 0
let letSendableNonTrivial = SendableKlass()
let letNonSendableNonTrivial = NonSendableKlass()
var varSendableTrivial = 0
var varSendableNonTrivial = SendableKlass()
var varNonSendableNonTrivial = NonSendableKlass()
}
func letSendableTrivialClassFieldTest() async {
let test = ClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'ClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letSendableNonTrivialClassFieldTest() async {
let test = ClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'ClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableNonTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letNonSendableNonTrivialClassFieldTest() async {
let test = ClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'ClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varSendableTrivialClassFieldTest() async {
let test = ClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'ClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varSendableNonTrivialClassFieldTest() async {
let test = ClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'ClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varNonSendableNonTrivialClassFieldTest() async {
let test = ClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'ClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
////////////////////////////////
// MARK: FinalClassFieldTests //
////////////////////////////////
final class FinalClassFieldTests { // expected-complete-note 6 {{}}
let letSendableTrivial = 0
let letSendableNonTrivial = SendableKlass()
let letNonSendableNonTrivial = NonSendableKlass()
var varSendableTrivial = 0
var varSendableNonTrivial = SendableKlass()
var varNonSendableNonTrivial = NonSendableKlass()
}
func letSendableTrivialFinalClassFieldTest() async {
let test = FinalClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'FinalClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letSendableNonTrivialFinalClassFieldTest() async {
let test = FinalClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'FinalClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableNonTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letNonSendableNonTrivialFinalClassFieldTest() async {
let test = FinalClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'FinalClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varSendableTrivialFinalClassFieldTest() async {
let test = FinalClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'FinalClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varSendableNonTrivialFinalClassFieldTest() async {
let test = FinalClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'FinalClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varNonSendableNonTrivialFinalClassFieldTest() async {
let test = FinalClassFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'FinalClassFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
////////////////////////////
// MARK: StructFieldTests //
////////////////////////////
struct StructFieldTests { // expected-complete-note 31 {{}}
let letSendableTrivial = 0
let letSendableNonTrivial = SendableKlass()
let letNonSendableNonTrivial = NonSendableKlass()
var varSendableTrivial = 0
var varSendableNonTrivial = SendableKlass()
var varNonSendableNonTrivial = NonSendableKlass()
}
func letSendableTrivialLetStructFieldTest() async {
let test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func letSendableNonTrivialLetStructFieldTest() async {
let test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableNonTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letNonSendableNonTrivialLetStructFieldTest() async {
let test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.letNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
func letSendableTrivialVarStructFieldTest() async {
var test = StructFieldTests()
test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letSendableNonTrivialVarStructFieldTest() async {
var test = StructFieldTests()
test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.letSendableNonTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func letNonSendableNonTrivialVarStructFieldTest() async {
var test = StructFieldTests()
test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.letNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
// Lets can access sendable let/var even if captured in a closure.
func letNonSendableNonTrivialLetStructFieldClosureTest() async {
let test = StructFieldTests()
let cls = {
print(test)
}
_ = cls
var cls2 = {}
cls2 = {
print(test)
}
_ = cls2
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.letSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
let z2 = test.varSendableNonTrivial
_ = z2
useValue(test)
}
func varSendableTrivialLetStructFieldTest() async {
let test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableTrivial // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varSendableNonTrivialLetStructFieldTest() async {
let test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableNonTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varNonSendableNonTrivialLetStructFieldTest() async {
let test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.varNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
func varSendableTrivialVarStructFieldTest() async {
var test = StructFieldTests()
test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varSendableNonTrivialVarStructFieldTest() async {
var test = StructFieldTests()
test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
_ = test.varSendableNonTrivial
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varNonSendableNonTrivialVarStructFieldTest() async {
var test = StructFieldTests()
test = StructFieldTests()
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.varNonSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
// vars cannot access sendable let/var if captured in a closure.
func varNonSendableNonTrivialLetStructFieldClosureTest1() async {
var test = StructFieldTests()
test = StructFieldTests()
let cls = {
test = StructFieldTests()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.letSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest2() async {
var test = StructFieldTests()
test = StructFieldTests()
let cls = {
test = StructFieldTests()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.varSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest3() async {
var test = StructFieldTests()
test = StructFieldTests()
let cls = {
test = StructFieldTests()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
// vars cannot access sendable let/var if captured in a closure.
func varNonSendableNonTrivialLetStructFieldClosureTest4() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
cls = {
test = StructFieldTests()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.letSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest5() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
cls = {
test = StructFieldTests()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
let z = test.varSendableNonTrivial // expected-tns-note {{access can happen concurrently}}
_ = z
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest6() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
cls = {
test = StructFieldTests()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest7() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
cls = {
test.varSendableNonTrivial = SendableKlass()
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest8() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
cls = {
useInOut(&test)
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureTest9() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive1() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
if await booleanFlag {
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
} else {
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test.varSendableNonTrivial = SendableKlass()
}
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive2() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
if await booleanFlag {
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test)
}
// We do not error when accessing the sendable field in this example since the
// transfer is not reachable from the closure. Instead we emit an error on test.
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive3() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
if await booleanFlag {
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
} else {
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
}
test.varSendableNonTrivial = SendableKlass()
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive4() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
for _ in 0..<1024 {
// TODO: The error here is b/c of the loop carry. We should probably store
// the operand instead of just the require inst, so we can better separate a
// loop carry use and an error due to multiple params. Then we could emit
// the error on test below. This is still correct though, just not as
// good... that is QoI though.
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-tns-note @-2 {{access can happen concurrently}}
// This is treated as a use since test is in box form and is mutable. So we
// treat assignment as a merge.
test = StructFieldTests()
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
}
test.varSendableNonTrivial = SendableKlass()
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive5() async {
var test = StructFieldTests()
test = StructFieldTests()
// The reason why we error here is that even though we reassign at the end of
// the for loop and currently understand that test has a new value different
// from the value assigned to test at the beginning of the for loop, when we
// merge back through the for loop, we have to merge the regions due to the
// union operation we perform. So the conservatism of the dataflow creates
// this. This is a case where we are going to need to be able to have the
// compiler explain the regions well.
for _ in 0..<1024 {
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-tns-note @-2 {{access can happen concurrently}}
// expected-complete-warning @-3 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
test = StructFieldTests()
}
test.varSendableNonTrivial = SendableKlass()
useValue(test)
}
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive6() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
// In this test case, the first transfer errors on the
// test.varSendableNonTrivial b/c of the cls. Since we don't have cls on the
// else path, it emits on useValue(test).
if await booleanFlag {
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
} else {
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
// In this case since we are tracking the transfer from the else statement, we
// track the closure.
func varNonSendableNonTrivialLetStructFieldClosureFlowSensitive7() async {
var test = StructFieldTests()
test = StructFieldTests()
var cls = {}
if await booleanFlag {
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
} else {
cls = {
useInOut(&test.varSendableNonTrivial)
}
_ = cls
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'StructFieldTests' into main actor-isolated context may introduce data races}}
}
test.varSendableNonTrivial = SendableKlass() // expected-tns-note {{access can happen concurrently}}
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
////////////////////////////
// MARK: TupleFieldTests //
////////////////////////////
func varSendableTrivialLetTupleFieldTest() async {
let test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '(Int, SendableKlass, NonSendableKlass)' into main actor-isolated context may introduce data races}}
let z = test.0
useValue(z)
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varSendableNonTrivialLetTupleFieldTest() async {
let test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '(Int, SendableKlass, NonSendableKlass)' into main actor-isolated context may introduce data races}}
let z = test.1
useValue(z)
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varNonSendableNonTrivialLetTupleFieldTest() async {
let test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '(Int, SendableKlass, NonSendableKlass)' into main actor-isolated context may introduce data races}}
let z = test.2 // expected-tns-note {{access can happen concurrently}}
useValue(z)
useValue(test)
}
func varSendableTrivialVarTupleFieldTest() async {
var test = (0, SendableKlass(), NonSendableKlass())
test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '(Int, SendableKlass, NonSendableKlass)' into main actor-isolated context may introduce data races}}
_ = test.0
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varSendableTrivialVarTupleFieldTest2() async {
var test = (0, SendableKlass(), NonSendableKlass())
test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test.2) // expected-tns-warning {{sending 'test.2' risks causing data races}}
// expected-tns-note @-1 {{sending 'test.2' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
_ = test.0
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varSendableNonTrivialVarTupleFieldTest() async {
var test = (0, SendableKlass(), NonSendableKlass())
test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '(Int, SendableKlass, NonSendableKlass)' into main actor-isolated context may introduce data races}}
_ = test.1
useValue(test) // expected-tns-note {{access can happen concurrently}}
}
func varNonSendableNonTrivialVarTupleFieldTest() async {
var test = (0, SendableKlass(), NonSendableKlass())
test = (0, SendableKlass(), NonSendableKlass())
await transferToMain(test) // expected-tns-warning {{sending 'test' risks causing data races}}
// expected-tns-note @-1 {{sending 'test' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '(Int, SendableKlass, NonSendableKlass)' into main actor-isolated context may introduce data races}}
let z = test.2 // expected-tns-note {{access can happen concurrently}}
useValue(z)
useValue(test)
}
//////////////////////////////
// MARK: Control Flow Tests //
//////////////////////////////
func controlFlowTest1() async {
let x = NonSendableKlass()
if await booleanFlag {
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
} else {
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
useValue(x) // expected-tns-note 2{{access can happen concurrently}}
}
// This test seems like something that we should not error upon. What is
// happening here is that when we get to the bottom of the for loop, we realize
// that x's old value and x/x's new value are in different regions. But when we
// merge back into the loop header, we note that the loop entry block has x/x's
// old value in the same region so when we merge, we get that x/x's old
// value/x's new value are all in the same region. We emit the error on useValue
// as well afterwards since if we exit from the loop header, we have that large
// merge region leave the for loop.
func controlFlowTest2() async {
var x = NonSendableKlass()
for _ in 0..<1024 {
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
// expected-tns-note @-2 {{access can happen concurrently}}
// expected-complete-warning @-3 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
x = NonSendableKlass()
}
useValue(x)
}
////////////////////////
// MARK: Actor Setter //
////////////////////////
actor ActorWithSetter {
var field = NonSendableKlass()
var twoFieldBox = TwoFieldKlassBox()
var twoFieldBoxInTuple = (NonSendableKlass(), TwoFieldKlassBox())
var recursive: ActorWithSetter? = nil
var classBox = TwoFieldKlassClassBox()
func test1() async {
let x = NonSendableKlass()
self.field = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
func test2() async {
let x = NonSendableKlass()
self.twoFieldBox.k1 = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
func test3() async {
let x = NonSendableKlass()
self.twoFieldBoxInTuple.1.k1 = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
// This triggers a crash in SILGen with tns enabled.
#if TYPECHECKER_ONLY
func recursive() async {
let x = NonSendableKlass()
await self.recursive!.twoFieldBoxInTuple.1.k2 = x
// expected-typechecker-only-warning @-1 {{non-sendable type '(NonSendableKlass, TwoFieldKlassBox)' in implicitly asynchronous access to actor-isolated property 'twoFieldBoxInTuple' cannot cross actor boundary}}
// expected-typechecker-only-warning @-2 {{non-sendable type '(NonSendableKlass, TwoFieldKlassBox)' in implicitly asynchronous access to actor-isolated property 'twoFieldBoxInTuple' cannot cross actor boundary}}
await transferToMain(x) // xpected-tns-warning {{call site passes `self` or a non-sendable argument of this function to another thread, potentially yielding a race with the caller}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
#endif
func classBox() async {
let x = NonSendableKlass()
self.classBox.k1 = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
}
final actor FinalActorWithSetter {
var field = NonSendableKlass()
var twoFieldBox = TwoFieldKlassBox()
var twoFieldBoxInTuple = (NonSendableKlass(), TwoFieldKlassBox())
var recursive: ActorWithSetter? = nil
var classBox = TwoFieldKlassClassBox()
func test1() async {
let x = NonSendableKlass()
self.field = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
func test2() async {
let x = NonSendableKlass()
self.twoFieldBox.k1 = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
func test3() async {
let x = NonSendableKlass()
self.twoFieldBoxInTuple.1.k1 = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
// This triggers a crash in SILGen with tns enabled.
#if TYPECHECKER_ONLY
func recursive() async {
let x = NonSendableKlass()
await self.recursive!.twoFieldBoxInTuple.1.k2 = x
// expected-typechecker-only-warning @-1 {{non-sendable type '(NonSendableKlass, TwoFieldKlassBox)' in implicitly asynchronous access to actor-isolated property 'twoFieldBoxInTuple' cannot cross actor boundary}}
// expected-typechecker-only-warning @-2 {{non-sendable type '(NonSendableKlass, TwoFieldKlassBox)' in implicitly asynchronous access to actor-isolated property 'twoFieldBoxInTuple' cannot cross actor boundary}}
await transferToMain(x) // xpected-tns-warning {{call site passes `self` or a non-sendable argument of this function to another thread, potentially yielding a race with the caller}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
#endif
func classBox() async {
let x = NonSendableKlass()
self.classBox.k1 = x
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
}
func functionArgumentIntoClosure(_ x: @escaping () -> ()) async {
let _ = { @MainActor in
let _ = x // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{task-isolated 'x' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
// expected-complete-warning @-2 {{capture of 'x' with non-sendable type '() -> ()' in a `@Sendable` closure}}
// expected-complete-note @-3 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
}
// Make sure we handle the merge of the actor isolated and disconnected state
// appropriately.
//
// TODO: Since we are accessing the var field, we miss an
// error. rdar://126170563.
@MainActor func actorMergeInLoopVar() async {
let a = MainActorIsolatedKlass()
var c = NonSendableKlass()
for _ in 0..<1024 {
await useValueAsync(c) // expected-tns-warning {{sending 'c' risks causing data races}}
// expected-tns-note @-1 {{sending main actor-isolated 'c' to nonisolated global function 'useValueAsync' risks causing data races between nonisolated and main actor-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' outside of main actor-isolated context may introduce data races}}
c = a.klass
}
}
@MainActor func actorMergeInLoopLet() async {
let a = MainActorIsolatedKlass()
var c = NonSendableKlass()
for _ in 0..<1024 {
await useValueAsync(c) // expected-tns-warning {{sending 'c' risks causing data races}}
// expected-tns-note @-1 {{sending main actor-isolated 'c' to nonisolated global function 'useValueAsync' risks causing data races between nonisolated and main actor-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' outside of main actor-isolated context may introduce data races}}
c = a.klassLet
}
}
func testGetActorName() async {
let a = MyActor()
let x = NonSendableKlass()
await a.useKlass(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'x' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
useValue(x) // expected-tns-note {{access can happen concurrently}}
}
extension MyActor {
func testCallBangIsolatedMethod(other: MyActor) async {
await klass.asyncCallWithIsolatedParameter(isolation: other) // expected-tns-warning {{sending 'self.klass' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'self.klass' to actor-isolated instance method 'asyncCallWithIsolatedParameter(isolation:)' risks causing data races between actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
}
}
extension FinalActor {
func testCallBangIsolatedMethod(other: MyActor) async {
await klass.asyncCallWithIsolatedParameter(isolation: other) // expected-tns-warning {{sending 'self.klass' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'self.klass' to actor-isolated instance method 'asyncCallWithIsolatedParameter(isolation:)' risks causing data races between actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
}
}
extension NonSendableKlass {
func directAsyncCallWithIsolatedParameter(isolation: isolated (any Actor)? = #isolation) async {
}
}
extension MyActor {
func testCallBangIsolatedDirectMethod(other: MyActor) async {
await klass.directAsyncCallWithIsolatedParameter(isolation: other) // expected-tns-warning {{sending 'self.klass' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'self.klass' to actor-isolated instance method 'directAsyncCallWithIsolatedParameter(isolation:)' risks causing data races between actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
}
}
extension FinalActor {
func testCallBangIsolatedDirectMethod(other: MyActor) async {
await klass.directAsyncCallWithIsolatedParameter(isolation: other) // expected-tns-warning {{sending 'self.klass' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'self.klass' to actor-isolated instance method 'directAsyncCallWithIsolatedParameter(isolation:)' risks causing data races between actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
}
}
actor DictionaryActorTest {
var data: [Int: Int] = [:]
// We used to crash on this due to isolation merging.
func doSomething(_ key: Int) {
assert(self.data[key] == 0)
}
}
extension MyActor {
// Make sure that we properly infer information from isolated parameters that
// aren't self.
func isolationInferenceFromNonSelfIsolatedParameters() {
useValue {
self.doSomething()
}
}
func isolationInferenceFromNonSelfIsolatedParameters2() {
useValue {
self.doSomething()
let x = NonSendableKlass()
self.useKlass(x)
await transferToMain(x)
// expected-tns-warning @-1 {{sending 'x' risks causing data races}}
// expected-tns-note @-2 {{sending 'self'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-3 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
}
}
func initAccessorTests() {
@available(SwiftStdlib 5.1, *)
actor NonisolatedAccessors {
nonisolated var a: Int = 0 {
init {
}
get { 0 }
set {}
}
init(value: Int) {
let escapingSelf: (NonisolatedAccessors) -> Void = { _ in }
// a is initialized here via default value
escapingSelf(self)
self.a = value // Ok (nonisolated)
print(a) // Ok (nonisolated)
}
}
@available(SwiftStdlib 5.1, *)
actor NonSendableInit {
var first: NonSendableKlass
var second: NonSendableKlass? = nil {
@storageRestrictions(initializes: first)
init(initialValue) {
first = initialValue!
}
get { fatalError() }
set { fatalError() }
}
@MainActor
var third: NonSendableKlass
@MainActor
var fourth: NonSendableKlass? = nil {
@storageRestrictions(initializes: third)
init(initialValue) {
third = initialValue!
}
get { fatalError() }
set { fatalError() }
}
}
}
func differentInstanceTest(_ a: MyActor, _ b: MyActor) async {
let x = NonSendableKlass()
await a.useKlass(x)
// expected-tns-warning @-1 {{sending 'x' risks causing data races}}
// expected-tns-note @-2 {{sending 'x' to actor-isolated instance method 'useKlass' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-3 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
await b.useKlass(x) // expected-tns-note {{access can happen concurrently}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into actor-isolated context may introduce data races}}
}
protocol AssociatedTypeTestProtocol {
associatedtype A: Actor
}
func associatedTypeTestBasic<T: AssociatedTypeTestProtocol>(_: T, _: isolated T.A) {
}
func associatedTypeTestBasic2<T: AssociatedTypeTestProtocol>(_: T, iso: isolated T.A, x: NonSendableKlass) async {
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'iso'-isolated 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and 'iso'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}}
}
func sendableGlobalActorIsolated() {
let x = NonSendableKlass()
let _ = { @Sendable @MainActor in
print(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{'x' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
// expected-complete-warning @-2 {{capture of 'x' with non-sendable type 'NonSendableKlass' in a `@Sendable` closure}}
}
print(x) // expected-tns-note {{access can happen concurrently}}
}
// We do not get an error here since we are transferring x both times to a main
// actor isolated thing function. We used to emit an error when using region
// isolation since we would trip on the store_borrow we used to materialize the
// value.
func testIndirectParameterSameIsolationNoError() async {
let x = NonSendableKlass()
await transferToMain(x) // expected-tns-warning {{sending 'x' risks causing data races}}
// expected-tns-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
await transferToMain(x) // expected-tns-note {{access can happen concurrently}}
}
extension MyActor {
func testNonSendableCaptures(sc: NonSendableKlass) {
Task {
_ = self
_ = sc
Task { [sc,self] in
_ = self
_ = sc
Task { // expected-tns-warning {{value of non-Sendable type '@isolated(any) @async @callee_guaranteed @substituted <τ_0_0> () -> @out τ_0_0 for <()>' accessed after being transferred}}
_ = sc
}
Task { // expected-tns-note {{access can happen concurrently}}
_ = sc
}
}
}
}
}
public struct TimeoutError: Error, CustomStringConvertible {
public var description: String { "Timed out" }
}
// We used to not merge the isolation below correctly causing us to emit a crash
// due to undefined behavior. Make sure we do not crash or emit an unhandled
// pattern error.
public func doNotCrashOrEmitUnhandledPatternErrors<T: Sendable>(
_ duration: Duration,
_ body: @escaping @Sendable () async throws -> T
) async throws -> T {
try await withThrowingTaskGroup(of: T.self) { taskGroup in
taskGroup.addTask {
try await Task.sleep(for: duration)
throw TimeoutError()
}
taskGroup.addTask {
return try await body()
}
for try await value in taskGroup {
taskGroup.cancelAll()
return value
}
throw CancellationError()
}
}
/// The following makes sure that when we have a function like test2 with an
/// assigned isolation that returns a Sendable value... we treat the value as
/// actually Sendable. This occurs in this example via the result of the default
/// parameter function for string.
///
/// We shouldn't emit any diagnostic here.
actor FunctionWithSendableResultAndIsolationActor {
func foo() -> String {
return string()
}
func string(someCondition: Bool = false) -> String {
return ""
}
}
@MainActor
func testThatGlobalActorTakesPrecedenceOverActorIsolationOnMethods() async {
let a = MyActor()
let ns = NonSendableKlass()
// 'ns' should be main actor isolated since useKlassMainActor is @MainActor
// isolated. Previously we would let MyActor take precedence here...
a.useKlassMainActor(ns)
// Meaning we would get an error here.
Task { @MainActor in print(ns) }
}
// Shouldn't get any errors from x.
//
// We used to look through the access to x.boolean and think that the closure
// was capturing x instead of y.
func testBooleanCapture(_ x: inout NonSendableKlass) {
let y = x.boolean
Task.detached { @MainActor [z = y] in
print(z)
}
}
|