Skip to content

Commit 5d6ce70

Browse files
authored
Merge pull request #72385 from gottesmm/pr-e589c5e900c4d5cfa1ef201d9f9f64ea1ffa03fb
[region-isolation] Add a test specifically for ownership.
2 parents 5806e7a + 99e2f2b commit 5d6ce70

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -disable-availability-checking -verify -enable-upcoming-feature RegionBasedIsolation -enable-experimental-feature TransferringArgsAndResults %s -o /dev/null
2+
3+
// This test validates the behavior of transfer non sendable around ownership
4+
// constructs like non copyable types, consuming/borrowing parameters, and inout
5+
// parameters.
6+
7+
// REQUIRES: concurrency
8+
// REQUIRES: asserts
9+
10+
////////////////////////
11+
// MARK: Declarations //
12+
////////////////////////
13+
14+
class Klass {}
15+
struct KlassBox {
16+
var data = Klass()
17+
}
18+
19+
actor Custom {
20+
}
21+
22+
@globalActor
23+
struct CustomActor {
24+
static var shared: Custom {
25+
return Custom()
26+
}
27+
}
28+
29+
@MainActor func transferToMain<T>(_ t: T) async {}
30+
@MainActor func consumeTransferToMain<T>(_ t: consuming T) async {}
31+
32+
/////////////////
33+
// MARK: Tests //
34+
/////////////////
35+
36+
func testConsuming(_ x: consuming Klass) async {
37+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
38+
// expected-note @-1 {{transferring nonisolated 'x' to main actor-isolated callee could cause races between main actor-isolated and nonisolated uses}}
39+
}
40+
41+
func testConsumingError(_ x: consuming Klass) async {
42+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
43+
// expected-note @-1 {{transferring nonisolated 'x' to main actor-isolated callee could cause races between main actor-isolated and nonisolated uses}}
44+
print(x)
45+
}
46+
47+
@CustomActor func testConsumingErrorGlobalActor(_ x: consuming Klass) async {
48+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
49+
// expected-note @-1 {{transferring global actor 'CustomActor'-isolated 'x' to main actor-isolated callee could cause races between main actor-isolated and global actor 'CustomActor'-isolated uses}}
50+
print(x)
51+
}
52+
53+
func testConsumingUseAfterConsumeError(_ x: consuming Klass) async { // expected-error {{'x' consumed more than once}}
54+
await consumeTransferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
55+
// expected-note @-1 {{transferring nonisolated 'x' to main actor-isolated callee could cause races between main actor-isolated and nonisolated uses}}
56+
// expected-note @-2 {{consumed here}}
57+
print(x)
58+
// expected-note @-1 {{consumed again here}}
59+
}
60+
61+
@CustomActor func testConsumingUseAfterConsumeErrorGlobalActor(_ x: consuming Klass) async { // expected-error {{'x' consumed more than once}}
62+
await consumeTransferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
63+
// expected-note @-1 {{transferring global actor 'CustomActor'-isolated 'x' to main actor-isolated callee could cause races between main actor-isolated and global actor 'CustomActor'-isolated uses}}
64+
// expected-note @-2 {{consumed here}}
65+
print(x)
66+
// expected-note @-1 {{consumed again here}}
67+
}
68+
69+
func testBorrowing(_ x: borrowing Klass) async { // expected-note {{value is task-isolated since it is in the same region as 'x'}}
70+
await transferToMain(x) // expected-warning {{task-isolated value of type 'Klass' transferred to main actor-isolated context}}
71+
}
72+
73+
func testBorrowingError(_ x: borrowing Klass) async { // expected-error {{'x' is borrowed and cannot be consumed}}
74+
// expected-note @-1 {{}}
75+
await transferToMain(x) // expected-warning {{task-isolated value of type 'Klass' transferred to main actor-isolated context}}
76+
print(x) // expected-note {{consumed here}}
77+
}
78+
79+
@CustomActor func testBorrowingErrorGlobalActor(_ x: borrowing Klass) async { // expected-error {{'x' is borrowed and cannot be consumed}}
80+
// expected-note @-1 {{}}
81+
await transferToMain(x) // expected-warning {{task-isolated value of type 'Klass' transferred to main actor-isolated context}}
82+
print(x) // expected-note {{consumed here}}
83+
}
84+
85+
func testInOut(_ x: inout Klass) async {
86+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
87+
// TODO: This is wrong. Should say task isolated!
88+
// expected-note @-2 {{transferring nonisolated 'x' to main actor-isolated callee could cause races between main actor-isolated and nonisolated uses}}
89+
}
90+
91+
func testInOutError(_ x: inout Klass) async {
92+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
93+
// expected-note @-1 {{transferring nonisolated 'x' to main actor-isolated callee}}
94+
print(x)
95+
}
96+
97+
@CustomActor func testInOutErrorMainActor(_ x: inout Klass) async {
98+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
99+
// expected-note @-1 {{transferring global actor 'CustomActor'-isolated 'x' to main actor-isolated callee}}
100+
print(x)
101+
}
102+
103+
@CustomActor func testInOutErrorMainActor2(_ x: inout Klass) async { // expected-error {{'x' used after consume}}
104+
await transferToMain(x) // expected-warning {{transferring 'x' may cause a race}}
105+
// expected-note @-1 {{transferring global actor 'CustomActor'-isolated 'x' to main actor-isolated callee}}
106+
_ = consume x // expected-note {{consumed here}}
107+
} // expected-note {{used here}}

0 commit comments

Comments
 (0)