Skip to content

Commit 154f47e

Browse files
authored
Merge pull request swiftlang#72252 from hborla/concurrency-lib-annotations
[Concurrency] Add a few missing `Sendable` annotations in the concurrency library.
2 parents 197b164 + 70d998a commit 154f47e

File tree

10 files changed

+131
-56
lines changed

10 files changed

+131
-56
lines changed

include/swift/AST/PrintOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ struct PrintOptions {
379379
/// Suppress the @isolated(any) attribute.
380380
bool SuppressIsolatedAny = false;
381381

382+
/// Suppress 'isolated' and '#isolation' on isolated parameters with optional type.
383+
bool SuppressOptionalIsolatedParams = false;
384+
382385
/// List of attribute kinds that should not be printed.
383386
std::vector<AnyAttrKind> ExcludeAttrList = {
384387
DeclAttrKind::Transparent, DeclAttrKind::Effects,

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ LANGUAGE_FEATURE(FreestandingMacros, 397, "freestanding declaration macros")
164164
SUPPRESSIBLE_LANGUAGE_FEATURE(RetroactiveAttribute, 364, "@retroactive")
165165
SUPPRESSIBLE_LANGUAGE_FEATURE(ExtensionMacroAttr, 0, "@attached(extension)")
166166
LANGUAGE_FEATURE(TypedThrows, 413, "Typed throws")
167-
LANGUAGE_FEATURE(OptionalIsolatedParameters, 420, "Optional isolated parameters")
167+
CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(OptionalIsolatedParameters, 420, "Optional isolated parameters")
168168
SUPPRESSIBLE_LANGUAGE_FEATURE(Extern, 0, "@_extern")
169169
LANGUAGE_FEATURE(ExpressionMacroDefaultArguments, 422, "Expression macro as caller-side default argument")
170170
LANGUAGE_FEATURE(BuiltinStoreRaw, 0, "Builtin.storeRaw")

lib/AST/ASTPrinter.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3083,6 +3083,14 @@ static void suppressingFeatureIsolatedAny(PrintOptions &options,
30833083
action();
30843084
}
30853085

3086+
static void suppressingFeatureOptionalIsolatedParameters(
3087+
PrintOptions &options,
3088+
llvm::function_ref<void()> action) {
3089+
llvm::SaveAndRestore<bool> scope(
3090+
options.SuppressOptionalIsolatedParams, true);
3091+
action();
3092+
}
3093+
30863094
static void suppressingFeatureAssociatedTypeImplements(PrintOptions &options,
30873095
llvm::function_ref<void()> action) {
30883096
unsigned originalExcludeAttrCount = options.ExcludeAttrList.size();
@@ -3660,8 +3668,11 @@ static void printParameterFlags(ASTPrinter &printer,
36603668
break;
36613669
}
36623670

3663-
if (flags.isIsolated())
3664-
printer.printKeyword("isolated", options, " ");
3671+
if (flags.isIsolated()) {
3672+
if (!(param && param->getInterfaceType()->isOptional() &&
3673+
options.SuppressOptionalIsolatedParams))
3674+
printer.printKeyword("isolated", options, " ");
3675+
}
36653676

36663677
if (flags.hasResultDependsOn())
36673678
printer.printKeyword("_resultDependsOn", options, " ");
@@ -3809,6 +3820,16 @@ void PrintAST::printOneParameter(const ParamDecl *param,
38093820
}
38103821

38113822
if (param->isDefaultArgument() && Options.PrintDefaultArgumentValue) {
3823+
auto defaultArgKind = param->getDefaultArgumentKind();
3824+
if (param->isIsolated() &&
3825+
defaultArgKind == DefaultArgumentKind::ExpressionMacro &&
3826+
Options.SuppressOptionalIsolatedParams) {
3827+
// If we're suppressing optional isolated parameters, print
3828+
// 'nil' instead of '#isolation'
3829+
Printer << " = nil";
3830+
return;
3831+
}
3832+
38123833
Printer.callPrintStructurePre(PrintStructureKind::DefaultArgumentClause);
38133834
SWIFT_DEFER {
38143835
Printer.printStructurePost(PrintStructureKind::DefaultArgumentClause);

stdlib/public/Concurrency/AsyncStream.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,20 @@ public struct AsyncStream<Element> {
330330
/// }
331331
///
332332
///
333+
@_alwaysEmitIntoClient
333334
public init(
334-
unfolding produce: @escaping () async -> Element?,
335+
unfolding produce: @escaping @Sendable () async -> Element?,
336+
onCancel: (@Sendable () -> Void)? = nil
337+
) {
338+
self.init(
339+
unfolding: produce as () async -> Element?,
340+
onCancel: onCancel
341+
)
342+
}
343+
344+
@usableFromInline
345+
internal init(
346+
unfolding produce: @escaping () async -> Element?,
335347
onCancel: (@Sendable () -> Void)? = nil
336348
) {
337349
let storage: _AsyncStreamCriticalStorage<Optional<() async -> Element?>>

stdlib/public/Concurrency/AsyncThrowingStream.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,17 @@ public struct AsyncThrowingStream<Element, Failure: Error> {
369369
/// print(error)
370370
/// }
371371
///
372+
@_alwaysEmitIntoClient
372373
public init(
374+
unfolding produce: @escaping @Sendable () async throws -> Element?
375+
) where Failure == Error {
376+
self.init(
377+
unfolding: produce as () async throws -> Element?
378+
)
379+
}
380+
381+
@usableFromInline
382+
internal init(
373383
unfolding produce: @escaping () async throws -> Element?
374384
) where Failure == Error {
375385
let storage: _AsyncStreamCriticalStorage<Optional<() async throws -> Element?>>

stdlib/public/Concurrency/Clock.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,19 @@ extension Clock {
6767
/// await someWork()
6868
/// }
6969
@available(SwiftStdlib 5.7, *)
70+
@_alwaysEmitIntoClient
71+
@_allowFeatureSuppression(OptionalIsolatedParameters)
7072
public func measure(
73+
isolation: isolated (any Actor)? = #isolation,
74+
_ work: () async throws -> Void
75+
) async rethrows -> Instant.Duration {
76+
try await measure(work)
77+
}
78+
79+
@available(SwiftStdlib 5.7, *)
80+
@_unsafeInheritExecutor
81+
@usableFromInline
82+
internal func measure(
7183
_ work: () async throws -> Void
7284
) async rethrows -> Instant.Duration {
7385
let start = now

stdlib/public/Concurrency/ContinuousClock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Swift
2121
///
2222
/// This clock is suitable for high resolution measurements of execution.
2323
@available(SwiftStdlib 5.7, *)
24-
public struct ContinuousClock {
24+
public struct ContinuousClock: Sendable {
2525
/// A continuous point in time used for `ContinuousClock`.
2626
public struct Instant: Codable, Sendable {
2727
internal var _value: Swift.Duration

stdlib/public/Concurrency/SuspendingClock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import Swift
2121
///
2222
/// This clock is suitable for high resolution measurements of execution.
2323
@available(SwiftStdlib 5.7, *)
24-
public struct SuspendingClock {
24+
public struct SuspendingClock: Sendable {
2525
public struct Instant: Codable, Sendable {
2626
internal var _value: Swift.Duration
2727

0 commit comments

Comments
 (0)