Skip to content

Commit 3ddf8c6

Browse files
committed
Actor isolation inference: @IBAction implies @mainactor(unsafe)
@IBAction calls are always delivered in the main thread, so reflect that by inferring `@MainActor(unsafe)` for such methods whenever no other actor isolation is provided. Implements rdar://84474640.
1 parent 42d0e4d commit 3ddf8c6

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,6 +3435,15 @@ ActorIsolation ActorIsolationRequest::evaluate(
34353435
}
34363436
}
34373437

3438+
// @IBAction implies @MainActor(unsafe).
3439+
if (value->getAttrs().hasAttribute<IBActionAttr>()) {
3440+
ASTContext &ctx = value->getASTContext();
3441+
if (Type mainActor = ctx.getMainActorType()) {
3442+
return inferredIsolation(
3443+
ActorIsolation::forGlobalActor(mainActor, /*unsafe=*/true));
3444+
}
3445+
}
3446+
34383447
// Default isolation for this member.
34393448
return defaultIsolation;
34403449
}

test/Concurrency/global_actor_inference.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,15 @@ func useUnsafeMainActor() {
517517
}
518518
}
519519

520+
// ----------------------------------------------------------------------
521+
// @IBAction implies @MainActor(unsafe)
522+
// ----------------------------------------------------------------------
523+
class SomeWidgetThing {
524+
@IBAction func onTouch(_ object: AnyObject) {
525+
onlyOnMainActor() // okay
526+
}
527+
}
528+
520529
// ----------------------------------------------------------------------
521530
// @_inheritActorContext
522531
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)