|
15 | 15 | ////////////////////////
|
16 | 16 |
|
17 | 17 | /// Classes are always non-sendable, so this is non-sendable
|
18 |
| -class NonSendableKlass { // expected-complete-note 9{{}} |
| 18 | +class NonSendableKlass { // expected-complete-note 16{{}} |
19 | 19 | var field: NonSendableKlass? = nil
|
20 | 20 |
|
21 | 21 | func asyncCall() async {}
|
@@ -44,6 +44,15 @@ func useValue<T>(_ x: T) {}
|
44 | 44 |
|
45 | 45 | var booleanFlag: Bool { false }
|
46 | 46 |
|
| 47 | +struct SingleFieldKlassBox { // expected-complete-note 2{{consider making struct 'SingleFieldKlassBox' conform to the 'Sendable' protocol}} |
| 48 | + var k = NonSendableKlass() |
| 49 | +} |
| 50 | + |
| 51 | +struct TwoFieldKlassBox { |
| 52 | + var k1 = NonSendableKlass() |
| 53 | + var k2 = NonSendableKlass() |
| 54 | +} |
| 55 | + |
47 | 56 | ////////////////////////////
|
48 | 57 | // MARK: Actor Self Tests //
|
49 | 58 | ////////////////////////////
|
@@ -187,10 +196,9 @@ extension Actor {
|
187 | 196 | let closure: () -> () = {
|
188 | 197 | print(self.klass)
|
189 | 198 | }
|
190 |
| - // NOTE: We do not error on this today since we assign into 1 and that makes |
191 |
| - // x assign fresh. It will be fixed in a forthcoming commit. |
| 199 | + |
192 | 200 | let x = (closure, 1)
|
193 |
| - await transferToMain(x) |
| 201 | + await transferToMain(x) // expected-tns-warning {{call site passes `self` or a non-sendable argument of this function to another thread, potentially yielding a race with the caller}} |
194 | 202 | // expected-complete-warning @-1 {{passing argument of non-sendable type '(() -> (), Int)' into main actor-isolated context may introduce data races}}
|
195 | 203 | // expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
|
196 | 204 | }
|
@@ -491,3 +499,121 @@ func testConversionsAndSendable(a: Actor, f: @Sendable () -> Void, f2: () -> Voi
|
491 | 499 | // expected-complete-warning @-1 {{passing argument of non-sendable type '() -> Void' into actor-isolated context may introduce data races}}
|
492 | 500 | // expected-complete-note @-2 {{a function type must be marked '@Sendable' to conform to 'Sendable'}}
|
493 | 501 | }
|
| 502 | + |
| 503 | +/////////////////////////////////////////////// |
| 504 | +// Multiple Field Var Assignment Merge Tests // |
| 505 | +/////////////////////////////////////////////// |
| 506 | + |
| 507 | +func singleFieldVarMergeTest() async { |
| 508 | + var box = SingleFieldKlassBox() |
| 509 | + box = SingleFieldKlassBox() |
| 510 | + |
| 511 | + // This transfers the entire region. |
| 512 | + await transferToMain(box.k) |
| 513 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 514 | + |
| 515 | + // But since box has only a single element, this doesn't race. |
| 516 | + box.k = NonSendableKlass() |
| 517 | + useValue(box.k) |
| 518 | + useValue(box) |
| 519 | + |
| 520 | + // We transfer the box back to main. |
| 521 | + await transferToMain(box) |
| 522 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'SingleFieldKlassBox' into main actor-isolated context may introduce data races}} |
| 523 | + |
| 524 | + // And reassign over the entire box, so again we can use it again. |
| 525 | + box = SingleFieldKlassBox() |
| 526 | + |
| 527 | + useValue(box) |
| 528 | + useValue(box.k) |
| 529 | + |
| 530 | + await transferToMain(box) // expected-tns-warning {{passing argument of non-sendable type 'SingleFieldKlassBox' from nonisolated context to main actor-isolated context at this call site could yield a race with accesses later in this function}} |
| 531 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'SingleFieldKlassBox' into main actor-isolated context may introduce data races}} |
| 532 | + |
| 533 | + |
| 534 | + // But if we use box.k here, we emit an error since we didn't reinitialize at |
| 535 | + // all. |
| 536 | + useValue(box.k) // expected-tns-note {{access here could race}} |
| 537 | +} |
| 538 | + |
| 539 | +func multipleFieldVarMergeTest1() async { |
| 540 | + var box = TwoFieldKlassBox() |
| 541 | + box = TwoFieldKlassBox() |
| 542 | + |
| 543 | + // This transfers the entire region. |
| 544 | + await transferToMain(box.k1) // expected-tns-warning {{passing argument of non-sendable type 'NonSendableKlass' from nonisolated context to main actor-isolated context at this call site could yield a race with accesses later in this function}} |
| 545 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 546 | + |
| 547 | + |
| 548 | + // So even if we reassign over k1, since we did a merge, this should error. |
| 549 | + box.k1 = NonSendableKlass() // expected-tns-note {{access here could race}} |
| 550 | + useValue(box) // expected-tns-note {{access here could race}} |
| 551 | +} |
| 552 | + |
| 553 | +func multipleFieldVarMergeTest2() async { |
| 554 | + var box = TwoFieldKlassBox() |
| 555 | + box = TwoFieldKlassBox() |
| 556 | + |
| 557 | + // This transfers the entire region. |
| 558 | + await transferToMain(box.k1) |
| 559 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 560 | + |
| 561 | + // But if we assign over box completely, we can use it again. |
| 562 | + box = TwoFieldKlassBox() |
| 563 | + |
| 564 | + useValue(box.k1) |
| 565 | + useValue(box.k2) |
| 566 | + useValue(box) |
| 567 | + |
| 568 | + await transferToMain(box.k2) |
| 569 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 570 | + |
| 571 | + // But if we assign over box completely, we can use it again. |
| 572 | + box = TwoFieldKlassBox() |
| 573 | + |
| 574 | + useValue(box.k1) |
| 575 | + useValue(box.k2) |
| 576 | + useValue(box) |
| 577 | +} |
| 578 | + |
| 579 | +func multipleFieldTupleMergeTest1() async { |
| 580 | + var box = (NonSendableKlass(), NonSendableKlass()) |
| 581 | + box = (NonSendableKlass(), NonSendableKlass()) |
| 582 | + |
| 583 | + // This transfers the entire region. |
| 584 | + await transferToMain(box.0) // expected-tns-warning {{passing argument of non-sendable type 'NonSendableKlass' from nonisolated context to main actor-isolated context at this call site could yield a race with accesses later in this function}} |
| 585 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 586 | + |
| 587 | + // So even if we reassign over k1, since we did a merge, this should error. |
| 588 | + box.0 = NonSendableKlass() // expected-tns-note {{access here could race}} |
| 589 | + useValue(box) // expected-tns-note {{access here could race}} |
| 590 | +} |
| 591 | + |
| 592 | +// TODO: Tuples today do not work since I need to change how we assign into |
| 593 | +// tuple addresses to use a single instruction. Today SILGen always uses |
| 594 | +// multiple values. I am going to fix this in a subsequent commit. |
| 595 | +func multipleFieldTupleMergeTest2() async { |
| 596 | + var box = (NonSendableKlass(), NonSendableKlass()) |
| 597 | + |
| 598 | + // This transfers the entire region. |
| 599 | + await transferToMain(box.0) // expected-tns-warning {{passing argument of non-sendable type 'NonSendableKlass' from nonisolated context to main actor-isolated context at this call site could yield a race with accesses later in this function}} |
| 600 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 601 | + |
| 602 | + let box2 = (NonSendableKlass(), NonSendableKlass()) |
| 603 | + // But if we assign over box completely, we can use it again. |
| 604 | + box = box2 // expected-tns-note {{access here could race}} |
| 605 | + |
| 606 | + useValue(box.0) // expected-tns-note {{access here could race}} |
| 607 | + useValue(box.1) // expected-tns-note {{access here could race}} |
| 608 | + useValue(box) // expected-tns-note {{access here could race}} |
| 609 | + |
| 610 | + await transferToMain(box.1) // expected-tns-note {{access here could race}} |
| 611 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableKlass' into main actor-isolated context may introduce data races}} |
| 612 | + |
| 613 | + // But if we assign over box completely, we can use it again. |
| 614 | + box = (NonSendableKlass(), NonSendableKlass()) // expected-tns-note {{access here could race}} |
| 615 | + |
| 616 | + useValue(box.0) // expected-tns-note {{access here could race}} |
| 617 | + useValue(box.1) // expected-tns-note {{access here could race}} |
| 618 | + useValue(box) // expected-tns-note {{access here could race}} |
| 619 | +} |
0 commit comments