Files
sx/issues/0052-slice-of-protocol-variadic-not-erased.md
agra ab572359ae lang: slice-of-protocol variadic ..xs: []P erases each arg to the protocol
packVariadicCallArgs stored the raw concrete arg into a [N x P] array when the
element type was a protocol, so an 8-byte struct landed in a 16-byte {ctx,
vtable} slot -> garbage vtable -> Bus error on dispatch. Now, when the slice
element type is a protocol, each arg is xx-erased to the protocol value via
buildProtocolErasure (same impl-driven machinery as the xx cast). This makes
..xs: []P the runtime, protocol-erased counterpart to the comptime
heterogeneous pack ..xs: P (which stays comptime-only): xs[runtime_i].method()
now works in an ordinary loop.

specs.md: full variadic/pack form-comparison table (concrete-vs-erased,
comptime-vs-runtime). Regression: examples/202. Issue 0052 (FIXED). 237 green.
2026-05-30 01:50:29 +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: s64; }
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 :: () -> s32 { 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.