@@ -88,6 +88,14 @@ bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
88
88
if (*derivableKind == KnownDerivableProtocolKind::Differentiable)
89
89
return true ;
90
90
91
+ if (*derivableKind == KnownDerivableProtocolKind::Encodable) {
92
+ return canDeriveEncodable (Nominal);
93
+ }
94
+
95
+ if (*derivableKind == KnownDerivableProtocolKind::Decodable) {
96
+ return canDeriveDecodable (Nominal);
97
+ }
98
+
91
99
if (auto *enumDecl = dyn_cast<EnumDecl>(Nominal)) {
92
100
switch (*derivableKind) {
93
101
// The presence of a raw type is an explicit declaration that
@@ -137,31 +145,13 @@ bool DerivedConformance::derivesProtocolConformance(DeclContext *DC,
137
145
default :
138
146
return false ;
139
147
}
140
- } else if (isa<StructDecl>(Nominal) || isa<ClassDecl>(Nominal)) {
141
- // Structs and classes can explicitly derive Encodable and Decodable
142
- // conformance (explicitly meaning we can synthesize an implementation if
143
- // a type conforms manually).
144
- if (*derivableKind == KnownDerivableProtocolKind::Encodable ||
145
- *derivableKind == KnownDerivableProtocolKind::Decodable) {
146
- // FIXME: This is not actually correct. We cannot promise to always
147
- // provide a witness here for all structs and classes. Unfortunately,
148
- // figuring out whether this is actually possible requires much more
149
- // context -- a TypeChecker and the parent decl context at least -- and is
150
- // tightly coupled to the logic within DerivedConformance.
151
- // This unfortunately means that we expect a witness even if one will not
152
- // be produced, which requires DerivedConformance::deriveCodable to output
153
- // its own diagnostics.
154
- return true ;
155
- }
156
-
157
- // Structs can explicitly derive Equatable conformance.
158
- if (isa<StructDecl>(Nominal)) {
159
- switch (*derivableKind) {
160
- case KnownDerivableProtocolKind::Equatable:
161
- return canDeriveEquatable (DC, Nominal);
162
- default :
163
- return false ;
164
- }
148
+ } else if (isa<StructDecl>(Nominal)) {
149
+ switch (*derivableKind) {
150
+ case KnownDerivableProtocolKind::Equatable:
151
+ // Structs can explicitly derive Equatable conformance.
152
+ return canDeriveEquatable (DC, Nominal);
153
+ default :
154
+ return false ;
165
155
}
166
156
}
167
157
return false ;
0 commit comments