|
| 1 | +// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -disable-availability-checking -verify -verify-additional-prefix complete- %s -o /dev/null -parse-as-library |
| 2 | +// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -enable-experimental-feature RegionBasedIsolation -disable-availability-checking -verify -verify-additional-prefix tns- %s -o /dev/null -parse-as-library |
| 3 | + |
| 4 | +// REQUIRES: concurrency |
| 5 | +// REQUIRES: asserts |
| 6 | + |
| 7 | +//////////////////////// |
| 8 | +// MARK: Declarations // |
| 9 | +//////////////////////// |
| 10 | + |
| 11 | +class NonSendableKlass {} |
| 12 | +final class SendableKlass : Sendable {} |
| 13 | + |
| 14 | +actor GlobalActorInstance {} |
| 15 | + |
| 16 | +@globalActor |
| 17 | +struct GlobalActor { |
| 18 | + static let shared = GlobalActorInstance() |
| 19 | +} |
| 20 | + |
| 21 | +func transferToNonIsolated<T>(_ t: T) async {} |
| 22 | +@MainActor func transferToMainActor<T>(_ t: T) async {} |
| 23 | +@GlobalActor func transferToGlobalActor<T>(_ t: T) async {} |
| 24 | +func useValue<T>(_ t: T) {} |
| 25 | + |
| 26 | +var booleanFlag: Bool { false } |
| 27 | + |
| 28 | +///////////////// |
| 29 | +// MARK: Tests // |
| 30 | +///////////////// |
| 31 | + |
| 32 | +private class NonSendableLinkedList<T> { // expected-complete-note 5{{}} |
| 33 | + var listHead: NonSendableLinkedListNode<T>? |
| 34 | + |
| 35 | + init() { listHead = nil } |
| 36 | +} |
| 37 | + |
| 38 | +private class NonSendableLinkedListNode<T> { // expected-complete-note 3{{}} |
| 39 | + var next: NonSendableLinkedListNode? |
| 40 | + var data: T? |
| 41 | + |
| 42 | + init() { next = nil } |
| 43 | +} |
| 44 | + |
| 45 | +@GlobalActor private var firstList = NonSendableLinkedList<Int>() |
| 46 | +@GlobalActor private var secondList = NonSendableLinkedList<Int>() |
| 47 | + |
| 48 | +@GlobalActor func useGlobalActor1() async { |
| 49 | + let x = firstList |
| 50 | + |
| 51 | + await transferToMainActor(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}} |
| 52 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableLinkedList<Int>' into main actor-isolated context may introduce data races}} |
| 53 | + |
| 54 | + let y = secondList.listHead!.next! |
| 55 | + |
| 56 | + await transferToMainActor(y) // 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}} |
| 57 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableLinkedListNode<Int>' into main actor-isolated context may introduce data races}} |
| 58 | +} |
| 59 | + |
| 60 | +@GlobalActor func useGlobalActor2() async { |
| 61 | + var x = NonSendableLinkedListNode<Int>() |
| 62 | + |
| 63 | + if booleanFlag { |
| 64 | + x = secondList.listHead!.next! |
| 65 | + } |
| 66 | + |
| 67 | + await transferToMainActor(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}} |
| 68 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableLinkedListNode<Int>' into main actor-isolated context may introduce data races}} |
| 69 | +} |
| 70 | + |
| 71 | +@GlobalActor func useGlobalActor3() async { |
| 72 | + var x = NonSendableLinkedListNode<Int>() |
| 73 | + |
| 74 | + if booleanFlag { |
| 75 | + x = secondList.listHead!.next! |
| 76 | + } |
| 77 | + |
| 78 | + await transferToGlobalActor(x) |
| 79 | +} |
| 80 | + |
| 81 | +@GlobalActor func useGlobalActor4() async { |
| 82 | + let x = NonSendableLinkedListNode<Int>() |
| 83 | + |
| 84 | + await transferToGlobalActor(x) |
| 85 | + |
| 86 | + useValue(x) |
| 87 | +} |
| 88 | + |
| 89 | +@GlobalActor func useGlobalActor5() async { |
| 90 | + let x = NonSendableLinkedListNode<Int>() |
| 91 | + |
| 92 | + await transferToNonIsolated(x) // expected-tns-warning {{passing argument of non-sendable type 'NonSendableLinkedListNode<Int>' from global actor 'GlobalActor'-isolated context to nonisolated context at this call site could yield a race with accesses later in this function}} |
| 93 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'NonSendableLinkedListNode<Int>' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 94 | + |
| 95 | + useValue(x) // expected-tns-note {{access here could race}} |
| 96 | +} |
| 97 | + |
| 98 | +private struct StructContainingValue { // expected-complete-note 2{{}} |
| 99 | + var x = NonSendableLinkedList<Int>() |
| 100 | + var y = SendableKlass() |
| 101 | +} |
| 102 | + |
| 103 | +@GlobalActor func useGlobalActor6() async { |
| 104 | + var x = StructContainingValue() |
| 105 | + x = StructContainingValue() |
| 106 | + |
| 107 | + await transferToNonIsolated(x) // expected-tns-warning {{passing argument of non-sendable type 'StructContainingValue' from global actor 'GlobalActor'-isolated context to nonisolated context at this call site could yield a race with accesses later in this function}} |
| 108 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'StructContainingValue' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 109 | + |
| 110 | + useValue(x) // expected-tns-note {{access here could race}} |
| 111 | +} |
| 112 | + |
| 113 | +@GlobalActor func useGlobalActor7() async { |
| 114 | + var x = StructContainingValue() |
| 115 | + x.x = firstList |
| 116 | + |
| 117 | + await transferToNonIsolated(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}} |
| 118 | + // expected-complete-warning @-1 {{passing argument of non-sendable type 'StructContainingValue' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 119 | + |
| 120 | + useValue(x) |
| 121 | +} |
| 122 | + |
| 123 | +@GlobalActor func useGlobalActor8() async { |
| 124 | + var x = (NonSendableLinkedList<Int>(), NonSendableLinkedList<Int>()) |
| 125 | + x = (NonSendableLinkedList<Int>(), NonSendableLinkedList<Int>()) |
| 126 | + |
| 127 | + await transferToNonIsolated(x) // expected-tns-warning {{passing argument of non-sendable type '(NonSendableLinkedList<Int>, NonSendableLinkedList<Int>)' from global actor 'GlobalActor'-isolated context to nonisolated context at this call site could yield a race with accesses later in this function}} |
| 128 | + // expected-complete-warning @-1 {{passing argument of non-sendable type '(NonSendableLinkedList<Int>, NonSendableLinkedList<Int>)' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 129 | + // expected-complete-warning @-2 {{passing argument of non-sendable type '(NonSendableLinkedList<Int>, NonSendableLinkedList<Int>)' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 130 | + |
| 131 | + useValue(x) // expected-tns-note {{access here could race}} |
| 132 | +} |
| 133 | + |
| 134 | +@GlobalActor func useGlobalActor9() async { |
| 135 | + var x = (NonSendableLinkedList<Int>(), NonSendableLinkedList<Int>()) |
| 136 | + |
| 137 | + x.1 = firstList |
| 138 | + |
| 139 | + await transferToNonIsolated(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}} |
| 140 | + // expected-complete-warning @-1 {{passing argument of non-sendable type '(NonSendableLinkedList<Int>, NonSendableLinkedList<Int>)' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 141 | + // expected-complete-warning @-2 {{passing argument of non-sendable type '(NonSendableLinkedList<Int>, NonSendableLinkedList<Int>)' outside of global actor 'GlobalActor'-isolated context may introduce data races}} |
| 142 | + |
| 143 | + useValue(x) |
| 144 | +} |
0 commit comments