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-10089] Combining CaseIterable and Strideable seems impossible. #52491

Open
swift-ci opened this issue Mar 12, 2019 · 3 comments
Open

[SR-10089] Combining CaseIterable and Strideable seems impossible. #52491

swift-ci opened this issue Mar 12, 2019 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10089
Radar None
Original Reporter Jessy (JIRA User)
Type Bug

Attachment: Download

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

md5: 4e2939aa390658c94ca485ef4e84a78e

Issue Description:

I think CaseIterable is calling distance(to:) behind the scenes?

@belkadan
Copy link
Contributor

Reproduced, though I'm confused about this. cc brentdax (JIRA User), @jtbandes, CodaFi (JIRA User)

@beccadax
Copy link
Contributor

The symptom is infinite recursion that looks like this:

    frame #​52829: 0x0000000100001603 Contents`CaseStrideable.distance(other=a, self=a) at Contents.swift:7:33
    frame #&#8203;52830: 0x0000000100002202 Contents`protocol witness for Strideable.distance(to:) in conformance Alphabet at <compiler-generated>:0
    frame #&#8203;52831: 0x00007fff6ec5126b libswiftCore.dylib`static (extension in Swift):Swift.Strideable.== infix(A, A) -> Swift.Bool + 219
    frame #&#8203;52832: 0x0000000100001ef6 Contents`protocol witness for static Equatable.== infix(_:_:) in conformance Alphabet at <compiler-generated>:0
    frame #&#8203;52833: 0x00007fff6eb2de99 libswiftCore.dylib`(extension in Swift):Swift.Collection< where A.Element: Swift.Equatable>.firstIndex(of: A.Element) -> Swift.Optional<A.Index> + 905
    frame #&#8203;52834: 0x0000000100002776 Contents`partial apply for Collection<>.firstIndex(of:) at <compiler-generated>:0
    frame #&#8203;52835: 0x0000000100001813 Contents`thunk for @escaping @callee_guaranteed (@in_guaranteed A) -> (@out Int?) at <compiler-generated>:0
    frame #&#8203;52836: 0x0000000100001894 Contents`partial apply for thunk for @escaping @callee_guaranteed (@in_guaranteed A) -> (@out Int?) at <compiler-generated>:0
    frame #&#8203;52837: 0x00000001000018ba Contents`thunk for @callee_guaranteed (@in_guaranteed A) -> (@unowned Int?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;52838: 0x0000000100001998 Contents`partial apply for thunk for @callee_guaranteed (@in_guaranteed A) -> (@unowned Int?, @error @owned Error) at <compiler-generated>:0
    frame #&#8203;52839: 0x00007fff6ec34299 libswiftCore.dylib`(extension in Swift):Swift.Sequence.compactMap<A>((A.Element) throws -> Swift.Optional<A1>) throws -> Swift.Array<A1> + 761
    frame #&#8203;52840: 0x0000000100001603 Contents`CaseStrideable.distance(other=a, self=a) at Contents.swift:7:33

In the playground provided, notice that distance(to:) contains this on line 7:

    let indices = [self, other].compactMap(Self.allCases.firstIndex)

Strideable provides a default implementation of == in terms of distance(to:), so when distance(to:) calls allCases.firstIndex(of:), it ends up calling back into distance(to:). Because this default implementation is available, Equatable doesn't derive a == operator as it normally would. (Having this default implementation probably made more sense before Equatable derivation.)

The user can fix this by implementing == by hand. We can't remove this default implementation now that we're ABI-stable; maybe we could deprecate it and make Equatable derivation ignore deprecated == operators or something?

(Edit: Why did JIRA think that expanding smilies in code blocks was a good idea?)

@glessard
Copy link
Contributor

This is similar to https://bugs.swift.org/browse/SR-7117

(There, it's Strideable's default implementation of < that is the issue.)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
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 crash Bug: A crash, i.e., an abnormal termination of software run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

5 participants