Skip to content

Add opt-in C23 constexpr scalar constants - #1190

Open
GtechGovind wants to merge 2 commits into
mozilla:mainfrom
GtechGovind:feat/c23-constexpr-constants
Open

GtechGovind wants to merge 2 commits into
mozilla:mainfrom
GtechGovind:feat/c23-constexpr-constants

Conversation

@GtechGovind

@GtechGovind GtechGovind commented Oct 9, 2026 •

Copy link
Copy Markdown

I added an opt-in [const] allow_constexpr_in_c setting for typed primitive C23 constants. It defaults to false, so existing C headers stay unchanged, and it is independent of the C++ settings.

I reused the existing constexpr eligibility check and initializer renderer instead of inspecting literal spellings or restricting arithmetic operators. Primitive constants receive an explicit conversion to the declared type; pointers, arrays, aggregates and aliases keep their macro output. The documentation covers the C23 compiler requirement, initializer diagnostics and preprocessor compatibility implications.

The C23 tests now use the existing fixture harness and checked-in expectations across all three C declaration styles, C++ compatibility, C++ and Cython. The fixture covers scalar types, bounds, arithmetic, shifts, compound negation, associated constants and macro fallbacks. The harness selects -std=c23 for fixtures that enable the setting, and CI installs GCC 14 for the compiler checks.

Validation:

  • CBINDGEN_TEST_VERIFY=1 CC=/opt/homebrew/bin/gcc-16 cargo +nightly test with Cython 3.0.2: 176 passed, zero failed; one existing doc test ignored. All 161 pre-existing fixtures passed unchanged.
  • Focused fixture verification with CC=/usr/bin/clang: passed with Apple Clang 21, including C++ and Cython variants.
  • cargo +stable fmt --check, cargo +stable clippy --workspace -- -D warnings, cargo +stable build --no-default-features and git diff --check: passed.
  • cargo +1.74.0 test nonexistent-test --verbose: passed the repository's MSRV compilation check (no tests selected).
  • cargo +stable package --allow-dirty: packaging and verification build passed. Full nightly tests from the packaged crate: 148 passed, zero failed; one existing doc test ignored.

Shared literal lowering is unchanged. Generated initializers still need to be valid C23 constant expressions; this change does not infer Rust intermediate types or evaluate constants. The additional cargo +stable clippy --all-targets -- -D warnings check reports the pre-existing eight-argument run_cbindgen harness function; the prescribed workspace check passes. Linux/ARM CI has not been reproduced locally.

Fixes #1095.

Comment thread tests/constant_c23.rs Outdated
Comment thread src/bindgen/ir/constant.rs Outdated
Comment thread src/bindgen/ir/constant.rs Outdated
Comment thread src/bindgen/ir/constant.rs Outdated
Comment thread src/bindgen/ir/constant.rs Outdated
@GtechGovind
GtechGovind marked this pull request as ready for review October 17, 2026 06:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Allow constexpr in C

2 participants