Consider the following code compiling in Xcode 16.0 (Swift 6.0.0.9.10) with complete
strict concurrency.
public typealias CompletionBlock = (Error?) -> Void
class SomeClass {
var someCallback: ((@escaping CompletionBlock) -> Void)?
}
import XCTest
@testable import testcompilerbug
final class testcompilerbugTests: XCTestCase {
var someObj: SomeClass!
override func setUpWithError() throws {
someObj = SomeClass()
}
override func tearDownWithError() throws {
someObj = nil
}
func testExample() throws {
let expectation = expectation(description: "asdfasdf")
someObj.someCallback = { completionBlock in
Task { @MainActor in
completionBlock(nil) // If I comment this out, the concurrency warning goes away
expectation.fulfill() //Sending 'expectation' risks causing data races; this is an error in the Swift 6 language mode
}
}
wait(for: [expectation])
}
}
I don't understand why i'd ever get an error for capturing expectation
as it's a Sendable type. Additionally, i don't understand why commenting out the call to completionBlock
has any impact on the safety of calling expectation ?
I saw this thread : Why does sending a sendable value risk causing data races? - #7 by willtemperley but i'm not sure it's the same as the issue referenced there seems to deal with return values ? I also don't have region based isolation enabled.