Skip to content

emit-C ignores @[align(N)] (§16.4): a record's C struct is C's natural layout, not TypeLayout's (Al is 16 bytes in C, 64 in the model) #2022

Closed

Description

@ehartford

Found while fixing #2017 (multi-field enum payloads in the C backend).

Spec §16.4 / with-abi.md §2: a field's @[align(N)] places it at TypeLayout's offset and gives the record that alignment. The C backend emits no alignment for it (grep _Alignas src/CCodegen.w is empty), so the C struct has C's natural layout while every TypeLayout-derived constant (comptime T.size(), element sizes) uses the model's.

Repro (main c884c1c, release binary):

type Al { a: i8, @[align(32)] b: i64 }
fn main:
    var v: Vec[Al] = Vec.new()
    for i in 0..4: v.push(Al { a: i as i8, b: i as i64 * 10 })
    var s: i64 = 0
    for x in v: s = s + x.b + x.a as i64
    let addr1 = &raw const v[1].b
    print(f"{s} {(addr1 as i64) % 32}")
  • emitted C: struct Al { int8_t a; int64_t b; }; (16 bytes; model 64, comptime Al.size() prints 64 in both backends)
  • LLVM backend prints 66 16, the emitted C program 66 8.

Expected: the C struct carries the model's alignment (_Alignas(N) on the field), so C's sizeof/offsets equal TypeLayout's, or emission refuses loudly. Note the LLVM backend's 16 is itself below the declared 32: a Vec buffer of an over-aligned element is not allocated at the element's alignment — that half may be its own bug.

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