swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.3k stars 10.34k forks source link

Data race safety bypass #76016

Open Jnosh opened 3 weeks ago

Jnosh commented 3 weeks ago

Description

The below snippet compiles without errors in Swift 6 mode but contains a data race.

Reproduction

// Compile using swiftc -swift-version 6

class Counter {
    private(set) var counter = 0

    func increase() {
        counter += 1
    }
}

func increaseCounter(_ counter: Counter) async {
    for _ in 0..<1000 {
        counter.increase()
    }
}

@MainActor
func race() async {
    let counter = Counter()

    let t1 = Task {
        await increaseCounter(counter)
    }
    let t2 = Task {
        await increaseCounter(counter)
    }

    await t1.value
    await t2.value

    precondition(counter.counter == 2000)
}

while true {
    await race()
}

Expected behavior

I believe the above snippet should be rejected at compile time in Swift 6 mode or with strict concurrency checking enabled.

Environment

Xcode 16 beta 6

swift-driver version: 1.115 Apple Swift version 6.0 (swiftlang-6.0.0.9.10 clang-1600.0.26.2) Target: arm64-apple-macosx15.0

Additional information

No response

jamieQ commented 3 weeks ago

this looks very similar to https://github.com/swiftlang/swift/issues/76003