Skip to content

[SE-0466] Add SWIFT_DEFAULT_ACTOR_ISOLATION build setting #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Sources/SWBUniversalPlatform/Specs/Swift.xcspec
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,22 @@
Category = "Upcoming Features";
Description = "Enables strict concurrency checking to produce warnings for possible data races. This is always 'complete' when in the Swift 6 language mode and produces errors instead of warnings.";
},

{
Name = "SWIFT_DEFAULT_ACTOR_ISOLATION";
Type = Enumeration;
Values = (
nonisolated,
MainActor
);
DefaultValue = "nonisolated";
CommandLineArgs = {
nonisolated = ();
MainActor = ( "-default-isolation=MainActor" );
};
DisplayName = "Default Actor Isolation";
Category = "Language";
Description = "Controls default actor isolation for unannotated code. When set to 'MainActor', `@MainActor` isolation will be inferred by default to mitigate false-positive data-race safety errors in sequential code.";
},
{
Name = "SWIFT_STRICT_MEMORY_SAFETY";
Type = Boolean;
Expand Down
88 changes: 88 additions & 0 deletions Tests/SWBTaskConstructionTests/SwiftTaskConstructionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3468,6 +3468,94 @@ fileprivate struct SwiftTaskConstructionTests: CoreBasedTests {
}
}

@Test(.requireSDKs(.macOS))
func defaultIsolationFlag() async throws {
try await withTemporaryDirectory { tmpDir in
let srcRoot = tmpDir.join("srcroot")
let testProject = try await TestProject(
"ProjectName",
sourceRoot: srcRoot,
groupTree: TestGroup(
"SomeFiles", path: "Sources",
children: [
TestFile("File1.swift"),
TestFile("File2.swift"),
TestFile("File3.swift"),
]),
targets: [
TestStandardTarget(
"Default",
type: .framework,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"GENERATE_INFOPLIST_FILE": "YES",
"PRODUCT_NAME": "$(TARGET_NAME)",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "5.0",
]),
],
buildPhases: [
TestSourcesBuildPhase([
TestBuildFile("File1.swift"),
]),
], dependencies: ["Nonisolated", "MainActor"]),
TestStandardTarget(
"Nonisolated",
type: .framework,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"GENERATE_INFOPLIST_FILE": "YES",
"PRODUCT_NAME": "$(TARGET_NAME)",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "5.0",
"SWIFT_DEFAULT_ACTOR_ISOLATION": "nonisolated",
]),
],
buildPhases: [
TestSourcesBuildPhase([
TestBuildFile("File2.swift"),
]),
]),
TestStandardTarget(
"MainActor",
type: .framework,
buildConfigurations: [
TestBuildConfiguration("Debug", buildSettings: [
"GENERATE_INFOPLIST_FILE": "YES",
"PRODUCT_NAME": "$(TARGET_NAME)",
"SWIFT_EXEC": swiftCompilerPath.str,
"SWIFT_VERSION": "5.0",
"SWIFT_DEFAULT_ACTOR_ISOLATION": "MainActor",
]),
],
buildPhases: [
TestSourcesBuildPhase([
TestBuildFile("File3.swift"),
]),
]),
])

let tester = try await TaskConstructionTester(getCore(), testProject)
await tester.checkBuild(BuildParameters(action: .install, configuration: "Debug"), runDestination: .macOS) { results in
results.checkTarget("Default") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineNoMatch([.prefix("-default-isolation")])
}
}
results.checkTarget("Nonisolated") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineNoMatch([.prefix("-default-isolation")])
}
}
results.checkTarget("MainActor") { target in
results.checkTask(.matchTarget(target), .matchRuleType("SwiftDriver Compilation")) { task in
task.checkCommandLineContains(["-default-isolation=MainActor"])
}
}
}
}
}

// Test frontend flag -library-level inference from the INSTALL_PATH.
@Test(.requireSDKs(.macOS))
func libraryLevel() async throws {
Expand Down