mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[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:
@@ -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
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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())() }
|
||||
|
||||
@@ -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>()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user