@@ -5732,6 +5732,75 @@ static void addAttributesForActorIsolation(ValueDecl *value,
57325732 }
57335733}
57345734
5735+ std::optional<DefaultIsolation>
5736+ DefaultActorIsolationRequest::evaluate (Evaluator &evaluator,
5737+ SourceFile *sourceFile) const {
5738+ if (!sourceFile)
5739+ return std::nullopt ;
5740+
5741+ auto &ctx = sourceFile->getASTContext ();
5742+ auto defaultModuleIsolaton = ctx.LangOpts .DefaultIsolationBehavior ;
5743+ auto mainActorType = ctx.getMainActorType ();
5744+ if (!mainActorType)
5745+ return defaultModuleIsolaton;
5746+
5747+ if (ctx.LangOpts .hasFeature (Feature::SwiftSettings)) {
5748+ auto options = sourceFile->getLanguageOptions ();
5749+ if (auto isolation = options.defaultIsolation ) {
5750+ return *isolation;
5751+ }
5752+ }
5753+
5754+ if (ctx.LangOpts .hasFeature (Feature::DefaultIsolationTypealias)) {
5755+ auto nonisolatedType = ctx.getnonisolatedType ();
5756+
5757+ auto decls = sourceFile->getTopLevelDecls ();
5758+ if (decls.empty ())
5759+ return defaultModuleIsolaton;
5760+
5761+ auto locInFile = decls.front ()->getStartLoc ();
5762+ auto defaultIsolationResult = TypeChecker::lookupUnqualified (
5763+ sourceFile->getModuleScopeContext (),
5764+ DeclNameRef (ctx.Id_DefaultIsolation ),
5765+ locInFile);
5766+ for (auto found : defaultIsolationResult) {
5767+ auto *decl = found.getValueDecl ();
5768+ if (!decl)
5769+ continue ;
5770+
5771+ auto *typealias = dyn_cast<TypeAliasDecl>(decl);
5772+ if (!typealias ||
5773+ !typealias->getDeclContext ()->isModuleScopeContext ())
5774+ continue ;
5775+
5776+ // We have a top-level 'DefaultIsolation' typealias. We can assume
5777+ // it's the only one, because multiple top-level typealiases with
5778+ // the same name is a redeclaration error, and default isolation
5779+ // cannot be set by an imported library.
5780+
5781+ // The typealias can only be used to set default isolation per file.
5782+ if (typealias->getFormalAccess () >= AccessLevel::Internal) {
5783+ typealias->diagnose (diag::default_isolation_internal);
5784+ break ;
5785+ }
5786+
5787+ auto type = typealias->getUnderlyingType ();
5788+ if (type->isEqual (mainActorType))
5789+ return DefaultIsolation::MainActor;
5790+
5791+ if (type->isEqual (nonisolatedType))
5792+ return DefaultIsolation::Nonisolated;
5793+
5794+ // The underlying type of the typealias must either be 'MainActor'
5795+ // or 'nonisolated'.
5796+ typealias->diagnose (diag::default_isolation_custom);
5797+ break ;
5798+ }
5799+ }
5800+
5801+ return defaultModuleIsolaton;
5802+ }
5803+
57355804// / Determine the default isolation and isolation source for this declaration,
57365805// / which may still be overridden by other inference rules.
57375806static std::tuple<InferredActorIsolation, ValueDecl *,
@@ -5741,17 +5810,13 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
57415810
57425811 // Determine whether default isolation is set to MainActor, either for
57435812 // the entire module or in this specific file.
5744- if (value->getModuleContext () == ctx.MainModule ) {
5813+ auto mainActorType = ctx.getMainActorType ();
5814+ if (mainActorType && value->getModuleContext () == ctx.MainModule ) {
57455815 // See if we have one specified by our file unit.
5746- auto defaultIsolation = ctx.LangOpts .DefaultIsolationBehavior ;
5747- if (ctx.LangOpts .hasFeature (Feature::SwiftSettings)) {
5748- if (auto *sourceFile = value->getDeclContext ()->getParentSourceFile ()) {
5749- auto options = sourceFile->getLanguageOptions ();
5750- if (auto isolation = options.defaultIsolation ) {
5751- defaultIsolation = *isolation;
5752- }
5753- }
5754- }
5816+ auto *sourceFile = value->getDeclContext ()->getParentSourceFile ();
5817+ auto defaultIsolation = evaluateOrDefault (
5818+ ctx.evaluator , DefaultActorIsolationRequest{sourceFile},
5819+ ctx.LangOpts .DefaultIsolationBehavior );
57555820
57565821 if (defaultIsolation == DefaultIsolation::MainActor) {
57575822 // Default global actor isolation does not apply to any declarations
@@ -5765,7 +5830,6 @@ computeDefaultInferredActorIsolation(ValueDecl *value) {
57655830 dc = dc->getParent ();
57665831 }
57675832
5768- auto mainActorType = ctx.getMainActorType ()->mapTypeOutOfContext ();
57695833 if (!inActorContext) {
57705834 // FIXME: deinit should be implicitly MainActor too.
57715835 if (isa<TypeDecl>(value) || isa<ExtensionDecl>(value) ||
0 commit comments