Skip to content

Add sendable annotation to userInfo closure #1979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ public final class JSONRPCConnection: Connection {
decoder.userInfo[.messageRegistryKey] = messageRegistry

// Setup callback for response type.
decoder.userInfo[.responseTypeCallbackKey] = { (id: RequestID) -> ResponseType.Type? in
decoder.userInfo[.responseTypeCallbackKey] = { @Sendable (id: RequestID) -> ResponseType.Type? in
// `outstandingRequests` should never be mutated in this closure. Reading is fine as all of our other writes are
// guarded by `queue`, but `JSONDecoder` could (since this is sendable) invoke this concurrently.
guard let outstanding = self.outstandingRequests[id] else {
logger.error("Unknown request for \(id, privacy: .public)")
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension CodingUserInfoKey {

extension JSONRPCMessage: Codable {

public typealias ResponseTypeCallback = (RequestID) -> ResponseType.Type?
public typealias ResponseTypeCallback = @Sendable (RequestID) -> ResponseType.Type?

private enum CodingKeys: String, CodingKey {
case jsonrpc
Expand Down
2 changes: 1 addition & 1 deletion Sources/SKTestSupport/CheckCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ package func checkDecoding<T: Codable & Equatable>(
package func checkCoding<T: Codable>(
_ value: T,
json: String,
userInfo: [CodingUserInfoKey: Any] = [:],
userInfo: [CodingUserInfoKey: any Sendable] = [:],
file: StaticString = #filePath,
line: UInt = #line,
body: (T) -> Void
Expand Down
10 changes: 6 additions & 4 deletions Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ final class CodingTests: XCTestCase {
return $0 == .string("unknown") ? nil : InitializeResult.self
}

var info = defaultCodingInfo as [CodingUserInfoKey: Any]
var info = defaultCodingInfo
info[CodingUserInfoKey.responseTypeCallbackKey] = responseTypeCallback

checkMessageDecodingError(
Expand Down Expand Up @@ -366,7 +366,9 @@ final class CodingTests: XCTestCase {
}
}

let defaultCodingInfo = [CodingUserInfoKey.messageRegistryKey: MessageRegistry.lspProtocol]
let defaultCodingInfo: [CodingUserInfoKey: any Sendable] = [
CodingUserInfoKey.messageRegistryKey: MessageRegistry.lspProtocol
]

private func checkMessageCoding<Request: RequestType & Equatable>(
_ value: Request,
Expand Down Expand Up @@ -417,7 +419,7 @@ private func checkMessageCoding<Response: ResponseType & Equatable>(
return $0 == .string("unknown") ? nil : Response.self
}

var codingInfo = defaultCodingInfo as [CodingUserInfoKey: Any]
var codingInfo = defaultCodingInfo
codingInfo[.responseTypeCallbackKey] = callback

checkCoding(JSONRPCMessage.response(value, id: id), json: json, userInfo: codingInfo, file: file, line: line) {
Expand Down Expand Up @@ -462,7 +464,7 @@ private func checkMessageCoding(
private func checkMessageDecodingError(
_ expected: MessageDecodingError,
json: String,
userInfo: [CodingUserInfoKey: Any] = defaultCodingInfo,
userInfo: [CodingUserInfoKey: any Sendable] = defaultCodingInfo,
file: StaticString = #filePath,
line: UInt = #line
) {
Expand Down