@@ -6109,6 +6109,23 @@ static bool sendableConformanceRequiresNonisolated(NominalTypeDecl *nominal) {
6109
6109
return requiresNonisolated;
6110
6110
}
6111
6111
6112
+ // / Determine the default isolation for the given declaration context.
6113
+ static DefaultIsolation getDefaultIsolationForContext (const DeclContext *dc) {
6114
+ // Check whether there is a file-specific setting.
6115
+ if (auto *sourceFile = dc->getParentSourceFile ()) {
6116
+ if (auto defaultIsolationInFile = sourceFile->getDefaultIsolation ())
6117
+ return defaultIsolationInFile.value ();
6118
+ }
6119
+
6120
+ // If we're in the main module, check the language option.
6121
+ ASTContext &ctx = dc->getASTContext ();
6122
+ if (dc->getParentModule () == ctx.MainModule )
6123
+ return ctx.LangOpts .DefaultIsolationBehavior ;
6124
+
6125
+ // Otherwise, default to nonisolated.
6126
+ return DefaultIsolation::Nonisolated;
6127
+ }
6128
+
6112
6129
// / Determine the default isolation and isolation source for this declaration,
6113
6130
// / which may still be overridden by other inference rules.
6114
6131
static std::tuple<InferredActorIsolation, ValueDecl *,
@@ -6152,12 +6169,27 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
6152
6169
return {};
6153
6170
6154
6171
if (dc != dyn_cast<DeclContext>(value)) {
6172
+ // If the nominal type is global-actor-isolated, there's nothing
6173
+ // more to look for.
6155
6174
if (getActorIsolation (nominal).isMainActor ())
6156
6175
break ;
6157
6176
6158
- return {};
6177
+ // If this is an extension of a nonisolated type, its isolation
6178
+ // is independent of the type.
6179
+ if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
6180
+ // If there were isolation attributes on the extension, respect
6181
+ // them.
6182
+ if (getIsolationFromAttributes (ext).has_value ())
6183
+ return {};
6184
+
6185
+ // Keep looking.
6186
+ } else {
6187
+ // The type is nonisolated, so its members are nonisolated.
6188
+ return {};
6189
+ }
6159
6190
}
6160
6191
}
6192
+
6161
6193
dc = dc->getParent ();
6162
6194
}
6163
6195
@@ -6187,12 +6219,8 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
6187
6219
return {};
6188
6220
};
6189
6221
6190
- DefaultIsolation defaultIsolation = ctx.LangOpts .DefaultIsolationBehavior ;
6191
- if (auto *SF = value->getDeclContext ()->getParentSourceFile ()) {
6192
- if (auto defaultIsolationInFile = SF->getDefaultIsolation ())
6193
- defaultIsolation = defaultIsolationInFile.value ();
6194
- }
6195
-
6222
+ DefaultIsolation defaultIsolation =
6223
+ getDefaultIsolationForContext (value->getDeclContext ());
6196
6224
// If we are required to use main actor... just use that.
6197
6225
if (defaultIsolation == DefaultIsolation::MainActor)
6198
6226
if (auto result =
@@ -8439,6 +8467,19 @@ ActorIsolation swift::inferConformanceIsolation(
8439
8467
// isolated to a global actor, we may use the conforming type's isolation.
8440
8468
auto nominalIsolation = getActorIsolation (nominal);
8441
8469
if (!nominalIsolation.isGlobalActor ()) {
8470
+ // If we are in an extension of the type, we might still infer an
8471
+ // isolated conformance depending on default isolation and on the extension
8472
+ // itself.
8473
+ if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
8474
+ // If there's an isolation-related attribute on the extension, use it.
8475
+ if (auto attrIsolation = getIsolationFromAttributes (ext))
8476
+ return *attrIsolation;
8477
+
8478
+ // If we're defaulting to main-actor isolation, use that.
8479
+ if (getDefaultIsolationForContext (dc) == DefaultIsolation::MainActor)
8480
+ return ActorIsolation::forMainActor (ctx);
8481
+ }
8482
+
8442
8483
return ActorIsolation::forNonisolated (false );
8443
8484
}
8444
8485
0 commit comments