Skip to content

c_import: exact packed(N) record layouts #1421

Closed

Description

@ehartford

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:

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

  1. added a commit that references this issue on Sep 27, 2026
    bb571e6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions