Skip to content

Tests: ffi layout coverage (§16) #500

Closed

Description

@ehartford

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

  1. Add tests only unless failures reveal an implementation bug.
  2. Create a packed struct fixture with u8 then u32/u16 fields and assert sizeof is the byte sum, not naturally padded size.
  3. 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.
  4. Add a small C header containing a union and import it with c_import; assert the With type can be constructed with one field and has the expected sizeof.
  5. 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

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

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    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.

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