Skip to content

Commit 3d6c6b4

Browse files
committed
Revert "Sema: Don't need to derive CaseIterable's AllCases associated type"
This reverts commit 3ae31d5.
1 parent db46d04 commit 3d6c6b4

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

lib/Sema/DerivedConformanceCaseIterable.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ static ArraySliceType *computeAllCasesType(NominalTypeDecl *enumDecl) {
6969
return ArraySliceType::get(enumType);
7070
}
7171

72+
static Type deriveCaseIterable_AllCases(DerivedConformance &derived) {
73+
// enum SomeEnum : CaseIterable {
74+
// @derived
75+
// typealias AllCases = [SomeEnum]
76+
// }
77+
auto *rawInterfaceType = computeAllCasesType(cast<EnumDecl>(derived.Nominal));
78+
return derived.getConformanceContext()->mapTypeIntoContext(rawInterfaceType);
79+
}
80+
7281
ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {
7382
// Conformance can't be synthesized in an extension.
7483
if (checkAndDiagnoseDisallowedContext(requirement))
@@ -102,3 +111,18 @@ ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {
102111

103112
return propDecl;
104113
}
114+
115+
Type DerivedConformance::deriveCaseIterable(AssociatedTypeDecl *assocType) {
116+
// Check that we can actually derive CaseIterable for this type.
117+
if (!canDeriveConformance(Nominal))
118+
return nullptr;
119+
120+
if (assocType->getName() == Context.Id_AllCases) {
121+
return deriveCaseIterable_AllCases(*this);
122+
}
123+
124+
Context.Diags.diagnose(assocType->getLoc(),
125+
diag::broken_case_iterable_requirement);
126+
return nullptr;
127+
}
128+

lib/Sema/DerivedConformances.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ class DerivedConformance {
163163
/// \returns the derived member, which will also be added to the type.
164164
ValueDecl *deriveCaseIterable(ValueDecl *requirement);
165165

166+
/// Derive a CaseIterable type witness for an enum if it has no associated
167+
/// values for any of its cases.
168+
///
169+
/// \returns the derived member, which will also be added to the type.
170+
Type deriveCaseIterable(AssociatedTypeDecl *assocType);
171+
166172
/// Determine if a RawRepresentable requirement can be derived for a type.
167173
///
168174
/// This is implemented for non-empty enums without associated values,

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5763,6 +5763,8 @@ TypeChecker::deriveTypeWitness(DeclContext *DC,
57635763
switch (*knownKind) {
57645764
case KnownProtocolKind::RawRepresentable:
57655765
return std::make_pair(derived.deriveRawRepresentable(AssocType), nullptr);
5766+
case KnownProtocolKind::CaseIterable:
5767+
return std::make_pair(derived.deriveCaseIterable(AssocType), nullptr);
57665768
case KnownProtocolKind::Differentiable:
57675769
return derived.deriveDifferentiable(AssocType);
57685770
default:

stdlib/public/core/CompilerProtocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ extension RawRepresentable where RawValue: Hashable, Self: Hashable {
249249
/// demonstrates this automatic implementation.
250250
public protocol CaseIterable {
251251
/// A type that can represent a collection of all values of this type.
252-
associatedtype AllCases: Collection = [Self]
252+
associatedtype AllCases: Collection
253253
where AllCases.Element == Self
254254

255255
/// A collection of all values of this type.

test/IDE/complete_override.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,11 @@ struct SynthesizedConformance3: Hashable {
896896
enum SynthesizedConformance4: CaseIterable {
897897
case a, b, c, d
898898
#^OVERRIDE_SYNTHESIZED_4^#
899-
// OVERRIDE_SYNTHESIZED_4: Begin completions, 3 items
899+
// OVERRIDE_SYNTHESIZED_4: Begin completions, 4 items
900900
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[InstanceVar]/Super/IsSystem: var hashValue: Int
901901
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[InstanceMethod]/Super/IsSystem: func hash(into hasher: inout Hasher) {|};
902902
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[StaticVar]/Super/IsSystem: static var allCases: [SynthesizedConformance4];
903+
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[AssociatedType]/Super/IsSystem: typealias AllCases = {#(Type)#};
903904
}
904905

905906
class SynthesizedConformance5: SynthesizedConformance2 {

0 commit comments

Comments
 (0)