diff --git a/include/swift/Basic/Features.def b/include/swift/Basic/Features.def index 355360f09e29c..220e93ae986cc 100644 --- a/include/swift/Basic/Features.def +++ b/include/swift/Basic/Features.def @@ -465,9 +465,6 @@ EXPERIMENTAL_FEATURE(ImportNonPublicCxxMembers, true) // Isolated deinit SUPPRESSIBLE_LANGUAGE_FEATURE(IsolatedDeinit, 371, "isolated deinit") -// When a parameter has unspecified isolation, infer it as main actor isolated. -EXPERIMENTAL_FEATURE(UnspecifiedMeansMainActorIsolated, false) - /// modify/read single-yield coroutines SUPPRESSIBLE_EXPERIMENTAL_FEATURE(CoroutineAccessors, true) diff --git a/include/swift/Basic/LangOptions.h b/include/swift/Basic/LangOptions.h index 0ab9297ad68f6..5d9cac773637b 100644 --- a/include/swift/Basic/LangOptions.h +++ b/include/swift/Basic/LangOptions.h @@ -96,6 +96,11 @@ namespace swift { TaskToThread, }; + enum class DefaultIsolation : uint8_t { + MainActor, + Nonisolated + }; + /// Describes the code size optimization behavior for code associated with /// declarations that are marked unavailable. enum class UnavailableDeclOptimization : uint8_t { @@ -632,6 +637,9 @@ namespace swift { /// Disables `DynamicActorIsolation` feature. bool DisableDynamicActorIsolation = false; + /// Defines the default actor isolation. + DefaultIsolation DefaultIsolationBehavior = DefaultIsolation::Nonisolated; + /// Whether or not to allow experimental features that are only available /// in "production". #ifdef NDEBUG diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td index c16f7b1a533a8..f446f81af66c3 100644 --- a/include/swift/Option/Options.td +++ b/include/swift/Option/Options.td @@ -985,6 +985,15 @@ def strict_concurrency : Joined<["-"], "strict-concurrency=">, "concurrency model, or 'complete' ('Sendable' and other checking is " "enabled for all code in the module)">; +def default_isolation : Separate<["-"], "default-isolation">, + Flags<[FrontendOption]>, + HelpText<"Specify the default actor isolation: MainActor or nonisolated. " + "Defaults to nonisolated.">, + MetaVarName<"MainActor|nonisolated">; +def default_isolation_EQ : Joined<["-"], "default-isolation=">, + Flags<[FrontendOption]>, + Alias; + def enable_experimental_feature : Separate<["-"], "enable-experimental-feature">, Flags<[FrontendOption, ModuleInterfaceOption]>, diff --git a/lib/AST/FeatureSet.cpp b/lib/AST/FeatureSet.cpp index 5a3d5f4616e37..136f05861196b 100644 --- a/lib/AST/FeatureSet.cpp +++ b/lib/AST/FeatureSet.cpp @@ -205,7 +205,6 @@ UNINTERESTING_FEATURE(StaticExclusiveOnly) UNINTERESTING_FEATURE(ExtractConstantsFromMembers) UNINTERESTING_FEATURE(GroupActorErrors) UNINTERESTING_FEATURE(SameElementRequirements) -UNINTERESTING_FEATURE(UnspecifiedMeansMainActorIsolated) static bool usesFeatureSendingArgsAndResults(Decl *decl) { auto isFunctionTypeWithSending = [](Type type) { diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index a47134776d6d1..df67034ce7333 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -969,9 +969,6 @@ static bool ParseEnabledFeatureArgs(LangOptions &Opts, ArgList &Args, if (Args.hasArg(OPT_strict_memory_safety)) Opts.enableFeature(Feature::StrictMemorySafety); - if (Opts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated)) - Opts.enableFeature(Feature::InferIsolatedConformances); - return HadError; } @@ -1817,6 +1814,27 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args, Opts.DisableDynamicActorIsolation |= Args.hasArg(OPT_disable_dynamic_actor_isolation); + if (const Arg *A = Args.getLastArg(options::OPT_default_isolation)) { + auto behavior = + llvm::StringSwitch>(A->getValue()) + .Case("MainActor", DefaultIsolation::MainActor) + .Case("nonisolated", DefaultIsolation::Nonisolated) + .Default(std::nullopt); + + if (behavior) { + Opts.DefaultIsolationBehavior = *behavior; + } else { + Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value, + A->getAsString(Args), A->getValue()); + HadError = true; + } + } else { + Opts.DefaultIsolationBehavior = DefaultIsolation::Nonisolated; + } + + if (Opts.DefaultIsolationBehavior == DefaultIsolation::MainActor) + Opts.enableFeature(Feature::InferIsolatedConformances); + #if !defined(NDEBUG) && SWIFT_ENABLE_EXPERIMENTAL_PARSER_VALIDATION /// Enable round trip parsing via the new swift parser unless it is disabled /// explicitly. The new Swift parser can have mismatches with C++ parser - diff --git a/lib/Sema/TypeCheckConcurrency.cpp b/lib/Sema/TypeCheckConcurrency.cpp index 5a36e479e2d66..359bc2a2362e8 100644 --- a/lib/Sema/TypeCheckConcurrency.cpp +++ b/lib/Sema/TypeCheckConcurrency.cpp @@ -5798,7 +5798,7 @@ computeDefaultInferredActorIsolation(ValueDecl *value) { // If we are required to use main actor... just use that. if (!ignoreUnspecifiedMeansMainActorIsolated && - ctx.LangOpts.hasFeature(Feature::UnspecifiedMeansMainActorIsolated)) + ctx.LangOpts.DefaultIsolationBehavior == DefaultIsolation::MainActor) if (auto result = globalActorHelper(ctx.getMainActorType()->mapTypeOutOfContext())) return *result; diff --git a/test/Concurrency/Runtime/unspecified_is_main_actor.swift b/test/Concurrency/Runtime/unspecified_is_main_actor.swift index fe2d312a18ece..51c3a732079c2 100644 --- a/test/Concurrency/Runtime/unspecified_is_main_actor.swift +++ b/test/Concurrency/Runtime/unspecified_is_main_actor.swift @@ -1,11 +1,10 @@ -// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -swift-version 6 -g -import-objc-header %S/Inputs/RunOnMainActor.h %import-libdispatch -enable-experimental-feature UnspecifiedMeansMainActorIsolated ) +// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -swift-version 6 -g -import-objc-header %S/Inputs/RunOnMainActor.h %import-libdispatch -Xfrontend -default-isolation=MainActor ) // REQUIRES: executable_test // REQUIRES: concurrency // REQUIRES: concurrency_runtime // REQUIRES: libdispatch // REQUIRES: asserts -// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated // UNSUPPORTED: freestanding diff --git a/test/Concurrency/assume_mainactor.swift b/test/Concurrency/assume_mainactor.swift index 4064bb7582de8..5ac53cb6bc911 100644 --- a/test/Concurrency/assume_mainactor.swift +++ b/test/Concurrency/assume_mainactor.swift @@ -1,10 +1,8 @@ -// RUN: %target-swift-frontend -swift-version 6 -emit-silgen -enable-experimental-feature UnspecifiedMeansMainActorIsolated %s | %FileCheck %s -// RUN: %target-swift-frontend -swift-version 6 -emit-sil -enable-experimental-feature UnspecifiedMeansMainActorIsolated %s -verify - -// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated +// RUN: %target-swift-frontend -swift-version 6 -emit-silgen -default-isolation MainActor %s | %FileCheck %s +// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify // READ THIS! This test is meant to FileCheck the specific isolation when -// UnspecifiedMeansMainActorIsolated is enabled. Please do not put other types +// `-default-isolation` is set to `MainActor`. Please do not put other types // of tests in here. class Klass { diff --git a/test/Concurrency/assume_mainactor_typechecker_errors.swift b/test/Concurrency/assume_mainactor_typechecker_errors.swift index 6f70c3bfa8579..7ca7f42af09c6 100644 --- a/test/Concurrency/assume_mainactor_typechecker_errors.swift +++ b/test/Concurrency/assume_mainactor_typechecker_errors.swift @@ -1,9 +1,7 @@ -// RUN: %target-swift-frontend -swift-version 6 -emit-sil -enable-experimental-feature UnspecifiedMeansMainActorIsolated %s -verify - -// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated +// RUN: %target-swift-frontend -swift-version 6 -emit-sil -default-isolation MainActor %s -verify // READ THIS! This test is meant to check the specific isolation when -// UnspecifiedMeansMainActorIsolated is enabled in combination with validating +// `-default-isolation` is set to `MainActor` in combination with validating // behavior around explicitly non-Sendable types that trigger type checker // specific errors. Please do not put other types of tests in here. diff --git a/test/Concurrency/isolated_conformance_default_actor.swift b/test/Concurrency/isolated_conformance_default_actor.swift index 11db1aefa69cf..3b30223420731 100644 --- a/test/Concurrency/isolated_conformance_default_actor.swift +++ b/test/Concurrency/isolated_conformance_default_actor.swift @@ -1,7 +1,6 @@ -// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances -enable-experimental-feature UnspecifiedMeansMainActorIsolated %s +// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.1-abi-triple -swift-version 6 -enable-experimental-feature IsolatedConformances -default-isolation MainActor %s // REQUIRES: swift_feature_IsolatedConformances -// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated // REQUIRES: concurrency nonisolated diff --git a/test/Concurrency/swiftsettings_defaultIsolation.swift b/test/Concurrency/swiftsettings_defaultIsolation.swift index aca191b7cbd37..7ecb695a07722 100644 --- a/test/Concurrency/swiftsettings_defaultIsolation.swift +++ b/test/Concurrency/swiftsettings_defaultIsolation.swift @@ -1,11 +1,10 @@ // RUN: %target-swift-frontend -enable-experimental-feature SwiftSettings -c -verify -swift-version 6 -verify-additional-prefix nonisolated- -disable-availability-checking %s // RUN: %target-swift-frontend -enable-experimental-feature SwiftSettings -c -verify -swift-version 6 -verify-additional-prefix main-actor- -disable-availability-checking -DSWIFT_SETTINGS_MAIN_ACTOR %s -// RUN: %target-swift-frontend -enable-experimental-feature UnspecifiedMeansMainActorIsolated -enable-experimental-feature SwiftSettings -c -verify -swift-version 6 -disable-availability-checking -verify-additional-prefix main-actor- %s -// RUN: %target-swift-frontend -enable-experimental-feature UnspecifiedMeansMainActorIsolated -enable-experimental-feature SwiftSettings -c -verify -swift-version 6 -disable-availability-checking -verify-additional-prefix nonisolated- -DSWIFT_SETTINGS_NONISOLATED %s +// RUN: %target-swift-frontend -default-isolation MainActor -enable-experimental-feature SwiftSettings -c -verify -swift-version 6 -disable-availability-checking -verify-additional-prefix main-actor- %s +// RUN: %target-swift-frontend -default-isolation MainActor -enable-experimental-feature SwiftSettings -c -verify -swift-version 6 -disable-availability-checking -verify-additional-prefix nonisolated- -DSWIFT_SETTINGS_NONISOLATED %s // REQUIRES: asserts // REQUIRES: concurrency -// REQUIRES: swift_feature_UnspecifiedMeansMainActorIsolated // REQUIRES: swift_feature_SwiftSettings #if SWIFT_SETTINGS_MAIN_ACTOR