Skip to content

Commit f475771

Browse files
committed
Use the API Name of Enum Parameters to Determine Coding Keys
Covers a missing case in codable synthesis for enums with argument payloads that have internal and external labels. When the name of the var decl is used, the internal name of the parameter becomes the key instead of the API name. In this case, this causes an invalid reference to an enum case with the internal name as an argument to be synthesized in the derived Decodable conformance which (hopefully) crashes downstream. rdar://86339848
1 parent 669d00e commit f475771

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ static bool superclassConformsTo(ClassDecl *target, KnownProtocolKind kpk) {
5454
static Identifier getVarNameForCoding(VarDecl *var,
5555
Optional<int> paramIndex = None) {
5656
auto &C = var->getASTContext();
57-
Identifier identifier = var->getName();
57+
Identifier identifier;
58+
if (auto *PD = dyn_cast<ParamDecl>(var)) {
59+
identifier = PD->getArgumentName();
60+
} else {
61+
identifier = var->getName();
62+
}
63+
5864
if (auto originalVar = var->getOriginalWrappedProperty())
5965
identifier = originalVar->getName();
6066

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ enum SimpleEnum : Codable {
66
case a(x: Int, y: Double)
77
case b(z: String)
88
case c(Int, String, b: Bool)
9+
case d(_ inner: Int)
910

1011
// These lines have to be within the SimpleEnum type because CodingKeys
1112
// should be private.
@@ -15,6 +16,7 @@ enum SimpleEnum : Codable {
1516
let _ = SimpleEnum.ACodingKeys.self
1617
let _ = SimpleEnum.BCodingKeys.self
1718
let _ = SimpleEnum.CCodingKeys.self
19+
let _ = SimpleEnum.DCodingKeys.self
1820

1921
// The enum should have a case for each of the cases.
2022
let _ = SimpleEnum.CodingKeys.a
@@ -29,6 +31,8 @@ enum SimpleEnum : Codable {
2931
let _ = SimpleEnum.CCodingKeys._0
3032
let _ = SimpleEnum.CCodingKeys._1
3133
let _ = SimpleEnum.CCodingKeys.b
34+
35+
let _ = SimpleEnum.DCodingKeys._0
3236
}
3337
}
3438

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %target-swift-frontend %s -emit-silgen
2+
3+
struct Boom: Decodable {}
4+
5+
enum Whiz: Decodable {
6+
case bang(_ boom: Boom)
7+
}

0 commit comments

Comments
 (0)