Skip to content

Layout attribute validation: @[align] rules, packed-field refs, repr coverage (§16.4) #449

Closed

Description

@ehartford

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

  1. Extend Parser.skip_attributes and declaration parsers in src/Parser.w to represent repr(C), repr(packed), and align(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)].
  2. Add Sema validation helpers in src/SemaCheck.w using Sema.type_layout_align_of from src/TypeLayout.w: reject non-power-of-two alignment, alignment below natural type alignment, and alignment above 65536 with diagnostics such as alignment must be a power of two, alignment is less than natural alignment of type, and alignment exceeds maximum 65536.
  3. Validate declaration placement: repr(C) and repr(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.
  4. Teach Sema to identify packed type field projections. Sema.check_field_access currently 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 emit cannot create reference to packed field.
  5. 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.
  6. 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.field fails while value reads/writes still lower with unaligned access.
  • repr(C) and repr(packed) have committed positive and negative tests.
  • with build && with build :fixpoint && with build :test pass.

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

  1. ehartford commented on Jun 16, 2026

    @ehartford
    ContributorAuthor

    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.

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