From 7b109c66f56cb6a3ec1d49313c110310f14dcd49 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Wed, 8 Oct 2025 13:01:10 -0700 Subject: [PATCH 1/2] [rbi] Move inout sending tests into their own file in preparation for work on inout sending. --- ...sfernonsendable_inout_sending_params.swift | 2575 +++++++++++++++++ .../transfernonsendable_sending_params.swift | 2493 ---------------- 2 files changed, 2575 insertions(+), 2493 deletions(-) create mode 100644 test/Concurrency/transfernonsendable_inout_sending_params.swift diff --git a/test/Concurrency/transfernonsendable_inout_sending_params.swift b/test/Concurrency/transfernonsendable_inout_sending_params.swift new file mode 100644 index 0000000000000..a2627846ede09 --- /dev/null +++ b/test/Concurrency/transfernonsendable_inout_sending_params.swift @@ -0,0 +1,2575 @@ +// RUN: %target-swift-frontend -emit-sil -parse-as-library -target %target-swift-5.1-abi-triple -strict-concurrency=complete -verify -verify-additional-prefix ni- %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability +// RUN: %target-swift-frontend -emit-sil -parse-as-library -target %target-swift-5.1-abi-triple -strict-concurrency=complete -verify -verify-additional-prefix ni-ns- %s -o /dev/null -enable-upcoming-feature GlobalActorIsolatedTypesUsability -enable-upcoming-feature NonisolatedNonsendingByDefault + +// REQUIRES: concurrency +// REQUIRES: swift_feature_GlobalActorIsolatedTypesUsability +// REQUIRES: swift_feature_NonisolatedNonsendingByDefault + +//////////////////////// +// MARK: Declarations // +//////////////////////// + +class NonSendableKlass { + func use() {} +} + +struct NonSendableStruct { + var first = NonSendableKlass() + var second = NonSendableKlass() +} + +class KlassWithNonSendableStructPair { + var ns1: NonSendableStruct + var ns2: (NonSendableStruct, NonSendableStruct) + + init() { + ns1 = NonSendableStruct() + ns2 = (ns1, ns1) + } +} + +final class FinalKlassWithNonSendableStructPair { + var ns1: NonSendableStruct + var ns2: (NonSendableStruct, NonSendableStruct) + + init() { + ns1 = NonSendableStruct() + ns2 = (ns1, ns1) + } +} + +func useValue(_ t: T) {} +func useValueAndReturnGeneric(_ t: T) -> T { t } +func useNonSendableKlassAndReturn(_ t: NonSendableKlass) -> NonSendableKlass { t } +func getAny() -> Any { fatalError() } + +actor Custom { +} + +@globalActor +struct CustomActor { + static var shared: Custom { + return Custom() + } +} + +@MainActor func transferToMain(_ t: T) {} +@CustomActor func transferToCustom(_ t: T) {} + +func throwingFunction() throws { fatalError() } + +func getBool() -> Bool { false } + +func transferArg(_ x: sending NonSendableKlass) { +} + +func transferArgAsync(_ x: sending NonSendableKlass) async { +} + +func transferArgWithOtherParam(_ x: sending NonSendableKlass, _ y: NonSendableKlass) { +} + +func transferArgWithOtherParam2(_ x: NonSendableKlass, _ y: sending NonSendableKlass) { +} + +func twoTransferArg(_ x: sending NonSendableKlass, _ y: sending NonSendableKlass) {} + +@MainActor var globalKlass = NonSendableKlass() + +struct MyError : Error {} + +func takeClosure(_ x: sending () -> ()) {} +func takeClosureAndParam(_ x: NonSendableKlass, _ y: sending () -> ()) {} + +/////////////////////////////// +// MARK: InOut Sending Tests // +/////////////////////////////// + +func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} +} // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + +func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async { + await transferToMain(x) + x = NonSendableKlass() +} + +func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} + + try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + + x = NonSendableKlass() +} + +func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} + + do { + try throwingFunction() + x = NonSendableKlass() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + + x = NonSendableKlass() +} + +func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} + + do { + try throwingFunction() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + + x = NonSendableKlass() +} + +func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} + + do { + try throwingFunction() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } +} // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + +actor InOutSendingWrongIsolationActor { + var ns = NonSendableKlass() + func testWrongIsolation(_ x: inout sending NonSendableKlass) { + x = ns + } // expected-warning {{'inout sending' parameter 'x' cannot be 'self'-isolated at end of function}} + // expected-note @-1 {{'self'-isolated 'x' risks causing races in between 'self'-isolated uses and caller uses since caller assumes value is not actor isolated}} + + func testWrongIsolation2(_ x: inout sending NonSendableKlass) { + let z = ns + x = z + } // expected-warning {{'inout sending' parameter 'x' cannot be 'self'-isolated at end of function}} + // expected-note @-1 {{'self'-isolated 'x' risks causing races in between 'self'-isolated uses and caller uses since caller assumes value is not actor isolated}} +} + +@MainActor +func testWrongIsolationGlobalIsolation(_ x: inout sending NonSendableKlass) { + x = globalKlass +} // expected-warning {{'inout sending' parameter 'x' cannot be main actor-isolated at end of function}} +// expected-note @-1 {{main actor-isolated 'x' risks causing races in between main actor-isolated uses and caller uses since caller assumes value is not actor isolated}} + +////////////////////////////////////// +// MARK: Return Inout Sending Tests // +////////////////////////////////////// + +func returnInOutSendingDirectly(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionLet(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + + +func returnInOutSendingViaHelper(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingHelperDirect(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingDirectlyIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + fatalError() +} + +func returnInOutSendingRegionLetIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingRegionVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + fatalError() +} + +func returnInOutSendingViaHelperIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingHelperDirectIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingViaHelperVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingDirectlyElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + print(x) + fatalError() + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingRegionLetElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + print(y) + fatalError() + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingRegionVarElse(_ x: inout sending NonSendableKlass, z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingViaHelperElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + print(y) + return z + } else { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingHelperDirectElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + if getBool() { + print(x) + return z + } else { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingViaHelperVarElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnInOutSendingDirectlyFor(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingDirectlyFor2(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(x) + return z +} + +func returnInOutSendingRegionLetFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionLetFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionLetFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return z +} + +func returnInOutSendingRegionVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionVarFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionVarFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return z +} + +func returnInOutSendingViaHelperFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingViaHelperFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingHelperDirectFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingViaHelperVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingDirectlyGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionLetGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingRegionVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + guard getBool() else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingViaHelperGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingHelperDirectGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingViaHelperVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnInOutSendingViaHelperVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingDirectly(_ x: inout sending T) -> T { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionLet(_ x: inout sending T) -> T { + let y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionVar(_ x: inout sending T) -> T { + var y = x + y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingViaHelper(_ x: inout sending T) -> T { + let y = x + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingHelperDirect(_ x: inout sending T) -> T { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingViaHelperVar(_ x: inout sending T) -> T { + var y = x + y = x + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingDirectlyIf(_ x: inout sending T) -> T { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + fatalError() +} + +func returnGenericInOutSendingRegionLetIf(_ x: inout sending T) -> T { + let y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingRegionVarIf(_ x: inout sending T) -> T { + var y = x + y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + fatalError() +} + +func returnGenericInOutSendingViaHelperIf(_ x: inout sending T) -> T { + let y = x + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingHelperDirectIf(_ x: inout sending T) -> T { + if getBool() { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingViaHelperVarIf(_ x: inout sending T) -> T { + var y = x + y = x + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingDirectlyElse(_ x: inout sending T) -> T { + if getBool() { + print(x) + fatalError() + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingRegionLetElse(_ x: inout sending T) -> T { + let y = x + if getBool() { + print(y) + fatalError() + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingRegionVarElse(_ x: inout sending T, z: T) -> T { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingViaHelperElse(_ x: inout sending T, _ z: T) -> T { + let y = x + if getBool() { + print(y) + return z + } else { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingHelperDirectElse(_ x: inout sending T, _ z: T) -> T { + if getBool() { + print(x) + return z + } else { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingViaHelperVarElse(_ x: inout sending T, _ z: T) -> T { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } +} + +func returnGenericInOutSendingDirectlyFor(_ x: inout sending T, _ z: T) -> T { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingDirectlyFor2(_ x: inout sending T, _ z: T) -> T { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(x) + return z +} + +func returnGenericInOutSendingRegionLetFor(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionLetFor2(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionLetFor3(_ x: inout sending T, _ z: T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return z +} + +func returnGenericInOutSendingRegionVarFor(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionVarFor2(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionVarFor3(_ x: inout sending T, _ z: T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return z +} + +func returnGenericInOutSendingViaHelperFor(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingViaHelperFor2(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingHelperDirectFor(_ x: inout sending T) -> T { + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingViaHelperVarFor(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingDirectlyGuard(_ x: inout sending T) -> T { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionLetGuard(_ x: inout sending T) -> T { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingRegionVarGuard(_ x: inout sending T) -> T { + var y = x + y = x + guard getBool() else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingViaHelperGuard(_ x: inout sending T) -> T { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingHelperDirectGuard(_ x: inout sending T) -> T { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} + } + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +func returnGenericInOutSendingViaHelperVarGuard(_ x: inout sending T) -> T { + var y = x + y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} + } + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} +} + +/////////////////////////////////////////////////// +// MARK: ReturnSendingInOutTestActor Actor Tests // +/////////////////////////////////////////////////// + +actor ReturnSendingInOutTestActor { + + func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} + } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + + func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async { + await transferToMain(x) + x = NonSendableKlass() + } + + func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} + + + try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + + x = NonSendableKlass() + } + + func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} + + + do { + try throwingFunction() + x = NonSendableKlass() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + + x = NonSendableKlass() + } + + func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} + + + do { + try throwingFunction() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + + x = NonSendableKlass() + } + + func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws { + await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} + + + do { + try throwingFunction() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + + func returnInOutSendingDirectly(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionLet(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingViaHelper(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingHelperDirect(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingDirectlyIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + fatalError() + } + + func returnInOutSendingRegionLetIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingRegionVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + fatalError() + } + + func returnInOutSendingViaHelperIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingHelperDirectIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingViaHelperVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingDirectlyElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + print(x) + fatalError() + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingRegionLetElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + print(y) + fatalError() + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingRegionVarElse(_ x: inout sending NonSendableKlass, z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingViaHelperElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + print(y) + return z + } else { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingHelperDirectElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + if getBool() { + print(x) + return z + } else { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingViaHelperVarElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnInOutSendingDirectlyFor(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingDirectlyFor2(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(x) + return z + } + + func returnInOutSendingRegionLetFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionLetFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionLetFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return z + } + + func returnInOutSendingRegionVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionVarFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionVarFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return z + } + + func returnInOutSendingViaHelperFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingViaHelperFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingHelperDirectFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingViaHelperVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingDirectlyGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionLetGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingRegionVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + guard getBool() else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingViaHelperGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingHelperDirectGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingViaHelperVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnInOutSendingViaHelperVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingDirectly(_ x: inout sending T) -> T { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionLet(_ x: inout sending T) -> T { + let y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionVar(_ x: inout sending T) -> T { + var y = x + y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingViaHelper(_ x: inout sending T) -> T { + let y = x + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingHelperDirect(_ x: inout sending T) -> T { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingViaHelperVar(_ x: inout sending T) -> T { + var y = x + y = x + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingDirectlyIf(_ x: inout sending T) -> T { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + fatalError() + } + + func returnGenericInOutSendingRegionLetIf(_ x: inout sending T) -> T { + let y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingRegionVarIf(_ x: inout sending T) -> T { + var y = x + y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + fatalError() + } + + func returnGenericInOutSendingViaHelperIf(_ x: inout sending T) -> T { + let y = x + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingHelperDirectIf(_ x: inout sending T) -> T { + if getBool() { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingViaHelperVarIf(_ x: inout sending T) -> T { + var y = x + y = x + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingDirectlyElse(_ x: inout sending T) -> T { + if getBool() { + print(x) + fatalError() + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingRegionLetElse(_ x: inout sending T) -> T { + let y = x + if getBool() { + print(y) + fatalError() + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingRegionVarElse(_ x: inout sending T, z: T) -> T { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingViaHelperElse(_ x: inout sending T, _ z: T) -> T { + let y = x + if getBool() { + print(y) + return z + } else { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingHelperDirectElse(_ x: inout sending T, _ z: T) -> T { + if getBool() { + print(x) + return z + } else { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingViaHelperVarElse(_ x: inout sending T, _ z: T) -> T { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + + func returnGenericInOutSendingDirectlyFor(_ x: inout sending T, _ z: T) -> T { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingDirectlyFor2(_ x: inout sending T, _ z: T) -> T { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(x) + return z + } + + func returnGenericInOutSendingRegionLetFor(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionLetFor2(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionLetFor3(_ x: inout sending T, _ z: T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return z + } + + func returnGenericInOutSendingRegionVarFor(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionVarFor2(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionVarFor3(_ x: inout sending T, _ z: T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return z + } + + func returnGenericInOutSendingViaHelperFor(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingViaHelperFor2(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingHelperDirectFor(_ x: inout sending T) -> T { + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingViaHelperVarFor(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingDirectlyGuard(_ x: inout sending T) -> T { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionLetGuard(_ x: inout sending T) -> T { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingRegionVarGuard(_ x: inout sending T) -> T { + var y = x + y = x + guard getBool() else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingViaHelperGuard(_ x: inout sending T) -> T { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingHelperDirectGuard(_ x: inout sending T) -> T { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + + func returnGenericInOutSendingViaHelperVarGuard(_ x: inout sending T) -> T { + var y = x + y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} + } +} + +//////////////////////////////////////////////////////////////// +// MARK: ReturnSendingInOutTestGlobalActorIsolatedClass Tests // +//////////////////////////////////////////////////////////////// + +@MainActor +class ReturnSendingInOutTestGlobalActorIsolatedClass { + + func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async { + await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} + } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + // expected-note @-2 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} + + func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async { + await transferToCustom(x) + x = NonSendableKlass() + } + + func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws { + await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} + + try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + + x = NonSendableKlass() + } + + func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws { + await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} + + do { + try throwingFunction() + x = NonSendableKlass() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + + x = NonSendableKlass() + } + + func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws { + await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} + + do { + try throwingFunction() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + + x = NonSendableKlass() + } + + func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws { + await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} + + do { + try throwingFunction() + } catch { + throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + } + } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} + + func returnInOutSendingDirectly(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionLet(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingViaHelper(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingHelperDirect(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingDirectlyIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + fatalError() + } + + func returnInOutSendingRegionLetIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingRegionVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + fatalError() + } + + func returnInOutSendingViaHelperIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingHelperDirectIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingViaHelperVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingDirectlyElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + if getBool() { + print(x) + fatalError() + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingRegionLetElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + print(y) + fatalError() + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingRegionVarElse(_ x: inout sending NonSendableKlass, z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingViaHelperElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + let y = x + if getBool() { + print(y) + return z + } else { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingHelperDirectElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + if getBool() { + print(x) + return z + } else { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingViaHelperVarElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnInOutSendingDirectlyFor(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingDirectlyFor2(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(x) + return z + } + + func returnInOutSendingRegionLetFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionLetFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionLetFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return z + } + + func returnInOutSendingRegionVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionVarFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionVarFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return z + } + + func returnInOutSendingViaHelperFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingViaHelperFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingHelperDirectFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingViaHelperVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingDirectlyGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionLetGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingRegionVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + guard getBool() else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingViaHelperGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingHelperDirectGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingViaHelperVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnInOutSendingViaHelperVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { + var y = x + y = x + return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingDirectly(_ x: inout sending T) -> T { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionLet(_ x: inout sending T) -> T { + let y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionVar(_ x: inout sending T) -> T { + var y = x + y = x + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingViaHelper(_ x: inout sending T) -> T { + let y = x + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingHelperDirect(_ x: inout sending T) -> T { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingViaHelperVar(_ x: inout sending T) -> T { + var y = x + y = x + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingDirectlyIf(_ x: inout sending T) -> T { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + fatalError() + } + + func returnGenericInOutSendingRegionLetIf(_ x: inout sending T) -> T { + let y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingRegionVarIf(_ x: inout sending T) -> T { + var y = x + y = x + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + fatalError() + } + + func returnGenericInOutSendingViaHelperIf(_ x: inout sending T) -> T { + let y = x + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingHelperDirectIf(_ x: inout sending T) -> T { + if getBool() { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingViaHelperVarIf(_ x: inout sending T) -> T { + var y = x + y = x + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingDirectlyElse(_ x: inout sending T) -> T { + if getBool() { + print(x) + fatalError() + } else { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingRegionLetElse(_ x: inout sending T) -> T { + let y = x + if getBool() { + print(y) + fatalError() + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingRegionVarElse(_ x: inout sending T, z: T) -> T { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingViaHelperElse(_ x: inout sending T, _ z: T) -> T { + let y = x + if getBool() { + print(y) + return z + } else { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingHelperDirectElse(_ x: inout sending T, _ z: T) -> T { + if getBool() { + print(x) + return z + } else { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingViaHelperVarElse(_ x: inout sending T, _ z: T) -> T { + var y = x + y = x + if getBool() { + print(y) + return z + } else { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + + func returnGenericInOutSendingDirectlyFor(_ x: inout sending T, _ z: T) -> T { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingDirectlyFor2(_ x: inout sending T, _ z: T) -> T { + for _ in 0..<1 { + if getBool() { + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(x) + return z + } + + func returnGenericInOutSendingRegionLetFor(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionLetFor2(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionLetFor3(_ x: inout sending T, _ z: T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return z + } + + func returnGenericInOutSendingRegionVarFor(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionVarFor2(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionVarFor3(_ x: inout sending T, _ z: T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return z + } + + func returnGenericInOutSendingViaHelperFor(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingViaHelperFor2(_ x: inout sending T) -> T { + let y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingHelperDirectFor(_ x: inout sending T) -> T { + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingViaHelperVarFor(_ x: inout sending T) -> T { + var y = x + y = x + for _ in 0..<1 { + if getBool() { + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + } + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingDirectlyGuard(_ x: inout sending T) -> T { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionLetGuard(_ x: inout sending T) -> T { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingRegionVarGuard(_ x: inout sending T) -> T { + var y = x + y = x + guard getBool() else { + print(y) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingViaHelperGuard(_ x: inout sending T) -> T { + let y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingHelperDirectGuard(_ x: inout sending T) -> T { + guard getBool() else { + print(x) + return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} + // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + + func returnGenericInOutSendingViaHelperVarGuard(_ x: inout sending T) -> T { + var y = x + y = x + guard getBool() else { + print(y) + return y // expected-warning {{'y' cannot be returned}} + // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } + return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} + // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} + } +} diff --git a/test/Concurrency/transfernonsendable_sending_params.swift b/test/Concurrency/transfernonsendable_sending_params.swift index f8024bc575571..7f24b3f23a93d 100644 --- a/test/Concurrency/transfernonsendable_sending_params.swift +++ b/test/Concurrency/transfernonsendable_sending_params.swift @@ -437,2499 +437,6 @@ func testNoCrashWhenSendingNoEscapeClosure() async { await test { print(c) } } -/////////////////////////////// -// MARK: InOut Sending Tests // -/////////////////////////////// - -func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} -} // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - -func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async { - await transferToMain(x) - x = NonSendableKlass() -} - -func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} - - try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - - x = NonSendableKlass() -} - -func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} - - do { - try throwingFunction() - x = NonSendableKlass() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - - x = NonSendableKlass() -} - -func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} - - do { - try throwingFunction() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - - x = NonSendableKlass() -} - -func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local nonisolated uses}} - - do { - try throwingFunction() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } -} // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - -actor InOutSendingWrongIsolationActor { - var ns = NonSendableKlass() - func testWrongIsolation(_ x: inout sending NonSendableKlass) { - x = ns - } // expected-warning {{'inout sending' parameter 'x' cannot be 'self'-isolated at end of function}} - // expected-note @-1 {{'self'-isolated 'x' risks causing races in between 'self'-isolated uses and caller uses since caller assumes value is not actor isolated}} - - func testWrongIsolation2(_ x: inout sending NonSendableKlass) { - let z = ns - x = z - } // expected-warning {{'inout sending' parameter 'x' cannot be 'self'-isolated at end of function}} - // expected-note @-1 {{'self'-isolated 'x' risks causing races in between 'self'-isolated uses and caller uses since caller assumes value is not actor isolated}} -} - -@MainActor -func testWrongIsolationGlobalIsolation(_ x: inout sending NonSendableKlass) { - x = globalKlass -} // expected-warning {{'inout sending' parameter 'x' cannot be main actor-isolated at end of function}} -// expected-note @-1 {{main actor-isolated 'x' risks causing races in between main actor-isolated uses and caller uses since caller assumes value is not actor isolated}} - -////////////////////////////////////// -// MARK: Return Inout Sending Tests // -////////////////////////////////////// - -func returnInOutSendingDirectly(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionLet(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - - -func returnInOutSendingViaHelper(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingHelperDirect(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingDirectlyIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - fatalError() -} - -func returnInOutSendingRegionLetIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingRegionVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - fatalError() -} - -func returnInOutSendingViaHelperIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingHelperDirectIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingViaHelperVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingDirectlyElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - print(x) - fatalError() - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingRegionLetElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - print(y) - fatalError() - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingRegionVarElse(_ x: inout sending NonSendableKlass, z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingViaHelperElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - print(y) - return z - } else { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingHelperDirectElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - if getBool() { - print(x) - return z - } else { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingViaHelperVarElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnInOutSendingDirectlyFor(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingDirectlyFor2(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(x) - return z -} - -func returnInOutSendingRegionLetFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionLetFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionLetFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return z -} - -func returnInOutSendingRegionVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionVarFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionVarFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return z -} - -func returnInOutSendingViaHelperFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingViaHelperFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingHelperDirectFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingViaHelperVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingDirectlyGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionLetGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingRegionVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - guard getBool() else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingViaHelperGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingHelperDirectGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingViaHelperVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnInOutSendingViaHelperVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingDirectly(_ x: inout sending T) -> T { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionLet(_ x: inout sending T) -> T { - let y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionVar(_ x: inout sending T) -> T { - var y = x - y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingViaHelper(_ x: inout sending T) -> T { - let y = x - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingHelperDirect(_ x: inout sending T) -> T { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingViaHelperVar(_ x: inout sending T) -> T { - var y = x - y = x - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingDirectlyIf(_ x: inout sending T) -> T { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - fatalError() -} - -func returnGenericInOutSendingRegionLetIf(_ x: inout sending T) -> T { - let y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingRegionVarIf(_ x: inout sending T) -> T { - var y = x - y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - fatalError() -} - -func returnGenericInOutSendingViaHelperIf(_ x: inout sending T) -> T { - let y = x - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingHelperDirectIf(_ x: inout sending T) -> T { - if getBool() { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingViaHelperVarIf(_ x: inout sending T) -> T { - var y = x - y = x - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingDirectlyElse(_ x: inout sending T) -> T { - if getBool() { - print(x) - fatalError() - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingRegionLetElse(_ x: inout sending T) -> T { - let y = x - if getBool() { - print(y) - fatalError() - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingRegionVarElse(_ x: inout sending T, z: T) -> T { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingViaHelperElse(_ x: inout sending T, _ z: T) -> T { - let y = x - if getBool() { - print(y) - return z - } else { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingHelperDirectElse(_ x: inout sending T, _ z: T) -> T { - if getBool() { - print(x) - return z - } else { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingViaHelperVarElse(_ x: inout sending T, _ z: T) -> T { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } -} - -func returnGenericInOutSendingDirectlyFor(_ x: inout sending T, _ z: T) -> T { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingDirectlyFor2(_ x: inout sending T, _ z: T) -> T { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(x) - return z -} - -func returnGenericInOutSendingRegionLetFor(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionLetFor2(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionLetFor3(_ x: inout sending T, _ z: T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return z -} - -func returnGenericInOutSendingRegionVarFor(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionVarFor2(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionVarFor3(_ x: inout sending T, _ z: T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return z -} - -func returnGenericInOutSendingViaHelperFor(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingViaHelperFor2(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingHelperDirectFor(_ x: inout sending T) -> T { - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingViaHelperVarFor(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingDirectlyGuard(_ x: inout sending T) -> T { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionLetGuard(_ x: inout sending T) -> T { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingRegionVarGuard(_ x: inout sending T) -> T { - var y = x - y = x - guard getBool() else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingViaHelperGuard(_ x: inout sending T) -> T { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingHelperDirectGuard(_ x: inout sending T) -> T { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'inout sending' parameter 'x' risks concurrent access as caller assumes 'x' and result can be sent to different isolation domains}} - } - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -func returnGenericInOutSendingViaHelperVarGuard(_ x: inout sending T) -> T { - var y = x - y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} - } - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' and result can be sent to different isolation domains}} -} - -/////////////////////////////////////////////////// -// MARK: ReturnSendingInOutTestActor Actor Tests // -/////////////////////////////////////////////////// - -actor ReturnSendingInOutTestActor { - - func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} - } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - - func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async { - await transferToMain(x) - x = NonSendableKlass() - } - - func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} - - - try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - - x = NonSendableKlass() - } - - func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} - - - do { - try throwingFunction() - x = NonSendableKlass() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - - x = NonSendableKlass() - } - - func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} - - - do { - try throwingFunction() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - - x = NonSendableKlass() - } - - func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws { - await transferToMain(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to main actor-isolated global function 'transferToMain' risks causing data races between main actor-isolated and local actor-isolated uses}} - - - do { - try throwingFunction() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - - func returnInOutSendingDirectly(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionLet(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingViaHelper(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingHelperDirect(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingDirectlyIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - fatalError() - } - - func returnInOutSendingRegionLetIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingRegionVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - fatalError() - } - - func returnInOutSendingViaHelperIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingHelperDirectIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingViaHelperVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingDirectlyElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - print(x) - fatalError() - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingRegionLetElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - print(y) - fatalError() - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingRegionVarElse(_ x: inout sending NonSendableKlass, z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingViaHelperElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - print(y) - return z - } else { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingHelperDirectElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - if getBool() { - print(x) - return z - } else { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingViaHelperVarElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnInOutSendingDirectlyFor(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingDirectlyFor2(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(x) - return z - } - - func returnInOutSendingRegionLetFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionLetFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionLetFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return z - } - - func returnInOutSendingRegionVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionVarFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionVarFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return z - } - - func returnInOutSendingViaHelperFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingViaHelperFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingHelperDirectFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingViaHelperVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingDirectlyGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionLetGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingRegionVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - guard getBool() else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingViaHelperGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingHelperDirectGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingViaHelperVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnInOutSendingViaHelperVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingDirectly(_ x: inout sending T) -> T { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionLet(_ x: inout sending T) -> T { - let y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionVar(_ x: inout sending T) -> T { - var y = x - y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingViaHelper(_ x: inout sending T) -> T { - let y = x - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingHelperDirect(_ x: inout sending T) -> T { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingViaHelperVar(_ x: inout sending T) -> T { - var y = x - y = x - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingDirectlyIf(_ x: inout sending T) -> T { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - fatalError() - } - - func returnGenericInOutSendingRegionLetIf(_ x: inout sending T) -> T { - let y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingRegionVarIf(_ x: inout sending T) -> T { - var y = x - y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - fatalError() - } - - func returnGenericInOutSendingViaHelperIf(_ x: inout sending T) -> T { - let y = x - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingHelperDirectIf(_ x: inout sending T) -> T { - if getBool() { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingViaHelperVarIf(_ x: inout sending T) -> T { - var y = x - y = x - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingDirectlyElse(_ x: inout sending T) -> T { - if getBool() { - print(x) - fatalError() - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingRegionLetElse(_ x: inout sending T) -> T { - let y = x - if getBool() { - print(y) - fatalError() - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingRegionVarElse(_ x: inout sending T, z: T) -> T { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingViaHelperElse(_ x: inout sending T, _ z: T) -> T { - let y = x - if getBool() { - print(y) - return z - } else { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingHelperDirectElse(_ x: inout sending T, _ z: T) -> T { - if getBool() { - print(x) - return z - } else { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingViaHelperVarElse(_ x: inout sending T, _ z: T) -> T { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - - func returnGenericInOutSendingDirectlyFor(_ x: inout sending T, _ z: T) -> T { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingDirectlyFor2(_ x: inout sending T, _ z: T) -> T { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(x) - return z - } - - func returnGenericInOutSendingRegionLetFor(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionLetFor2(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionLetFor3(_ x: inout sending T, _ z: T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return z - } - - func returnGenericInOutSendingRegionVarFor(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionVarFor2(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionVarFor3(_ x: inout sending T, _ z: T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return z - } - - func returnGenericInOutSendingViaHelperFor(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingViaHelperFor2(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingHelperDirectFor(_ x: inout sending T) -> T { - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingViaHelperVarFor(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingDirectlyGuard(_ x: inout sending T) -> T { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionLetGuard(_ x: inout sending T) -> T { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingRegionVarGuard(_ x: inout sending T) -> T { - var y = x - y = x - guard getBool() else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingViaHelperGuard(_ x: inout sending T) -> T { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingHelperDirectGuard(_ x: inout sending T) -> T { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - - func returnGenericInOutSendingViaHelperVarGuard(_ x: inout sending T) -> T { - var y = x - y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is 'self'-isolated}} - } -} - -//////////////////////////////////////////////////////////////// -// MARK: ReturnSendingInOutTestGlobalActorIsolatedClass Tests // -//////////////////////////////////////////////////////////////// - -@MainActor -class ReturnSendingInOutTestGlobalActorIsolatedClass { - - func testInOutSendingReinit(_ x: inout sending NonSendableKlass) async { - await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} - } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - // expected-note @-2 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} - - func testInOutSendingReinit2(_ x: inout sending NonSendableKlass) async { - await transferToCustom(x) - x = NonSendableKlass() - } - - func testInOutSendingReinit3(_ x: inout sending NonSendableKlass) async throws { - await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} - - try throwingFunction() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - - x = NonSendableKlass() - } - - func testInOutSendingReinit4(_ x: inout sending NonSendableKlass) async throws { - await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} - - do { - try throwingFunction() - x = NonSendableKlass() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - - x = NonSendableKlass() - } - - func testInOutSendingReinit5(_ x: inout sending NonSendableKlass) async throws { - await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} - - do { - try throwingFunction() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - - x = NonSendableKlass() - } - - func testInOutSendingReinit6(_ x: inout sending NonSendableKlass) async throws { - await transferToCustom(x) // expected-warning {{sending 'x' risks causing data races}} - // expected-note @-1 {{sending 'x' to global actor 'CustomActor'-isolated global function 'transferToCustom' risks causing data races between global actor 'CustomActor'-isolated and local main actor-isolated uses}} - - do { - try throwingFunction() - } catch { - throw MyError() // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - } - } // expected-note {{'inout sending' parameter must be reinitialized before function exit with a non-actor-isolated value}} - - func returnInOutSendingDirectly(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionLet(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingViaHelper(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingHelperDirect(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingDirectlyIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - fatalError() - } - - func returnInOutSendingRegionLetIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingRegionVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - fatalError() - } - - func returnInOutSendingViaHelperIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingHelperDirectIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingViaHelperVarIf(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingDirectlyElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - if getBool() { - print(x) - fatalError() - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingRegionLetElse(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - print(y) - fatalError() - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingRegionVarElse(_ x: inout sending NonSendableKlass, z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingViaHelperElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - let y = x - if getBool() { - print(y) - return z - } else { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingHelperDirectElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - if getBool() { - print(x) - return z - } else { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingViaHelperVarElse(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnInOutSendingDirectlyFor(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingDirectlyFor2(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(x) - return z - } - - func returnInOutSendingRegionLetFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionLetFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionLetFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return z - } - - func returnInOutSendingRegionVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionVarFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionVarFor3(_ x: inout sending NonSendableKlass, _ z: NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return z - } - - func returnInOutSendingViaHelperFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingViaHelperFor2(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingHelperDirectFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingViaHelperVarFor(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingDirectlyGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionLetGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingRegionVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - guard getBool() else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingViaHelperGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingHelperDirectGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return useNonSendableKlassAndReturn(x) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingViaHelperVarGuard(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnInOutSendingViaHelperVar(_ x: inout sending NonSendableKlass) -> NonSendableKlass { - var y = x - y = x - return useNonSendableKlassAndReturn(y) // expected-warning {{result of global function 'useNonSendableKlassAndReturn' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useNonSendableKlassAndReturn' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingDirectly(_ x: inout sending T) -> T { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionLet(_ x: inout sending T) -> T { - let y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionVar(_ x: inout sending T) -> T { - var y = x - y = x - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingViaHelper(_ x: inout sending T) -> T { - let y = x - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingHelperDirect(_ x: inout sending T) -> T { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingViaHelperVar(_ x: inout sending T) -> T { - var y = x - y = x - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingDirectlyIf(_ x: inout sending T) -> T { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - fatalError() - } - - func returnGenericInOutSendingRegionLetIf(_ x: inout sending T) -> T { - let y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingRegionVarIf(_ x: inout sending T) -> T { - var y = x - y = x - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - fatalError() - } - - func returnGenericInOutSendingViaHelperIf(_ x: inout sending T) -> T { - let y = x - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingHelperDirectIf(_ x: inout sending T) -> T { - if getBool() { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingViaHelperVarIf(_ x: inout sending T) -> T { - var y = x - y = x - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingDirectlyElse(_ x: inout sending T) -> T { - if getBool() { - print(x) - fatalError() - } else { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingRegionLetElse(_ x: inout sending T) -> T { - let y = x - if getBool() { - print(y) - fatalError() - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingRegionVarElse(_ x: inout sending T, z: T) -> T { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingViaHelperElse(_ x: inout sending T, _ z: T) -> T { - let y = x - if getBool() { - print(y) - return z - } else { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingHelperDirectElse(_ x: inout sending T, _ z: T) -> T { - if getBool() { - print(x) - return z - } else { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingViaHelperVarElse(_ x: inout sending T, _ z: T) -> T { - var y = x - y = x - if getBool() { - print(y) - return z - } else { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - - func returnGenericInOutSendingDirectlyFor(_ x: inout sending T, _ z: T) -> T { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingDirectlyFor2(_ x: inout sending T, _ z: T) -> T { - for _ in 0..<1 { - if getBool() { - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(x) - return z - } - - func returnGenericInOutSendingRegionLetFor(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionLetFor2(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionLetFor3(_ x: inout sending T, _ z: T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return z - } - - func returnGenericInOutSendingRegionVarFor(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionVarFor2(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionVarFor3(_ x: inout sending T, _ z: T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return z - } - - func returnGenericInOutSendingViaHelperFor(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingViaHelperFor2(_ x: inout sending T) -> T { - let y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingHelperDirectFor(_ x: inout sending T) -> T { - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingViaHelperVarFor(_ x: inout sending T) -> T { - var y = x - y = x - for _ in 0..<1 { - if getBool() { - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - } - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingDirectlyGuard(_ x: inout sending T) -> T { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionLetGuard(_ x: inout sending T) -> T { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingRegionVarGuard(_ x: inout sending T) -> T { - var y = x - y = x - guard getBool() else { - print(y) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingViaHelperGuard(_ x: inout sending T) -> T { - let y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingHelperDirectGuard(_ x: inout sending T) -> T { - guard getBool() else { - print(x) - return x // expected-warning {{'inout sending' parameter 'x' cannot be returned}} - // expected-note @-1 {{returning 'x' risks concurrent access as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return useValueAndReturnGeneric(x) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - - func returnGenericInOutSendingViaHelperVarGuard(_ x: inout sending T) -> T { - var y = x - y = x - guard getBool() else { - print(y) - return y // expected-warning {{'y' cannot be returned}} - // expected-note @-1 {{returning 'y' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } - return useValueAndReturnGeneric(y) // expected-warning {{result of global function 'useValueAndReturnGeneric' cannot be returned}} - // expected-note @-1 {{returning result of global function 'useValueAndReturnGeneric' risks concurrent access to 'inout sending' parameter 'x' as caller assumes 'x' is not actor-isolated and result is main actor-isolated}} - } -} - ////////////////////// // MARK: Misc Tests // ////////////////////// From 13e8eed3a86dbf90a1e8497457a12a48a400acd6 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Wed, 8 Oct 2025 16:57:39 -0700 Subject: [PATCH 2/2] [rbi] Cleanup handling of how we send parameters to fix issues with inout sending. We previously were not "unsending" inout sending parameters after sending them so they could not be used again in the caller and could not be forwarded into other 'inout sending' parameters. While looking at the code I realized it was pretty obtuse/confusing so I cleaned up the logic and fixed a few other issues in the process. Now we follow the following pattern in the non-isolation crossing case: 1. We first require the callee operand. 2. We then merge/require all of the non-explicitly sent parameters. 3. We then through all of the parameters and require/send all of the sending parameters. 4. At the end of processing, we unsend all of the sending parameters that were 'inout sending' parameters. In the case of isolation crossing applies we: 1. Require all parameters that are not explicitly marked as sending and then send them all at once. We are really just saving a little work by not merging them into one large region and then requiring/sending that region once. 2. Then for each sending parameter, we require/send them one by one interleaving the requires/sends. This ensures that if a value is passed to different explicitly sending parameters, we get an error. 3. Then once we have finished processing results, we perform an undo send on all of the 'inout sending' params. rdar://154440896 --- include/swift/SIL/ApplySite.h | 22 ++- lib/SILOptimizer/Analysis/RegionAnalysis.cpp | 174 +++++++++++------- ...sfernonsendable_inout_sending_params.swift | 14 ++ .../transfernonsendable_sending_params.swift | 34 ++++ 4 files changed, 175 insertions(+), 69 deletions(-) diff --git a/include/swift/SIL/ApplySite.h b/include/swift/SIL/ApplySite.h index 62b7c21c03169..fd44606b80e8b 100644 --- a/include/swift/SIL/ApplySite.h +++ b/include/swift/SIL/ApplySite.h @@ -645,14 +645,27 @@ class ApplySite { return getSubstCalleeConv().getParamInfoForSILArg(calleeArgIndex); } + /// Returns true if \p op is the callee operand of this apply site + /// and not an argument operand. + /// + /// If this instruction is not a full apply site, this always returns false. + bool isCalleeOperand(const Operand &op) const; + + /// Returns true if this is an 'out' parameter. bool isSending(const Operand &oper) const { - if (isIndirectErrorResultOperand(oper)) + if (isIndirectErrorResultOperand(oper) || oper.isTypeDependent() || + isCalleeOperand(oper)) return false; if (isIndirectResultOperand(oper)) return getSubstCalleeType()->hasSendingResult(); return getArgumentParameterInfo(oper).hasOption(SILParameterInfo::Sending); } + /// Returns true if this operand is an 'inout sending' parameter. + bool isInOutSending(const Operand &oper) const { + return isSending(oper) && getArgumentConvention(oper).isInoutConvention(); + } + /// Return true if 'operand' is addressable after type substitution in the /// caller's context. bool isAddressable(const Operand &operand) const; @@ -1037,6 +1050,13 @@ inline bool ApplySite::isIndirectErrorResultOperand(const Operand &op) const { return fas.isIndirectErrorResultOperand(op); } +inline bool ApplySite::isCalleeOperand(const Operand &op) const { + auto fas = asFullApplySite(); + if (!fas) + return false; + return fas.isCalleeOperand(op); +} + } // namespace swift #endif diff --git a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp b/lib/SILOptimizer/Analysis/RegionAnalysis.cpp index c257a4b1d793f..c430f56bcbb04 100644 --- a/lib/SILOptimizer/Analysis/RegionAnalysis.cpp +++ b/lib/SILOptimizer/Analysis/RegionAnalysis.cpp @@ -1723,9 +1723,13 @@ struct PartitionOpBuilder { PartitionOp::Send(lookupValueID(representative), op)); } - void addUndoSend(SILValue representative, SILInstruction *unsendingInst) { + void addUndoSend(TrackableValue value, SILInstruction *unsendingInst) { + if (value.isSendable()) + return; + + auto representative = value.getRepresentative().getValue(); assert(valueHasID(representative) && - "value should already have been encountered"); + "sent value should already have been encountered"); currentInstPartitionOps->emplace_back( PartitionOp::UndoSend(lookupValueID(representative), unsendingInst)); @@ -2430,7 +2434,7 @@ class PartitionOpTranslator { })) continue; - builder.addUndoSend(trackedArgValue.getRepresentative().getValue(), ai); + builder.addUndoSend(trackedArgValue, ai); } } } @@ -2567,60 +2571,53 @@ class PartitionOpTranslator { // gather our non-sending parameters. SmallVector nonSendingParameters; SmallVector sendingIndirectResults; - if (fas.getNumArguments()) { - // NOTE: We want to process indirect parameters as if they are - // parameters... so we process them in nonSendingParameters. - for (auto &op : fas.getOperandsWithoutSelf()) { - // If op is the callee operand, skip it. - if (fas.isCalleeOperand(op)) - continue; - if (fas.isSending(op)) { - if (fas.isIndirectResultOperand(op)) { - sendingIndirectResults.push_back(&op); - continue; - } + // NOTE: We want to process indirect parameters as if they are + // parameters... so we process them in nonSendingParameters. + for (auto &op : fas->getAllOperands()) { + // If op is the callee operand or type dependent operand, skip it. + if (op.isTypeDependent()) + continue; - // Attempt to lookup the value we are passing as sending. We want to - // require/send value if it is non-Sendable and require its base if it - // is non-Sendable as well. - if (auto lookupResult = tryToTrackValue(op.get())) { - builder.addRequire(*lookupResult); - builder.addSend(lookupResult->value, &op); - } - } else { - nonSendingParameters.push_back(&op); + if (fas.isCalleeOperand(op)) { + if (auto calleeResult = tryToTrackValue(op.get())) { + builder.addRequire(*calleeResult); } + continue; } - } - // If our self parameter was sending, send it. Otherwise, just - // stick it in the non self operand values array and run multiassign on - // it. - if (fas.hasSelfArgument()) { - auto &selfOperand = fas.getSelfArgumentOperand(); - if (fas.getArgumentParameterInfo(selfOperand) - .hasOption(SILParameterInfo::Sending)) { - if (auto lookupResult = tryToTrackValue(selfOperand.get())) { - builder.addRequire(*lookupResult); - builder.addSend(lookupResult->value, &selfOperand); - } - } else { - nonSendingParameters.push_back(&selfOperand); + // If our parameter is not sending, just add it to the non-sending + // parameters array and continue. + if (!fas.isSending(op)) { + nonSendingParameters.push_back(&op); + continue; } - } - // Require our callee operand if it is non-Sendable. - // - // DISCUSSION: Even though we do not include our callee operand in the same - // region as our operands/results, we still need to require that it is live - // at the point of application. Otherwise, we will not emit errors if the - // closure before this function application is already in the same region as - // a sent value. In such a case, the function application must error. - if (auto calleeResult = tryToTrackValue(fas.getCallee())) { - builder.addRequire(*calleeResult); + // Otherwise, first handle indirect result operands. + if (fas.isIndirectResultOperand(op)) { + sendingIndirectResults.push_back(&op); + continue; + } + + // Attempt to lookup the value we are passing as sending. We want to + // require/send value if it is non-Sendable and require its base if it + // is non-Sendable as well. + if (auto lookupResult = tryToTrackValue(op.get())) { + builder.addRequire(*lookupResult); + builder.addSend(lookupResult->value, &op); + } } + SWIFT_DEFER { + for (auto &op : fas->getAllOperands()) { + if (!fas.isInOutSending(op)) + continue; + if (auto lookupResult = tryToTrackValue(op.get())) { + builder.addUndoSend(lookupResult->value, op.getUser()); + } + } + }; + SmallVector applyResults; getApplyResults(*fas, applyResults); @@ -2688,36 +2685,77 @@ class PartitionOpTranslator { /// Handles the semantics for SIL applies that cross isolation. /// - /// Semantically this causes all arguments of the applysite to be sent. + /// Semantically we are attempting to implement the following: + /// + /// * Step 1: Require all non-sending parameters and then send those + /// parameters. We perform all of the requires first and the sends second + /// since all of the parameters are getting sent to the same isolation + /// domains and become part of the same region in our callee. So in a + /// certain sense, we are performing a require over the entire merge of the + /// parameter regions and then send each constituant part of the region + /// without requiring again so we do not emit use-after-send diagnostics. + /// + /// * Step 2: Require/Send each of the sending parameters one by one. This + /// includes both 'sending' and 'inout sending' parameters. We purposely + /// interleave the require/send operations to ensure that if one passes a + /// value twice to different 'sending' or 'inout sending' parameters, we + /// will emit an error. + /// + /// * Step 3: Unsend each of the unsending parameters. Since our caller + /// ensures that 'inout sending' parameters are disconnected on return and + /// are in different regions from all other parameters, we can just simply + /// unsend the parameter so we can use it again later. void translateIsolationCrossingSILApply(FullApplySite applySite) { - // Require all operands first before we emit a send. - for (auto op : applySite.getArguments()) { - if (auto lookupResult = tryToTrackValue(op)) { + SmallVector inoutSendingParams; + + // First go through and require all of our operands that are not 'sending' + for (auto &op : applySite.getArgumentOperands()) { + if (applySite.isSending(op)) + continue; + if (auto lookupResult = tryToTrackValue(op.get())) builder.addRequire(*lookupResult); - } } - auto handleSILOperands = [&](MutableArrayRef operands) { - for (auto &op : operands) { - if (auto lookupResult = tryToTrackValue(op.get())) { - builder.addSend(lookupResult->value, &op); - } + // Then go through our operands again and send all of our non-sending + // parameters. We do not interleave these sends with our requires since we + // are considering these values to be merged into the same region. We could + // also merge them in the caller but there is no point in doing so + // semantically since the values cannot be used again locally. + for (auto &op : applySite.getOperandsWithoutIndirectResults()) { + if (applySite.isSending(op)) + continue; + if (auto lookupResult = tryToTrackValue(op.get())) { + builder.addSend(lookupResult->value, &op); } }; - auto handleSILSelf = [&](Operand *self) { - if (auto lookupResult = tryToTrackValue(self->get())) { - builder.addSend(lookupResult->value, self); + // Then go through our 'sending' params and require/send each in sequence. + // + // We do this interleaved so that if a value is passed to multiple 'sending' + // parameters, we emit errors. + for (auto &op : applySite.getOperandsWithoutIndirectResults()) { + if (!applySite.isSending(op)) + continue; + auto lookupResult = tryToTrackValue(op.get()); + if (!lookupResult) + continue; + builder.addRequire(*lookupResult); + builder.addSend(lookupResult->value, &op); + } + + // Now use a SWIFT_DEFER so that when the function is done executing, we + // unsend 'inout sending' params. + SWIFT_DEFER { + for (auto &op : applySite.getOperandsWithoutIndirectResults()) { + if (!applySite.isInOutSending(op)) + continue; + auto lookupResult = tryToTrackValue(op.get()); + if (!lookupResult) + continue; + builder.addUndoSend(lookupResult->value, *applySite); } }; - if (applySite.hasSelfArgument()) { - handleSILOperands(applySite.getOperandsWithoutIndirectResultsOrSelf()); - handleSILSelf(&applySite.getSelfArgumentOperand()); - } else { - handleSILOperands(applySite.getOperandsWithoutIndirectResults()); - } - // Create a new assign fresh for each one of our values and unless our // return value is sending, emit an extra error bit on the results that are // non-Sendable. diff --git a/test/Concurrency/transfernonsendable_inout_sending_params.swift b/test/Concurrency/transfernonsendable_inout_sending_params.swift index a2627846ede09..331ce3ae46fd5 100644 --- a/test/Concurrency/transfernonsendable_inout_sending_params.swift +++ b/test/Concurrency/transfernonsendable_inout_sending_params.swift @@ -80,6 +80,9 @@ struct MyError : Error {} func takeClosure(_ x: sending () -> ()) {} func takeClosureAndParam(_ x: NonSendableKlass, _ y: sending () -> ()) {} +func useInOutSending(_ x: inout sending NonSendableKlass) {} +func useInOutSending(_ x: inout sending NonSendableKlass, + _ y: inout sending NonSendableKlass) {} /////////////////////////////// // MARK: InOut Sending Tests // @@ -162,6 +165,17 @@ func testWrongIsolationGlobalIsolation(_ x: inout sending NonSendableKlass) { } // expected-warning {{'inout sending' parameter 'x' cannot be main actor-isolated at end of function}} // expected-note @-1 {{main actor-isolated 'x' risks causing races in between main actor-isolated uses and caller uses since caller assumes value is not actor isolated}} +func passInOutSendingToInOutSending(_ x: inout sending NonSendableKlass) { + useInOutSending(&x) +} + +func passInOutSendingMultipleTimes(_ x: inout NonSendableStruct) { + useInOutSending(&x.first, &x.second) // expected-warning {{sending 'x.first' risks causing data races}} + // expected-note @-1 {{task-isolated 'x.first' is passed as a 'sending' parameter}} + // expected-warning @-2 {{sending 'x.second' risks causing data races}} + // expected-note @-3 {{task-isolated 'x.second' is passed as a 'sending' parameter}} +} + ////////////////////////////////////// // MARK: Return Inout Sending Tests // ////////////////////////////////////// diff --git a/test/Concurrency/transfernonsendable_sending_params.swift b/test/Concurrency/transfernonsendable_sending_params.swift index 7f24b3f23a93d..dc2b5e9ffdbb4 100644 --- a/test/Concurrency/transfernonsendable_sending_params.swift +++ b/test/Concurrency/transfernonsendable_sending_params.swift @@ -69,11 +69,22 @@ func transferArgAsync(_ x: sending NonSendableKlass) async { func transferArgWithOtherParam(_ x: sending NonSendableKlass, _ y: NonSendableKlass) { } +@MainActor +func transferArgWithOtherParamIsolationCrossing(_ x: sending NonSendableKlass, _ y: NonSendableKlass) async { +} + func transferArgWithOtherParam2(_ x: NonSendableKlass, _ y: sending NonSendableKlass) { } +@MainActor +func transferArgWithOtherParam2IsolationCrossing(_ x: NonSendableKlass, _ y: sending NonSendableKlass) async { +} + func twoTransferArg(_ x: sending NonSendableKlass, _ y: sending NonSendableKlass) {} +@MainActor +func twoTransferArgIsolationCrossing(_ x: sending NonSendableKlass, _ y: sending NonSendableKlass) async {} + @MainActor var globalKlass = NonSendableKlass() struct MyError : Error {} @@ -114,6 +125,22 @@ func testSimpleTransferUseOfOtherParamNoError2() { useValue(k) } +func testSimpleTransferUseOfOtherParamError() async { + let k = NonSendableKlass() + await transferArgWithOtherParamIsolationCrossing(k, k) // expected-warning {{sending 'k' risks causing data races}} + // expected-note @-1 {{sending 'k' to main actor-isolated global function 'transferArgWithOtherParamIsolationCrossing' risks causing data races between main actor-isolated and local nonisolated uses}} + // expected-note @-2 {{access can happen concurrently}} +} + +// TODO: Improve this error message. We should say that we are emitting an +// error. Also need to add SILLocation to ApplyInst. +func testSimpleTransferUseOfOtherParam2Error() async { + let k = NonSendableKlass() + await transferArgWithOtherParam2IsolationCrossing(k, k) // expected-warning {{sending 'k' risks causing data races}} + // expected-note @-1 {{sending 'k' to main actor-isolated global function 'transferArgWithOtherParam2IsolationCrossing' risks causing data races between main actor-isolated and local nonisolated uses}} + // expected-note @-2 {{access can happen concurrently}} +} + @MainActor func transferToMain2(_ x: sending NonSendableKlass, _ y: NonSendableKlass, _ z: NonSendableKlass) async { } @@ -358,6 +385,13 @@ func doubleArgument() async { // expected-note @-2 {{access can happen concurrently}} } +func doubleArgumentIsolationCrossing() async { + let x = NonSendableKlass() + await twoTransferArgIsolationCrossing(x, x) // expected-warning {{sending 'x' risks causing data races}} + // expected-note @-1 {{'x' used after being passed as a 'sending' parameter}} + // expected-note @-2 {{access can happen concurrently}} +} + func testTransferSrc(_ x: sending NonSendableKlass) async { let y = NonSendableKlass() await transferToMain(y) // expected-warning {{sending 'y' risks causing data races}}