Files
sx/issues/0052-slice-of-protocol-variadic-not-erased.md
agra c21b683b08 docs(issues): mark 17 already-fixed issues RESOLVED with verified banners
Each banner was re-verified against the current binary (repro now behaves
correctly) and cites the actual fix location in current src/** plus the covering
regression example. Closes the stale-but-fixed backlog: 0019, 0042-0056, 0131.
No compiler change.
2026-06-21 09:25:52 +03:00

77 lines
2.9 KiB
Markdown

**FIXED.** `packVariadicCallArgs` ([src/ir/lower.zig](../src/ir/lower.zig))
> **RESOLVED.** A slice-of-protocol variadic `..xs: []P` stored each raw 8-byte
> concrete arg into a 16-byte protocol-sized array slot, yielding garbage
> `{ctx, vtable}` values and a Bus error on `xs[i].method()` dispatch.
> Fixed in `packVariadicCallArgs` (`src/ir/lower/pack.zig`): it now computes
> `elem_is_protocol` and, per arg, `buildProtocolErasure`-erases each concrete
> value into a real protocol value before storing.
> Regression test: `examples/0535-packs-slice-of-protocol-variadic.sx`.
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](../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
```sx
#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.
```zig
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.