Skip to content

Use C11 static_assert

Erik Faye-Lund requested to merge kusma/mesa:werror-vla-msvc-compat into main

Since we're using C11 as well as C++14, we should now be able to use static_assert instead of our own open-coded, compiler-specific STATIC_ASSERT-macros.

However, it turns out, we're doing few things wrong when we do so:

  1. In C, we can't pass variables to static_assert() at all. It's not guaranteed to be supported by the standard, and some in-use versions if both GCC and Clang errors out, no matter how many const-specifiers are used. So we either do what we can to turn conditions constant, or we switch to plain-old assert(). The current implementation seems to more or less just work because we're lucky and things gets inlined enough for the compiler to see what's going on. It's not guaranteed to work as far as I can tell.
  2. _Static_assert (which the static_assert macro expands to) is a declaration, not a statement. And even if statements are allowed anywhere inside blocks, they are not allowed in swtich-cases. So we need to insert a block in these cases.

In the long run, we should probably also clean up things a bit more, by removing STATIC_ASSERT and use static_assert directly. But that's quite a bit more work, and this series is already complicated enough.

The end-goal here is to re-enable -Werror=vla in c_msvc_compat_args et al, to avoid needless MSVC regressions in the future.

Merge request reports