|
| 1 | +// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -parse-as-library -swift-version 4 %s 2>&1 | %FileCheck %s |
| 2 | + |
| 3 | +// Codable class with non-Codable property. |
| 4 | +class C1 : Codable { |
| 5 | + class Nested {} |
| 6 | + var a: String = "" |
| 7 | + var b: Int = 0 |
| 8 | + var c: Nested = Nested() |
| 9 | + |
| 10 | + // CHECK: error: type 'C1' does not conform to protocol 'Decodable' |
| 11 | + // CHECK: note: cannot automatically synthesize 'Decodable' because 'C1.Nested' does not conform to 'Decodable' |
| 12 | + // CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable' |
| 13 | + |
| 14 | + // CHECK: error: type 'C1' does not conform to protocol 'Encodable' |
| 15 | + // CHECK: note: cannot automatically synthesize 'Encodable' because 'C1.Nested' does not conform to 'Encodable' |
| 16 | + // CHECK: note: protocol requires function 'encode(to:)' with type 'Encodable' |
| 17 | +} |
| 18 | + |
| 19 | +// Codable class with non-enum CodingKeys. |
| 20 | +class C2 : Codable { |
| 21 | + var a: String = "" |
| 22 | + var b: Int = 0 |
| 23 | + var c: Double? |
| 24 | + |
| 25 | + struct CodingKeys : CodingKey { |
| 26 | + var stringValue = "" |
| 27 | + var intValue = nil |
| 28 | + init?(stringValue: String) {} |
| 29 | + init?(intValue: Int) {} |
| 30 | + } |
| 31 | + |
| 32 | + // CHECK: error: type 'C2' does not conform to protocol 'Decodable' |
| 33 | + // CHECK: note: cannot automatically synthesize 'Decodable' because 'CodingKeys' is not an enum |
| 34 | + // CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable' |
| 35 | + |
| 36 | + // CHECK: error: type 'C2' does not conform to protocol 'Encodable' |
| 37 | + // CHECK: note: cannot automatically synthesize 'Encodable' because 'CodingKeys' is not an enum |
| 38 | + // CHECK: note: protocol requires function 'encode(to:)' with type 'Encodable' |
| 39 | +} |
| 40 | + |
| 41 | +// Codable class with CodingKeys not conforming to CodingKey. |
| 42 | +class C3 : Codable { |
| 43 | + var a: String = "" |
| 44 | + var b: Int = 0 |
| 45 | + var c: Double? |
| 46 | + |
| 47 | + enum CodingKeys : String { |
| 48 | + case a |
| 49 | + case b |
| 50 | + case c |
| 51 | + } |
| 52 | + |
| 53 | + // CHECK: error: type 'C3' does not conform to protocol 'Decodable' |
| 54 | + // CHECK: note: cannot automatically synthesize 'Decodable' because 'CodingKeys' does not conform to CodingKey |
| 55 | + // CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable' |
| 56 | + |
| 57 | + // CHECK: error: type 'C3' does not conform to protocol 'Encodable' |
| 58 | + // CHECK: note: cannot automatically synthesize 'Encodable' because 'CodingKeys' does not conform to CodingKey |
| 59 | + // CHECK: note: protocol requires function 'encode(to:)' with type 'Encodable' |
| 60 | +} |
| 61 | + |
| 62 | +// Codable class with extraneous CodingKeys |
| 63 | +class C4 : Codable { |
| 64 | + var a: String = "" |
| 65 | + var b: Int = 0 |
| 66 | + var c: Double? |
| 67 | + |
| 68 | + enum CodingKeys : String, CodingKey { |
| 69 | + case a |
| 70 | + case a2 |
| 71 | + case b |
| 72 | + case b2 |
| 73 | + case c |
| 74 | + case c2 |
| 75 | + } |
| 76 | + |
| 77 | + // CHECK: error: type 'C4' does not conform to protocol 'Decodable' |
| 78 | + // CHECK: note: CodingKey case 'a2' does not match any stored properties |
| 79 | + // CHECK: note: CodingKey case 'b2' does not match any stored properties |
| 80 | + // CHECK: note: CodingKey case 'c2' does not match any stored properties |
| 81 | + // CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable' |
| 82 | + |
| 83 | + // CHECK: error: type 'C4' does not conform to protocol 'Encodable' |
| 84 | + // CHECK: note: CodingKey case 'a2' does not match any stored properties |
| 85 | + // CHECK: note: CodingKey case 'b2' does not match any stored properties |
| 86 | + // CHECK: note: CodingKey case 'c2' does not match any stored properties |
| 87 | + // CHECK: note: protocol requires function 'encode(to:)' with type 'Encodable' |
| 88 | +} |
| 89 | + |
| 90 | +// Codable class with non-decoded property (which has no default value). |
| 91 | +class C5 : Codable { |
| 92 | + var a: String = "" |
| 93 | + var b: Int |
| 94 | + var c: Double? |
| 95 | + |
| 96 | + enum CodingKeys : String, CodingKey { |
| 97 | + case a |
| 98 | + case c |
| 99 | + } |
| 100 | + |
| 101 | + // CHECK: error: class 'C5' has no initializers |
| 102 | + // CHECK: note: stored property 'b' without initial value prevents synthesized initializers |
| 103 | + |
| 104 | + // CHECK: error: type 'C5' does not conform to protocol 'Decodable' |
| 105 | + // CHECK: note: cannot automatically synthesize 'Decodable' because 'b' does not have a matching CodingKey and does not have a default value |
| 106 | + // CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable' |
| 107 | +} |
| 108 | + |
| 109 | +// Codable class with non-decoded property (which has no default value). |
| 110 | +class C6 : Codable { |
| 111 | + var a: String = "" |
| 112 | + var b: Int |
| 113 | + var c: Double? |
| 114 | + |
| 115 | + enum CodingKeys : String, CodingKey { |
| 116 | + case a |
| 117 | + case c |
| 118 | + } |
| 119 | + |
| 120 | + init() { |
| 121 | + b = 5 |
| 122 | + } |
| 123 | + |
| 124 | + // CHECK: error: type 'C6' does not conform to protocol 'Decodable' |
| 125 | + // CHECK: note: cannot automatically synthesize 'Decodable' because 'b' does not have a matching CodingKey and does not have a default value |
| 126 | + // CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable' |
| 127 | +} |
| 128 | + |
| 129 | +// Classes cannot yet synthesize Encodable or Decodable in extensions. |
| 130 | +class C7 {} |
| 131 | +extension C7 : Codable {} |
| 132 | +// CHECK: error: implementation of 'Decodable' cannot be automatically synthesized in an extension yet |
| 133 | +// CHECK: error: implementation of 'Encodable' cannot be automatically synthesized in an extension yet |
0 commit comments