Skip to content

Tests: bitpacked coverage (§4) #484

Closed

Description

@ehartford

Tests: bitpacked coverage (§4)

Summary

The compiler has substantial bitpacked implementation code, but the test suite does not cover the full §4.3b contract tracked by the requirements campaign. Add focused behavior and compile-error tests for layout, allowed field types, nested bitpacked structs, address-taking rejection, backing casts, non-standard widths, and masked arithmetic.

Spec

docs/with-specification.md:1478-1570 defines @[bitpacked]: fields occupy exactly their declared bit widths with no padding, layout is MSB-first, field types must have known bit width, bool is one bit, nested bitpacked structs are inlined, field references are not allowed, backing casts expose the underlying bits, and sub-byte/non-standard integer widths exist for this feature.

Current state

  • Parser support exists at src/Parser.w:492-495 and src/Parser.w:1231-1265 for the @[bitpacked] attribute.
  • src/SemaDecl.w:237-249 validates bitpacked field types and emits "bitpacked fields must be integer, bool, or bitpacked struct type" for invalid fields.
  • src/SemaDecl.w:552-553 records bitpacked type IDs.
  • src/Sema.w:1260-1266 registers sub-byte and non-standard integer widths used by bitpacked structs.
  • src/SemaCheck.w:4260-4265 rejects address-of bitpacked fields with "cannot take address of bitpacked field".
  • src/Codegen.w:2888-2920 computes bitpacked backing type, total bits, field offsets, and MSB-first layout.
  • src/CodegenDispatch.w:1466-1501, src/CodegenDispatch.w:3002-3043, and src/CodegenDispatch.w:3424-3456 implement bitpacked extraction, literal construction, and field stores.
  • A scan of test/ did not find dedicated bitpacked behavior/compile-error coverage despite the implementation hooks above.

Implementation plan

  1. Add behavior tests that construct bitpacked values, cast to backing integers, and assert exact bit patterns. Use small fields where MSB-first layout is visually obvious.
  2. Add nested bitpacked tests proving nested bits are inlined without padding.
  3. Add mutable field update tests proving read-modify-write preserves neighboring fields.
  4. Add compile-error tests for invalid field types and taking a pointer/reference to a bitpacked field.
  5. Add tests for non-standard widths (u1, u7, u12, u21, u24 as appropriate) and masking behavior on assignment/arithmetic.

Test plan

  • test/behavior/behav_bitpacked_layout.w: exact backing integer assertions for several field orders and bool fields.
  • test/behavior/behav_bitpacked_nested.w: nested bitpacked struct layout and field access.
  • test/behavior/behav_bitpacked_update.w: mutate one field and assert other fields are unchanged.
  • test/compile_errors/err_bitpacked_invalid_field_type.w: pointer/string/slice/normal struct field rejection.
  • test/compile_errors/err_bitpacked_field_address.w: address-of field rejection.
  • Run with build, with build :fixpoint, and with build :test.

Acceptance criteria

  • Tests prove exact no-padding MSB-first layout.
  • Tests cover bool fields and non-standard integer widths.
  • Tests cover nested bitpacked structs.
  • Compile-error tests cover invalid field types and address-taking.
  • Field mutation preserves adjacent packed fields.

Dependencies

No implementation dependency; this is test coverage for existing code. If a test exposes a behavior bug, fix the root cause rather than weakening the test.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 4.3.3.1, 4.3.3.10, 4.3.3.12, 4.3.3.13, 4.3.3.14, 4.3.3.16, 4.3.3.2, 4.3.3.3, 4.3.3.4, 4.3.3.5, 4.3.3.6, 4.3.3.7, 4.3.3.8.

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing the covered surface: single-level bitpacked (pack/backing-cast/mutation round-trip) + pointer-field and field-address rejections pinned (behav_bitpacked_single, err_bitpacked_pointer_field, err_bitpacked_field_address, ec03406). The nested chained-read case (o.inner.a mis-extracts bits) is a real miscompile filed as #635. Note: spec §4.3b's 0xBC annotation for 1_101_1100 is an arithmetic typo — correct MSB-first value is 0xDC (compiler is right). §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