@@ -459,6 +459,15 @@ static bool validateCaseCodingKeysEnum(const DerivedConformance &derived,
459
459
? addImplicitCaseCodingKeys (
460
460
enumDecl, elementDecl, codingKeysEnum)
461
461
: caseCodingKeysDecls.front ();
462
+
463
+ if (!result) {
464
+ // There is no coding key defined for this element,
465
+ // which is okay, because not all elements have to
466
+ // be considered for serialization. Attempts to
467
+ // en-/decode them will be handled at runtime.
468
+ return true ;
469
+ }
470
+
462
471
auto *codingKeysTypeDecl = dyn_cast<TypeDecl>(result);
463
472
if (!codingKeysTypeDecl) {
464
473
result->diagnose (diag::codable_codingkeys_type_is_not_an_enum_here,
@@ -625,7 +634,7 @@ static ThrowStmt *createThrowCodingErrorStmt(ASTContext &C, Expr *containerExpr,
625
634
assert (contextDecl && " Missing Context decl." );
626
635
627
636
auto *debugMessageExpr = new (C) StringLiteralExpr (
628
- StringRef ( " Invalid number of keys found, expected one. " ), SourceRange (),
637
+ C. AllocateCopy (debugMessage ), SourceRange (),
629
638
/* Implicit */ true );
630
639
631
640
auto *contextTypeExpr =
@@ -1069,14 +1078,14 @@ deriveBodyEncodable_enum_encode(AbstractFunctionDecl *encodeDecl, void *) {
1069
1078
// This case should not be encodable, so throw an error if an attempt
1070
1079
// is made to encode it
1071
1080
auto debugMessage =
1072
- " Case '" + elt->getBaseIdentifier ().str () +
1073
- " ' cannot be decoded because it is not defined in CodingKeys." ;
1081
+ " Case '" + elt->getBaseIdentifier ().str (). str () +
1082
+ " ' cannot be encoded because it is not defined in CodingKeys." ;
1074
1083
auto *selfRefExpr = new (C) DeclRefExpr (
1075
1084
ConcreteDeclRef (selfRef), DeclNameLoc (), /* Implicit */ true );
1076
1085
1077
1086
auto *throwStmt = createThrowCodingErrorStmt (
1078
1087
C, containerExpr, C.getEncodingErrorDecl (), C.Id_invalidValue ,
1079
- selfRefExpr, debugMessage. str () );
1088
+ selfRefExpr, debugMessage);
1080
1089
1081
1090
caseStatements.push_back (throwStmt);
1082
1091
} else {
0 commit comments