From 96877aff7f63db6b01b3ec7b5769c0e1b3c79a4e Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 14 Jan 2025 16:36:17 +0000 Subject: [PATCH] Fix compiling on 6.1 (#3057) Motivation: Swift 6.1 nightlies have added many new warnings and errors to NIO. This patch resolves all of them. Modifications: - Clean up missing imports - Move away from @_implementationOnly where necessary - Fix a few things where Sendable mismatches were present. Result: Clean compile again --------- Co-authored-by: Johannes Weiss --- Package.swift | 3 ++- Sources/NIOCore/EventLoop.swift | 5 +++-- Sources/NIOCore/FileRegion.swift | 3 --- Sources/NIOEmbedded/AsyncTestingEventLoop.swift | 9 +++++---- Sources/NIOHTTP1/HTTPDecoder.swift | 13 +++++++++---- Sources/NIOPosix/DatagramVectorReadManager.swift | 1 + Sources/NIOPosix/PendingDatagramWritesManager.swift | 1 + Sources/NIOPosix/SelectableEventLoop.swift | 2 +- Sources/NIOPosix/SelectorKqueue.swift | 1 + Tests/NIOPosixTests/HappyEyeballsTest.swift | 1 + Tests/NIOPosixTests/MulticastTest.swift | 2 +- .../PendingDatagramWritesManagerTests.swift | 1 + 12 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Package.swift b/Package.swift index 610fbf6db..14f9183f9 100644 --- a/Package.swift +++ b/Package.swift @@ -443,6 +443,7 @@ let package = Package( "NIOConcurrencyHelpers", "NIOEmbedded", "CNIOLinux", + "CNIODarwin", "NIOTLS", ] ), @@ -558,7 +559,7 @@ let package = Package( if Context.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil { package.dependencies += [ .package(url: "https://github.com/apple/swift-atomics.git", from: "1.1.0"), - .package(url: "https://github.com/apple/swift-collections.git", from: "1.0.2"), + .package(url: "https://github.com/apple/swift-collections.git", from: "1.1.0"), .package(url: "https://github.com/apple/swift-system.git", from: "1.4.0"), ] } else { diff --git a/Sources/NIOCore/EventLoop.swift b/Sources/NIOCore/EventLoop.swift index 021725af9..1b619a3b0 100644 --- a/Sources/NIOCore/EventLoop.swift +++ b/Sources/NIOCore/EventLoop.swift @@ -761,12 +761,13 @@ extension EventLoop { /// - Returns: An `EventLoopFuture` containing the result of `task`'s execution. @inlinable @preconcurrency - public func submit(_ task: @escaping @Sendable () throws -> T) -> EventLoopFuture { + public func submit(_ task: @escaping @Sendable () throws -> T) -> EventLoopFuture { let promise: EventLoopPromise = makePromise(file: #fileID, line: #line) self.execute { do { - promise.succeed(try task()) + // UnsafeUnchecked is allowed here because we know we are on the EL. + promise.assumeIsolatedUnsafeUnchecked().succeed(try task()) } catch let err { promise.fail(err) } diff --git a/Sources/NIOCore/FileRegion.swift b/Sources/NIOCore/FileRegion.swift index 1b1f1c5b0..7ceaacd5d 100644 --- a/Sources/NIOCore/FileRegion.swift +++ b/Sources/NIOCore/FileRegion.swift @@ -93,9 +93,6 @@ public struct FileRegion: Sendable { } } -@available(*, unavailable) -extension FileRegion: Sendable {} - extension FileRegion { /// Create a new `FileRegion` forming a complete file. /// diff --git a/Sources/NIOEmbedded/AsyncTestingEventLoop.swift b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift index 3321a6a60..b0ee04036 100644 --- a/Sources/NIOEmbedded/AsyncTestingEventLoop.swift +++ b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift @@ -131,7 +131,7 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { self.scheduledTasks.removeFirst { $0.id == taskID } } - private func insertTask( + private func insertTask( taskID: UInt64, deadline: NIODeadline, promise: EventLoopPromise, @@ -145,7 +145,8 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { insertOrder: self.nextTaskNumber(), task: { do { - promise.succeed(try task()) + // UnsafeUnchecked is acceptable because we know we're in the loop here. + promise.assumeIsolatedUnsafeUnchecked().succeed(try task()) } catch let err { promise.fail(err) } @@ -159,7 +160,7 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { /// - see: `EventLoop.scheduleTask(deadline:_:)` @discardableResult @preconcurrency - public func scheduleTask( + public func scheduleTask( deadline: NIODeadline, _ task: @escaping @Sendable () throws -> T ) -> Scheduled { @@ -201,7 +202,7 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { /// - see: `EventLoop.scheduleTask(in:_:)` @discardableResult @preconcurrency - public func scheduleTask(in: TimeAmount, _ task: @escaping @Sendable () throws -> T) -> Scheduled { + public func scheduleTask(in: TimeAmount, _ task: @escaping @Sendable () throws -> T) -> Scheduled { self.scheduleTask(deadline: self.now + `in`, task) } diff --git a/Sources/NIOHTTP1/HTTPDecoder.swift b/Sources/NIOHTTP1/HTTPDecoder.swift index a38c775cf..763319891 100644 --- a/Sources/NIOHTTP1/HTTPDecoder.swift +++ b/Sources/NIOHTTP1/HTTPDecoder.swift @@ -12,9 +12,14 @@ // //===----------------------------------------------------------------------===// -@_implementationOnly import CNIOLLHTTP import NIOCore +#if compiler(>=6.1) +private import CNIOLLHTTP +#else +@_implementationOnly import CNIOLLHTTP +#endif + extension UnsafeMutablePointer where Pointee == llhttp_t { /// Returns the `KeepAliveState` for the current message that is parsed. fileprivate var keepAliveState: KeepAliveState { @@ -631,7 +636,7 @@ public final class HTTPDecoder: ByteToMessageDecoder, HTTPDecoderDelega self.url = String(decoding: bytes, as: Unicode.UTF8.self) } - func didFinishHead( + fileprivate func didFinishHead( versionMajor: Int, versionMinor: Int, isUpgrade: Bool, @@ -815,7 +820,7 @@ extension HTTPParserError { /// - Parameter fromCHTTPParserErrno: The error from the underlying library. /// - Returns: The corresponding `HTTPParserError`, or `nil` if there is no /// corresponding error. - static func httpError(fromCHTTPParserErrno: llhttp_errno_t) -> HTTPParserError? { + fileprivate static func httpError(fromCHTTPParserErrno: llhttp_errno_t) -> HTTPParserError? { switch fromCHTTPParserErrno { case HPE_INTERNAL: return .invalidInternalState @@ -874,7 +879,7 @@ extension HTTPMethod { /// /// - Parameter httpParserMethod: The method returned by `http_parser`. /// - Returns: The corresponding `HTTPMethod`. - static func from(httpParserMethod: llhttp_method) -> HTTPMethod { + fileprivate static func from(httpParserMethod: llhttp_method) -> HTTPMethod { switch httpParserMethod { case HTTP_DELETE: return .DELETE diff --git a/Sources/NIOPosix/DatagramVectorReadManager.swift b/Sources/NIOPosix/DatagramVectorReadManager.swift index 7f6ced454..88224bf1d 100644 --- a/Sources/NIOPosix/DatagramVectorReadManager.swift +++ b/Sources/NIOPosix/DatagramVectorReadManager.swift @@ -11,6 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// +import CNIODarwin import CNIOLinux import NIOCore diff --git a/Sources/NIOPosix/PendingDatagramWritesManager.swift b/Sources/NIOPosix/PendingDatagramWritesManager.swift index 8c058bd4e..e9f201ce9 100644 --- a/Sources/NIOPosix/PendingDatagramWritesManager.swift +++ b/Sources/NIOPosix/PendingDatagramWritesManager.swift @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// import Atomics +import CNIODarwin import CNIOLinux import NIOCore diff --git a/Sources/NIOPosix/SelectableEventLoop.swift b/Sources/NIOPosix/SelectableEventLoop.swift index 9449d6297..04e0cc0bb 100644 --- a/Sources/NIOPosix/SelectableEventLoop.swift +++ b/Sources/NIOPosix/SelectableEventLoop.swift @@ -60,7 +60,7 @@ public protocol NIOEventLoopMetricsDelegate: Sendable { /// The whole processing of I/O and tasks is done by a `NIOThread` that is tied to the `SelectableEventLoop`. This `NIOThread` /// is guaranteed to never change! @usableFromInline -internal final class SelectableEventLoop: EventLoop { +internal final class SelectableEventLoop: EventLoop, @unchecked Sendable { static let strictModeEnabled: Bool = { switch getenv("SWIFTNIO_STRICT").map({ String.init(cString: $0).lowercased() }) { diff --git a/Sources/NIOPosix/SelectorKqueue.swift b/Sources/NIOPosix/SelectorKqueue.swift index 4c45dd159..34651bd49 100644 --- a/Sources/NIOPosix/SelectorKqueue.swift +++ b/Sources/NIOPosix/SelectorKqueue.swift @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +import NIOConcurrencyHelpers import NIOCore #if canImport(Darwin) diff --git a/Tests/NIOPosixTests/HappyEyeballsTest.swift b/Tests/NIOPosixTests/HappyEyeballsTest.swift index 29f35820f..7e91b60a6 100644 --- a/Tests/NIOPosixTests/HappyEyeballsTest.swift +++ b/Tests/NIOPosixTests/HappyEyeballsTest.swift @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +import CNIOLinux import NIOEmbedded import XCTest diff --git a/Tests/NIOPosixTests/MulticastTest.swift b/Tests/NIOPosixTests/MulticastTest.swift index c9ae19d29..e2fe3a8b0 100644 --- a/Tests/NIOPosixTests/MulticastTest.swift +++ b/Tests/NIOPosixTests/MulticastTest.swift @@ -11,7 +11,7 @@ // SPDX-License-Identifier: Apache-2.0 // //===----------------------------------------------------------------------===// - +import CNIOLinux import NIOCore import NIOPosix import XCTest diff --git a/Tests/NIOPosixTests/PendingDatagramWritesManagerTests.swift b/Tests/NIOPosixTests/PendingDatagramWritesManagerTests.swift index b94e37cf9..dee71c2a6 100644 --- a/Tests/NIOPosixTests/PendingDatagramWritesManagerTests.swift +++ b/Tests/NIOPosixTests/PendingDatagramWritesManagerTests.swift @@ -12,6 +12,7 @@ // //===----------------------------------------------------------------------===// +import CNIODarwin import CNIOLinux import NIOEmbedded import XCTest