Skip to content

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

Open
@jamieQ

Description

@jamieQ

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

Metadata

Metadata

Assignees

Labels

accepts invalidBug: Accepts invalidbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfconcurrencyFeature: umbrella label for concurrency language features

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions