rust-lang / rust-lang/rust-bindgen
Doesn't handle `static const` structs correctly
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
struct foo {
int a;
int b;
};
static const struct foo FOO1 = {
.a = 12,
.b = 23,
};
Bindgen Invocation
$ bindgen input.h
Actual Results
/* automatically generated by rust-bindgen 0.54.1 */
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct foo {
pub a: ::std::os::raw::c_int,
pub b: ::std::os::raw::c_int,
}
#[test]
fn bindgen_test_layout_foo() {
assert_eq!(
::std::mem::size_of::<foo>(),
8usize,
concat!("Size of: ", stringify!(foo))
);
assert_eq!(
::std::mem::align_of::<foo>(),
4usize,
concat!("Alignment of ", stringify!(foo))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).a as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(foo), "::", stringify!(a))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<foo>())).b as *const _ as usize },
4usize,
concat!("Offset of field: ", stringify!(foo), "::", stringify!(b))
);
}
extern "C" {
pub static FOO1: foo;
}
Expected Results
I'm not really sure what I expected to happen, but creating a broken binding seems very misleading. The FOO1 static will not ever work correctly. In C that static const will not be available at link-time, so attempting to use the constant in Rust just creates a confusing link error.
input.7rcbfp3g-cgu.0:(.text._ZN5input4main17h59f421c58bec5f03E+0x7): undefined reference to `FOO1'
It would be great if bindgen could convert the struct literal into an equivalent Rust one, but I suspect that is difficult. Note that the bindings do work fine here for (some?) other types, if I make a static const int BAR = 12, it does generate pub const BAR: ::std::os::raw::c_int = 12;
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 by reproducing the example from the issue with bindgen 0.54.1 and inspect how static const struct declarations are converted into Rust bindings. Done should mean the generated binding does not create an unusable link-time reference to FOO1, with the chosen behavior covered by an appropriate regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c, rust
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100