Skip to content

Array literal passed to a []T parameter segfaults: the literal is typed as the slice and lowered as its fat pointer #1229

Closed

Description

@ehartford

Silent miscompile. Compiles clean, --validate-all says ok, segfaults at run time.

fn first(values: []i32) -> i32:
    if values.len() == 0: return -1
    values[0]

fn main:
    print(f"{first([5, 6])}")     // exit 139

first(xs) with let xs = [5, 6], a Vec, an empty Vec and xs[0..2] all work.

Root cause (exact lines).

  • src/SemaCheck.w check_array_literal (~12157): under an expected TY_SLICE it sets target_ty to the slice type, so the literal is typed as []i32. The call is then type-compatible, and note_slice_coerce_call_arg (~925), which would have rejected a non-place argument with "pass a named variable here…", is never reached.
  • src/MirLower.w NK_ARRAY_LIT (~13991-13997): emits RK_AGGREGATE with the elements as fields into a temp of expr_type(node), which is the slice type. MIR: _2 = aggregate(fields=[const 5, const 6]); call first(copy _2). The named case emits _5 = slice(_1, 0, len); the literal case emits no slice.
  • Codegen passes that aggregate as the fat pointer: {ptr = 5, len = 6}.

Same cause as: memcmp([1u8, 2u8], [1u8, 3u8]) through a modeled c_import buffer parameter panics "buffer arguments must have equal length".

Expected. Either the literal works (build the [T; N] backing array in a temp that lives for the statement, then slice it; the mission-preferred answer: a literal argument is the obvious spelling), or Sema rejects it with the existing diagnostic. Never a clean compile and a crash.

Tool that stayed silent. --validate-all reports ok for an aggregate whose field count and types do not match its declared slice type. The MIR typed validator should reject aggregate into a TY_SLICE place.

Found while rewriting examples/c-interop.

Activity

  1. added a commit that references this issue on Sep 22, 2026
    83b3f1f
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