Skip to content

Commit 8e0a260

Browse files
authored
[Sema]: Add Codable synthesis for enums with associated values (swiftlang#34855)
* [Sema]: Add Codable synthesis for enums with associated values * Incorporate review feedback for enum Codable synthesis * Implement enum specific versions of existing Codable tests * Encode parameterless enum cases as * Add test for overloaded case identifiers * Align code generation with latest proposal revision * Put enum codable derivation behind flag * clang-format sources * Address review feedback and fix tests * Add diagnostic for conflicting parameter identifiers * Restructure code after rebase
1 parent bc7eb59 commit 8e0a260

31 files changed

+1889
-97
lines changed

include/swift/AST/Decl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6531,7 +6531,7 @@ class EnumElementDecl : public DeclContext, public ValueDecl {
65316531
bool isIndirect() const {
65326532
return getAttrs().hasAttribute<IndirectAttr>();
65336533
}
6534-
6534+
65356535
/// Do not call this!
65366536
/// It exists to let the AST walkers get the raw value without forcing a request.
65376537
LiteralExpr *getRawValueUnchecked() const { return RawValueExpr; }

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,6 +2917,13 @@ NOTE(decodable_suggest_overriding_init_here,none,
29172917
"did you mean to override 'init(from:)'?", ())
29182918
NOTE(codable_suggest_overriding_init_here,none,
29192919
"did you mean to override 'init(from:)' and 'encode(to:)'?", ())
2920+
NOTE(codable_enum_duplicate_case_name_here,none,
2921+
"cannot automatically synthesize %0 because %1 has duplicate "
2922+
"case name %2", (Type, Type, Identifier))
2923+
NOTE(codable_enum_duplicate_parameter_name_here,none,
2924+
"cannot automatically synthesize %0 for %1 because "
2925+
"user defined parameter name %2 in %3 conflicts with "
2926+
"automatically generated parameter name", (Type, Type, Identifier, Identifier))
29202927

29212928
WARNING(decodable_property_will_not_be_decoded, none,
29222929
"immutable property will not be decoded because it is declared with "

include/swift/AST/KnownIdentifiers.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
IDENTIFIER(AllCases)
2626
IDENTIFIER(allCases)
27+
IDENTIFIER(allKeys)
2728
IDENTIFIER(alloc)
2829
IDENTIFIER(allocWithZone)
2930
IDENTIFIER(allZeros)
@@ -43,16 +44,20 @@ IDENTIFIER(callAsFunction)
4344
IDENTIFIER(Change)
4445
IDENTIFIER_WITH_NAME(code_, "_code")
4546
IDENTIFIER(CodingKeys)
47+
IDENTIFIER(codingPath)
4648
IDENTIFIER(combine)
4749
IDENTIFIER_(Concurrency)
4850
IDENTIFIER(container)
51+
IDENTIFIER(Context)
4952
IDENTIFIER(CoreGraphics)
5053
IDENTIFIER(CoreMedia)
5154
IDENTIFIER(CGFloat)
5255
IDENTIFIER(CoreFoundation)
56+
IDENTIFIER(count)
5357
IDENTIFIER(CVarArg)
5458
IDENTIFIER(Darwin)
5559
IDENTIFIER(dealloc)
60+
IDENTIFIER(debugDescription)
5661
IDENTIFIER(Decodable)
5762
IDENTIFIER(decode)
5863
IDENTIFIER(decodeIfPresent)
@@ -82,18 +87,21 @@ IDENTIFIER(fromRaw)
8287
IDENTIFIER(hash)
8388
IDENTIFIER(hasher)
8489
IDENTIFIER(hashValue)
90+
IDENTIFIER(init)
8591
IDENTIFIER(initialize)
8692
IDENTIFIER(initStorage)
8793
IDENTIFIER(initialValue)
8894
IDENTIFIER(into)
8995
IDENTIFIER(intValue)
96+
IDENTIFIER(invalidValue)
9097
IDENTIFIER(Key)
9198
IDENTIFIER(KeyedDecodingContainer)
9299
IDENTIFIER(KeyedEncodingContainer)
93100
IDENTIFIER(keyedBy)
94101
IDENTIFIER(keyPath)
95102
IDENTIFIER(makeIterator)
96103
IDENTIFIER(makeAsyncIterator)
104+
IDENTIFIER(nestedContainer)
97105
IDENTIFIER(Iterator)
98106
IDENTIFIER(AsyncIterator)
99107
IDENTIFIER(load)
@@ -134,6 +142,8 @@ IDENTIFIER(to)
134142
IDENTIFIER(toRaw)
135143
IDENTIFIER(Type)
136144
IDENTIFIER(type)
145+
IDENTIFIER(typeMismatch)
146+
IDENTIFIER(underlyingError)
137147
IDENTIFIER(Value)
138148
IDENTIFIER(value)
139149
IDENTIFIER_WITH_NAME(value_, "_value")

include/swift/AST/KnownStdlibTypes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ KNOWN_STDLIB_TYPE_DECL(Decoder, ProtocolDecl, 1)
9090
KNOWN_STDLIB_TYPE_DECL(KeyedEncodingContainer, NominalTypeDecl, 1)
9191
KNOWN_STDLIB_TYPE_DECL(KeyedDecodingContainer, NominalTypeDecl, 1)
9292
KNOWN_STDLIB_TYPE_DECL(RangeReplaceableCollection, ProtocolDecl, 1)
93+
KNOWN_STDLIB_TYPE_DECL(EncodingError, NominalTypeDecl, 0)
94+
KNOWN_STDLIB_TYPE_DECL(DecodingError, NominalTypeDecl, 0)
9395

9496
KNOWN_STDLIB_TYPE_DECL(Result, NominalTypeDecl, 2)
9597

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ namespace swift {
247247
/// Enable experimental flow-sensitive concurrent captures.
248248
bool EnableExperimentalFlowSensitiveConcurrentCaptures = false;
249249

250+
/// Enable experimental derivation of `Codable` for enums.
251+
bool EnableExperimentalEnumCodableDerivation = false;
252+
250253
/// Disable the implicit import of the _Concurrency module.
251254
bool DisableImplicitConcurrencyModuleImport = false;
252255

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ def enable_experimental_flow_sensitive_concurrent_captures :
222222
Flag<["-"], "enable-experimental-flow-sensitive-concurrent-captures">,
223223
HelpText<"Enable flow-sensitive concurrent captures">;
224224

225+
def enable_experimental_enum_codable_derivation :
226+
Flag<["-"], "enable-experimental-enum-codable-derivation">,
227+
HelpText<"Enable experimental derivation of Codable for enums">;
228+
225229
def enable_resilience : Flag<["-"], "enable-resilience">,
226230
HelpText<"Deprecated, use -enable-library-evolution instead">;
227231
}

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
388388
Opts.EnableExperimentalFlowSensitiveConcurrentCaptures |=
389389
Args.hasArg(OPT_enable_experimental_flow_sensitive_concurrent_captures);
390390

391+
Opts.EnableExperimentalEnumCodableDerivation |=
392+
Args.hasArg(OPT_enable_experimental_enum_codable_derivation);
393+
391394
Opts.DisableImplicitConcurrencyModuleImport |=
392395
Args.hasArg(OPT_disable_implicit_concurrency_module_import);
393396

0 commit comments

Comments
 (0)