Skip to content

Commit 3b9b540

Browse files
committed
Clean up modeling of CodingKeysValidity
The quad-state here is completely superfluous. Break it down into an enum class that encodes the relevant cases we care about and clean up its sole caller.
1 parent 05f418a commit 3b9b540

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static Identifier getVarNameForCoding(VarDecl *var) {
7575
/// match with the stored vars of the given type.
7676
///
7777
/// \param codingKeysDecl The \c CodingKeys enum decl to validate.
78-
static bool validateCodingKeysEnum(DerivedConformance &derived,
78+
static bool validateCodingKeysEnum(const DerivedConformance &derived,
7979
EnumDecl *codingKeysDecl) {
8080
auto conformanceDC = derived.getConformanceContext();
8181

@@ -160,11 +160,14 @@ static bool validateCodingKeysEnum(DerivedConformance &derived,
160160
}
161161

162162
/// A type which has information about the validity of an encountered
163-
/// CodingKeys type.
164-
struct CodingKeysValidity {
165-
bool hasType;
166-
bool isValid;
167-
CodingKeysValidity(bool ht, bool iv) : hasType(ht), isValid(iv) {}
163+
/// \c CodingKeys type.
164+
enum class CodingKeysClassification {
165+
/// A \c CodingKeys declaration was found, but it is invalid.
166+
Invalid,
167+
/// No \c CodingKeys declaration was found, so it must be synthesized.
168+
NeedsSynthesizedCodingKeys,
169+
/// A valid \c CodingKeys declaration was found.
170+
Valid,
168171
};
169172

170173
/// Returns whether the given type has a valid nested \c CodingKeys enum.
@@ -176,12 +179,14 @@ struct CodingKeysValidity {
176179
/// enum.
177180
///
178181
/// \returns A \c CodingKeysValidity value representing the result of the check.
179-
static CodingKeysValidity hasValidCodingKeysEnum(DerivedConformance &derived) {
182+
static CodingKeysClassification
183+
classifyCodingKeys(const DerivedConformance &derived) {
180184
auto &C = derived.Context;
181185
auto codingKeysDecls =
182186
derived.Nominal->lookupDirect(DeclName(C.Id_CodingKeys));
183-
if (codingKeysDecls.empty())
184-
return CodingKeysValidity(/*hasType=*/false, /*isValid=*/true);
187+
if (codingKeysDecls.empty()) {
188+
return CodingKeysClassification::NeedsSynthesizedCodingKeys;
189+
}
185190

186191
// Only ill-formed code would produce multiple results for this lookup.
187192
// This would get diagnosed later anyway, so we're free to only look at the
@@ -192,7 +197,7 @@ static CodingKeysValidity hasValidCodingKeysEnum(DerivedConformance &derived) {
192197
if (!codingKeysTypeDecl) {
193198
result->diagnose(diag::codable_codingkeys_type_is_not_an_enum_here,
194199
derived.getProtocolType());
195-
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/false);
200+
return CodingKeysClassification::Invalid;
196201
}
197202

198203
// CodingKeys may be a typealias. If so, follow the alias to its canonical
@@ -216,7 +221,7 @@ static CodingKeysValidity hasValidCodingKeysEnum(DerivedConformance &derived) {
216221
C.Diags.diagnose(loc, diag::codable_codingkeys_type_does_not_conform_here,
217222
derived.getProtocolType());
218223

219-
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/false);
224+
return CodingKeysClassification::Invalid;
220225
}
221226

222227
// CodingKeys must be an enum for synthesized conformance.
@@ -225,11 +230,12 @@ static CodingKeysValidity hasValidCodingKeysEnum(DerivedConformance &derived) {
225230
codingKeysTypeDecl->diagnose(
226231
diag::codable_codingkeys_type_is_not_an_enum_here,
227232
derived.getProtocolType());
228-
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/false);
233+
return CodingKeysClassification::Invalid;
229234
}
230235

231-
bool valid = validateCodingKeysEnum(derived, codingKeysEnum);
232-
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/valid);
236+
return validateCodingKeysEnum(derived, codingKeysEnum)
237+
? CodingKeysClassification::Valid
238+
: CodingKeysClassification::Invalid;
233239
}
234240

235241
/// Synthesizes a new \c CodingKeys enum based on the {En,De}codable members of
@@ -1071,19 +1077,18 @@ static bool canSynthesize(DerivedConformance &derived, ValueDecl *requirement) {
10711077

10721078
// If the target already has a valid CodingKeys enum, we won't need to
10731079
// synthesize one.
1074-
auto validity = hasValidCodingKeysEnum(derived);
1075-
1076-
// We found a type, but it wasn't valid.
1077-
if (!validity.isValid)
1080+
switch (classifyCodingKeys(derived)) {
1081+
case CodingKeysClassification::Invalid:
10781082
return false;
1079-
1080-
// We can try to synthesize a type here.
1081-
if (!validity.hasType) {
1083+
case CodingKeysClassification::NeedsSynthesizedCodingKeys: {
10821084
auto *synthesizedEnum = synthesizeCodingKeysEnum(derived);
10831085
if (!synthesizedEnum)
10841086
return false;
1087+
}
1088+
LLVM_FALLTHROUGH;
1089+
case CodingKeysClassification::Valid:
1090+
return true;
10851091
}
1086-
10871092
return true;
10881093
}
10891094

0 commit comments

Comments
 (0)