Repository navigation
Description
Summary
Implement the missing §16.4 layout validation and safety checks for C-facing layout attributes. This should make @[align], packed field access, and C representation rules loud and predictable instead of accepting invalid ABI promises silently.
Spec
§16.4 says imported C types automatically have repr(C) and manually defined interop types must be explicitly annotated (docs/with-specification.md:8477). repr(packed) implies repr(C), sets field alignment to 1, emits unaligned loads/stores, and creating a reference to a packed field is a compile error (docs/with-specification.md:8492). Custom alignment applies to variables, struct fields, and function parameters (docs/with-specification.md:8518), must be a power of two, at least natural alignment, at most 65536, and violations are compile-time errors (docs/with-specification.md:8536). Local aligned variables require aligned stack allocation and aligned fields raise the struct's own alignment (docs/with-specification.md:8541).
Current state
The parser resets pending attribute state and recognizes packed/bitpacked, but not repr(C), repr(packed), declaration-level align, local align, parameter align, or target as general attributes (src/Parser.w:439, src/Parser.w:492). Struct fields have a special inline @[align(N)] parser that records an integer with no validation (src/Parser.w:1541). Sema has layout helpers that read explicit field alignment slots (src/TypeLayout.w:66) and propagate max field alignment to struct alignment (src/TypeLayout.w:112), while codegen pads structs according to the parsed explicit alignment (src/Codegen.w:2926). The missing root cause is that layout attributes are parsed as ad hoc flags/integers instead of a validated semantic attribute model, so invalid values like @[align(3)] or @[align(131072)] reach layout/codegen, packed-field references are treated like ordinary field projections, and repr(C) is effectively no-op syntax.
Implementation plan
- Extend
Parser.skip_attributesand declaration parsers insrc/Parser.wto representrepr(C),repr(packed), andalign(N)uniformly for type declarations, fields, locals, and parameters. Keep existing@[packed]as a compatibility spelling if desired, but canonicalize it to the same internal representation as@[repr(packed)]. - Add Sema validation helpers in
src/SemaCheck.wusingSema.type_layout_align_offromsrc/TypeLayout.w: reject non-power-of-two alignment, alignment below natural type alignment, and alignment above 65536 with diagnostics such asalignment must be a power of two,alignment is less than natural alignment of type, andalignment exceeds maximum 65536. - Validate declaration placement:
repr(C)andrepr(packed)apply only to layout-bearing type declarations;align(N)applies only to variables, struct fields, and function parameters per §16.4. Reject misplaced uses instead of ignoring them. - Teach Sema to identify packed type field projections.
Sema.check_field_accesscurrently returns field types for all structs/generic structs without packed/ref checks (src/SemaCheck.w:5522); add a check for reference creation over packed fields in unary reference/address paths and emitcannot create reference to packed field. - Carry validated local/parameter alignment to MIR/codegen. Struct field alignment already has codegen support (src/Codegen.w:2936); locals need aligned stack allocas, and parameter alignment should either be represented as an ABI/precondition attribute or rejected until the backend can honor it.
- Add repr coverage to C and LLVM backends:
repr(C)should pin source field order and C ABI layout;repr(packed)should keep current packed LLVM struct lowering and unaligned access behavior. Do not silently accept a representation the backend cannot honor.
Test plan
Add test/spec/spec_ss16_4_layout_attributes.w for positive repr(C), repr(packed), field @[align(16)], and size/alignment assertions. Add compile-error tests under test/compile_errors/: err_align_not_power_of_two.w, err_align_too_large.w, err_align_less_than_natural.w, err_align_wrong_target.w, and err_packed_field_ref.w. Add one behavior test for an aligned field increasing alignof[Struct]() and one packed layout test that proves no padding via sizeof.
Acceptance criteria
- Invalid
@[align(3)],@[align(131072)], and below-natural alignments fail at check time. @[align]on locals, fields, and parameters is either honored or rejected loudly for unsupported placements; no placement is silently ignored.- Creating
&packed.fieldfails while value reads/writes still lower with unaligned access. repr(C)andrepr(packed)have committed positive and negative tests.with build && with build :fixpoint && with build :testpass.
Dependencies
Overlaps #500 for ffi layout coverage. No hard dependency.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.4.1.12, 16.4.1.13, 16.4.1.14, 16.4.1.15, 16.4.1.16, 16.4.1.17, 16.4.1.18, 16.4.1.2, 16.4.1.5.
Activity
Layout attribute validation: @[align] rules, packed-field refs, repr (#…
ehartford commentedon Jun 16, 2026
Implemented in feca151. @[repr(C)]/@[repr(packed)] now parse (repr(packed) drives packed layout; bad args error). Field @[align(N)] is validated in collect_type_decl — power of two, ≤65536, ≥ natural alignment, each a compile error. Packed struct types are tracked and &/&raw references to a packed field are rejected. alignofT reports the layout-model alignment (honoring @[align]), matching Type.align() and the struct's size/stride. Tests: spec_ss16_4_layout_attributes plus err_align_not_power_of_two / err_align_too_large / err_align_less_than_natural / err_packed_field_ref. build + fixpoint + compile-error/spec/behavior suites pass.
Note: local/parameter @[align] placements and full sound over-alignment of allocation start addresses (alloca alignment escalation) are not part of this change; alignof/size/stride honor @[align] but guaranteed over-aligned storage is a follow-up.