Skip to content

Commit b8b80b6

Browse files
authored
Merge pull request #32824 from slavapestov/fix-codingkeys-synthesis
Sema: Force synthesis of the CodingKeys nested type in EmittedMembersRequest
2 parents c647979 + eb74f78 commit b8b80b6

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/Sema/TypeCheckDecl.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,14 @@ EmittedMembersRequest::evaluate(Evaluator &evaluator,
25232523
forceConformance(Context.getProtocol(KnownProtocolKind::Hashable));
25242524
forceConformance(Context.getProtocol(KnownProtocolKind::Differentiable));
25252525

2526+
// If the class conforms to Encodable or Decodable, even via an extension,
2527+
// the CodingKeys enum is synthesized as a member of the type itself.
2528+
// Force it into existence.
2529+
(void) evaluateOrDefault(Context.evaluator,
2530+
ResolveImplicitMemberRequest{CD,
2531+
ImplicitMemberAction::ResolveCodingKeys},
2532+
{});
2533+
25262534
// If the class has a @main attribute, we need to force synthesis of the
25272535
// $main function.
25282536
(void) evaluateOrDefault(Context.evaluator,

test/SILGen/synthesized_conformance_class.swift

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ final class Final<T> {
44
var x: T
55
init(x: T) { self.x = x }
66
}
7+
78
// CHECK-LABEL: final class Final<T> {
89
// CHECK: @_hasStorage final var x: T { get set }
910
// CHECK: init(x: T)
@@ -51,6 +52,16 @@ class Nonfinal<T> {
5152
// CHECK: func encode(to encoder: Encoder) throws
5253
// CHECK: }
5354

55+
// Make sure that CodingKeys members are actually emitted.
56+
57+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}21__derived_enum_equalsySbAFyx_G_AHtFZ : $@convention(method) <T> (Final<T>.CodingKeys, Final<T>.CodingKeys, @thin Final<T>.CodingKeys.Type) -> Bool {
58+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}9hashValueSivg : $@convention(method) <T> (Final<T>.CodingKeys) -> Int {
59+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}4hash4intoys6HasherVz_tF : $@convention(method) <T> (@inout Hasher, Final<T>.CodingKeys) -> () {
60+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}11stringValueSSvg : $@convention(method) <T> (Final<T>.CodingKeys) -> @owned String {
61+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}11stringValueAFyx_GSgSS_tcfC : $@convention(method) <T> (@owned String, @thin Final<T>.CodingKeys.Type) -> Optional<Final<T>.CodingKeys> {
62+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}8intValueSiSgvg : $@convention(method) <T> (Final<T>.CodingKeys) -> Optional<Int> {
63+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}8intValueAFyx_GSgSi_tcfC : $@convention(method) <T> (Int, @thin Final<T>.CodingKeys.Type) -> Optional<Final<T>.CodingKeys> {
64+
5465
extension Final: Encodable where T: Encodable {}
5566
// CHECK-LABEL: // Final<A>.encode(to:)
5667
// CHECK-NEXT: sil hidden [ossa] @$s29synthesized_conformance_class5FinalCAASERzlE6encode2toys7Encoder_p_tKF : $@convention(method) <T where T : Encodable> (@in_guaranteed Encoder, @guaranteed Final<T>) -> @error Error {

0 commit comments

Comments
 (0)