Re: [PATCH 1/1] compiler.h: Add missing include statement for build_bug.h

From: Linus Torvalds
Date: Thu Nov 14 2024 - 13:41:00 EST


On Thu, 14 Nov 2024 at 10:28, Linus Torvalds
<torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> There's probably some trick I'm missing, but yeah, it looks like we
> need our BUILD_BUG_ON_ZERO() thing with that crazy bitfield hack.

.. and right after sending that, I figured out the trick.

You can use 'static_assert()' inside a type definition.

So the way to turn it into an expression is to just use the same
'sizeof(empty struct)' trick that we use for BUILD_BUG_ON_ZERO:

#define Static_assert(a,msg) \
sizeof(struct{_Static_assert(a,msg);})

works as a way to make _Static_assert() be an expression. What a horrid hack.

I don't know if this is worth it, but it does at least have the
advantage of having a message, so that the error case can explain
itself rather than get that odd

"negative width in bit-field"

error message.

I dunno.

Linus