Skip to content

Commit 8c9b8a7

Browse files
committed
[Sema] Validate that @_inheritActorContext is used only on @Sendable/sending and async/@isolated(any) parameters
1 parent 77f0129 commit 8c9b8a7

File tree

5 files changed

+55
-250
lines changed

5 files changed

+55
-250
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8602,6 +8602,16 @@ ERROR(inherit_actor_context_only_on_func_types,none,
86028602
"%0 only applies to parameters with function types (got: %1)",
86038603
(DeclAttribute, Type))
86048604

8605+
ERROR(inherit_actor_context_only_on_sending_or_Sendable_params,none,
8606+
"%0 only applies to 'sending' parameters or parameters with "
8607+
"'@Sendable' function types",
8608+
(DeclAttribute))
8609+
8610+
ERROR(inherit_actor_context_only_on_async_or_isolation_erased_params,none,
8611+
"%0 only applies to '@isolated(any)' parameters or parameters with "
8612+
"asynchronous function types",
8613+
(DeclAttribute))
8614+
86058615
//===----------------------------------------------------------------------===//
86068616
// MARK: @concurrent and nonisolated(nonsending) attributes
86078617
//===----------------------------------------------------------------------===//

lib/Sema/TypeCheckAttr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7861,6 +7861,24 @@ void AttributeChecker::visitInheritActorContextAttr(
78617861
attr, paramTy);
78627862
return;
78637863
}
7864+
7865+
// The type has to be either `@Sendable` or `sending` _and_
7866+
// `async` or `@isolated(any)`.
7867+
if (!(funcTy->isSendable() || P->isSending())) {
7868+
diagnose(attr->getLocation(),
7869+
diag::inherit_actor_context_only_on_sending_or_Sendable_params,
7870+
attr)
7871+
.warnUntilFutureSwiftVersion();
7872+
}
7873+
7874+
// Eiether `async` or `@isolated(any)`.
7875+
if (!(funcTy->isAsync() || funcTy->getIsolation().isErased())) {
7876+
diagnose(
7877+
attr->getLocation(),
7878+
diag::inherit_actor_context_only_on_async_or_isolation_erased_params,
7879+
attr)
7880+
.warnUntilFutureSwiftVersion();
7881+
}
78647882
}
78657883

78667884
void AttributeChecker::visitMarkerAttr(MarkerAttr *attr) {

test/ASTGen/attrs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,5 @@ struct AnyEraser: EraserProto {
257257
init<T: EraserProto>(erasing: T) {}
258258
}
259259

260-
func takeNone(@_inheritActorContext param: () async -> ()) { }
261-
func takeAlways(@_inheritActorContext(always) param: () async -> ()) { }
260+
func takeNone(@_inheritActorContext param: @Sendable () async -> ()) { }
261+
func takeAlways(@_inheritActorContext(always) param: sending @isolated(any) () -> ()) { }

0 commit comments

Comments
 (0)