No MainActor warning in extension

The following example doesn't generate any concurrency warnings in Swift 5.10:

@MainActor
public final class Test {
    func process() {
    }
}

public extension Test {
    func start() {
        let browser = NWBrowser(
            for: .bonjour(type: MIDINetworkBonjourServiceType, domain: nil),
            using: NWParameters()
        )
        browser.browseResultsChangedHandler = { [weak self] newResult, changes in
            Task { [weak self] in
                self?.process() // no warning, compiles ok
            }
        }
    }
}

If I move start out of extension, then I get a warning as I would expect.

Is this a bug, or am I missing something here?

The following example doesn't generate any concurrency
warnings in Swift 5.10:

It does if you set Strict Concurrency Checking to Complete:

browser.browseResultsChangedHandler = { [weak self] newResult, changes in
    Task { [weak self] in
        self?.process() // no warning, compiles ok
     // ^ Expression is 'async' but is not marked with 'await'
    }
}

IME there’s not much point in trying to reason about Swift concurrency without enabling strict checking.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Thank you!