swiftlang / swift

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

Data race safety regression with non-sendable class in Swift 6 compiler #76003

Open jamieQ opened 2 months ago

jamieQ commented 2 months ago

Description

the following code should produce a compile-time error disallowing the use of a non-sendable class across two isolation domains. running the code with TSAN enabled detects a data race at runtime.

Reproduction

class NonSendableClass {
  var data: Int = 0
}

func f(_ ns: NonSendableClass) async {
  ns.data += 1
}

@MainActor
func main() async {
  let nonSendableObject = NonSendableClass()
  Task {
    await f(nonSendableObject)
  }
  await Task.yield()
  _ = nonSendableObject.data
}
await main()

Expected behavior

the use of nonSendableObject in this way should produce a compiler error detecting a potential data race.

Environment

swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1) Target: arm64-apple-macosx14.0

(Xcode 16, beta 5)

Additional information

originally reported via: https://forums.swift.org/t/a-non-sendable-class-can-bypass-swift6s-concurrency-check-and-cause-data-race.

notes:

  1. changing to Task.detached causes a compiler error to be produced
  2. the 5.10 compiler appeared to produce a warning with complete concurrency checking enabled
xedin commented 2 months ago

@gottesmm I think this is a region based isolation issue, isn't it?