@@ -615,7 +615,9 @@ static bool shouldDiagnoseExistingDataRaces(const DeclContext *dc) {
615615 if (dc->getParentModule ()->isConcurrencyChecked ())
616616 return true ;
617617
618- return contextRequiresStrictConcurrencyChecking (dc);
618+ return contextRequiresStrictConcurrencyChecking (dc, [](const AbstractClosureExpr *) {
619+ return Type ();
620+ });
619621}
620622
621623// / Determine the default diagnostic behavior for this language mode.
@@ -1843,20 +1845,6 @@ namespace {
18431845 }
18441846 }
18451847
1846- bool isInAsynchronousContext () const {
1847- auto *dc = getDeclContext ();
1848-
1849- if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
1850- return func->isAsyncContext ();
1851- }
1852-
1853- if (auto closure = dyn_cast<AbstractClosureExpr>(dc)) {
1854- return closure->isBodyAsync ();
1855- }
1856-
1857- return false ;
1858- }
1859-
18601848 enum class AsyncMarkingResult {
18611849 FoundAsync, // successfully marked an implicitly-async operation
18621850 NotFound, // fail: no valid implicitly-async operation was found
@@ -1917,7 +1905,7 @@ namespace {
19171905
19181906 if (auto declRef = dyn_cast_or_null<DeclRefExpr>(context)) {
19191907 if (usageEnv (declRef) == VarRefUseEnv::Read) {
1920- if (!isInAsynchronousContext ())
1908+ if (!getDeclContext ()-> isAsyncContext ())
19211909 return AsyncMarkingResult::SyncContext;
19221910
19231911 declRef->setImplicitlyAsync (target);
@@ -1926,7 +1914,7 @@ namespace {
19261914 } else if (auto lookupExpr = dyn_cast_or_null<LookupExpr>(context)) {
19271915 if (usageEnv (lookupExpr) == VarRefUseEnv::Read) {
19281916
1929- if (!isInAsynchronousContext ())
1917+ if (!getDeclContext ()-> isAsyncContext ())
19301918 return AsyncMarkingResult::SyncContext;
19311919
19321920 lookupExpr->setImplicitlyAsync (target);
@@ -1939,7 +1927,7 @@ namespace {
19391927 // actor-isolated non-isolated-self calls are implicitly async
19401928 // and thus OK.
19411929
1942- if (!isInAsynchronousContext ())
1930+ if (!getDeclContext ()-> isAsyncContext ())
19431931 return AsyncMarkingResult::SyncContext;
19441932
19451933 isAsyncCall = true ;
@@ -1955,7 +1943,7 @@ namespace {
19551943 auto concDecl = memberRef->first ;
19561944 if (decl == concDecl.getDecl () && !apply->isImplicitlyAsync ()) {
19571945
1958- if (!isInAsynchronousContext ())
1946+ if (!getDeclContext ()-> isAsyncContext ())
19591947 return AsyncMarkingResult::SyncContext;
19601948
19611949 // then this ValueDecl appears as the called value of the ApplyExpr.
@@ -2068,7 +2056,7 @@ namespace {
20682056 return false ;
20692057
20702058 // If we are not in an asynchronous context, complain.
2071- if (!isInAsynchronousContext ()) {
2059+ if (!getDeclContext ()-> isAsyncContext ()) {
20722060 if (auto calleeDecl = apply->getCalledValue ()) {
20732061 ctx.Diags .diagnose (
20742062 apply->getLoc (), diag::actor_isolated_call_decl,
@@ -3562,7 +3550,9 @@ void swift::checkOverrideActorIsolation(ValueDecl *value) {
35623550 overridden->diagnose (diag::overridden_here);
35633551}
35643552
3565- bool swift::contextRequiresStrictConcurrencyChecking (const DeclContext *dc) {
3553+ bool swift::contextRequiresStrictConcurrencyChecking (
3554+ const DeclContext *dc,
3555+ llvm::function_ref<Type(const AbstractClosureExpr *)> getType) {
35663556 // If Swift >= 6, everything uses strict concurrency checking.
35673557 if (dc->getASTContext ().LangOpts .isSwiftVersionAtLeast (6 ))
35683558 return true ;
@@ -3574,6 +3564,12 @@ bool swift::contextRequiresStrictConcurrencyChecking(const DeclContext *dc) {
35743564 if (auto explicitClosure = dyn_cast<ClosureExpr>(closure)) {
35753565 if (getExplicitGlobalActor (const_cast <ClosureExpr *>(explicitClosure)))
35763566 return true ;
3567+
3568+ if (auto type = getType (closure)) {
3569+ if (auto fnType = type->getAs <AnyFunctionType>())
3570+ if (fnType->isAsync () || fnType->isSendable ())
3571+ return true ;
3572+ }
35773573 }
35783574
35793575 // Async and @Sendable closures use concurrency features.
@@ -4049,11 +4045,12 @@ static bool hasKnownUnsafeSendableFunctionParams(AbstractFunctionDecl *func) {
40494045}
40504046
40514047Type swift::adjustVarTypeForConcurrency (
4052- Type type, VarDecl *var, DeclContext *dc) {
4048+ Type type, VarDecl *var, DeclContext *dc,
4049+ llvm::function_ref<Type(const AbstractClosureExpr *)> getType) {
40534050 if (!var->predatesConcurrency ())
40544051 return type;
40554052
4056- if (contextRequiresStrictConcurrencyChecking (dc))
4053+ if (contextRequiresStrictConcurrencyChecking (dc, getType ))
40574054 return type;
40584055
40594056 bool isLValue = false ;
@@ -4172,11 +4169,12 @@ static AnyFunctionType *applyUnsafeConcurrencyToFunctionType(
41724169
41734170AnyFunctionType *swift::adjustFunctionTypeForConcurrency (
41744171 AnyFunctionType *fnType, ValueDecl *decl, DeclContext *dc,
4175- unsigned numApplies, bool isMainDispatchQueue) {
4172+ unsigned numApplies, bool isMainDispatchQueue,
4173+ llvm::function_ref<Type(const AbstractClosureExpr *)> getType) {
41764174 // Apply unsafe concurrency features to the given function type.
4175+ bool strictChecking = contextRequiresStrictConcurrencyChecking (dc, getType);
41774176 fnType = applyUnsafeConcurrencyToFunctionType (
4178- fnType, decl, contextRequiresStrictConcurrencyChecking (dc), numApplies,
4179- isMainDispatchQueue);
4177+ fnType, decl, strictChecking, numApplies, isMainDispatchQueue);
41804178
41814179 Type globalActorType;
41824180 if (decl) {
@@ -4190,7 +4188,7 @@ AnyFunctionType *swift::adjustFunctionTypeForConcurrency(
41904188 case ActorIsolation::GlobalActorUnsafe:
41914189 // Only treat as global-actor-qualified within code that has adopted
41924190 // Swift Concurrency features.
4193- if (!contextRequiresStrictConcurrencyChecking (dc) )
4191+ if (!strictChecking )
41944192 return fnType;
41954193
41964194 LLVM_FALLTHROUGH;
@@ -4232,7 +4230,10 @@ AnyFunctionType *swift::adjustFunctionTypeForConcurrency(
42324230}
42334231
42344232bool swift::completionContextUsesConcurrencyFeatures (const DeclContext *dc) {
4235- return contextRequiresStrictConcurrencyChecking (dc);
4233+ return contextRequiresStrictConcurrencyChecking (
4234+ dc, [](const AbstractClosureExpr *) {
4235+ return Type ();
4236+ });
42364237}
42374238
42384239AbstractFunctionDecl const *swift::isActorInitOrDeInitContext (
0 commit comments