Skip to content

emit-C: Option[&T] / Option[Box[T]] / Option[*T] are tagged structs in C, not with-abi §3's nullable pointer; records holding one diverge from TypeLayout #2036

Closed

Description

@ehartford

Found by #2022's new emit-C layout asserts (_Static_assert(sizeof/_Alignof/offsetof == TypeLayout's) on every emitted record).

with-abi.md §3: Option[T] whose payload is a single non-null address (&T, *T, an extern fn, a std Box[T]) lowers to that nullable pointer (8 bytes). The C backend emits every Option as {int32_t tag; union {...}} (16 bytes), so:

  • Option[&i32]: C 16 bytes, model 8.
  • type Node { v: i32, next: Option[Box[Node]] } (test/emit_c/generic_struct_instances.w): C 24 bytes, model 16.

Inside one C program the representation is self-consistent, so the corpus passes. But every TypeLayout-derived constant (comptime T.size(), offsets, element sizes the compiler computes from the model) disagrees with the C object, and the C program's data is not the ABI the LLVM backend's is.

#2022 asserts every record except those that hold a nullable-pointer Option (CCodegen.record_holds_nullable_option). That exemption is this issue, and it goes away when the C backend represents these Options as the pointer: Some(x) = x, None = NULL, discriminant from the null test, the payload downcast being the value itself.

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on fix-2036 (PR #2044, commit e607ab3). Nullable-pointer Options are the pointer in emitted C, and every emitted record's layout is asserted against TypeLayout with no exemption. Fixture: test/emit_c/option_nullable_pointer.w.

  2. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main (stack #2045: #2037 + #2044; seed-driven battery GREEN on 0577d56, tree identical to main).

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