Swift6 race warning

I'm trying to fix some Swift6 warnings, this one seems too strict, I'm not sure how to fix it. The variable path is a String, which should be immutable, it's a local variable and never used again inside of the function, but still Swift6 complains about it being a race condition, passing it to the task

What should I do here to fix the warning?

I tried a bunch of things but wasn’t able to reproduce this. Here’s an example of what I did:

import Foundation
import Photos

@MainActor
func show(imageInfo: (path: String, asset: PHAsset?)) {
}

class Test {
    func onCaptureCompleted(_ imageURL: URL, asset: PHAsset?) {
        let path = imageURL.path()
        Task { @MainActor in
            show(imageInfo: (path, asset))
        }
    }
}

func main() {
    print("Hello Cruel World!")
}

main()

This compiles just fine with Xcode 16.2 in Swift 6 language mode.

Which version of Xcode are you using?

Can you post a snippet that shows the problem? Please post it as text rather than an image. See tips 5 and 6 in Quinn’s Top Ten DevForums Tips.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for taking a look. I tried some more things and it looks like it's a misleading compiler error, maybe nothing to do with the path variable at all even though the compiler says it is.

I took the path variable out of the function temporarily, then it gave another error: "Task or actor isolated value cannot be sent; this is an error in the Swift 6 language mode".

The containing class is marked as @Observable, so I'm assuming really it is complaining that "self" is getting pulled into the Task which can cause race conditions even though the compiler error does not say that. I added the Sendable protocol to the container class and the warning went away.

Thanks for taking the time to look into it.

Side note: Swift6 makes writing code miserable, it's so broken still, good idea poor execution.

It would maybe help the guys at Apple to fix this sort of thing (or at least provide a better error) if you would post your code - as Quinn asked you to.

Swift 6 makes writing code miserable

Keep in mind that the Swift 6 language mode is optional; you can always revert to Swift 5 mode.

In general, I’m the opposite an early adopter. I let other folks deal with all the grief associated with new OS versions, APIs, and tools. By the time I get around to it, the early adopters have encounter all the bumps, and either smoothed them over or published a map for how to avoid them.

Now, Swift concurrency is an exception to my general rule because I’m super interested in this technology. But that’s just me. It’s fine for you to take your time here. Adoption is only going to get easier as the tools, SDKs, and documentation improve.

it looks like it's a misleading compiler error

Right. That’s why I was trying to reproduce it. I originally suspected that this was caused by you putting both values into a tuple, but I wasn’t able to confirm that because I could never reproduce the problem. If you can help me out there, that’d be grand.

Oh, and it’ll also make for a reduced test project that you can use to file a bug about the misleading diagnostics.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Swift6 race warning
 
 
Q