util: make STATIC_ASSERT usable at file scope #1952

pull real-or-random wants to merge 2 commits into bitcoin-core:master from real-or-random:worktree-better-static-assert changing 3 files +55 −63
  1. real-or-random commented at 3:41 PM on October 1, 2026: contributor

    Replace the switch-based STATIC_ASSERT, which is a statement and thus only usable inside functions, with a declaration-like macro. It uses _Static_assert in C11 mode and, as an extension, with GCC >= 4.6 and Clang in pre-C11 modes. Other compilers fall back to declaring an extern array whose size is negative if the assertion fails.

    Since STATIC_ASSERT now expands to a declaration, move its use in secp256k1_ge_impl_to_bytes before VERIFY_CHECK to avoid mixing declarations and code, which C89 forbids.

  2. util: make STATIC_ASSERT usable at file scope
    Replace the switch-based STATIC_ASSERT, which is a statement and thus
    only usable inside functions, with a declaration-like macro. It uses
    _Static_assert in C11 mode and, as an extension, with GCC >= 4.6 and
    Clang in pre-C11 modes. Other compilers fall back to declaring an
    extern array whose size is negative if the assertion fails.
    
    Since STATIC_ASSERT now expands to a declaration, move its use in
    secp256k1_ge_impl_to_bytes before VERIFY_CHECK to avoid mixing
    declarations and code, which C89 forbids.
    7d53109b85
  3. real-or-random added the label tweak/refactor on Oct 1, 2026
  4. real-or-random added the label meta/development on Oct 1, 2026
  5. assumptions: move static assertions to file scope
    Now that STATIC_ASSERT can be used at file scope, the
    secp256k1_assumption_checker function, which existed only to provide a
    function context for the assertions, is no longer needed.
    20fcbae219
  6. theStack commented at 5:10 PM on October 2, 2026: contributor

    Concept ACK

  7. in src/util.h:76 in 7d53109b85
      80 | -        case /* ERROR: static assertion failed */ (expr): \
      81 | -        ; \
      82 | -    } \
      83 | -} while(0)
      84 | +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
      85 | +#  define STATIC_ASSERT(expr) _Static_assert(expr, #expr)
    


    theStack commented at 2:43 PM on October 5, 2026:

    potential follow-up nit: seems like currently only the clang-cl Windows CI job is hitting this conditional compilation block (verified by putting garbage inside, see https://github.com/theStack/secp256k1/commit/1d8e8948e0e2e06616f73c1a729cff1e31bd5740), maybe long-term it would make sense to add an explicit C11 mode CI job


    real-or-random commented at 9:21 AM on October 7, 2026:

    Agreed... Or maybe we want to reconsider upgrading to --std=c99 at least.

  8. in src/util.h:82 in 7d53109b85
      86 | +#elif !defined(__cplusplus) && (defined(__clang__) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))))
      87 | +/* GCC >= 4.6 and Clang support _Static_assert as an extension in pre-C11 modes,
      88 | + * but g++ does not support it in C++. */
      89 | +#  define STATIC_ASSERT(expr) __extension__ _Static_assert(expr, #expr)
      90 | +#else
      91 | +#  define STATIC_ASSERT(expr) extern int secp256k1_static_assert_dummy[(expr) ? 1 : -1]
    


    theStack commented at 2:57 PM on October 5, 2026:

    heh, i've only seen the dummy[1 - 2 * !!(expr)] approach to achieve the same and found that always a bit weird, using a ternary seems more straight-forward

  9. theStack approved
  10. theStack commented at 3:04 PM on October 5, 2026: contributor

    ACK 20fcbae2197486b5dce996eb246677c2c7b73595

  11. theStack merged this on Oct 19, 2026
  12. theStack closed this on Oct 19, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-10-23 09:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me