A Swift concurrency threading surprise with protocol-typed object

Compiling the example above with -strict-concurrency=complete produces the following warning:

test.swift:17:21: warning: main actor-isolated instance method 'doSomething()' cannot be used to satisfy nonisolated protocol requirement
    @MainActor func doSomething() {
                    ^
test.swift:4:10: note: mark the protocol requirement 'doSomething()' 'async' to allow actor-isolated conformances
    func doSomething()

If you want to be sure you have the most-protective concurrency warnings you should enable that option, though this may result in warnings on some patterns that you as the developer 'know' are safe but which the Swift compiler cannot (yet) recognize.

3 Likes