Skip to content

Commit 3ce0f94

Browse files
authored
Merge pull request #85187 from xedin/rdar-161739470
[AST] NonisolatedNonsendingByDefault: Print `@concurrent` on `nonisol…
2 parents 3a2f47e + 9f7d31e commit 3ce0f94

File tree

6 files changed

+72
-22
lines changed

6 files changed

+72
-22
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6660,7 +6660,18 @@ class TypePrinter : public TypeVisitor<TypePrinter, void, NonRecursivePrintOptio
66606660

66616661
auto isolation = info.getIsolation();
66626662
switch (isolation.getKind()) {
6663-
case FunctionTypeIsolation::Kind::NonIsolated:
6663+
case FunctionTypeIsolation::Kind::NonIsolated: {
6664+
// When `NonisolatedNonsendingByDefault` is enabled and async
6665+
// function type is nonisolated it could only mean that it is
6666+
// either explicitly `@concurrent` or inferred to be one and
6667+
// it should be printed accordingly.
6668+
if (ctx.LangOpts.hasFeature(Feature::NonisolatedNonsendingByDefault) &&
6669+
fnType->isAsync()) {
6670+
Printer << "@concurrent ";
6671+
}
6672+
break;
6673+
}
6674+
66646675
case FunctionTypeIsolation::Kind::Parameter:
66656676
break;
66666677

test/Concurrency/attr_execution/nonisolated_nonsending_by_default.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
func testCasts() {
66
let defaultedType = (() async -> ()).self
77
_ = defaultedType as (@concurrent () async -> ()).Type
8-
// expected-error@-1 {{cannot convert value of type '(nonisolated(nonsending) () async -> ()).Type' to type '(() async -> ()).Type' in coercion}}
8+
// expected-error@-1 {{cannot convert value of type '(nonisolated(nonsending) () async -> ()).Type' to type '(@concurrent () async -> ()).Type' in coercion}}
99
_ = defaultedType as (nonisolated(nonsending) () async -> ()).Type // Ok
1010
}
11+
12+
protocol TestWitnessFixIts {
13+
func test(_: @concurrent () async -> Void)
14+
// expected-note@-1 {{protocol requires function 'test' with type '(@concurrent () async -> Void) -> ()'}}
15+
}
16+
17+
do {
18+
struct Test: TestWitnessFixIts { // expected-error {{type 'Test' does not conform to protocol 'TestWitnessFixIts'}}
19+
// expected-note@-1 {{add stubs for conformance}} {{35-35=\n func test(_: @concurrent () async -> Void) {\n <#code#>\n \}\n}}
20+
func test(_: () async -> Void) {}
21+
// expected-note@-1 {{candidate has non-matching type '(nonisolated(nonsending) () async -> Void) -> ()'}}
22+
}
23+
}

test/Concurrency/transfernonsendable.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,8 +1878,11 @@ func mutableLocalCaptureDataRace() async {
18781878
x = 0
18791879
_ = x
18801880

1881-
Task.detached { x = 1 } // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1882-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1881+
Task.detached { x = 1 }
1882+
// expected-ni-warning @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1883+
// expected-ni-note @-2 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1884+
// expected-ni-ns-warning @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
1885+
// expected-ni-ns-note @-4 {{Passing value of non-Sendable type '@concurrent () async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
18831886

18841887
x = 2 // expected-note {{access can happen concurrently}}
18851888
}
@@ -1888,8 +1891,11 @@ func mutableLocalCaptureDataRace2() async {
18881891
var x = 0
18891892
x = 0
18901893

1891-
Task.detached { x = 1 } // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1892-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1894+
Task.detached { x = 1 }
1895+
// expected-ni-warning @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1896+
// expected-ni-note @-2 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1897+
// expected-ni-ns-warning @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
1898+
// expected-ni-ns-note @-4 {{Passing value of non-Sendable type '@concurrent () async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
18931899

18941900
print(x) // expected-note {{access can happen concurrently}}
18951901
}

test/Concurrency/transfernonsendable_closureliterals_isolationinference.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,11 @@ func test_CallerSyncNormal_CalleeAsyncMainActorIsolated() async {
170170

171171
// CHECK-LABEL: // closure #1 in test_CallerSyncNormal_CalleeAsyncMainActorIsolated()
172172
// CHECK-NEXT: // Isolation: global_actor. type: CustomActor
173-
await normalGlobalActorAcceptsAsyncClosure { } // expected-error {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
174-
// expected-note @-1 {{sending global actor 'CustomActor'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'normalGlobalActorAcceptsAsyncClosure' risks causing races in between global actor 'CustomActor'-isolated and main actor-isolated uses}}
173+
await normalGlobalActorAcceptsAsyncClosure { }
174+
// expected-ni-error @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
175+
// expected-ni-note @-2 {{sending global actor 'CustomActor'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'normalGlobalActorAcceptsAsyncClosure' risks causing races in between global actor 'CustomActor'-isolated and main actor-isolated uses}}
176+
// expected-ni-ns-error @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
177+
// expected-ni-ns-note @-4 {{sending global actor 'CustomActor'-isolated value of non-Sendable type '@concurrent () async -> ()' to main actor-isolated global function 'normalGlobalActorAcceptsAsyncClosure' risks causing races in between global actor 'CustomActor'-isolated and main actor-isolated uses}}
175178

176179
// CHECK-LABEL: // closure #2 in test_CallerSyncNormal_CalleeAsyncMainActorIsolated()
177180
// CHECK-NEXT: // Isolation: nonisolated
@@ -286,8 +289,11 @@ func test_CallerAsyncNormal_CalleeAsyncMainActorIsolated() async {
286289

287290
// CHECK-LABEL: // closure #1 in test_CallerAsyncNormal_CalleeAsyncMainActorIsolated()
288291
// CHECK-NEXT: // Isolation: global_actor. type: CustomActor
289-
await asyncNormalGlobalActorAcceptsAsyncClosure { } // expected-error {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
290-
// expected-note @-1 {{sending global actor 'CustomActor'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'asyncNormalGlobalActorAcceptsAsyncClosure' risks causing races in between global actor 'CustomActor'-isolated and main actor-isolated uses}}
292+
await asyncNormalGlobalActorAcceptsAsyncClosure { }
293+
// expected-ni-error @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
294+
// expected-ni-note @-2 {{sending global actor 'CustomActor'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'asyncNormalGlobalActorAcceptsAsyncClosure' risks causing races in between global actor 'CustomActor'-isolated and main actor-isolated uses}}
295+
// expected-ni-ns-error @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
296+
// expected-ni-ns-note @-4 {{sending global actor 'CustomActor'-isolated value of non-Sendable type '@concurrent () async -> ()' to main actor-isolated global function 'asyncNormalGlobalActorAcceptsAsyncClosure' risks causing races in between global actor 'CustomActor'-isolated and main actor-isolated uses}}
291297

292298
// CHECK-LABEL: // closure #2 in test_CallerAsyncNormal_CalleeAsyncMainActorIsolated()
293299
// CHECK-NEXT: // Isolation: nonisolated
@@ -397,8 +403,11 @@ extension MyActor {
397403

398404
// CHECK-LABEL: // closure #1 in MyActor.test_CallerSyncNormal_CalleeAsyncMainActorIsolated()
399405
// CHECK-NEXT: // Isolation: actor_instance. name: 'self'
400-
await normalGlobalActorAcceptsAsyncClosure { print(self) } // expected-error {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
401-
// expected-note @-1 {{sending 'self'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'normalGlobalActorAcceptsAsyncClosure' risks causing races in between 'self'-isolated and main actor-isolated uses}}
406+
await normalGlobalActorAcceptsAsyncClosure { print(self) }
407+
// expected-ni-error @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
408+
// expected-ni-note @-2 {{sending 'self'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'normalGlobalActorAcceptsAsyncClosure' risks causing races in between 'self'-isolated and main actor-isolated uses}}
409+
// expected-ni-ns-error @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
410+
// expected-ni-ns-note @-4 {{sending 'self'-isolated value of non-Sendable type '@concurrent () async -> ()' to main actor-isolated global function 'normalGlobalActorAcceptsAsyncClosure' risks causing races in between 'self'-isolated and main actor-isolated uses}}
402411

403412
// CHECK-LABEL: // closure #2 in MyActor.test_CallerSyncNormal_CalleeAsyncMainActorIsolated()
404413
// CHECK-NEXT: // Isolation: nonisolated
@@ -513,8 +522,11 @@ extension MyActor {
513522

514523
// CHECK-LABEL: // closure #1 in MyActor.test_CallerAsyncNormal_CalleeAsyncMainActorIsolated()
515524
// CHECK-NEXT: // Isolation: actor_instance. name: 'self'
516-
await asyncNormalGlobalActorAcceptsAsyncClosure { print(self) } // expected-error {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
517-
// expected-note @-1 {{sending 'self'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'asyncNormalGlobalActorAcceptsAsyncClosure' risks causing races in between 'self'-isolated and main actor-isolated uses}}
525+
await asyncNormalGlobalActorAcceptsAsyncClosure { print(self) }
526+
// expected-ni-error @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
527+
// expected-ni-note @-2 {{sending 'self'-isolated value of non-Sendable type '() async -> ()' to main actor-isolated global function 'asyncNormalGlobalActorAcceptsAsyncClosure' risks causing races in between 'self'-isolated and main actor-isolated uses}}
528+
// expected-ni-ns-error @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
529+
// expected-ni-ns-note @-4 {{sending 'self'-isolated value of non-Sendable type '@concurrent () async -> ()' to main actor-isolated global function 'asyncNormalGlobalActorAcceptsAsyncClosure' risks causing races in between 'self'-isolated and main actor-isolated uses}}
518530

519531
// CHECK-LABEL: // closure #2 in MyActor.test_CallerAsyncNormal_CalleeAsyncMainActorIsolated()
520532
// CHECK-NEXT: // Isolation: nonisolated

test/Concurrency/transfernonsendable_nonisolatedunsafe.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -target %target-swift-5.1-abi-triple -verify %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability
2-
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -target %target-swift-5.1-abi-triple -verify %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability -enable-upcoming-feature NonisolatedNonsendingByDefault
1+
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -target %target-swift-5.1-abi-triple -verify -verify-additional-prefix ni- %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability
2+
// RUN: %target-swift-frontend -emit-sil -strict-concurrency=complete -target %target-swift-5.1-abi-triple -verify -verify-additional-prefix ni-ns- %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability -enable-upcoming-feature NonisolatedNonsendingByDefault
33

44
// READ THIS: This test is intended to centralize all tests that use
55
// nonisolated(unsafe).
@@ -1009,8 +1009,12 @@ func closureTests() async {
10091009

10101010
func testWithTaskDetached() async {
10111011
let x1 = NonSendableKlass()
1012-
Task.detached { _ = x1 } // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1013-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1012+
Task.detached { _ = x1 }
1013+
// expected-ni-warning @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1014+
// expected-ni-note @-2 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1015+
// expected-ni-ns-warning @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
1016+
// expected-ni-ns-note @-4 {{Passing value of non-Sendable type '@concurrent () async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1017+
10141018
Task.detached { _ = x1 } // expected-note {{access can happen concurrently}}
10151019

10161020
nonisolated(unsafe) let x2 = NonSendableKlass()
@@ -1024,8 +1028,12 @@ func closureTests() async {
10241028

10251029
nonisolated(unsafe) let x4a = NonSendableKlass()
10261030
let x4b = NonSendableKlass()
1027-
Task.detached { _ = x4a; _ = x4b } // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1028-
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1031+
Task.detached { _ = x4a; _ = x4b }
1032+
// expected-ni-warning @-1 {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1033+
// expected-ni-note @-2 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1034+
// expected-ni-ns-warning @-3 {{sending value of non-Sendable type '@concurrent () async -> ()' risks causing data races}}
1035+
// expected-ni-ns-note @-4 {{Passing value of non-Sendable type '@concurrent () async -> ()' as a 'sending' argument to static method 'detached(name:priority:operation:)' risks causing races in between local and caller code}}
1036+
10291037
Task.detached { _ = x4a; _ = x4b } // expected-note {{access can happen concurrently}}
10301038
}
10311039

test/Concurrency/transfernonsendable_typed_errors.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -swift-version 6 -Xllvm -sil-regionbasedisolation-force-use-of-typed-errors -emit-sil -o /dev/null %s -verify -target %target-swift-5.1-abi-triple
2-
// RUN: %target-swift-frontend -swift-version 6 -Xllvm -sil-regionbasedisolation-force-use-of-typed-errors -emit-sil -o /dev/null %s -verify -target %target-swift-5.1-abi-triple -enable-upcoming-feature NonisolatedNonsendingByDefault
1+
// RUN: %target-swift-frontend -swift-version 6 -Xllvm -sil-regionbasedisolation-force-use-of-typed-errors -emit-sil -o /dev/null %s -verify -verify-additional-prefix ni- -target %target-swift-5.1-abi-triple
2+
// RUN: %target-swift-frontend -swift-version 6 -Xllvm -sil-regionbasedisolation-force-use-of-typed-errors -emit-sil -o /dev/null %s -verify -verify-additional-prefix ni-ns- -target %target-swift-5.1-abi-triple -enable-upcoming-feature NonisolatedNonsendingByDefault
33

44
// REQUIRES: concurrency
55
// REQUIRES: asserts

0 commit comments

Comments
 (0)