forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisolated_conformance.swift
127 lines (99 loc) · 4.79 KB
/
isolated_conformance.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
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
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances %s
// REQUIRES: swift_feature_IsolatedConformances
protocol P {
func f() // expected-note 2{{mark the protocol requirement 'f()' 'async' to allow actor-isolated conformances}}
}
// ----------------------------------------------------------------------------
// Definition of isolated conformances
// ----------------------------------------------------------------------------
// expected-note@+3{{add '@preconcurrency' to the 'P' conformance to defer isolation checking to run time}}{{25-25=@preconcurrency }}
// expected-note@+2{{add 'isolated' to the 'P' conformance to restrict it to main actor-isolated code}}{{25-25=isolated }}
@MainActor
class CWithNonIsolated: P {
func f() { } // expected-error{{main actor-isolated instance method 'f()' cannot be used to satisfy nonisolated requirement from protocol 'P'}}
// expected-note@-1{{add 'nonisolated' to 'f()' to make this instance method not isolated to the actor}}
}
actor SomeActor { }
// Isolated conformances need a global-actor-constrained type.
class CNonIsolated: isolated P { // expected-error{{isolated conformance is only permitted on global-actor-isolated types}}
func f() { }
}
extension SomeActor: isolated P { // expected-error{{isolated conformance is only permitted on global-actor-isolated types}}
nonisolated func f() { }
}
@globalActor
struct SomeGlobalActor {
static let shared = SomeActor()
}
// Isolation of the function needs to match that of the enclosing type.
@MainActor
class CMismatchedIsolation: isolated P {
@SomeGlobalActor func f() { } // expected-error{{global actor 'SomeGlobalActor'-isolated instance method 'f()' cannot be used to satisfy nonisolated requirement from protocol 'P'}}
}
@MainActor
class C: isolated P {
func f() { } // okay
}
// Associated conformances with isolation
protocol Q {
associatedtype A: P
}
// expected-error@+2{{conformance of 'SMissingIsolation' to 'Q' depends on main actor-isolated conformance of 'C' to 'P'; mark it as 'isolated'}}{{27-27=isolated }}
@MainActor
struct SMissingIsolation: Q {
typealias A = C
}
struct PWrapper<T: P>: P {
func f() { }
}
// expected-error@+2{{conformance of 'SMissingIsolationViaWrapper' to 'Q' depends on main actor-isolated conformance of 'C' to 'P'; mark it as 'isolated'}}
@MainActor
struct SMissingIsolationViaWrapper: Q {
typealias A = PWrapper<C>
}
@SomeGlobalActor
class C2: isolated P {
func f() { }
}
@MainActor
struct S: isolated Q {
typealias A = C
}
// expected-error@+2{{main actor-isolated conformance of 'SMismatchedActors' to 'Q' cannot depend on global actor 'SomeGlobalActor'-isolated conformance of 'C2' to 'P'}}
@MainActor
struct SMismatchedActors: isolated Q {
typealias A = C2
}
// ----------------------------------------------------------------------------
// Use checking of isolated conformances.
// ----------------------------------------------------------------------------
// expected-note@+1{{requirement specified as 'T' : 'P' [with T = C]}}
struct PSendableWrapper<T: P & Sendable>: P {
func f() { }
}
// expected-note@+1{{requirement specified as 'T' : 'P' [with T = C]}}
struct PSendableMetaWrapper<T: P & SendableMetatype>: P {
func f() { }
}
@MainActor
func testIsolationConformancesInTypes() {
typealias A1 = PWrapper<C>
typealias A2 = PSendableWrapper<C> // expected-error{{isolated conformance of 'C' to 'P' cannot be used to satisfy conformance requirement for a `Sendable` type parameter 'T'}}
typealias A3 = PSendableMetaWrapper<C> // expected-error{{isolated conformance of 'C' to 'P' cannot be used to satisfy conformance requirement for a `SendableMetatype` type parameter 'T'}}
}
func acceptP<T: P>(_: T) { }
func acceptSendableP<T: Sendable & P>(_: T) { }
// expected-note@-1{{'acceptSendableP' declared here}}
func acceptSendableMetaP<T: SendableMetatype & P>(_: T) { }
// expected-note@-1{{'acceptSendableMetaP' declared here}}
@MainActor
func testIsolationConformancesInCall(c: C) {
acceptP(c) // okay
acceptSendableP(c) // expected-error{{isolated conformance of 'C' to 'P' cannot be used to satisfy conformance requirement for a `Sendable` type parameter}}
acceptSendableMetaP(c) // expected-error{{isolated conformance of 'C' to 'P' cannot be used to satisfy conformance requirement for a `Sendable` type parameter}}
}
func testIsolationConformancesFromOutside(c: C) {
acceptP(c) // expected-error{{main actor-isolated isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
let _: any P = c // expected-error{{main actor-isolated isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
let _ = PWrapper<C>() // expected-error{{main actor-isolated isolated conformance of 'C' to 'P' cannot be used in nonisolated context}}
}