Repository navigation
Description
Gap
C can place a field below its natural alignment without packing the whole record to 1: #pragma pack(push, N) with 1 < N < the field's alignment, or __attribute__((packed, aligned(N))). wingdi.h's BITMAPFILEHEADER is the common case: under #pragma pack(2) its DWORD bfSize sits at offset 2 and the record's alignment is 2. winnt.h has more (IMAGE_DOS_HEADER, IMAGE_SYMBOL, IMAGE_RELOCATION, FPO_DATA, ...). The windows.h closure has 29 such records.
With has no spelling for this layout:
- §16.4 "Custom alignment", rule 2: "Alignment must be at least the natural alignment of the type." So a field can't be written
@[align(2)] bfSize: c_ulong. Before c_import: a file-scope record with no tag and no typedef renders as 'type struct (unnamed at ...)' and the import fails to parse (winnt.h C_ASSERT(TYPE_ALIGNMENT(...))) #1396's demotion commit, c_import emitted exactly that and got "alignment is less than natural alignment of type" (146 errors over windows.h). repr(packed)/@[packed](§16.4) "sets alignment to 1 for all fields", so the record's own alignment becomes 1 instead of N. Sizes can still match, but alignment, and so the layout of any record that embeds this one, is wrong.
Current behavior (PR #1419)
Such a record imports opaque (type BITMAPFILEHEADER = opaque, §16.9), the same as a record with bitfields. You can use it by pointer, but not construct it or access its fields.
D51 allows this without a ruling. The asymmetry rule in docs/Ruling-modeled-C-ownership-effects-conventions-and-foreign-lifetimes.md §1: "A convention may be inferred silently when being wrong can only remove capability or reject a valid program." Importing opaque can only remove capability. Emitting an approximate layout could let safe code read or write at the wrong offsets, and is forbidden anyway by §16.4 rule 2.
Wanted
A spec'd layout for "packed to N" records, e.g. a repr(packed(N)) form that caps every field's alignment at N (Rust's #[repr(packed(N))], Zig's per-field align(N) below natural in extern struct). c_import would then emit these records exactly (field offsets, size and alignment all matching clang), and the opaque demotion for below-natural fields would retire. The spec text needs Eric's wording first. Until then these records stay opaque.
Activity
Codegen: a record's embedded-alignment scan is a map lookup, not a la…
D71 facade rulings: ok lists, valid on failed, callback-scope handles…