Optional promotion breaks exhaustive switching?

i had always assumed that

case wrapped:

promotes to

case wrapped?:

if the switch parameter is optional.

so i was surprised that the following causes a compiler error:

func test1(bool:Bool?)
{
    switch bool
    {
    case nil, true:
        break
    case false:
        break
    }
}
switch.swift:3:5: error: switch must be exhaustive
    switch bool
    ^
switch.swift:3:5: note: add missing case: '.some(_)'
    switch bool

instead it must be written as:

func test2(bool:Bool?)
{
    switch bool
    {
    case nil, true?:
        break
    case false?:
        break
    }
}

why?

1 Like

Also asked at the end of this post. Not answered.

1 Like

Looks like a bug to me, I don’t see any reason why this switch shouldn’t be considered exhaustive.

1 Like

This has been reported/discussed recently here Optional boolean doesn't satisfy switch exhaustivity with literal case statements · Issue #61817 · apple/swift · GitHub

2 Likes