Skip to content

Swift 6 MainActor isolation issue? #106

Open

Description

@EddieLukeAtmey

Hi, I'm writing like this in code swift 5.9, it's working:

UIStackView().then($0.addSubview).then {
    $0.axis = .vertical
    $0.addArrangedSubviews(...)
}

However, when I upgrade to swift 6.0 or 6.2, the compiler is complaining:

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

I tried config in build setting's Default actor isolation = MainActor but it's not working. i suspect the cause is in the spm defination:

// swift-tools-version:5.0

maybe this could help fix it? https://www.avanderlee.com/concurrency/default-actor-isolation-in-swift-6-2/

// swift-tools-version: 6.2
...
.target(
    name: "DefaultActorIsolationPackage",
    swiftSettings: [
        /// You can add a new `defaultIsolation` Swift setting to any of your SPM targets.
        .defaultIsolation(MainActor.self)
    ]
)

I'm not totally sure about this, as i'm not an expert on those Actors thingy.

Activity

  1. added a commit that references this issue on Mar 10, 2026
    31ffbdf
  2. devxoul commented on Mar 10, 2026

    @devxoul
    Owner

    Thanks for reporting this! The root cause is that the library was compiled with swift-tools-version:5.0 (Swift 5 language mode), where non-escaping closures don't inherit the caller's actor isolation.

    In Swift 6 language mode, non-escaping closure parameters automatically inherit the caller's actor isolation — so bumping swift-tools-version to 6.0 fixes this without any API changes. No overloads or @preconcurrency workarounds needed.

    PR: #107

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions