GNU bug report logs -
#75964
Switching the Emacs build to -Wswitch-enum in src/
Previous Next
Full log
Message #197 received at 75964 <at> debbugs.gnu.org (full text, mbox):
> From: Stefan Monnier <monnier <at> iro.umontreal.ca>
> Cc: pipcet <at> protonmail.com, eggert <at> cs.ucla.edu, 75964 <at> debbugs.gnu.org,
> stefankangas <at> gmail.com, acorallo <at> gnu.org
> Date: Tue, 04 Feb 2025 11:55:12 -0500
>
> >> Yes, this part of C sucks and I had forgotten about it.
> > What sucks and why?
>
> That
>
> switch ((enum Lisp_Type) 53) {
> case Lisp_Symbol:
> case Lisp_Type_Unused0:
> case Lisp_Int0:
> case Lisp_Int1:
> case Lisp_String:
> case Lisp_Vectorlike:
> case Lisp_Cons:
> case Lisp_Float:
> return 1;
> }
> return 2;
>
> should return 2, because I'd prefer that it aborts and/or that the
> programmer have a way to say explicitly what should happen. `default:`
> works for that in the case where you have listed *all* the enum's
> values, but then you get stiffed in the case where you add an enum value
> because the compiler won't tell you that you forgot to update this
> `switch` accordingly.
So you want a device that would abort for unexpected values? Then why
use switch at all? You want eassert or even a simple if:
if ((enum foo) foo > max_enum_value)
emacs_abort ();
Or what am I missing?
This bug report was last modified 127 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.