tech-kern archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: Do all -current compilers support C23 typed enum syntax?
> Date: Thu, 3 Dec 2026 14:12:24 -0800
> From: Chris Hanson <cmhanson%eschatologist.net@localhost>
>
> I explained briefly in another response, but without an explicit
> type, the type used to represent an enum is up to the compiler. It's
> int by default, but doesn't have to be, and can be changed by things
> like compiler arguments (to e.g. prefer the narrowest integer type,
> or the widest).
>
> And if you want to actually get compiler enforcement for invalid
> assignments and a good debugging experience, you actually need to
> use the enum name (or a typedef) in structure members, variables,
> and arguments.
>
> These combine to mean that
>
> enum call_options { a = 1, b = 2, };
> int some_call(enum call_options options);
>
> does not get a stable ABI, but one that can change based on compiler
> arguments. Using "enum call_options : int" tells the compiler how to
> represent the type in a way that cannot be overridden. And the above
> is strictly superior to
>
> enum { a = 1, b = 2, };
> int some_call(int /* see above */ options);
>
> because just using int and a comment doesn't get any compiler
> enforcement or debugging affordances.
I'm not convinced these goals -- stable ABI, i.e., ABI that survives
changes to the enumeration across versions, and compiler enforcement
of enumerated values -- are compatible.
Perhaps when used exclusively for function _arguments_ they are
compatible -- in which case your typedef macro approach sounds fine at
first impression -- but if you use them in any other way, like
structure members or function _return values_, you will put users in a
bind. Here's how:
/* chanson.h */
enum cmdname: int16_t {
CMD_INVALID = -1,
CMD_UNINITIALIZED = 0,
CMD_OPEN = 1,
CMD_CLOSE = 2,
CMD_DONE = 3,
};
struct cmd {
enum cmdname name;
char arg;
};
int send_cmd(const struct cmd *);
int recv_cmd(struct cmd *);
By defining the underlying type of cmdname to be int16_t, you have
ensured that (within the NetBSD/SysV C ABI) offsetof(struct cmd, arg)
is 2, not 1 (-fshort-enums, char) or 4 (default, int) -- fine. And
you've licensed the compiler to complain if users try to fit any other
value into the name member of a struct cmd.
What happens when a caller of this library uses recv_cmd?
struct cmd cmd;
if (recv_cmd(&cmd) == -1)
goto fail;
switch (cmd.name) {
case CMD_INVALID:
case CMD_UNINITIALIZED:
case CMD_DONE:
goto fail;
case CMD_OPEN:
do_open(cmd.arg);
break;
case CMD_CLOSE:
do_close(cmd.arg);
break;
default:
panic("unknown command");
}
cmd.name = CMD_DONE; /* error if I don't use CMD_* here, yay */
cmd.arg = 0;
The compiler, being very clever, sees that all enumerated values of
enum cmd have been covered by the switch, so it helpfully deletes some
code it can _prove_ is dead -- this part:
default:
panic("unknown command");
In other words, the compiler can prove it is safe to turn the switch
into:
if (cmd.name <= CMD_UNINITIALIZED || cmd.name == CMD_DONE) {
goto fail;
} else if (cmd.name == CMD_OPEN) {
do_open(cmd.arg);
} else {
do_close(cmd.arg);
}
Now what happens when you extend enum cmdname? By using int16_t you
made sure to reserve space for nearly 2^15 positive command numbers!
enum cmdname: int16_t {
CMD_INVALID = -1,
CMD_UNINITIALIZED = 0,
CMD_OPEN = 1,
CMD_CLOSE = 2,
CMD_DONE = 3,
CMD_FROBNICATE = 4,
};
Is that a compatible ABI change?
Home |
Main Index |
Thread Index |
Old Index