swiftlang / swift

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

Actor-isolated property wrapper leads to compiling code that crashes at runtime #77604

Open stephencelis opened 2 days ago

stephencelis commented 2 days ago

Description

It is possible to actor isolate the wrappedValue of a property wrapper and interact with it in a nonisolated way, leading to a crash.

Reproduction

@propertyWrapper
struct Count {
  @MainActor var value = 0

  @MainActor
  var wrappedValue: Int {
    get { value }
    set { value = newValue }
  }
}

@Test func f() async {
  @Count var count
  count += 1  // 💥
}

Stack dump

N/A. Produces code that crashes at runtime. Doesn't crash the compiler.

Expected behavior

I expect the line that crashes to not compile with a message such as:

Main actor-isolated property 'wrappedValue' can not be mutated from a nonisolated context

Environment

swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4) Target: arm64-apple-macosx15.0

Additional information

No response

stephencelis commented 2 days ago

(@tshortli Related to some of the other issues we've been talking about.)