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
|
// RUN: %target-swift-frontend -strict-concurrency=complete -disable-availability-checking -parse-as-library -emit-sil -o /dev/null -verify -verify-additional-prefix tns- %s -enable-upcoming-feature GlobalActorIsolatedTypesUsability
// REQUIRES: concurrency
// REQUIRES: asserts
/*
This file tests the early features of the flow-sensitive Sendable checking implemented by the TransferNonSendable SIL pass.
In particular, it tests the behavior of applications that cross isolation domains:
- non-Sendable values are statically grouped into "regions" of values that may alias or point to each other
- passing a non-Sendable value to an isolation-crossing call "consumed" the region of that value
- accessing a non-Sendable value requires its region to have not been consumed
All non-Sendable arguments, and "self" for non-Sendable classes, are assumed to be in the same region at the start of
each function, and that region must be preserved at all times - it cannot be passed to another isolation domain.
*/
class NonSendable { // expected-complete-note 73{{}}
var x : Any
var y : Any
init() {
x = 0;
y = 0;
}
init(_ z : Any) {
x = z;
y = z;
}
}
actor A {
func run_closure(_ closure : () -> ()) async {
closure();
}
func foo(_ ns : Any) {}
func foo_multi(_ : Any, _ : Any, _ : Any) {}
func foo_vararg(_ : Any ...) {}
}
func foo_noniso(_ ns : Any) {}
func foo_noniso_multi(_ : Any, _ : Any, _ : Any) {}
func foo_noniso_vararg(_ : Any ...) {}
var bool : Bool { false }
func test_isolation_crossing_sensitivity(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
// This call does not consume ns0
foo_noniso(ns0);
// This call consumes ns1
await a.foo(ns1); // expected-tns-warning {{sending 'ns1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
print(ns0);
print(ns1); // expected-tns-note {{access can happen concurrently}}
}
func test_arg_nonconsumable(a : A, ns_arg : NonSendable) async {
let ns_let = NonSendable();
// Safe to consume an rvalue.
await a.foo(NonSendable()); // expected-complete-warning {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
// Safe to consume an lvalue.
await a.foo(ns_let); // expected-complete-warning {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
// Not safe to consume an arg.
await a.foo(ns_arg); // expected-tns-warning {{sending 'ns_arg' risks causing data races}}
// expected-tns-note @-1 {{sending task-isolated 'ns_arg' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and task-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
// Check for no duplicate warnings once self is "consumed"
await a.foo(NonSendable()); // expected-complete-warning {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
func test_closure_capture(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let captures0 = { print(ns0) };
let captures12 = { print(ns1); print(ns2)};
let captures3 = { print(ns3) };
let captures3indirect = { captures3() };
// all lets should still be accessible - no consumed have happened yet
print(ns0)
print(ns1)
print(ns2)
print(ns3)
// this should consume ns0
await a.run_closure(captures0)
// expected-tns-warning @-1 {{sending 'captures0' risks causing data races}}
// expected-tns-note @-2 {{sending 'captures0' to actor-isolated instance method 'run_closure' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
print(ns0) // expected-tns-note {{access can happen concurrently}}
print(ns1)
print(ns2)
print(ns3)
// this should consume ns1 and ns2
await a.run_closure(captures12)
// expected-tns-warning @-1 {{sending 'captures12' risks causing data races}}
// expected-tns-note @-2 {{sending 'captures12' to actor-isolated instance method 'run_closure' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// This only touches ns1/ns2 so we get an error on ns1 since it is first.
print(ns0)
print(ns1) // expected-tns-note {{access can happen concurrently}}
print(ns2)
print(ns3)
// this should consume ns3
await a.run_closure(captures3indirect)
// expected-tns-warning @-1 {{sending 'captures3indirect' risks causing data races}}
// expected-tns-note @-2 {{sending 'captures3indirect' to actor-isolated instance method 'run_closure' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
// This only uses ns3 so we should only emit an error on ns3.
print(ns0)
print(ns1)
print(ns2)
print(ns3) // expected-tns-note {{access can happen concurrently}}
}
func test_regions(a : A, b : Bool) async {
// definitely aliases - should be in the same region
let ns0_0 = NonSendable();
let ns0_1 = ns0_0;
// possibly aliases - should be in the same region
let ns1_0 = NonSendable();
var ns1_1 = NonSendable();
if (b) {
ns1_1 = ns1_0;
}
// accessible via pointer - should be in the same region
let ns2_0 = NonSendable();
let ns2_1 = NonSendable();
ns2_0.x = ns2_1;
// check for each of the above pairs that consuming half of it consumes the other half
if (b) {
await a.foo(ns0_0) // expected-tns-warning {{sending 'ns0_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if (b) {
print(ns0_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns0_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns0_1) // expected-tns-warning {{sending 'ns0_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns0_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns0_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns1_0) // expected-tns-warning {{sending 'ns1_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns1_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns1_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns1_1) // expected-tns-warning {{sending 'ns1_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns1_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns1_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns2_0) // expected-tns-warning {{sending 'ns2_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns2_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns2_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns2_1) // expected-tns-warning {{sending 'ns2_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns2_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns2_1) // expected-tns-note {{access can happen concurrently}}
}
}
}
func test_indirect_regions(a : A, b : Bool) async {
// in each of the following, nsX_0 and nsX_1 should be in the same region for various reasons
// 1 constructed using 0
let ns0_0 = NonSendable();
let ns0_1 = NonSendable(NonSendable(ns0_0));
// 1 constructed using 0, indirectly
let ns1_0 = NonSendable();
let ns1_aux = NonSendable(ns1_0);
let ns1_1 = NonSendable(ns1_aux);
// 1 and 0 constructed with same aux value
let ns2_aux = NonSendable();
let ns2_0 = NonSendable(ns2_aux);
let ns2_1 = NonSendable(ns2_aux);
// 0 points to aux, which points to 1
let ns3_aux = NonSendable();
let ns3_0 = NonSendable();
let ns3_1 = NonSendable();
ns3_0.x = ns3_aux;
ns3_aux.x = ns3_1;
// 1 and 0 point to same aux value
let ns4_aux = NonSendable();
let ns4_0 = NonSendable();
let ns4_1 = NonSendable();
ns4_0.x = ns4_aux;
ns4_1.x = ns4_aux;
// same aux value points to both 0 and 1
let ns5_aux = NonSendable();
let ns5_0 = ns5_aux.x;
let ns5_1 = ns5_aux.y;
// now check for each pair that consuming half of it consumed the other half
if (b) {
await a.foo(ns0_0) // expected-tns-warning {{sending 'ns0_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns0_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns0_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns0_1) // expected-tns-warning {{sending 'ns0_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns0_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns0_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns1_0) // expected-tns-warning {{sending 'ns1_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns1_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns1_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns1_1) // expected-tns-warning {{sending 'ns1_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns1_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns1_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns2_0) // expected-tns-warning {{sending 'ns2_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns2_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns2_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns2_1) // expected-tns-warning {{sending 'ns2_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns2_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns2_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns3_0) // expected-tns-warning {{sending 'ns3_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns3_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns3_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns3_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns3_1) // expected-tns-warning {{sending 'ns3_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns3_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns3_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns3_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns4_0) // expected-tns-warning {{sending 'ns4_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns4_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns4_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns4_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns4_1) // expected-tns-warning {{sending 'ns4_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns4_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if b {
print(ns4_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns4_1) // expected-tns-note {{access can happen concurrently}}
}
}
if (b) {
await a.foo(ns5_0) // expected-tns-warning {{sending 'ns5_0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns5_0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any' into actor-isolated context may introduce data races}}
if b {
print(ns5_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns5_1) // expected-tns-note {{access can happen concurrently}}
}
} else {
await a.foo(ns5_1) // expected-tns-warning {{sending 'ns5_1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns5_1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any' into actor-isolated context may introduce data races}}
if b {
print(ns5_0) // expected-tns-note {{access can happen concurrently}}
} else {
print(ns5_1) // expected-tns-note {{access can happen concurrently}}
}
}
}
// class methods in general cannot consume self, check that this is true except for Sendable classes (and actors)
class C_NonSendable {
func bar() {}
func bar(a : A) async {
let captures_self = { self.bar() }
// this is not a cross-isolation call, so it should be permitted
foo_noniso(captures_self)
// this is a cross-isolation call that captures non-Sendable self, so it should not be permitted
await a.foo(captures_self) // expected-tns-warning {{sending 'captures_self' risks causing data races}}
// expected-tns-note @-1 {{sending task-isolated 'captures_self' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and task-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '() -> ()' into actor-isolated context may introduce data races}}
// expected-complete-note @-3 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
}
final class C_Sendable : Sendable {
func bar() {}
func bar(a : A) async {
let captures_self = { self.bar() }
// this is not a cross-isolation call, so it should be permitted
foo_noniso(captures_self)
// this is a cross-isolation, but self is Sendable, so it should be permitted
await a.foo(captures_self)
// expected-complete-warning @-1 {{passing argument of non-sendable type '() -> ()' into actor-isolated context may introduce data races}}
// expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
}
actor A_Sendable {
func bar() {}
func bar(a : A) async {
let captures_self = { self.bar() }
// This is not a cross-isolation call, so it should be permitted
foo_noniso(captures_self)
// This is a cross-isolation call passing a call that should be treated
// as isolated to self since it captures self which is isolated to our
// actor and is non-Sendable. For now, we ban this since we do not
// support the ability to dynamically invoke the synchronous closure on
// the specific actor.
await a.foo(captures_self) // expected-tns-warning {{sending 'captures_self' risks causing data races}}
// expected-tns-note @-1 {{sending 'self'-isolated 'captures_self' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and 'self'-isolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type '() -> ()' into actor-isolated context may introduce data races}}
// expected-complete-note @-3 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
}
}
func basic_loopiness(a : A, b : Bool) async {
let ns = NonSendable()
while (b) {
await a.foo(ns) // expected-tns-warning {{sending 'ns' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-tns-note @-2 {{access can happen concurrently}}
// expected-complete-warning @-3 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
}
func basic_loopiness_unsafe(a : A, b : Bool) async {
var ns0 = NonSendable()
var ns1 = NonSendable()
var ns2 = NonSendable()
var ns3 = NonSendable()
//should merge all vars
while (b) {
(ns0, ns1, ns2, ns3) = (ns1, ns2, ns3, ns0)
}
await a.foo(ns0) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo(ns3) // expected-tns-note {{access can happen concurrently}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
func basic_loopiness_safe(a : A, b : Bool) async {
var ns0 = NonSendable()
var ns1 = NonSendable()
var ns2 = NonSendable()
var ns3 = NonSendable()
//should keep ns0 and ns3 separate
while (b) {
(ns0, ns1, ns2, ns3) = (ns1, ns0, ns3, ns2)
}
await a.foo(ns0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo(ns3)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
struct StructBox {
var contents : NonSendable
init() {
contents = NonSendable()
}
}
func test_struct_assign_doesnt_merge(a : A, b : Bool) async {
let ns0 = NonSendable()
let ns1 = NonSendable()
//this should merge ns0 and ns1
var box = StructBox()
box.contents = ns0
box.contents = ns1
await a.foo(ns0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo(ns1)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
class ClassBox {
var contents : NonSendable
init() {
contents = NonSendable()
}
}
func test_class_assign_merges(a : A, b : Bool) async {
let ns0 = NonSendable()
let ns1 = NonSendable()
// This should merge ns0 and ns1.
var box = ClassBox()
box = ClassBox()
box.contents = ns0
box.contents = ns1
await a.foo(ns0) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo(ns1) // expected-tns-note {{access can happen concurrently}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
func test_stack_assign_doesnt_merge(a : A, b : Bool) async {
let ns0 = NonSendable()
let ns1 = NonSendable()
//this should NOT merge ns0 and ns1
var contents : NonSendable
contents = ns0
contents = ns1
foo_noniso(contents) //must use var to avoid warning
await a.foo(ns0)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo(ns1)
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
func test_stack_assign_and_capture_merges(a : A, b : Bool) async {
let ns0 = NonSendable()
let ns1 = NonSendable()
//this should NOT merge ns0 and ns1
var contents = NonSendable()
let closure = { print(contents) }
foo_noniso(closure) //must use let to avoid warning
contents = ns0
contents = ns1
await a.foo(ns0) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo(ns1) // expected-tns-note {{access can happen concurrently}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
func test_tuple_formation(a : A, i : Int) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
let ns012 = (ns0, ns1, ns2);
let ns13 = (ns1, ns3);
switch (i) {
case 0:
await a.foo(ns0) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if bool {
foo_noniso(ns0); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns1); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns2); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns3); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns4);
} else if bool {
foo_noniso(ns012); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso(ns13); // expected-tns-note {{access can happen concurrently}}
}
case 1:
await a.foo(ns1) // expected-tns-warning {{sending 'ns1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if bool {
foo_noniso(ns0); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns1); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns2); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns3); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns4);
} else if bool {
foo_noniso(ns012); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso(ns13); // expected-tns-note {{access can happen concurrently}}
}
case 2:
await a.foo(ns2) // expected-tns-warning {{sending 'ns2' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
if bool {
foo_noniso(ns0); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns1); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns2); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns3); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns4);
} else if bool {
foo_noniso(ns012); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso(ns13); // expected-tns-note {{access can happen concurrently}}
}
case 3:
await a.foo(ns4) // expected-tns-warning {{sending 'ns4' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns4' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
foo_noniso(ns0);
foo_noniso(ns1);
foo_noniso(ns2);
foo_noniso(ns4); // expected-tns-note {{access can happen concurrently}}
foo_noniso(ns012);
foo_noniso(ns13);
case 4:
await a.foo(ns012) // expected-tns-warning {{sending value of non-Sendable type '(NonSendable, NonSendable, NonSendable)' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// TODO: Interestingly with complete, we emit this error 3 times?!
// expected-complete-warning @-2 3{{passing argument of non-sendable type '(NonSendable, NonSendable, NonSendable)' into actor-isolated context may introduce data races}}
if bool {
foo_noniso(ns0); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns1); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns2); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns3); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns4);
} else if bool {
foo_noniso(ns012); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso(ns13); // expected-tns-note {{access can happen concurrently}}
}
default:
await a.foo(ns13) // expected-tns-warning {{sending value of non-Sendable type '(NonSendable, NonSendable)' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-1 2{{passing argument of non-sendable type '(NonSendable, NonSendable)' into actor-isolated context may introduce data races}}
if bool {
foo_noniso(ns0); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns1); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns2); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns3); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso(ns4);
} else if bool {
foo_noniso(ns012); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso(ns13); // expected-tns-note {{access can happen concurrently}}
}
}
}
func reuse_args_safe(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
// this should be perfectly legal - arguments are assumed to come from the same region
await a.foo_multi(ns0, ns0, ns0);
// expected-complete-warning @-1 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo_multi(ns1, ns2, ns1);
// expected-complete-warning @-1 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
}
func one_consume_many_require(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
await a.foo_multi(ns0, ns1, ns2);
// expected-complete-warning @-1 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
// expected-tns-warning @-2 {{sending 'ns0' risks causing data races}}
// expected-tns-note @-3 {{sending 'ns0' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-tns-warning @-4 {{sending 'ns1' risks causing data races}}
// expected-tns-note @-5 {{sending 'ns1' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-tns-warning @-6 {{sending 'ns2' risks causing data races}}
// expected-tns-note @-7 {{sending 'ns2' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
foo_noniso_multi(ns0, ns3, ns4); // expected-tns-note {{access can happen concurrently}}
foo_noniso_multi(ns3, ns1, ns4); // expected-tns-note {{access can happen concurrently}}
foo_noniso_multi(ns4, ns3, ns2); // expected-tns-note {{access can happen concurrently}}
}
func one_consume_one_require(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
await a.foo_multi(ns0, ns1, ns2);
// expected-complete-warning @-1 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
// expected-tns-warning @-2 {{sending 'ns0' risks causing data races}}
// expected-tns-note @-3 {{sending 'ns0' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-tns-warning @-4 {{sending 'ns1' risks causing data races}}
// expected-tns-note @-5 {{sending 'ns1' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-tns-warning @-6 {{sending 'ns2' risks causing data races}}
// expected-tns-note @-7 {{sending 'ns2' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
foo_noniso_multi(ns0, ns1, ns2); // expected-tns-note 3{{access can happen concurrently}}
}
func many_consume_one_require(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
let ns5 = NonSendable();
await a.foo_multi(ns0, ns3, ns3) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo_multi(ns4, ns1, ns4) // expected-tns-warning {{sending 'ns1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo_multi(ns5, ns5, ns2) // expected-tns-warning {{sending 'ns2' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
foo_noniso_multi(ns0, ns1, ns2); //expected-tns-note 3{{access can happen concurrently}}
}
func many_consume_many_require(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
let ns5 = NonSendable();
let ns6 = NonSendable();
let ns7 = NonSendable();
await a.foo_multi(ns0, ns3, ns3) // expected-tns-warning {{sending 'ns0' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns0' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo_multi(ns4, ns1, ns4) // expected-tns-warning {{sending 'ns1' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns1' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
await a.foo_multi(ns5, ns5, ns2) // expected-tns-warning {{sending 'ns2' risks causing data races}}
// expected-tns-note @-1 {{sending 'ns2' to actor-isolated instance method 'foo_multi' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 3{{passing argument of non-sendable type 'NonSendable' into actor-isolated context may introduce data races}}
foo_noniso_multi(ns0, ns6, ns7); // expected-tns-note {{access can happen concurrently}}
foo_noniso_multi(ns6, ns1, ns7); // expected-tns-note {{access can happen concurrently}}
foo_noniso_multi(ns7, ns6, ns2); // expected-tns-note {{access can happen concurrently}}
}
func reuse_args_safe_vararg(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
// this should be perfectly legal - arguments are assumed to come from the same region
await a.foo_vararg(ns0, ns0, ns0);
// expected-complete-warning @-1 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
await a.foo_vararg(ns1, ns2, ns1);
// expected-complete-warning @-1 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
}
func one_consume_many_require_varag(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
//TODO: find a way to make the type used in the diagnostic more specific than the signature type
await a.foo_vararg(ns0, ns1, ns2);
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
if bool {
foo_noniso_vararg(ns0, ns3, ns4); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso_vararg(ns3, ns1, ns4); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso_vararg(ns4, ns3, ns2); // expected-tns-note {{access can happen concurrently}}
}
}
func one_consume_one_require_vararg(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
await a.foo_vararg(ns0, ns1, ns2);
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
foo_noniso_vararg(ns0, ns1, ns2); // expected-tns-note 1{{access can happen concurrently}}
}
func many_consume_one_require_vararg(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
let ns5 = NonSendable();
await a.foo_vararg(ns0, ns3, ns3)
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
await a.foo_vararg(ns4, ns1, ns4)
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
await a.foo_vararg(ns5, ns5, ns2)
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
foo_noniso_vararg(ns0, ns1, ns2); // expected-tns-note 3{{access can happen concurrently}}
}
func many_consume_many_require_vararg(a : A) async {
let ns0 = NonSendable();
let ns1 = NonSendable();
let ns2 = NonSendable();
let ns3 = NonSendable();
let ns4 = NonSendable();
let ns5 = NonSendable();
let ns6 = NonSendable();
let ns7 = NonSendable();
await a.foo_vararg(ns0, ns3, ns3)
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
await a.foo_vararg(ns4, ns1, ns4)
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
await a.foo_vararg(ns5, ns5, ns2)
// expected-tns-warning @-1 {{sending value of non-Sendable type 'Any...' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'Any...' into actor-isolated context may introduce data races}}
if bool {
foo_noniso_vararg(ns0, ns6, ns7); // expected-tns-note {{access can happen concurrently}}
} else if bool {
foo_noniso_vararg(ns6, ns1, ns7); // expected-tns-note {{access can happen concurrently}}
} else {
foo_noniso_vararg(ns7, ns6, ns2); // expected-tns-note {{access can happen concurrently}}
}
}
enum E { // expected-complete-note {{consider making enum 'E' conform to the 'Sendable' protocol}}
case E1(NonSendable)
case E2(NonSendable)
case E3(NonSendable)
}
func enum_test(a : A) async {
let e1 = E.E1(NonSendable())
let e2 = E.E2(NonSendable())
let e3 = E.E3(NonSendable())
let e4 = E.E1(NonSendable())
switch (e1) {
//this case merges e1 and e2
case let .E1(ns1):
switch (e2) {
case let .E2(ns2):
ns1.x = ns2.x
default: ()
}
//this case consumes e3
case .E2:
switch (e3) {
case let .E3(ns3):
await a.foo(ns3.x); // expected-tns-warning {{sending value of non-Sendable type 'Any' with later accesses from nonisolated context to actor-isolated context risks causing data races}}
// expected-complete-warning @-1 {{passing argument of non-sendable type 'Any' into actor-isolated context may introduce data races}}
default: ()
}
//this case does nothing
case .E3:
foo_noniso(e4);
}
await a.foo(e1); // expected-tns-warning {{sending 'e1' risks causing data races}}
// expected-tns-note @-1 {{sending 'e1' to actor-isolated instance method 'foo' risks causing data races between actor-isolated and local nonisolated uses}}
// expected-complete-warning @-2 {{passing argument of non-sendable type 'E' into actor-isolated context may introduce data races}}
foo_noniso(e2); // expected-tns-note {{access can happen concurrently}}
foo_noniso(e3); // expected-tns-note {{access can happen concurrently}}
foo_noniso(e4);
}
|