Skip to content
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

[stdlib] Implement Never conformance to Codable #64899

Merged
merged 5 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions stdlib/public/core/Policy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ extension Never: Identifiable {
}
}

@available(SwiftStdlib 5.9, *)
extension Never: Encodable {
@available(SwiftStdlib 5.9, *)
natecook1000 marked this conversation as resolved.
Show resolved Hide resolved
public func encode(to: Encoder) throws {}
natecook1000 marked this conversation as resolved.
Show resolved Hide resolved
}

@available(SwiftStdlib 5.9, *)
extension Never: Decodable {
@available(SwiftStdlib 5.9, *)
public init(from decoder: Decoder) throws {
natecook1000 marked this conversation as resolved.
Show resolved Hide resolved
let context = DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "Unable to decode an instance of Never.")
throw DecodingError.dataCorrupted(context)
}
}

//===----------------------------------------------------------------------===//
// Standardized aliases
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 2 additions & 0 deletions test/api-digester/stability-stdlib-abi-without-asserts.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,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.
Expand Down
18 changes: 18 additions & 0 deletions test/stdlib/CodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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())() }
Expand Down
10 changes: 10 additions & 0 deletions test/stdlib/Never.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>()
}