Something broke SE-0313?

Did something break SE-0313, or am I missing something?

I am not able to compile the declaration below, neither in Swift 5 mode nor in Swift 6 mode.

let f = BankAccount.deposit (amount:)
// error: Call to actor-isolated instance method 'deposit(amount:)' in a synchronous main actor-isolated context

Context

@main
enum SE_0313 {
    static func main () async {
        /* From SE-0313 */
        // type of f is (isolated BankAccount) -> (Double) -> Void
        let f = BankAccount.deposit (amount:)
        // error: Call to actor-isolated instance method 'deposit(amount:)' in a synchronous main actor-isolated context
    }
}

/* From SE-0313 */

actor BankAccount {
  let accountNumber: Int
  var balance: Double

  init (accountNumber: Int, initialDeposit: Double) {
    self.accountNumber = accountNumber
    self.balance = initialDeposit
  }
  
  func deposit (amount: Double) {
    assert(amount >= 0)
    balance = balance + amount
  }
}

/*
 A function can become actor-isolated by indicating that one of its parameters is isolated. For example, the deposit(amount:) operation can now be expressed as a module-scope function as follows:
 */
func deposit (amount: Double, to account: isolated BankAccount) {
  assert (amount >= 0)
  account.balance = account.balance + amount
}

Note: if BankAccount were a class the declaration would compile.

1 Like

This looks like a bug to me, since you're not actually calling BankAccount.deposit(amount:).

These errors are at least wrong I think:

@globalActor
actor A {
  static let shared = A()
  func f() {}
}
let isolation = A()
A.shared.assumeIsolated { _ in
  let f = A.f // Call to actor-isolated instance method 'f()'
              // in a synchronous nonisolated context
}
Task { @A in
  let f = A.f // Call to actor-isolated instance method 'f()'
              // in a synchronous nonisolated context
}
1 Like

i agree (reported here). it appears that the diagnostic may have regressed somewhat around Swift 5.9. in the 5.8 compiler it errors with:

=== <source>:11:29 ===
10 |     func test() async {
11 |         let f = BankAccount.deposit(amount:)
   |                             ^ error: actor-isolated instance method 'deposit(amount:)' can not be referenced from a non-isolated context
12 |     }
  ...
25 |   
26 |   func deposit (amount: Double) {
   |        ^ note: calls to instance method 'deposit(amount:)' from outside of its actor context are implicitly asynchronous
27 |     assert(amount >= 0)
2 Likes