Skip to content

Commit 89b9e48

Browse files
authored
[stdlib] Implement Never conformance to Codable (swiftlang#64899)
Proposed as SE-0396: Conform Never to Codable; approved on 5/5/2023.
1 parent a2c4c57 commit 89b9e48

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Diff for: stdlib/public/core/Policy.swift

+17
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ extension Never: Identifiable {
7575
}
7676
}
7777

78+
@available(SwiftStdlib 5.9, *)
79+
extension Never: Encodable {
80+
@available(SwiftStdlib 5.9, *)
81+
public func encode(to encoder: any Encoder) throws {}
82+
}
83+
84+
@available(SwiftStdlib 5.9, *)
85+
extension Never: Decodable {
86+
@available(SwiftStdlib 5.9, *)
87+
public init(from decoder: any Decoder) throws {
88+
let context = DecodingError.Context(
89+
codingPath: decoder.codingPath,
90+
debugDescription: "Unable to decode an instance of Never.")
91+
throw DecodingError.typeMismatch(Never.self, context)
92+
}
93+
}
94+
7895
//===----------------------------------------------------------------------===//
7996
// Standardized aliases
8097
//===----------------------------------------------------------------------===//

Diff for: test/api-digester/stability-stdlib-abi-without-asserts.test

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Protocol Error has added inherited protocol Sendable
7373
Protocol Error has generic signature change from to <Self : Swift.Sendable>
7474
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>'
7575
Constructor _SmallString.init(taggedCocoa:) has return type change from Swift._SmallString to Swift._SmallString?
76+
Enum Never has added a conformance to an existing protocol Decodable
77+
Enum Never has added a conformance to an existing protocol Encodable
7678
Enum Never has added a conformance to an existing protocol Identifiable
7779

7880
// These haven't actually been removed; they are simply marked unavailable.

Diff for: test/stdlib/CodableTests.swift

+18
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,20 @@ class TestCodable : TestCodableSuper {
669669
}
670670
}
671671

672+
// MARK: - Never
673+
@available(SwiftStdlib 5.9, *)
674+
func test_Never() {
675+
struct Nope: Codable {
676+
var no: Never
677+
}
678+
679+
do {
680+
let neverJSON = Data(#"{"no":"never"}"#.utf8)
681+
_ = try JSONDecoder().decode(Nope.self, from: neverJSON)
682+
fatalError("Incorrectly decoded `Never` instance.")
683+
} catch {}
684+
}
685+
672686
// MARK: - NSRange
673687
lazy var nsrangeValues: [Int : NSRange] = [
674688
#line : NSRange(),
@@ -1058,6 +1072,10 @@ if #available(SwiftStdlib 5.6, *) {
10581072
tests["test_Dictionary_JSON"] = TestCodable.test_Dictionary_JSON
10591073
}
10601074

1075+
if #available(SwiftStdlib 5.9, *) {
1076+
tests["test_Never"] = TestCodable.test_Never
1077+
}
1078+
10611079
var CodableTests = TestSuite("TestCodable")
10621080
for (name, test) in tests {
10631081
CodableTests.test(name) { test(TestCodable())() }

Diff for: test/stdlib/Never.swift

+10
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ _ = ConformsToComparable<Never>()
1111

1212
struct ConformsToHashable<T: Hashable> {}
1313
_ = ConformsToHashable<Never>()
14+
15+
if #available(SwiftStdlib 5.5, *) {
16+
struct ConformsToIdentifiable<T: Identifiable> {}
17+
_ = ConformsToIdentifiable<Never>()
18+
}
19+
20+
if #available(SwiftStdlib 5.9, *) {
21+
struct ConformsToCodable<T: Codable> {}
22+
_ = ConformsToCodable<Never>()
23+
}

0 commit comments

Comments
 (0)