rust-lang / rust-lang/rust-bindgen

packed type cannot transitively contain a `#[repr(align)]` type

Open
#2,179 10 comments 14 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

rust-for-linux
Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

Input C/C++ Header
#include <stdint.h>

#pragma pack(push, 2)

typedef struct _UART_SETTINGS
{
	uint16_t Baudrate;
	uint16_t spbrg;
	uint16_t brgh;
	uint16_t parity;
	uint16_t stop_bits;
	uint8_t flow_control; /* 0- off, 1 - Simple CTS RTS */
	uint8_t reserved_1;
	union {
		uint32_t bOptions;
		struct
		{
			unsigned invert_tx : 1;
			unsigned invert_rx : 1;
			unsigned half_duplex : 1;
			unsigned reserved_bits : 13;
			unsigned reserved_bits2 : 16;
		} opts;
	} options;
} UART_SETTINGS;

#pragma pack(pop)

Bindgen Invocation
$ bindgen test.h >> test.rs
Actual Results
error[E0588]: packed type cannot transitively contain a `#[repr(align)]` type
   --> src\main.rs:149:1
    |
149 | / pub union _UART_SETTINGS__bindgen_ty_1 {
150 | |     pub bOptions: u32,
151 | |     pub opts: _UART_SETTINGS__bindgen_ty_1__bindgen_ty_1,
152 | | }
    | |_^
    |
note: `_UART_SETTINGS__bindgen_ty_1__bindgen_ty_1` has a `#[repr(align)]` attribute
   --> src\main.rs:156:1
    |
156 | / pub struct _UART_SETTINGS__bindgen_ty_1__bindgen_ty_1 {
157 | |     pub _bitfield_align_1: [u8; 0],
158 | |     pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>,
159 | | }
    | |_^
Expected Results

compile successfully. If bitfields are removed and or packed is removed this work.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue from the provided test.h using bindgen test.h > test.rs, then inspect the generated declarations around src/main.rs:149 and src/main.rs:156. Compare the generated output with bitfields or packing removed; done means the packed, bitfield-containing bindings compile successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.