Skip to content

Missing concurrency diagnostics when a local var is captured by concurent Tasks. #82827

Open
@CrystDragon

Description

@CrystDragon

Description

The Following code compiles but contains a race the compiler failed to detect:

@MainActor
func test() {
    var s = 1
    Task { @MainActor  in
        for _ in 0..<100 {
            s += 1
        }
    }
    Task.detached {
        for _ in 0..<100 {
            s += 1
        }
    }
}

Reproduction

See above in description.

Expected behavior

s is being captured into 2 closures with different isolation, that should be invalid.

Environment

  • Swift version 6.2-dev (LLVM ac66248b7c45109, Swift 92fd571)
  • Target: x86_64-unknown-linux-gnu
  • Build config: +assertions
  • Internal compiler ID: swiftdevsnapshot

Additional information

This bug may be somewhat related to #76929.

The difference is that, in this bug, a var is declared in a @MainActor scope, and later sent into a nonisolated region; while in the referenced one, a var is declared in a nonisolated scope and later sent into a @MainActor region.

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