Article View: muc.lists.netbsd.tech.kern
Article #46Re: Do all -current compilers support C23 typed enum syntax?
From: Taylor R Campbell
Date: Fri, 04 Dec 2026 03:07
Date: Fri, 04 Dec 2026 03:07
118 lines
5573 bytes
5573 bytes
> Date: Thu, 3 Dec 2026 14:12:24 -0800
> From: Chris Hanson <cmhanson@eschatologist.net>
>
> 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?
--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Technical Details
Message-ID:
<20261204030713.17DC084DF1@mail.netbsd.org>Path:
mbox-mo1.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!newsfeed.bofh.team!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mailReferences:
<440649C8-8CC6-4695-B7A8-6FBEA32999F6@eschatologist.net>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!newsfeed.bofh.team!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail From: Taylor R Campbell <riastradh@NetBSD.org> Newsgroups: muc.lists.netbsd.tech.kern Subject: Re: Do all -current compilers support C23 typed enum syntax? Date: Fri, 4 Dec 2026 03:07:12 +0000 Organization: Newsgate at muc.de e.V. Sender: tech-kern-owner@NetBSD.org Approved: news-admin@muc.de Distribution: world Message-ID: <20261204030713.17DC084DF1@mail.netbsd.org> References: <440649C8-8CC6-4695-B7A8-6FBEA32999F6@eschatologist.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2"; logging-data="92491"; mail-complaints-to="news-admin@muc.de" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netbsd.org; s=20240131; t=1796353640; bh=b9lBeNCn3TxXepd5ba6j5wO+WhUp0rctogZnHknrps8=; h=To:CC:In-reply-to:Subject:Date:From:List-Id:List-Unsubscribe; b=sYjrjjN2Wdz34BWArS8JCWQnjGxaQoyDtI7Ts+ewd8kBzs8YpEGynyFkx2pF6WHPi fcXuElP1LcBL3FUvtisZLiB9pC9qA+4xFCmEKFGko2tB1tez8jVP9J1dm/HyPapqOx 4+qm/tyKJkxxQw9u/Oj4g/kGUlEknIXmVe1kpF28= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netbsd.org; s=20240131; t=1796353633; bh=b9lBeNCn3TxXepd5ba6j5wO+WhUp0rctogZnHknrps8=; h=To:CC:In-reply-to:Subject:Date:From; b=OrzNOz2+GrSpTiwlwGY85dzvkQrJIuMtfCCTjLtUzHJOU4XNjxOkViymnZWy5esNE w6mYAaZD8rPd6KVEa/KVdOqEuPuLvzSQk+aHVBdr4+NCtuk1n5XWHUf2lSLGjfsbRk ok80jVqMB21Ih+knMFZnqxhOsc1kX/st7pv0+FaY= In-reply-to: <440649C8-8CC6-4695-B7A8-6FBEA32999F6@eschatologist.net> (cmhanson@eschatologist.net) Precedence: bulk X-Newsgate-Id: f02c70b8849f+ X-No-Archive: Yes