@@ -492,6 +492,10 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
492492
493493 // Initialize ExpectedExecutor if the function is an actor-isolated
494494 // function or closure.
495+ bool wantDataRaceChecks = getOptions ().EnableActorDataRaceChecks &&
496+ !F.isAsync () &&
497+ !isInActorDestructor (FunctionDC);
498+
495499 if (auto *funcDecl =
496500 dyn_cast_or_null<AbstractFunctionDecl>(FunctionDC->getAsDecl ())) {
497501 auto actorIsolation = getActorIsolation (funcDecl);
@@ -508,7 +512,7 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
508512 // are prone to dynamic data races in code that does not enforce Sendable
509513 // completely.
510514 if (F.isAsync () ||
511- (funcDecl-> isLocalCapture () && ! isInActorDestructor ( funcDecl))) {
515+ (wantDataRaceChecks && funcDecl-> isLocalCapture ( ))) {
512516 auto loc = RegularLocation::getAutoGeneratedLocation (F.getLocation ());
513517 ManagedValue selfArg = ManagedValue::forUnmanaged (F.getSelfArgument ());
514518 ExpectedExecutor = emitLoadActorExecutor (loc, selfArg);
@@ -517,14 +521,17 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
517521 }
518522
519523 case ActorIsolation::GlobalActor:
520- if (F.isAsync () || ! isInActorDestructor (funcDecl) ) {
524+ if (F.isAsync () || wantDataRaceChecks ) {
521525 ExpectedExecutor =
522526 emitLoadGlobalActorExecutor (actorIsolation.getGlobalActor ());
523527 }
524528 break ;
525529 }
526530 } else if (auto *closureExpr = dyn_cast<AbstractClosureExpr>(FunctionDC)) {
527- bool wantExecutor = F.isAsync () || !isInActorDestructor (closureExpr);
531+ bool wantExecutor = F.isAsync () ||
532+ (wantDataRaceChecks &&
533+ !(isa<ClosureExpr>(closureExpr) &&
534+ cast<ClosureExpr>(closureExpr)->isUnsafeMainActor ()));
528535 auto actorIsolation = closureExpr->getActorIsolation ();
529536 switch (actorIsolation.getKind ()) {
530537 case ClosureActorIsolation::Independent:
0 commit comments