[stdlib] Implement Never conformance to Codable (#64899)

Proposed as SE-0396: Conform Never to Codable;
approved on 5/5/2023.
This commit is contained in:
Nate Cook
2023-05-09 13:10:10 -05:00
committed by GitHub
parent a2c4c578c2
commit 89b9e48eae
4 changed files with 47 additions and 0 deletions

View File

@@ -75,6 +75,23 @@ extension Never: Identifiable {
}
}
@available(SwiftStdlib 5.9, *)
extension Never: Encodable {
@available(SwiftStdlib 5.9, *)
public func encode(to encoder: any Encoder) throws {}
}
@available(SwiftStdlib 5.9, *)
extension Never: Decodable {
@available(SwiftStdlib 5.9, *)
public init(from decoder: any Decoder) throws {
let context = DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Unable to decode an instance of Never.")
throw DecodingError.typeMismatch(Never.self, context)
}
}
//===----------------------------------------------------------------------===//
// Standardized aliases
//===----------------------------------------------------------------------===//

View File

@@ -73,6 +73,8 @@ Protocol Error has added inherited protocol Sendable
Protocol Error has generic signature change from to <Self : Swift.Sendable>
Constructor _SmallString.init(taggedCocoa:) has mangled name changing from 'Swift._SmallString.init(taggedCocoa: Swift.AnyObject) -> Swift._SmallString' to 'Swift._SmallString.init(taggedCocoa: Swift.AnyObject) -> Swift.Optional<Swift._SmallString>'
Constructor _SmallString.init(taggedCocoa:) has return type change from Swift._SmallString to Swift._SmallString?
Enum Never has added a conformance to an existing protocol Decodable
Enum Never has added a conformance to an existing protocol Encodable
Enum Never has added a conformance to an existing protocol Identifiable
// These haven't actually been removed; they are simply marked unavailable.

View File

@@ -669,6 +669,20 @@ class TestCodable : TestCodableSuper {
}
}
// MARK: - Never
@available(SwiftStdlib 5.9, *)
func test_Never() {
struct Nope: Codable {
var no: Never
}
do {
let neverJSON = Data(#"{"no":"never"}"#.utf8)
_ = try JSONDecoder().decode(Nope.self, from: neverJSON)
fatalError("Incorrectly decoded `Never` instance.")
} catch {}
}
// MARK: - NSRange
lazy var nsrangeValues: [Int : NSRange] = [
#line : NSRange(),
@@ -1058,6 +1072,10 @@ if #available(SwiftStdlib 5.6, *) {
tests["test_Dictionary_JSON"] = TestCodable.test_Dictionary_JSON
}
if #available(SwiftStdlib 5.9, *) {
tests["test_Never"] = TestCodable.test_Never
}
var CodableTests = TestSuite("TestCodable")
for (name, test) in tests {
CodableTests.test(name) { test(TestCodable())() }

View File

@@ -11,3 +11,13 @@ _ = ConformsToComparable<Never>()
struct ConformsToHashable<T: Hashable> {}
_ = ConformsToHashable<Never>()
if #available(SwiftStdlib 5.5, *) {
struct ConformsToIdentifiable<T: Identifiable> {}
_ = ConformsToIdentifiable<Never>()
}
if #available(SwiftStdlib 5.9, *) {
struct ConformsToCodable<T: Codable> {}
_ = ConformsToCodable<Never>()
}