Files
swift-mirror/test/ClangImporter/sending.swift
Robert Widmann edcde7c55c [ClangImporter] Import 'swift_attr("sending")' As a Type Attribute
Previously, `__attribute__((swift_attr("sending")))` would only be
resolved when attached to declarations. This patch expands it to be
a type attribute as well, which enables it to occur in the result and
parameter positions for blocks, function pointers, and lambdas.

Resolves rdar://148435359
2025-05-07 18:19:38 +01:00

56 lines
2.3 KiB
Swift

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -swift-version 6 -disable-availability-checking -emit-sil -o /dev/null %s -parse-as-library -enable-experimental-feature SendingArgsAndResults -verify -import-objc-header %S/Inputs/sending.h
// REQUIRES: concurrency
// REQUIRES: swift_feature_SendingArgsAndResults
////////////////////////
// MARK: Declarations //
////////////////////////
// Make our non sendable c struct non-Sendable
@available(*, unavailable)
extension NonSendableCStruct: Sendable {}
@MainActor func sendToMain<T>(_ t: T) async {}
func useValue<T>(_ t: T) {}
/////////////////
// MARK: Tests //
/////////////////
func funcTestSendingResult() async {
let x = NonSendableCStruct()
let y = sendUserDefinedFromGlobalFunction(x)
await sendToMain(x)
useValue(y)
// Just to show that without the sending param, we generate diagnostics.
let x2 = NonSendableCStruct()
let y2 = returnUserDefinedFromGlobalFunction(x2)
await sendToMain(x2) // expected-error {{sending 'x2' risks causing data races}}
// expected-note @-1 {{sending 'x2' to main actor-isolated global function 'sendToMain' risks causing data races between main actor-isolated and local nonisolated uses}}
useValue(y2) // expected-note {{access can happen concurrently}}
}
func funcTestSendingArg() async {
let x = NonSendableCStruct()
sendUserDefinedIntoGlobalFunction(x) // expected-error {{sending 'x' risks causing data races}}
// expected-note @-1 {{'x' used after being passed as a 'sending' parameter}}
useValue(x) // expected-note {{access can happen concurrently}}
}
func funcTestSendingClosureArg() async {
sendingWithCompletionHandler { (x: sending NonSendableCStruct) in
sendUserDefinedIntoGlobalFunction(x) // expected-error {{sending 'x' risks causing data races}}
// expected-note @-1 {{'x' used after being passed as a 'sending' parameter}}
useValue(x) // expected-note {{access can happen concurrently}}
}
let x = sendingWithLazyReturn { () -> sending NonSendableCStruct in
NonSendableCStruct()
}
sendUserDefinedIntoGlobalFunction(x) // expected-error {{sending 'x' risks causing data races}}
// expected-note @-1 {{'x' used after being passed as a 'sending' parameter}}
useValue(x) // expected-note {{access can happen concurrently}}
}