Files
sx/issues/0052-slice-of-protocol-variadic-not-erased.md
agra d8076b9333 lang: rename signed integer types sN -> iN
Surface rename of the signed integer family: s1..s64 become i1..i64
(u1..u64, usize, isize unchanged). 'string' keeps the s-prefix arm in
name classification; width parsing moves to the i-prefix arm next to
isize.

Internal TypeId tags follow the surface (.s8/.s16/.s32/.s64 ->
.i8/.i16/.i32/.i64), as do mono-key mangle fragments (ptr_i64,
tu_i64_bool) and all display/diagnostic formatting (i{d}).

Migrated in the same sweep: stdlib + examples + issue repros + FFI C
companions (shared symbol names like ffi_id_i64), expected
stdout/stderr/ir snapshots, specs.md, readme.md, CLAUDE.md/AGENTS.md,
implementation_plan.md, docs/, issue writeups. Vendored stb_image and
historical flow state left untouched.

zig build test: 426/426; examples suite: 595/595.
2026-06-12 09:31:53 +03:00

2.4 KiB

FIXED. packVariadicCallArgs (src/ir/lower.zig) now detects a protocol element type and xx-erases each arg into the [N]P array via buildProtocolErasure, instead of storing the raw concrete value. Regression: examples/202-slice-of-protocol-variadic.sx.

Symptom

A slice-of-protocol variadic ..xs: []P (P a protocol) compiles but crashes at runtime (Bus error) the moment an element is used:

Bus error at address 0x3fff

Reproduction

#import "modules/std.sx";
Show :: protocol { show :: () -> string; }
A :: struct { x: i64; }
impl Show for A { show :: (self: *A) -> string => "A"; }

each :: (..xs: []Show) -> void {
    i := 0;
    while i < xs.len { print("{}\n", xs[i].show()); i = i + 1; }
}
main :: () -> i32 { each(A.{ x = 1 }, A.{ x = 2 }); 0; }

Root cause

packVariadicCallArgs packs trailing args into a [N x elem_ty] stack array. It special-cased is_any = (elem_ty == .any) (box each arg to Any), but for any other non-builtin elem_ty it stored the raw lowered arg into the array slot. When elem_ty is a protocol struct (16 bytes {ctx, vtable}), an 8-byte concrete A value was written into a protocol-sized slot — a size/type mismatch producing a garbage {ctx, vtable}. Indexing it and calling .show() then jumped through a bad vtable → Bus error.

Fix

Mirror the xx cast: when the slice element type is a protocol, erase each arg to the protocol value before storing.

const elem_is_protocol = blk: {
    if (elem_ty.isBuiltin()) break :blk false;
    const ei = self.module.types.get(elem_ty);
    break :blk ei == .@"struct" and ei.@"struct".is_protocol;
};
// ... per arg, in the non-`is_any` path:
} else if (elem_is_protocol) {
    var source_ty = self.inferExprType(arg_node);
    if (source_ty == .unresolved) source_ty = self.builder.getRefType(val);
    if (source_ty != elem_ty) val = self.buildProtocolErasure(val, arg_node, source_ty, elem_ty);
}

This makes ..xs: []P the runtime, protocol-erased counterpart to the comptime heterogeneous pack ..xs: P (which stays comptime-only per Decision 1). See specs.md §"Variadic Heterogeneous Type Packs" for the full form-comparison table.

Verification

examples/202-slice-of-protocol-variadic.sx prints [0]=A [1]=B [2]=A and the empty call is a no-op. zig build test + bash tests/run_examples.sh (237) green.