Article View: muc.lists.netbsd.tech.kern
Article #48Re: Do all -current compilers support C23 typed enum syntax?
From: Chris Hanson
Date: Fri, 04 Dec 2026 03:33
Date: Fri, 04 Dec 2026 03:33
49 lines
4400 bytes
4400 bytes
On Dec 3, 2026, at 7:07 PM, Taylor R Campbell <riastradh@NetBSD.org> wrote:
>
> 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”);
This is a really interesting observation! In Swift you have to explicitly mark the default case as supporting an unknown value to avoid such an optimization; it wouldn’t surprise me if the C-family compiler folks at Apple initially avoided such an optimization because of the risks.
However, it looks like sometime in the last couple years Apple has also decided they want to support the optimization you describe: clang now has additional __attribute__(()) magic for telling the compiler how to handle this case more explicitly, by indicating whether an enum should be treated as open or closed:
#if __has_attribute(enum_extensibility)
#define __CF_ENUM_ATTRIBUTES __attribute__((enum_extensibility(open)))
#define __CF_CLOSED_ENUM_ATTRIBUTES __attribute__((enum_extensibility(closed)))
#define __CF_OPTIONS_ATTRIBUTES __attribute__((flag_enum,enum_extensibility(open)))
// ...
#endif
Then in the definition of the wrapper macros they stick the appropriate ATTRIBUTES macro in the right place for the definition. However, I have no idea whether any other compiler (like, oh, GCC!) understands these attributes though, and it looks like they were added at least in part to improve the ergonomics of importing C APIs to Swift rather than to improve C ergonomics: <https://www.swift.org/blog/improving-usability-of-c-libraries-in-swift/>
(The “flag_enum” stuff is an enum used to define bit flags which may be combined -- another common use for C enums which may have caused the compiler to avoid such optimizations in the past.)
-- Chris
--
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:
<7775F45E-DD07-404B-86FD-59C4A5CFCDA6@eschatologist.net>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder8.news.weretis.net!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mailReferences:
<20261204030713.17DC084DF1@mail.netbsd.org>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder8.news.weretis.net!3.eu.feeder.erje.net!feeder.erje.net!news2.arglkargh.de!news.karotte.org!news.muc.de!.POSTED.news.muc.de!not-for-mail From: Chris Hanson <cmhanson@eschatologist.net> Newsgroups: muc.lists.netbsd.tech.kern Subject: Re: Do all -current compilers support C23 typed enum syntax? Date: Thu, 3 Dec 2026 19:33:04 -0800 Organization: Newsgate at muc.de e.V. Sender: tech-kern-owner@NetBSD.org Approved: news-admin@muc.de Distribution: world Message-ID: <7775F45E-DD07-404B-86FD-59C4A5CFCDA6@eschatologist.net> References: <20261204030713.17DC084DF1@mail.netbsd.org> MIME-Version: 1.0 (Mac OS X Mail 16.0 \(3901.200.71.1.16\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: news.muc.de; posting-host="news.muc.de:193.149.48.2"; logging-data="10814"; mail-complaints-to="news-admin@muc.de" Authentication-Results: mail.netbsd.org (amavisd-new); dkim=pass (2048-bit key) header.d=eschatologist.net X-Sender-Id: dreamhost|x-authsender|cmhanson@eschatologist.net X-Sender-Id: dreamhost|x-authsender|cmhanson@eschatologist.net X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|cmhanson@eschatologist.net X-MailChannels-Auth-Id: dreamhost X-Wiry-Inform: 4b4093f702a67984_1796355195462_3390900960 X-MC-Loop-Signature: 1796355195462:2018348808 X-MC-Ingress-Time: 1796355195461 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eschatologist.net; s=dreamhost; t=1796355195; bh=avDpUNfOR6Zh+Kp9ozBBrAuQxgyd5DDtNekfRZP6D+c=; h=Content-Type:Subject:From:Date:Cc:Content-Transfer-Encoding:To; b=PdcVEgWJLZI9ybNf0CmQOH2uO0l26H121iLxYArlOr0rVrW2f+NVEVczRdUnzhz/A nTuY8YHl+CN39zE2ycMvc2f9aoCwe2fpBnxptzMxVODCOOVywJW5Xkun8CUTnNJYNs MSPIfv2dJh3NIpSTfvTBuJb2U7jO6+g8laESoDl4sb/Lsy3Qwvx6ue0jKalrckOIqt dlzs+PUw7B2xEFzcAQT/DRwCFS2LCsye5w3ocVjogqFZVHMJWM6i1c8rmKA3P8XrK8 1h+iX6aaWw0GoDGCK43uOZzPTr2ATt3frUAo8bBpANC7FJTz0jj2N7n/sXnnpAPfyu /j49PkVSfYIfg== In-Reply-To: <20261204030713.17DC084DF1@mail.netbsd.org> Precedence: bulk X-Newsgate-Id: f02c70b8849f+ X-No-Archive: Yes