@@ -6088,6 +6088,23 @@ static bool sendableConformanceRequiresNonisolated(NominalTypeDecl *nominal) {
6088
6088
return requiresNonisolated;
6089
6089
}
6090
6090
6091
+ // / Determine the default isolation for the given declaration context.
6092
+ static DefaultIsolation getDefaultIsolationForContext (const DeclContext *dc) {
6093
+ // Check whether there is a file-specific setting.
6094
+ if (auto *sourceFile = dc->getParentSourceFile ()) {
6095
+ if (auto defaultIsolationInFile = sourceFile->getDefaultIsolation ())
6096
+ return defaultIsolationInFile.value ();
6097
+ }
6098
+
6099
+ // If we're in the main module, check the language option.
6100
+ ASTContext &ctx = dc->getASTContext ();
6101
+ if (dc->getParentModule () == ctx.MainModule )
6102
+ return ctx.LangOpts .DefaultIsolationBehavior ;
6103
+
6104
+ // Otherwise, default to nonisolated.
6105
+ return DefaultIsolation::Nonisolated;
6106
+ }
6107
+
6091
6108
// / Determine the default isolation and isolation source for this declaration,
6092
6109
// / which may still be overridden by other inference rules.
6093
6110
static std::tuple<InferredActorIsolation, ValueDecl *,
@@ -6131,12 +6148,27 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
6131
6148
return {};
6132
6149
6133
6150
if (dc != dyn_cast<DeclContext>(value)) {
6151
+ // If the nominal type is global-actor-isolated, there's nothing
6152
+ // more to look for.
6134
6153
if (getActorIsolation (nominal).isMainActor ())
6135
6154
break ;
6136
6155
6137
- return {};
6156
+ // If this is an extension of a nonisolated type, its isolation
6157
+ // is independent of the type.
6158
+ if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
6159
+ // If there were isolation attributes on the extension, respect
6160
+ // them.
6161
+ if (getIsolationFromAttributes (ext).has_value ())
6162
+ return {};
6163
+
6164
+ // Keep looking.
6165
+ } else {
6166
+ // The type is nonisolated, so its members are nonisolated.
6167
+ return {};
6168
+ }
6138
6169
}
6139
6170
}
6171
+
6140
6172
dc = dc->getParent ();
6141
6173
}
6142
6174
@@ -6166,12 +6198,8 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
6166
6198
return {};
6167
6199
};
6168
6200
6169
- DefaultIsolation defaultIsolation = ctx.LangOpts .DefaultIsolationBehavior ;
6170
- if (auto *SF = value->getDeclContext ()->getParentSourceFile ()) {
6171
- if (auto defaultIsolationInFile = SF->getDefaultIsolation ())
6172
- defaultIsolation = defaultIsolationInFile.value ();
6173
- }
6174
-
6201
+ DefaultIsolation defaultIsolation =
6202
+ getDefaultIsolationForContext (value->getDeclContext ());
6175
6203
// If we are required to use main actor... just use that.
6176
6204
if (defaultIsolation == DefaultIsolation::MainActor)
6177
6205
if (auto result =
@@ -8426,6 +8454,19 @@ ActorIsolation swift::inferConformanceIsolation(
8426
8454
// isolated to a global actor, we may use the conforming type's isolation.
8427
8455
auto nominalIsolation = getActorIsolation (nominal);
8428
8456
if (!nominalIsolation.isGlobalActor ()) {
8457
+ // If we are in an extension of the type, we might still infer an
8458
+ // isolated conformance depending on default isolation and on the extension
8459
+ // itself.
8460
+ if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
8461
+ // If there's an isolation-related attribute on the extension, use it.
8462
+ if (auto attrIsolation = getIsolationFromAttributes (ext))
8463
+ return *attrIsolation;
8464
+
8465
+ // If we're defaulting to main-actor isolation, use that.
8466
+ if (getDefaultIsolationForContext (dc) == DefaultIsolation::MainActor)
8467
+ return ActorIsolation::forMainActor (ctx);
8468
+ }
8469
+
8429
8470
return ActorIsolation::forNonisolated (false );
8430
8471
}
8431
8472
0 commit comments