Allow case labels in switch statements that are out of range of the type being switched on.

The case label values are converted to the promoted type of the expression being switched on, as if by a cast. In practice, this means discarding the high bits of a 32-bit value to produce a 16-bit one.

Code requiring this is dubious and would be a good candidate for a warning or a lint error, but it's allowed under the C standards.

The following code demonstrates the issue:

#include <stdio.h>
int main(void)
{
    int i = 0x1234;
    switch (i) {
        case 0xABCD1234:
            puts("good");
            break;
        default:
            puts("bad");
    }
}
This commit is contained in:
Stephen Heumann 2016-10-12 19:15:35 -05:00
parent 953a7c36d5
commit 0705a337b0

View File

@ -427,7 +427,7 @@ var
val := long(expressionValue).lsw;
if val <> expressionValue then
if not stPtr^.isLong then
Error(71);
expressionValue := val; {convert out-of-range value to (U)Word}
if stPtr = nil then
Error(72)
else begin