Skip to content

Unsoundness causing crash when capturing nonisolated local in escaping @MainActor closure #82259

Open
@KeithBauerANZ

Description

@KeithBauerANZ

Description

No response

Reproduction

func escape(
    _ closure: @escaping @MainActor (String) -> Void
) {
    Task.detached { @MainActor in
        var i = 0
        while true {
            closure("MainActor \(i)")
            i += 1
        }
    }
}

func doTheThing() async {
    var x = ""
    escape {
        x = $0
    }
    var i = 0
    while true {
        x = "Nonisolated \(i)"
        i += 1
        print(x)
    }
}

await doTheThing()
swiftc -swift-version 6 NonisolatedLocalCapturedByMainActorClosure.swift && ./NonisolatedLocalCapturedByMainActorClosure
MainActor 210
MainActor 479
MainActor 502
(... tons more output)
MainActor 100354
Nonisolated 5010
Nonisolated 5011
Nonisolated 5012
Nonisolated 5013
Nonisolated 5014
Nonisolated 5015
Nonisolated 5016
Nonisolated 5017
Nonisolated 5018
Nonisolated 5019
Nonisolated 5020
Nonisolated 5021
Nonisolated 5022
Nonisolated 5023
Nonisolated 5024
zsh: segmentation fault  ./NonisolatedLocalCapturedByMainActorClosure

Expected behavior

There should be an error capturing the mutable nonisolated local in the @MainActor closure

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions