Skip to content

migrate: bitfield records import opaque; STC's hmap/hset and coroutine runtime need C's bitfield layout (brief) #2553

Closed

Description

@ehartford

Brief: C bitfield records in with migrate (STC Phase 3 blocker)

The blocker. STC's default hash map keeps its probe metadata in
struct hmap_meta { uint16_t hashx:6, dist:10; } (hmap.h:57). §16.9 says a
record with bitfields imports opaque, so every hmap instantiation is unusable
after migration (57 "field access requires a concrete struct" errors in
hmap_test.w). The ruling of 2026-09-12 makes STC hmap/hset With's default
HashMap/HashSet engine, so Phase 3 cannot finish without bitfield records.

What the others do (verified in .reference/). Zig's translate-c
(.reference/zig/lib/compiler/aro_translate_c, translate-c/src): a record
with bitfields is demoted to opaque, as With does today. Rust bindgen: emits a
__BindgenBitfieldUnit<[u8; N]> storage field plus generated getter/setter
methods per bitfield, bit offsets from clang's layout. Swift's ClangImporter:
imports bitfields as computed properties over the storage, offsets from clang.
Go cgo: bitfields are not accessible (padding). Mojo: no C record import.

What the spec says. §16.9: "c_import emits type Name = opaque for
forward-declared C structs (no body) and structs with bitfields." §4.3b gives
With its own @[bitpacked] structs — MSB-first, no padding — which is not C's
layout (LSB-first within each storage unit on every supported target, Itanium
and MSVC alike), so @[bitpacked] cannot be the mirror.

Proposal (the migrator, not c_import). A migrated record with bitfields
keeps C's layout: the bitfields that share a storage unit (clang's layout:
clang_Cursor_getOffsetOfField, clang_getFieldDeclBitWidth) become one
field of the declared integer type, __bits_<n>: u16 = 0, under @[repr(C)],
so sizeof and every non-bitfield neighbour stay C's. A read of a bitfield
member lowers to ((m.__bits_0 >> 6) & 0x3ff) cast to the member's type; a
write to the read-modify-write m.__bits_0 = (m.__bits_0 & ~(0x3ff << 6)) | ((v as u16 & 0x3ff) << 6); ++, += and the rest through those two. Signed
members sign-extend from their width. &m->dist is refused (C refuses it
too). A designated initializer names the member and lowers to the same stores.
The record is no longer opaque; c_import (hand c_import in programs) keeps
§16.9 unless you extend it the same way.

Mission fit. Law 1 (the compiler knows the layout: clang hands it over),
D37 (the migrator is raw and faithful — bit arithmetic is what the C compiler
emits too), §Runtime "modeled C becomes humane" comes later, in the facade.

Prediction. 85% you take the proposal for with migrate as written and
leave §16.9's c_import sentence as is for now; the alternative you might
prefer is extending c_import the same way in the same change (one rule for
both importers), which I would also do if you say so.

Also blocked by the same rule (found 2026-10-22): STC's coroutine runtime (src/stc_core.c): struct cco_group { int32_t spawn_count:24; uint32_t on_error:7; uint32_t cancelled:1; } and the task state's n_scopes:7, scoped:1, so stc_core.c is excluded from the std.stc corpus beside cregex until this lands, and hmap_test is excluded from the compiled test programs.

This is a C-interop decision (D98: Eric's). The brief above is the proposal; the migrator work follows the ruling.

Activity

  1. ehartford commented on Oct 22, 2026

    @ehartford
    ContributorAuthor

    Ruled (D147, docs/meetings/2026-10-22-D147-bitfield-records-in-with-migrate.md) and implemented in #2559 (main 8e7f0f3): storage units under @[repr(C)], a getter and a setter per member, stores that truncate to the width, refusals for a unit Itanium and MSVC lay out differently / a packed unaligned unit / a volatile member, and a comptime layout assertion on the migrating data model. STC's stc_core and hmap_test are in the corpus: 8 of 8 upstream programs pass. c_import keeps §16.9's demotion for now, with the spec note that the migrator's representation is the intended one for both.

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