rust-lang / rust-lang/rust-bindgen
Avoid assigning (wrong) types to constants using #define
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 5.3k
- Forks
- 829
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 15
Description
Input C/C++ Header
#define MYCONST 2
Bindgen Invocation
$ bindgen input.h
Actual Results
/* automatically generated by rust-bindgen 0.59.1 */
pub const MYCONST: u32 = 2;
Expected Results
It would be better to generate something like
macro_rules! MYCONST { () => { 2 }; }
and to expect users to use MYCONST!() instead of MYCONST.
Reasoning
Constants using #define in a C header file do not have a type. They are inserted by the preprocessor as a literal where needed. Translating them to a Rust const with a type (u32, i32, or others) may lead to unexpected results. For example, i32 can be interchaged with std::os::raw::c_int on most platforms. But code that relies on that will not be portable. See discussion on Rust Users Forum "Interfacing C code with bindgen: #define and types".
Migration
Maybe it can be made possible to enable this new described behavior with an option that is not enabled by default; or the generated macro_rules!s could be created in addition to the consts (unless being switched off by an option).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the input.h example and run the bindgen invocation shown to compare the current handling of #define constants with the expected output. Review the constant-generation entry point and decide how an opt-in or default behavior should avoid assigning a fixed Rust type. Done means the migration behavior is settled and the generated result matches the agreed macro_rules! form.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, cpp, rust
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100