From 87dbd0216c47ea2e7ddb1b545271b716e03b943e Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Thu, 28 Feb 2019 11:55:37 +0000 Subject: [PATCH] fix new Swift 5 warnings for NIO 1 (#850) Motivation: Swift 5 brought a bunch of new warnings for NIO 1. Modifications: fix the warnings Result: happier compiles --- Sources/NIO/BaseSocket.swift | 2 +- Sources/NIO/ByteBuffer-core.swift | 11 ++++++----- Sources/NIO/IntegerTypes.swift | 15 ++++++++++++--- Tests/NIOTests/ChannelPipelineTest.swift | 14 ++++++-------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/Sources/NIO/BaseSocket.swift b/Sources/NIO/BaseSocket.swift index b19f9a23b0..44a04ee4b1 100644 --- a/Sources/NIO/BaseSocket.swift +++ b/Sources/NIO/BaseSocket.swift @@ -365,7 +365,7 @@ class BaseSocket: Selectable { let storage = UnsafeMutableRawBufferPointer.allocate(byteCount: MemoryLayout.stride, alignment: MemoryLayout.alignment) // write zeroes into the memory as Linux's getsockopt doesn't zero them out - storage.initializeMemory(as: UInt8.self, repeating: 0) + _ = storage.initializeMemory(as: UInt8.self, repeating: 0) var val = storage.bindMemory(to: T.self).baseAddress! // initialisation will be done by getsockopt defer { diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index b48770e061..bcfb777af5 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -29,7 +29,7 @@ let sysFree: @convention(c) (UnsafeMutableRawPointer?) -> Void = free internal func initializeMemory(as type: T.Type, repeating repeatedValue: T) -> UnsafeMutableBufferPointer { let ptr = self.bindMemory(to: T.self) - ptr.initialize(from: repeatElement(repeatedValue, count: self.count / MemoryLayout.stride)) + _ = ptr.initialize(from: repeatElement(repeatedValue, count: self.count / MemoryLayout.stride)) return ptr } @@ -59,6 +59,7 @@ let sysFree: @convention(c) (UnsafeMutableRawPointer?) -> Void = free #endif extension _ByteBufferSlice: Equatable { + @_versioned static func ==(_ lhs: _ByteBufferSlice, _ rhs: _ByteBufferSlice) -> Bool { return lhs._begin == rhs._begin && lhs.upperBound == rhs.upperBound } @@ -237,10 +238,10 @@ public struct ByteBuffer { public typealias _Index = UInt32 public typealias _Capacity = UInt32 - @_versioned private(set) var _storage: _Storage - @_versioned private(set) var _readerIndex: _Index = 0 - @_versioned private(set) var _writerIndex: _Index = 0 - @_versioned private(set) var _slice: Slice + @_versioned var _storage: _Storage + @_versioned var _readerIndex: _Index = 0 + @_versioned var _writerIndex: _Index = 0 + @_versioned var _slice: Slice // MARK: Internal _Storage for CoW @_versioned final class _Storage { diff --git a/Sources/NIO/IntegerTypes.swift b/Sources/NIO/IntegerTypes.swift index 3a19780eb4..940f29499f 100644 --- a/Sources/NIO/IntegerTypes.swift +++ b/Sources/NIO/IntegerTypes.swift @@ -17,24 +17,31 @@ /// A 24-bit unsigned integer value type. @_versioned struct _UInt24: ExpressibleByIntegerLiteral { +#if swift(>=4.1.50) // Swift 4.2 or better compiler in Swift 4 mode + @_versioned typealias IntegerLiteralType = UInt16 +#else + typealias IntegerLiteralType = UInt16 +#endif @_versioned var b12: UInt16 @_versioned var b3: UInt8 - private init(b12: UInt16, b3: UInt8) { + @_versioned + init(_b12 b12: UInt16, b3: UInt8) { self.b12 = b12 self.b3 = b3 } + @_versioned init(integerLiteral value: UInt16) { - self.init(b12: value, b3: 0) + self.init(_b12: value, b3: 0) } static let bitWidth: Int = 24 static var max: _UInt24 { - return .init(b12: .max, b3: .max) + return .init(_b12: .max, b3: .max) } static let min: _UInt24 = 0 @@ -67,12 +74,14 @@ extension _UInt24 { } extension _UInt24: Equatable { + @_versioned static func ==(_ lhs: _UInt24, _ rhs: _UInt24) -> Bool { return lhs.b12 == rhs.b12 && lhs.b3 == rhs.b3 } } extension _UInt24: CustomStringConvertible { + @_versioned var description: String { return Int(self).description } diff --git a/Tests/NIOTests/ChannelPipelineTest.swift b/Tests/NIOTests/ChannelPipelineTest.swift index f5f36ca809..7104c98386 100644 --- a/Tests/NIOTests/ChannelPipelineTest.swift +++ b/Tests/NIOTests/ChannelPipelineTest.swift @@ -122,21 +122,19 @@ class ChannelPipelineTest: XCTestCase { XCTAssertFalse(try channel.finish()) } - func testConnectingDoesntCallBind() throws { + func testConnectingDoesntCallBind() { let channel = EmbeddedChannel() var ipv4SocketAddress = sockaddr_in() ipv4SocketAddress.sin_port = (12345 as UInt16).bigEndian let sa = SocketAddress(ipv4SocketAddress, host: "foobar.com") - _ = try channel.pipeline.add(handler: NoBindAllowed()).wait() - _ = try channel.pipeline.add(handler: TestChannelOutboundHandler { data in + XCTAssertNoThrow(try channel.pipeline.add(handler: NoBindAllowed()).wait()) + XCTAssertNoThrow(try channel.pipeline.add(handler: TestChannelOutboundHandler { data in data - }).wait() + }).wait()) - _ = try channel.connect(to: sa).wait() - defer { - XCTAssertFalse(try channel.finish()) - } + XCTAssertNoThrow(try channel.connect(to: sa).wait()) + XCTAssertNoThrow(XCTAssertFalse(try channel.finish())) } private final class TestChannelOutboundHandler: ChannelOutboundHandler {