Skip to content

@isolated(any) closure can be assigned to non isolated closure property #80823

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
ricocrescenzio95 opened this issue Apr 15, 2025 · 2 comments · May be fixed by #80812
Open

@isolated(any) closure can be assigned to non isolated closure property #80823

ricocrescenzio95 opened this issue Apr 15, 2025 · 2 comments · May be fixed by #80812
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@ricocrescenzio95
Copy link

Description

The compiler allows to set an @isolated(any) closure to a property that is not isolated itself. Shouldn't this throw a compilation error?

struct Example {
  var getString: @Sendable () -> String

  init(getString: @isolated(any) @escaping @Sendable () -> String) {
    self.getString = getString
  }
}

Reproduction

struct Example {
  var getString: @Sendable () -> String

  init(getString: @isolated(any) @escaping @Sendable () -> String) {
    self.getString = getString
  }
}

await Task.detached {
  let e = Example { @MainActor in
    MainActor.assertIsolated() // This crashes
    return "Hello, world!"
  }
  print(e.getString())
}.value

Expected behavior

The compiler should not allow self.getString = getString, unless var getString: @Sendable () -> String is changed to var getString: @isolated(any) @Sendable () -> String.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

No response

@ricocrescenzio95 ricocrescenzio95 added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Apr 15, 2025
@adozenlines
Copy link

What happens when you change var getString: @Sendable () -> String to var getString: () -> String?

@ricocrescenzio95
Copy link
Author

What happens when you change var getString: @Sendable () -> String to var getString: () -> String?

Same behavior, app compiles but it crashes on the MainActor.assertIsolated()

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. triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants