Skip to content

size_t == uintptr_t check is incorrect #3312

@pekkarr

Description

@pekkarr

Input C/C++ Header

typedef __SIZE_TYPE__ size_t;

Bindgen Invocation

# When using clang >= 21.1.0
bindgen input.h -- --target=m68k-unknown-linux-gnu

Actual Results

panicked at bindgen/codegen/mod.rs:946:25:
assertion `left == right` failed: Target platform requires `--no-size_t-is-usize`. The alignment of `size_t` (2) does not match the target pointer size (4)
  left: 2
 right: 4

Expected Results

This should produce an empty file without panicking.

Explanation

When the size_t_is_usize option is true, bindgen checks that size_t has the same underlying type as uintptr_t (see #1903). This check is implemented incorrectly as of bindgen 0.72.1.

For size_t to be same as uintptr_t, the types must have the same size and alignment. The actual check however asserts that the alignment of size_t is equal to the size of a pointer, not alignment.

assert_eq!(
layout.align,
ctx.target_pointer_size(),
"Target platform requires `--no-size_t-is-usize`. The alignment of `{spelling}` ({}) does not match the target pointer size ({})",
layout.align,
ctx.target_pointer_size(),
);

This comes up on m68k because both size_t and uintptr_t have size 4 and alignment 2 on that architecture (though some Linux distros plan to change to a 4 byte alignment).

This bug is observable only when using clang 21.1.0 or newer, because the default alignment on m68k was changed in that release.

(This was discovered when porting linux-raw-sys to m68k.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions