Skip to content

Handling all values of a modulo operator in a switch statement should be considered exhaustive #81761

Open
@TheGnuGod

Description

@TheGnuGod

Description

This bug is of the compiler error "Switch must be exhaustive"

When writing a switch statement of some integer dividend modulo some other integer divisor handling all cases from (-divisor)+1 up to divisor-1` is not considered exhaustive even though every possible outcome is considered.

Similarly, writing a switch statement of some unsigned integer dividend modulo some other integer divisor handling all cases from 0 up to divisor-1 is not considered exhaustive even though every possible outcome is considered.

Reproduction

func buggy_int_switch(of number: Int) -> String {
    switch number % 3 {
    case -2:
        return "negative two"
    case -1:
        return "negative one"
    case 0:
        return "zero"
    case 1:
        return "one"
    case 2:
        return "two"
    }
}

func buggy_uint_switch(of number: UInt) -> String {
    switch number % 3 {
    case 0:
        return "zero"
    case 1:
        return "one"
    case 2:
        return "two"
    }
}

Expected behavior

I expected this code to compile, since all possible values of both modulo operators are considered, but I receive two errors, both saying "Switch must be exhaustive", despite the code handling every possible output of the modulo operator.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.triage neededThis issue needs more specific labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions