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. Signed-off-by: Alexey Dobriyan <adobri...@gmail.com> --- Documentation/process/coding-style.rst | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst index 63c41125e713..f8d5151eb0d2 100644 --- a/Documentation/process/coding-style.rst +++ b/Documentation/process/coding-style.rst @@ -1302,11 +1302,26 @@ asked the kernel to crash if a WARN*() fires, and such users must be prepared to deal with the consequences of a system that is somewhat more likely to crash. -Use BUILD_BUG_ON() for compile-time assertions -********************************************** +Use ``_Static_assert``/``static_assert`` to test things at compile time +*********************************************************************** -The use of BUILD_BUG_ON() is acceptable and encouraged, because it is a -compile-time assertion that has no effect at runtime. +C99 has standardized ``_Static_assert`` keyword and C23 added ``static_assert`` +later making Linux specific ``BUILD_BUG_ON`` macro obsolete. + +Note that both forms of static assertions are classified as declaration and +thus can be used (almost) anywhere in the code including top-level. +Don't introduce fake functions to test things at compile time: + +.. code-block:: c + + _Static_assert(sizeof(u32) == 4); + static_assert(sizeof(struct S) == 8); + +Also note that the second argument to ``static_assert`` can be omitted as an +extension. Sometimes filename and line number of a compile error is all you +need. + +See https://en.cppreference.com/w/c/language/_Static_assert for more information. References ---------- -- 2.49.0