When translating C structs that use bitfields, C2Rust generates Rust code using the BitfieldStruct derive macro and #[bitfield(...)] attributes from the c2rust_bitfields crate. However, the generated code does not include the required import:
use c2rust_bitfields::BitfieldStruct;
As a result, the generated Rust code fails to compile.
Reproducer
#include <stdio.h>
#include <stdint.h>
struct PacketHeader {
uint8_t version : 3;
uint8_t type : 2;
uint8_t flags : 3;
uint16_t sequence : 10;
uint16_t length : 6;
};
int main(void) {
struct PacketHeader h = {0};
h.version = 5;
h.sequence = 513;
printf("version=%u sequence=%u\n", h.version, h.sequence);
return 0;
}
Generated Rust (excerpt)
#[derive(Copy, Clone, BitfieldStruct)]
#[repr(C)]
pub struct PacketHeader {
#[bitfield(padding)]
pub c2rust_padding: [u8; 1],
#[bitfield(name = "version", ty = "uint8_t", bits = "0..=2")]
#[bitfield(name = "type_0", ty = "uint8_t", bits = "3..=4")]
#[bitfield(name = "flags", ty = "uint8_t", bits = "5..=7")]
#[bitfield(name = "sequence", ty = "uint16_t", bits = "16..=25")]
#[bitfield(name = "length", ty = "uint16_t", bits = "26..=31")]
pub version_type_0_flags_sequence_length: [u8; 3],
}
Compilation Error:
error: cannot find derive macro `BitfieldStruct` in this scope
--> src/main.rs:16:23
|
16 | #[derive(Copy, Clone, BitfieldStruct)]
| ^^^^^^^^^^^^^^
error: cannot find attribute `bitfield` in this scope
--> src/main.rs:19:7
|
19 | #[bitfield(padding)]
Expected Behavior
When generating Rust code that uses BitfieldStruct and #[bitfield], C2Rust should automatically emit the required import:
use c2rust_bitfields::BitfieldStruct;
so that the generated code compiles without manual modification.
Environment
- c2rust version: v0.21.0
- platform: Ubuntu 24.04
- Rust: nightly-2022-08-08
- Clang version: 17.0.6
When translating C structs that use bitfields, C2Rust generates Rust code using the
BitfieldStructderive macro and#[bitfield(...)]attributes from thec2rust_bitfieldscrate. However, the generated code does not include the required import:As a result, the generated Rust code fails to compile.
Reproducer
Generated Rust (excerpt)
Compilation Error:
Expected Behavior
When generating Rust code that uses
BitfieldStructand#[bitfield], C2Rust should automatically emit the required import:so that the generated code compiles without manual modification.
Environment