Skip to content

Sema: Don't need to derive CaseIterable's AllCases associated type #33371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions lib/Sema/DerivedConformanceCaseIterable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,6 @@ static ArraySliceType *computeAllCasesType(NominalTypeDecl *enumDecl) {
return ArraySliceType::get(enumType);
}

static Type deriveCaseIterable_AllCases(DerivedConformance &derived) {
// enum SomeEnum : CaseIterable {
// @derived
// typealias AllCases = [SomeEnum]
// }
auto *rawInterfaceType = computeAllCasesType(cast<EnumDecl>(derived.Nominal));
return derived.getConformanceContext()->mapTypeIntoContext(rawInterfaceType);
}

ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {
// Conformance can't be synthesized in an extension.
if (checkAndDiagnoseDisallowedContext(requirement))
Expand Down Expand Up @@ -111,18 +102,3 @@ ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) {

return propDecl;
}

Type DerivedConformance::deriveCaseIterable(AssociatedTypeDecl *assocType) {
// Check that we can actually derive CaseIterable for this type.
if (!canDeriveConformance(Nominal))
return nullptr;

if (assocType->getName() == Context.Id_AllCases) {
return deriveCaseIterable_AllCases(*this);
}

Context.Diags.diagnose(assocType->getLoc(),
diag::broken_case_iterable_requirement);
return nullptr;
}

6 changes: 0 additions & 6 deletions lib/Sema/DerivedConformances.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,6 @@ class DerivedConformance {
/// \returns the derived member, which will also be added to the type.
ValueDecl *deriveCaseIterable(ValueDecl *requirement);

/// Derive a CaseIterable type witness for an enum if it has no associated
/// values for any of its cases.
///
/// \returns the derived member, which will also be added to the type.
Type deriveCaseIterable(AssociatedTypeDecl *assocType);

/// Determine if a RawRepresentable requirement can be derived for a type.
///
/// This is implemented for non-empty enums without associated values,
Expand Down
2 changes: 0 additions & 2 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5691,8 +5691,6 @@ TypeChecker::deriveTypeWitness(DeclContext *DC,
switch (*knownKind) {
case KnownProtocolKind::RawRepresentable:
return std::make_pair(derived.deriveRawRepresentable(AssocType), nullptr);
case KnownProtocolKind::CaseIterable:
return std::make_pair(derived.deriveCaseIterable(AssocType), nullptr);
case KnownProtocolKind::Differentiable:
return derived.deriveDifferentiable(AssocType);
default:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/CompilerProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ extension RawRepresentable where RawValue: Hashable, Self: Hashable {
/// demonstrates this automatic implementation.
public protocol CaseIterable {
/// A type that can represent a collection of all values of this type.
associatedtype AllCases: Collection
associatedtype AllCases: Collection = [Self]
where AllCases.Element == Self

/// A collection of all values of this type.
Expand Down
3 changes: 1 addition & 2 deletions test/IDE/complete_override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -896,11 +896,10 @@ struct SynthesizedConformance3: Hashable {
enum SynthesizedConformance4: CaseIterable {
case a, b, c, d
#^OVERRIDE_SYNTHESIZED_4^#
// OVERRIDE_SYNTHESIZED_4: Begin completions, 4 items
// OVERRIDE_SYNTHESIZED_4: Begin completions, 3 items
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[InstanceVar]/Super/IsSystem: var hashValue: Int
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[InstanceMethod]/Super/IsSystem: func hash(into hasher: inout Hasher) {|};
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[StaticVar]/Super/IsSystem: static var allCases: [SynthesizedConformance4];
// OVERRIDE_SYNTHESIZED_4-DAG: Decl[AssociatedType]/Super/IsSystem: typealias AllCases = {#(Type)#};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we are skipping type requirements when a witness has already been synthesized.

}

class SynthesizedConformance5: SynthesizedConformance2 {
Expand Down