Skip to content

Commit 8232001

Browse files
authored
Merge pull request swiftlang#10416 from itaiferber/master
Add unit test to confirm ordering of Codable diags
2 parents 65391cc + 1c020fc commit 8232001

File tree

3 files changed

+244
-1
lines changed

3 files changed

+244
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2049,7 +2049,7 @@ ERROR(broken_decodable_requirement,none,
20492049
"Decodable protocol is broken: unexpected requirement", ())
20502050

20512051
NOTE(codable_extraneous_codingkey_case_here,none,
2052-
"CodingKey case %0 does match any stored properties", (Identifier))
2052+
"CodingKey case %0 does not match any stored properties", (Identifier))
20532053
NOTE(codable_non_conforming_property_here,none,
20542054
"cannot automatically synthesize %0 because %1 does not conform to %0", (Type, Type))
20552055
NOTE(codable_non_decoded_property_here,none,
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 struct with non-Codable property.
4+
struct S1 : Codable {
5+
struct Nested {}
6+
var a: String = ""
7+
var b: Int = 0
8+
var c: Nested = Nested()
9+
10+
// CHECK: error: type 'S1' does not conform to protocol 'Decodable'
11+
// CHECK: note: cannot automatically synthesize 'Decodable' because 'S1.Nested' does not conform to 'Decodable'
12+
// CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable'
13+
14+
// CHECK: error: type 'S1' does not conform to protocol 'Encodable'
15+
// CHECK: note: cannot automatically synthesize 'Encodable' because 'S1.Nested' does not conform to 'Encodable'
16+
// CHECK: note: protocol requires function 'encode(to:)' with type 'Encodable'
17+
}
18+
19+
// Codable struct with non-enum CodingKeys.
20+
struct S2 : Codable {
21+
var a: String = ""
22+
var b: Int = 0
23+
var c: Double?
24+
25+
struct CodingKeys : CodingKey {
26+
var stringValue: String = ""
27+
var intValue: Int? = nil
28+
init?(stringValue: String) {}
29+
init?(intValue: Int) {}
30+
}
31+
32+
// CHECK: error: type 'S2' 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 'S2' 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 struct with CodingKeys not conforming to CodingKey.
42+
struct S3 : 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 'S3' 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 'S3' 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 struct with extraneous CodingKeys
63+
struct S4 : 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 'S4' 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 'S4' 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 struct with non-decoded property (which has no default value).
91+
struct S5 : 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: type 'S5' does not conform to protocol 'Decodable'
102+
// CHECK: note: cannot automatically synthesize 'Decodable' because 'b' does not have a matching CodingKey and does not have a default value
103+
// CHECK: note: protocol requires initializer 'init(from:)' with type 'Decodable'
104+
}
105+
106+
// Structs cannot yet synthesize Encodable or Decodable in extensions.
107+
struct S6 {}
108+
extension S6 : Codable {}
109+
// CHECK: error: implementation of 'Decodable' cannot be automatically synthesized in an extension yet
110+
// CHECK: error: implementation of 'Encodable' cannot be automatically synthesized in an extension yet

0 commit comments

Comments
 (0)