Skip to content

Incorrect data race diagnostic when capturing weak reference to actor-isolated class via a non-isolated context #85402

@Jnosh

Description

@Jnosh

Description

The following snippet compiles successfully using Swift 6.2 but fails to compile with the most recent main snapshot.

A reference to a MainActor isolated instance is first weakly captured in a non-isolated closure. Then that weak reference is captured in a MainActor isolated closure but the compiler erroneously won't allow accessing it there.

No error when capturing a strong reference or turning the weak ref into a strong one before capturing it in the inner, isolated, closure.

Reproduction

// Compile with: swiftc -swift-version 6

func notIsolated(_: @Sendable () -> Void) {}
func onMainActor(_: @MainActor () -> Void) {}

@MainActor
final class IsolatedClass {
    func foo() {
        notIsolated { [weak self] in
            onMainActor {
                // error: sending 'self' risks causing data races [#SendingRisksDataRace]
                _ = self
            }
        }
    }
}

Expected behavior

Compiles successfully

Environment

main-snapshot-2025-11-03 installed via swiftly

Apple Swift version 6.3-dev (LLVM bff1370bd79c983, Swift 57cf4ce)

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