Skip to content

Commit dbe19b6

Browse files
committed
[TypeChecker] Remove @_inheritActorContext effect from synchronous types
The compiler would previously accept use of `@_inheritActorContext` on a parameter with a synchronous function type which wasn't marked as `@isolated(any)`. That is incorrect because in such cases the attribute has no effect and furthermore would prevent Sendable and isolation checking. Uses like that are currently diagnosed by the type-checker but we need to go one step further and remove the effect in such case to prevent invalid uses. Resolves: rdar://143581268
1 parent c146993 commit dbe19b6

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7889,10 +7889,10 @@ void AttributeChecker::visitInheritActorContextAttr(
78897889
.warnUntilFutureSwiftVersion();
78907890
}
78917891

7892-
// Eiether `async` or `@isolated(any)`.
7892+
// Either `async` or `@isolated(any)`.
78937893
if (!(funcTy->isAsync() || funcTy->getIsolation().isErased())) {
7894-
diagnose(
7895-
attr->getLocation(),
7894+
diagnoseAndRemoveAttr(
7895+
attr,
78967896
diag::inherit_actor_context_only_on_async_or_isolation_erased_params,
78977897
attr)
78987898
.warnUntilFutureSwiftVersion();

test/Concurrency/actor_isolation.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,3 +1752,29 @@ actor UserDefinedActorSelfDotMethod {
17521752
return functionRef // expected-error {{cannot convert return expression of type '(isolated Self) -> () -> ()' to return type '(UserDefinedActorSelfDotMethod) -> @isolated(any) () -> Void'}}
17531753
}
17541754
}
1755+
1756+
func requireSendableInheritContext(@_inheritActorContext _: @Sendable () -> ()) {}
1757+
// expected-warning@-1 {{@_inheritActorContext only applies to '@isolated(any)' parameters or parameters with asynchronous function types; this will be an error in a future Swift language mode}}
1758+
1759+
actor InvalidInheritedActorIsolation {
1760+
func actorFunction() {} // expected-note {{calls to instance method 'actorFunction()' from outside of its actor context are implicitly asynchronous}}
1761+
1762+
func test() {
1763+
requireSendableInheritContext {
1764+
self.actorFunction()
1765+
// expected-error@-1 {{call to actor-isolated instance method 'actorFunction()' in a synchronous nonisolated context}}
1766+
}
1767+
}
1768+
}
1769+
1770+
@MainActor
1771+
class InvalidInheritedGlobalActorIsolation {
1772+
func mainActorFunction() {} // expected-note {{calls to instance method 'mainActorFunction()' from outside of its actor context are implicitly asynchronous}}
1773+
1774+
func test() {
1775+
requireSendableInheritContext {
1776+
self.mainActorFunction()
1777+
// expected-error@-1 {{call to main actor-isolated instance method 'mainActorFunction()' in a synchronous nonisolated context}}
1778+
}
1779+
}
1780+
}

test/Concurrency/predates_concurrency.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,26 @@ func testSendableMetatypeDowngrades() {
370370
// expected-complete-tns-warning@-1 {{type 'T' does not conform to the 'SendableMetatype' protocol}}
371371
}
372372
}
373+
374+
do {
375+
func test(@_inheritActorContext _: @Sendable () -> Void) async {
376+
// expected-warning@-1 {{@_inheritActorContext only applies to '@isolated(any)' parameters or parameters with asynchronous function types; this will be an error in a future Swift language mode}}
377+
}
378+
379+
@MainActor
380+
@preconcurrency
381+
class Test {
382+
struct V {}
383+
384+
var value: V? // expected-note {{property declared here}}
385+
386+
func run() async {
387+
await test {
388+
if let value {
389+
// expected-warning@-1 {{main actor-isolated property 'value' can not be referenced from a Sendable closure; this is an error in the Swift 6 language mode}}
390+
print(value)
391+
}
392+
}
393+
}
394+
}
395+
}

0 commit comments

Comments
 (0)