Skip to content

Commit f94d394

Browse files
[DerivedConformance] Skipping validation of var declaration conformance for error types.
1 parent d423887 commit f94d394

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ static CodableConformanceType typeConformsToCodable(DeclContext *context,
9999
/// \param proto The \c ProtocolDecl to check conformance to.
100100
static CodableConformanceType
101101
varConformsToCodable(DeclContext *DC, VarDecl *varDecl, ProtocolDecl *proto) {
102-
auto declInterfaceType = varDecl->getValueInterfaceType();
103-
if (declInterfaceType->hasError()) {
104-
return CodableConformanceType::TypeNotValidated;
105-
}
106-
107102
// If the decl doesn't yet have a type, we may be seeing it before the type
108103
// checker has gotten around to evaluating its type. For example:
109104
//
@@ -117,7 +112,8 @@ varConformsToCodable(DeclContext *DC, VarDecl *varDecl, ProtocolDecl *proto) {
117112
// // hasn't yet been evaluated
118113
// }
119114
bool isIUO = varDecl->isImplicitlyUnwrappedOptional();
120-
return typeConformsToCodable(DC, declInterfaceType, isIUO, proto);
115+
return typeConformsToCodable(DC, varDecl->getValueInterfaceType(), isIUO,
116+
proto);
121117
}
122118

123119
/// Retrieve the variable name for the purposes of encoding/decoding.
@@ -180,10 +176,18 @@ static bool validateCodingKeysEnum(DerivedConformance &derived,
180176
propertiesAreValid = false;
181177
continue;
182178
}
183-
179+
180+
auto varDecl = it->second;
181+
182+
// If the element declaration type has an error, let's skip conformance
183+
// validation because this was caused by another problem that will
184+
// produce a diagnostic elsewhere anyway.
185+
if (varDecl->getInterfaceType()->hasError())
186+
continue;
187+
184188
// We have a property to map to. Ensure it's {En,De}codable.
185189
auto conformance =
186-
varConformsToCodable(conformanceDC, it->second, derived.Protocol);
190+
varConformsToCodable(conformanceDC, varDecl, derived.Protocol);
187191
switch (conformance) {
188192
case Conforms:
189193
// The property was valid. Remove it from the list.
@@ -222,9 +226,12 @@ static bool validateCodingKeysEnum(DerivedConformance &derived,
222226
if (!properties.empty() &&
223227
derived.Protocol->isSpecificProtocol(KnownProtocolKind::Decodable)) {
224228
for (auto it = properties.begin(); it != properties.end(); ++it) {
229+
auto *varDecl = it->second;
230+
if (varDecl->getInterfaceType()->hasError())
231+
continue;
232+
225233
// If the var is default initializable, then it need not have an explicit
226234
// initial value.
227-
auto *varDecl = it->second;
228235
if (auto pbd = varDecl->getParentPatternBinding()) {
229236
if (pbd->isDefaultInitializable())
230237
continue;
@@ -361,6 +368,12 @@ static EnumDecl *synthesizeCodingKeysEnum(DerivedConformance &derived) {
361368
for (auto *varDecl : target->getStoredProperties()) {
362369
if (!varDecl->isUserAccessible())
363370
continue;
371+
372+
// If the synthetized element type has an error, let's skip conformance
373+
// validation because this was caused by another problem that will
374+
// produce a diagnostic elsewhere anyway.
375+
if (varDecl->getInterfaceType()->hasError())
376+
continue;
364377

365378
// Despite creating the enum in the context of the type, we're
366379
// concurrently checking the variables for the current protocol

test/decl/protocol/special/coding/iuo_crash.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
// Crash due to codable synthesis with implicitly unwrapped optionals of
44
// ill-formed types.
55
// rdar://problem/60985179
6-
struct X: Codable { // expected-error 2{{type 'X' does not conform to protocol}}
6+
struct X: Codable {
77
var c: Undefined! // expected-error{{cannot find type 'Undefined' in scope}}
88
}

test/decl/protocol/special/coding/struct_codable_simple.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ struct SR_12248_3: Encodable {
4646
var x: Int // expected-error {{invalid redeclaration of 'x'}}
4747
}
4848

49-
struct NotConforms: Codable { // expected-error {{type 'NotConforms' does not conform to protocol 'Decodable'}}
50-
// expected-error@-1 {{type 'NotConforms' does not conform to protocol 'Encodable'}}
49+
struct NotConforms: Codable {
5150
enum CodingKeys: CodingKey {
5251
case x
5352
case y

0 commit comments

Comments
 (0)