Subject: Re: bug#75451: scratch/igc: Enable CHECK_STRUCTS

From: Stefan Kangas <[email-masked]>
Date: Mon, 20 Jan 2025 09:54:28 -0800

Pip Cet <[email-masked]> writes:

> Using "break" instead of "return" is impossible:
>
> * "default: eassume (0);"  means you lose compiler warnings if the enum is
> expanded.  -Wswitch-enum generates the compiler warnings, but they're
> lost in a sea of false positives because sometimes we cannot switch over
> an enum exhaustively.
>
> * "default: emacs_abort ();" will generate code to handle the "impossible"
> case
>
> * Omitting the default statement will make GCC treat the fall-through case
> as reachable
>
> This is because this code:
>
> #include <stdio.h>
>
> enum three { A, B, C };
>
> int main (void)
> {
>   volatile enum three four = C + 1;
>   volatile const char *bad = 0;
>   switch (four) {
>   case A:
>   case B:
>   case C:
>     printf ("%s\n", "good");
>     break;
>   default:
>     printf ("%s\n", bad);
>   }
>   return 0;
> }
>
> is treated by GCC as valid code and the "bad" printf cannot be
> determined to be unreachable.

I have no reason to doubt that you are correct, but what are the
consequences for us, in concrete terms, that make you say that "using
'break' instead of 'return' is impossible"?  Given that most programs
run just fine despite the shortcomings and/or bugs you have identified
in GCC, I think that saying using the switch statement in the normal way
is "impossible" might be overstating the case.

My apologies if I'm missing the point that you're making here.

BTW, has this been reported to the GCC developers?

> Let's introduce macros for the different cases, and use the same macro
> in the same case, until GCC offers a fix.

I'm with Gerd here, and not too excited about the prospect of yet
another macro.  I'd rather do without the additional warning, and just
have it crash at runtime instead, like we do now.

But are we sure that installing my patch would make things worse?
If I'm reading Paul correctly, he's saying that a static check is
more reliable.