pointfreeco / swift-case-paths

🧰 Case paths extends the key path hierarchy to enum cases.
https://www.pointfree.co/collections/enums-and-structs/case-paths
MIT License
904 stars 105 forks source link

@CasePathable triggers compiler bug with Never payloads #161

Closed jshier closed 3 months ago

jshier commented 3 months ago

Starting in 1.4.0, enums with Never associated values, even as part of a generic type, trigger some sort of compiler bug (or misleading diagnostic) where it thinks switches on the enum may have unknown values. For example:

@CasePathable
enum Action {
    case alert(Never)
}

produces the warning Switch covers known cases, but 'Action' may have additional unknown values in its generated code. More specifically:

public struct AllCasePaths: Sequence {
    public subscript(root: Action) -> PartialCaseKeyPath<Action> {
        switch root { // Warning produced here.
        case .alert:
            return \.alert
        }
    }
}

This enum compiles cleanly if any other type is used as an associated value.

Environment

Edited to simplify example.

jshier commented 3 months ago

I can't reproduce the Never in generics case outside of a TCA example:

@CasePathable
enum Action {
    case alert(AlertState<Never> .Action)
}

@Reducer
struct Feature2 {}

This produces the same warning. If I use my own generic type, even with a child type, it doesn't warn.

jshier commented 3 months ago

Perhaps the issue is related to the fact that a PartialCasePath<Never> can't be instantiated?

stephencelis commented 3 months ago

Thanks for the report! Luckily we can work around it by ditching the switch and leveraging CasePathable.is, but the warning is strange and I'm not sure if a bug should be filed with Apple.

stephencelis commented 3 months ago

@jshier Since this is just producing a warning, we'll probably not cut a release immediately after merging, and instead let the dust settle on the 1.4 release a little longer just in case there are other issues lurking.