From: David Laight <david.laight.linux@gmail.com>
To: Alexey Dobriyan <adobriyan@gmail.com>
Cc: corbet@lwn.net, workflows@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 6/9] CodingStyle: recommend static_assert/_Static_assert
Date: Sat, 27 Dec 2025 19:45:57 +0000 [thread overview]
Message-ID: <20251227194557.29d2afc5@pumpkin> (raw)
In-Reply-To: <c6f0c3d8-eb2b-49e1-b874-610fbeb248e4@p183>
On Sat, 27 Dec 2025 17:51:49 +0300
Alexey Dobriyan <adobriyan@gmail.com> wrote:
> On Tue, May 13, 2025 at 08:40:31PM +0100, David Laight wrote:
> > On Fri, 9 May 2025 23:34:27 +0300
> > Alexey Dobriyan <adobriyan@gmail.com> wrote:
> >
> > > Linux's BUG_ON is done backwards (condition is inverted).
> > > But it is a long story.
> > >
> > > However C11/C23 allow to partially transition to what all normal
> > > programmers are used to, namely assert().
> > >
> > > Deprecate BUILD_BUG_ON, recommend static_assert/_Static_assert.
> > > And then some day BUG_ON will be flipped as well.
> >
> > _Static_assert() is broken by design and only usable for trival tests.
>
> It is not broken by design. I was going to recommend it
> for "static_assert(sizeof(struct S) == ...)" type of things. For ABI types and
> similar stuff.
As I said, it can only be used for trivial tests.
Checking the sizes on structures is one of them.
You can't put one inside a compile-time conditional and the tested value
has to be an 'integer constant expression' not just a 'compile time constant'.
In particular that means you can't use it to check constant parameters to
inline functions or variables defined within statements blocks.
When I was rewriting min() there was an outer builtin_choose_expr(),
_Static_assert() within the 'unselected' expression would trip.
That really isn't what you want.
So there are many places where BUILD_BUG_ON() can be used but
_Static_assert() cannot be used.
BUILD_BUG_ON() cannot be deprecated until there is a working replacement.
That won't happen until the C language group actually understand how the
language is actually used :-)
The _Pragma(warning/error...) are just as useless.
They can only report things detected by pre-processor conditionals,
not checks that rely on the optimiser to have deleted unreachable code.
>
> BTW BUILD_BUG_ON is broken by design too, there are places with fake functions
> for a block so that they can put statement in.
It works 'as designed' within the constraints of the language.
The error message required a log of 'lateral thought'.
Some of the 'fake functions' may well be replaceable with something
based on _Static_assert() - but that is only a small number.
>
> > clang also output the entire expansion of the conditional (even when
> > a message is specified) which can lead to very very very very long lines.
>
> Oh, that's very unfortunate.
>
> > It isn't at all suitable for many of the checks in the kernel.
>
> STATIC_ASSERT could be arranged.
>
> > Look at the signedness test in min() as an example.
>
> The very fact you all made giant mess trying to imitate min<T, U>()
> should not block progress of using standard (and better!) stuff.
There are other 'sanity' checks like those in FIELD_PREP().
Without assigning the parameters to local variables the expansion
of FIELD_PREP(GENMASK(8, 5) val) comes to around 18KB.
And that is a typical use - not the triple-nests min() that came
out as multi-megabyte and broke compilation.
David
next prev parent reply other threads:[~2025-12-27 19:46 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-09 20:34 [PATCH 1/9] CodingStyle: make Documentation/CodingStyle into symlink Alexey Dobriyan
2025-05-09 20:34 ` [PATCH 2/9] CodingStyle: delete explicit numbering Alexey Dobriyan
2025-05-12 9:06 ` Jani Nikula
2025-05-09 20:34 ` [PATCH 3/9] CodingStyle: advise on using "sysctl" in sysctl variables Alexey Dobriyan
2025-05-09 20:34 ` [PATCH 4/9] CodingStyle: mention "typedef struct S {} S;" if typedef is used Alexey Dobriyan
2025-05-10 6:18 ` Greg KH
2025-05-13 18:34 ` Alexey Dobriyan
2026-01-07 12:22 ` Krzysztof Kozlowski
2025-05-10 10:47 ` Mauro Carvalho Chehab
2025-05-10 10:47 ` Mauro Carvalho Chehab
2025-05-13 18:37 ` Alexey Dobriyan
2025-05-09 20:34 ` [PATCH 5/9] CodingStyle: institute better inline assembly formatting Alexey Dobriyan
2025-05-13 19:41 ` David Laight
2025-05-09 20:34 ` [PATCH 6/9] CodingStyle: recommend static_assert/_Static_assert Alexey Dobriyan
2025-05-10 6:21 ` Greg KH
2025-05-13 18:41 ` Alexey Dobriyan
2025-05-13 19:40 ` David Laight
2025-12-27 14:51 ` Alexey Dobriyan
2025-12-27 19:45 ` David Laight [this message]
2026-01-07 12:28 ` Krzysztof Kozlowski
2025-05-09 20:34 ` [PATCH 7/9] CodingStyle: new variable declaration placement rule Alexey Dobriyan
2026-01-07 12:28 ` Krzysztof Kozlowski
2025-05-09 20:34 ` [PATCH 8/9] CodingStyle: tell people how to split long "for" loops Alexey Dobriyan
2025-05-10 18:56 ` David Laight
2025-05-12 16:20 ` Alexey Dobriyan
2025-05-12 16:59 ` Greg KH
2025-05-12 19:09 ` David Laight
2026-01-07 12:29 ` Krzysztof Kozlowski
2025-05-09 20:34 ` [PATCH 9/9] CodingStyle: flip the rule about curlies Alexey Dobriyan
2025-05-09 21:44 ` Randy Dunlap
2025-05-10 6:18 ` Greg KH
2025-05-12 16:43 ` Jeff Johnson
2025-05-12 16:56 ` Greg KH
2025-05-13 19:06 ` Alexey Dobriyan
2025-05-15 16:33 ` Jeff Johnson
2026-01-07 12:29 ` Krzysztof Kozlowski
2026-01-07 12:32 ` Krzysztof Kozlowski
2025-05-09 20:40 ` [PATCH 1/9] CodingStyle: make Documentation/CodingStyle into symlink Ozgur Kara
2025-05-10 10:05 ` Jonathan Corbet
2025-05-12 16:08 ` Alexey Dobriyan
2025-05-12 16:57 ` Greg KH
2025-05-13 18:32 ` Alexey Dobriyan
2025-05-14 18:55 ` Jonathan Corbet
2025-05-13 4:12 ` Al Viro
2025-05-13 18:33 ` Alexey Dobriyan
2025-05-13 19:04 ` Al Viro
2025-05-13 19:26 ` Alexey Dobriyan
2025-05-13 19:50 ` Al Viro
2026-01-07 12:27 ` Krzysztof Kozlowski
2025-05-19 16:21 ` Pavel Machek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251227194557.29d2afc5@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=adobriyan@gmail.com \
--cc=corbet@lwn.net \
--cc=linux-kernel@vger.kernel.org \
--cc=workflows@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.