Repository navigation
Description
Summary
Add missing tests for C-facing layout behavior that is already partially implemented: packed structs, unaligned field access, and C union import/lowering. This should lock down §16.4 behavior before larger layout-attribute work changes the same code.
Spec
§16.4 says repr(packed) implies C layout with alignment 1, no padding, and unaligned loads/stores (docs/with-specification.md:8492). Unions share offset 0, have the size of their largest field, and c_import translates C union declarations directly (docs/with-specification.md:8511).
Current state
The parser has a packed type flag and union type declarations (src/Parser.w:492, src/Parser.w:1339). Sema layout computes union field offsets as 0 and union size as max field size rounded to max alignment (src/TypeLayout.w:86, src/TypeLayout.w:139). Codegen declares unions as storage for the largest field and maps every source field to offset 0 (src/Codegen.w:2980). Aggregate lowering stores union initializers through the alloca at offset 0 (src/CodegenDispatch.w:3060). Existing implementation notes say packed layout and union translation work, but the committed tests do not directly prove these requirements.
Implementation plan
- Add tests only unless failures reveal an implementation bug.
- Create a packed struct fixture with
u8thenu32/u16fields and assertsizeofis the byte sum, not naturally padded size. - Exercise packed field reads/writes by value. Do not test taking references here; that is an implementation gap in Layout attribute validation: @[align] rules, packed-field refs, repr coverage (§16.4) #449.
- Add a small C header containing a
unionand import it withc_import; assert the With type can be constructed with one field and has the expectedsizeof. - Add a behavior test for writing one union field and reading the same field. Cross-field unsafe read semantics are Union safety rules: single initializer, unsafe non-last-written reads (§16.4) #542, so do not mark that accepted as safe in this coverage issue.
Test plan
Add test/spec/spec_ss16_4_packed_layout.w for packed size and value access. Add test/behavior/behav_c_import_union_layout.h and .w for C union translation and size. Add the new files to any test corpus/manifest if this repo requires explicit registration.
Acceptance criteria
- Packed layout size and unaligned value access are covered.
- C union declarations imported by
c_importare covered. - Tests avoid blessing unsafe non-last-written union reads; that belongs to Union safety rules: single initializer, unsafe non-last-written reads (§16.4) #542.
with build && with build :fixpoint && with build :testpass.
Dependencies
Overlaps #449 for packed-field reference rejection and #542 for union safety rules.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.4.1.11, 16.4.1.3, 16.4.1.4.
Activity
ehartford commentedon Jul 3, 2026
Closing — c_import union layout (largest-member size + last-write read: behav_c_import_union_layout) and @[repr(packed)] field value round-trip (spec_ss16_4_packed_value_access) pinned (3628d82); packed sizeof + native union already covered (spec_ss16_4_layout_attributes, behav_union_layout, spec_ss16_4_union_safety). §16.4.
test: coverage-sweep batch 5 (#485 #486 #487 #500 #505 #510)