Skip to content
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

[SR-9582] Change "can derive conformances" condition for CaseIterable #52029

Closed
dan-zheng opened this issue Dec 28, 2018 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@dan-zheng
Copy link
Collaborator

Previous ID SR-9582
Radar None
Original Reporter @dan-zheng
Type Bug
Status Closed
Resolution Duplicate
Environment

Reproducible from this commit on master: 0d4a585

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 04cbf9b825ea59a4ae32fe7c39ff9815

duplicates:

  • SR-8776 Confusing error with broken CaseIterable conformance
  • SR-8777 CaseIterable synthesis should work in the presence of a manual AllCases typealias

relates to:

  • SR-8776 Confusing error with broken CaseIterable conformance
  • SR-8777 CaseIterable synthesis should work in the presence of a manual AllCases typealias

Issue Description:

Crasher

// error.swift
enum Directions : CaseIterable {
  case north
  case south
  // The user customized the `AllCases` requirement but not `allCases`.
  typealias AllCases = UnsafeBufferPointer<Directions>
}

Error:

error.swift:1:6: error: type 'Directions' does not conform to protocol 'CaseIterable'
enum Directions : CaseIterable {
     ^
error.swift:1:6: error: protocol 'CaseIterable' is broken; cannot derive conformance for type 'Directions'
enum Directions : CaseIterable {
     ^

Explanation

CaseIterable has two requirements:

public protocol CaseIterable {
  associatedtype AllCases: Collection
    where AllCases.Element == Self
  static var allCases: AllCases { get }
}

The condition for deriving conformances to CaseIterable currently doesn't take into account whether other requirements have been user-defined/customized. (This is true for all derived conformances, in general.)

The code synthesis for allCases creates an ArrayExpr (from this commit). This is problematic if AllCases is defined to be a collection that is not ExpressibleByArrayLiteral, as in the crasher program above.


Possible solutions

One solution is to "can derive conformances" condition for CaseIterable so that synthesis is possible only if no requirements have been user-customized. It may be useful to generalize this condition for all protocol derived conformances though - I think it's common that synthesis is intended only when no requirements are customized.

Another solution is to change the synthesis condition to account for whether AllCases conforms to ExpressibleByArrayLiteral. This seems like a weird, undocumented "point of customization" for users though.

@belkadan
Copy link
Contributor

belkadan commented Jan 7, 2019

I feel like this is already covered by either SR-8776 or SR-8777 that Robert found. What do you think?

@dan-zheng
Copy link
Collaborator Author

Oops, this is definitely covered by those two SRs. Closing as duplicate.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants