An incomplete CaseIterable conformance

Could anything bad happen if my enum's custom CaseIterable conformance doesn't list all possible values?

Even though it has compiler support for a synthesized conformance, CaseIterable isn’t treated specially at run time, so this is the same question as any other conformance not quite matching its protocol. If someone is using the enum in a way that expects a value to be in the list somewhere, for example, it might now fatalError or even do something memory-unsafe, depending on their implementation. But nothing in the stdlib consumes CaseIterable specifically, so it won’t break anything there.

1 Like

I've seen implementations which rely on being able to do this. e.g. where you have a symbolic all case for Reasons, but don't want to include that re. CaseIterable since it's semantically not a 'real' enum value.

I see this often when bridging or inheriting from C/C++/Objective-C, where enums were often used in myriad ways, not just for mutually exclusive options. e.g. bitsets, bitmasks, etc. Translating them into Swift's fussier semantics can be challenging; if CaseIterable weren't flexible like this, it'd be much harder to use.

That said, it would be nice if there were some way to get a compiler warning about missing cases, because sometimes you do intend to enumerate them all. I imagine it'd be a hard heuristic, though - you need some simple way to opt out, for the cases where you're not intending full enumeration, and there's infinite possible ways to write allCases. Still, if the compiler recognised some basic ones - e.g. where it just returns a literal - that could be nice.