8000 Actor-isolated property wrapper leads to compiling code that crashes at runtime · Issue #77604 · swiftlang/swift · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stephencelis opened this issue Nov 14, 2024 · 4 comments
Labels< 8000 /span>
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software property wrappers Feature: property wrappers triage needed This issue needs more specific labels

Comments

@stephencelis
Copy link
Contributor
stephencelis commented Nov 14, 2024

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 stephencelis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Nov 14, 2024
@stephencelis
Copy link
Contributor Author

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

@tshortli tshortli added the property wrappers Feature: property wrappers label Nov 14, 2024
@ArihantMarwaha
Copy link

Proposed Solution:

Modify the Swift compiler to:

  1. Detect actor-isolated property wrapper properties
  2. Prevent nonisolated access/mutation
  3. Emit a compile-time error instead of a runtime crash

Recommended Fix Prototype:

@propertyWrapper
struct Count {
  @MainActor private var value = 0

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

// Compiler should now prevent this
@Test func f() async {
  @Count var count
  count += 1  // ❌ Compile-time error
}

Potential Compiler Error Message

Error: Cannot mutate main actor-isolated property 'wrappedValue' from a nonisolated context
Suggestion: Use '@MainActor' or transfer the mutation to the main actor

@ArihantMarwaha
Copy link

would love to work on this fix if given a go ahead

@marieandromeda
Copy link

I just ran into this myself, and luckily I searched before posting a new issue. Here was my test case, if it's useful:

@MainActor @propertyWrapper class FootGun {
    var wrappedValue = 42
}

await Task.detached {
    @FootGun var x
    print(x)    //    <-- Crashes here
}.value

Context: I was trying to come up with a way to ensure that property wrapper access is isolated to the main actor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software property wrappers Feature: property wrappers triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

4 participants
0