forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch_inference.swift
44 lines (35 loc) · 1.47 KB
/
dispatch_inference.swift
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
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %import-libdispatch -strict-concurrency=complete %s -emit-sil -o /dev/null -verify -strict-concurrency=targeted
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple %import-libdispatch -strict-concurrency=complete %s -emit-sil -o /dev/null -verify -verify-additional-prefix tns-
// REQUIRES: concurrency
// REQUIRES: libdispatch
import Dispatch
// Tests the inference of @_unsafeSendable and @MainActor when working with
// the Dispatch library, and specifically, DispatchQueue.
@MainActorfunc onlyOnMainActor(){}
func testMe(){
DispatchQueue.main.async{
onlyOnMainActor() // okay, due to inference of @MainActor-ness
}
DispatchQueue.main.sync{
onlyOnMainActor()
}
}
func testUnsafeSendableInMainAsync()async{
varx=5
DispatchQueue.main.async{
x =17 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
// expected-tns-warning @-1 {{sending 'x' risks causing data races}}
// expected-tns-note @-2 {{'x' is captured by a main actor-isolated closure. main actor-isolated uses in closure may race against later nonisolated uses}}
}
print(x) // expected-tns-note {{access can happen concurrently}}
}
func testUnsafeSendableInAsync(queue:DispatchQueue)async{
varx=5
queue.async{
x =17 // expected-warning{{mutation of captured var 'x' in concurrently-executing code}}
}
queue.sync{
x =17 // okay
}
print(x)
}