Skip to content

Fix Swift 6 @MainActor isolation error - #107

Open
devxoul wants to merge 2 commits into
mainfrom
fix/swift6-mainactor-isolation
Open

devxoul wants to merge 2 commits into
mainfrom
fix/swift6-mainactor-isolation

Conversation

@devxoul

@devxoul devxoul commented Mar 10, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fix the Swift 6 strict concurrency error when using then/with/do with @MainActor-isolated types such as UIKit classes.

Closes #106

Problem

In Swift 6 strict concurrency, calling then on a @MainActor-isolated type like UIStackView produces:

Converting function value of type '@mainactor (UIStackView) throws -> Void' to '(UIStackView) throws -> Void' loses global actor 'MainActor'

This happens because the library was compiled with swift-tools-version:5.0 (Swift 5 language mode), where non-escaping closures do not inherit the caller's actor isolation.

Changes

Package.swift

  • Bump swift-tools-version from 5.0 to 6.0. In Swift 6 language mode, non-escaping closure parameters automatically inherit the caller's actor isolation — no API changes needed.
  • Update minimum deployment targets to macOS 10.13 / iOS 12 / tvOS 12 / watchOS 4 (required by Swift 6 toolchain).

Tests

  • Add 3 new @MainActor-annotated tests validating that then, with, and do compile and behave correctly with @MainActor-isolated types.

Context

  • Zero API surface changes. Existing consumers on Swift 6 get the fix automatically.
  • Consumers still on Swift 5.x should pin to a previous release, as the new swift-tools-version:6.0 requires Swift 6 tooling.

Testing

  • All existing tests pass.
  • 3 new tests (testThen_MainActorIsolated, testWith_MainActorIsolated, testDo_MainActorIsolated) verify the fix.

Summary by cubic

Fixes Swift 6 strict concurrency errors when calling then/with/do on @MainActor-isolated types by building Then in Swift 6 mode. Also updates minimum platform versions, adds @MainActor tests, and sets Tuist SWIFT_VERSION to 6.0.

  • Migration
    • Requires Swift 6 toolchain (swift-tools-version: 6.0).
    • Minimum targets: macOS 10.13, iOS 12, tvOS 12, watchOS 4.
    • If you need Swift 5.x, pin to a previous Then release.

Written for commit 3b2f791. Summary will update on new commits.

In Swift 6 language mode, non-escaping closures automatically inherit
the caller's actor isolation. This resolves the type mismatch error when
using then/with/do with @MainActor-isolated types such as UIKit classes.

Closes #106

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

@typeey

typeey Bot commented Oct 15, 2026

Copy link
Copy Markdown

I restarted before I could confirm a reply to your earlier request. I didn't rerun it automatically — please ask again if you still need it.

@typeey typeey Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core fix needs a regression test covering the function-value conversion reported in #106; the added inline-closure tests do not establish it. GitHub CI passes on all four platforms at 3b2f791, but local compiler/build execution was unavailable during this review.

Out-of-diff findings moved from inline comments:

  • Tests/ThenTests/ThenTests.swift:81 (RIGHT): The new tests exercise closure-literal isolation inheritance rather than the actor-qualified method-reference conversion this PR claims to fix.

The test uses MyView().then { $0.title = "hello" }, whereas #106 reports UIStackView().then($0.addSubview) losing MainActor during function-value conversion. Closure inheritance predates Swift 6: SE-0306 states that a non-@Sendable closure formed within an actor-isolated context is actor-isolated. All three public callback signatures remain unchanged, so these tests do not demonstrate why changing the library's language mode resolves the reported error.

Add a Swift 6 consumer regression using the reported method-reference form and its enclosing isolation context. Compile it against the separately built base and head library to demonstrate failure before and success after; if both behave identically, revise the proposed fix and rationale before dropping Swift 5 toolchain support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Swift 6 MainActor isolation issue?

1 participant