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
|
// RUN: %target-swift-frontend -disable-availability-checking -enable-experimental-flow-sensitive-concurrent-captures -verify -emit-sil %s -o - >/dev/null
// RUN: %target-swift-frontend -disable-availability-checking -enable-experimental-flow-sensitive-concurrent-captures -verify -emit-sil %s -o - >/dev/null -strict-concurrency=targeted
// RUN: %target-swift-frontend -disable-availability-checking -enable-experimental-flow-sensitive-concurrent-captures -verify -emit-sil %s -o - >/dev/null -strict-concurrency=complete
// RUN: %target-swift-frontend -disable-availability-checking -enable-experimental-flow-sensitive-concurrent-captures -verify -emit-sil %s -o - >/dev/null -strict-concurrency=complete -enable-upcoming-feature RegionBasedIsolation
// REQUIRES: concurrency
// REQUIRES: asserts
func f(_: @escaping @Sendable () -> Void) { }
open class F {
func useConcurrent(_: @escaping @Sendable () -> Void) { }
}
extension Int {
mutating func addOne() {
self += 1
}
}
func inoutUserInt(_ t: inout Int) {}
func testCaseTrivialValue() {
var i = 17
f {
print(i + 17)
print(i + 18)
print(i + 19)
}
i = 20
i += 21
// We only emit a warning here since we use the last write.
//
// TODO: Should we emit for all writes?
i.addOne() // expected-warning {{'i' mutated after capture by sendable closure}}
// expected-note @-14 {{variable defined here}}
// expected-note @-14 {{variable captured by sendable closure}}
// expected-note @-14 {{capturing use}}
// expected-note @-14 {{capturing use}}
// expected-note @-14 {{capturing use}}
}
func testCaseTrivialValue2() {
var i2 = 17
f {
print(i2 + 17)
print(i2 + 18)
print(i2 + 19)
}
i2 = 20
i2 += 21 // expected-warning {{'i2' mutated after capture by sendable closure}}
// expected-note @-9 {{variable defined here}}
// expected-note @-9 {{variable captured by sendable closure}}
// expected-note @-9 {{capturing use}}
// expected-note @-9 {{capturing use}}
// expected-note @-9 {{capturing use}}
}
func testCaseTrivialValue3() {
var i3 = 17
f {
print(i3 + 17)
print(i3 + 18)
print(i3 + 19)
}
i3 = 20 // expected-warning {{'i3' mutated after capture by sendable closure}}
// expected-note @-8 {{variable defined here}}
// expected-note @-8 {{variable captured by sendable closure}}
// expected-note @-8 {{capturing use}}
// expected-note @-8 {{capturing use}}
// expected-note @-8 {{capturing use}}
}
func testCaseTrivialValue4() {
var i4 = 17
f {
print(i4 + 17)
print(i4 + 18)
print(i4 + 19)
}
inoutUserInt(&i4) // expected-warning {{'i4' mutated after capture by sendable closure}}
// expected-note @-8 {{variable defined here}}
// expected-note @-8 {{variable captured by sendable closure}}
// expected-note @-8 {{capturing use}}
// expected-note @-8 {{capturing use}}
// expected-note @-8 {{capturing use}}
}
class Klass: UnsafeSendable { // expected-warning{{'UnsafeSendable' is deprecated: Use @unchecked Sendable instead}}
var next: Klass? = nil
}
func inoutUserKlass(_ k: inout Klass) {}
func inoutUserOptKlass(_ k: inout Klass?) {}
func testCaseClass() {
var i = Klass()
f {
print(i)
}
i = Klass() // expected-warning {{'i' mutated after capture by sendable closure}}
// expected-note @-6 {{variable defined here}}
// expected-note @-6 {{variable captured by sendable closure}}
// expected-note @-6 {{capturing use}}
}
func testCaseClassInout() {
var i2 = Klass()
f {
print(i2)
}
inoutUserKlass(&i2) // expected-warning {{'i2' mutated after capture by sendable closure}}
// expected-note @-5 {{variable defined here}}
// expected-note @-5 {{variable captured by sendable closure}}
// expected-note @-5 {{capturing use}}
}
func testCaseClassInoutField() {
var i2 = Klass()
i2 = Klass()
f {
print(i2)
}
// Since we are passing in .next inout and we are using a class this isn't a
// violation.
inoutUserOptKlass(&i2.next)
}
////////////////////////////
// Non Trivial Value Type //
////////////////////////////
struct NonTrivialValueType: Sendable {
var i: Int
var k: Klass? = nil
init(_ inputI: Int, _ inputKlass: Klass) {
self.i = inputI
self.k = inputKlass
}
}
func testCaseNonTrivialValue() {
var i = NonTrivialValueType(17, Klass())
f {
print(i.i + 17)
print(i.i + 18)
print(i.i + 19)
}
i.i = 20
i.i += 21
// We only emit a warning here since we use the last write.
//
// TODO: Should we emit for all writes?
i.i.addOne() // expected-warning {{'i' mutated after capture by sendable closure}}
// expected-note @-14 {{variable defined here}}
// expected-note @-14 {{variable captured by sendable closure}}
// expected-note @-14 {{capturing use}}
// expected-note @-14 {{capturing use}}
// expected-note @-14 {{capturing use}}
}
func testCaseNonTrivialValueInout() {
var i = NonTrivialValueType(17, Klass())
f {
print(i.i + 17)
print(i.k ?? "none")
}
// We only emit a warning here since we use the last write.
inoutUserOptKlass(&i.k) // expected-warning {{'i' mutated after capture by sendable closure}}
// expected-note @-8 {{variable defined here}}
// expected-note @-8 {{variable captured by sendable closure}}
// expected-note @-8 {{capturing use}}
// expected-note @-8 {{capturing use}}
}
protocol MyProt {
var i: Int { get set }
var k: Klass? { get set }
}
func testCaseAddressOnlyAllocBoxToStackable<T : MyProt & Sendable>(i : T) {
var i2 = i
f {
print(i2.i + 17)
print(i2.k ?? "none")
}
// TODO: Make sure we emit these once we support address only types!
inoutUserOptKlass(&i2.k) // xpected-warning {{'i2' mutated after capture by sendable closure}}
// xpected-note @-8 {{variable defined here}}
// xpected-note @-8 {{variable captured by sendable closure}}
// xpected-note @-8 {{capturing use}}
// xpected-note @-8 {{capturing use}}
}
// Alloc box to stack can't handle this test case, so show off a bit and make
// sure we can emit a great diagnostic here!
func testCaseAddressOnlyNoAllocBoxToStackable<T : MyProt & Sendable>(i : T) {
let f2 = F()
var i2 = i
f2.useConcurrent {
print(i2.i + 17)
print(i2.k ?? "none")
}
// TODO: Make sure we emit these once we support address only types!
inoutUserOptKlass(&i2.k) // xpected-warning {{'i2' mutated after capture by sendable closure}}
// xpected-note @-8 {{variable defined here}}
// xpected-note @-8 {{variable captured by sendable closure}}
// xpected-note @-8 {{capturing use}}
// xpected-note @-8 {{capturing use}}
}
|