1

Can someone explain why Xcode (w Swift 5.6 or later) gives a "must be marked @Sendable" warning in one case but not the other?

Case 1:

    func myFunc() {
        @MainActor finishOnMain(_ foo: Foo) {
            // do something with foo
        }
        Task {
            if let foo = await GetFoo() {
                finishOnMain(foo)
            }
        }
    }

Generates warning: "Concurrently-executed local function must be marked sendable"

Case 2:

    func myFunc() {
        Task {
            if let foo = await GetFoo() {
                await MainActor.run {
                    // do something with foo
                }
            }
        }
    }

No warning.

Of course Case 2 doesn't use a local function so the warning wouldn't be exactly the same, but I would think it suffers from the same problem. Maybe this is just a bug/limitation of Xcode? Also I realize that marking the class Foo (or whatever) as @Sendable fixes the problem, but for me it's not always possible.

John Scalo
  • 3,194
  • 1
  • 27
  • 35
  • Related: https://stackoverflow.com/questions/73813225/is-there-a-difference-between-mainactor-in-and-mainactor-run – vadian Jul 26 '23 at 14:34
  • Using MainActor.run in the same manner as the way DispatchQueue.main.async used to be used works a bit contrary to the design of async await. Use the natural flow, return to the original actor. If you turn on strict concurrency I’m sure you’ll find several other warnings. Apple is slowly introducing these so developers can adjust but once Swift 6 hits these things will be errors. – lorem ipsum Jul 26 '23 at 14:43

0 Answers0