Repository navigation
Description
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-495andsrc/Parser.w:1231-1265for the@[bitpacked]attribute. src/SemaDecl.w:237-249validates bitpacked field types and emits "bitpacked fields must be integer, bool, or bitpacked struct type" for invalid fields.src/SemaDecl.w:552-553records bitpacked type IDs.src/Sema.w:1260-1266registers sub-byte and non-standard integer widths used by bitpacked structs.src/SemaCheck.w:4260-4265rejects address-of bitpacked fields with "cannot take address of bitpacked field".src/Codegen.w:2888-2920computes bitpacked backing type, total bits, field offsets, and MSB-first layout.src/CodegenDispatch.w:1466-1501,src/CodegenDispatch.w:3002-3043, andsrc/CodegenDispatch.w:3424-3456implement 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
- 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.
- Add nested bitpacked tests proving nested bits are inlined without padding.
- Add mutable field update tests proving read-modify-write preserves neighboring fields.
- Add compile-error tests for invalid field types and taking a pointer/reference to a bitpacked field.
- Add tests for non-standard widths (
u1,u7,u12,u21,u24as 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, andwith 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
ehartford commentedon Jul 3, 2026
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.
test: coverage-sweep batch 2 (#483 #484 #522 #493 #494 #514 #515 #532)