From 0b8e94773659d7d551e23982fc7f9372ac7f49e1 Mon Sep 17 00:00:00 2001 From: agra Date: Fri, 29 May 2026 17:45:22 +0300 Subject: [PATCH] lang 2.4: bind protocol-constrained packs (per-shape mono, concrete elements) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `..xs: Protocol` now binds like the comptime `..$args` pack instead of falling through to a runtime `[]Protocol` slice: each call site monomorphizes with the concrete per-position arg types, and `xs[i]` is the concrete element via AST substitution (Decision 1 — a pack is a comptime mechanism, no runtime pack value). So `xs[i]`'s own fields/methods dispatch statically and elements may be heterogeneous, while `xs.len` is a comptime constant. Mechanism: one `isPackParam(p) = is_variadic and (is_comptime or is_pack)` predicate replaces the four `is_variadic and is_comptime` pack-detection sites (call-arg split, mangle, arg lowering, monomorphizePackFn), and the early call dispatch routes any `isPackFn` call to `lowerPackFnCall` before the `hasComptimeParams` gate (which is false for a protocol pack). examples/191-protocol-pack.sx exercises N=0, N=2, concrete field access, and a heterogeneous IntBox+StrBox pack. Conformance checking and projection (`xs.T` / `xs.value`) are the remaining 2.4 work. --- examples/191-protocol-pack.sx | 46 +++++++++++++++++++++++++++ src/ir/lower.zig | 26 ++++++++++----- tests/expected/191-protocol-pack.exit | 1 + tests/expected/191-protocol-pack.txt | 4 +++ 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 examples/191-protocol-pack.sx create mode 100644 tests/expected/191-protocol-pack.exit create mode 100644 tests/expected/191-protocol-pack.txt diff --git a/examples/191-protocol-pack.sx b/examples/191-protocol-pack.sx new file mode 100644 index 0000000..be9a57a --- /dev/null +++ b/examples/191-protocol-pack.sx @@ -0,0 +1,46 @@ +// Feature 1 — heterogeneous protocol-constrained variadic pack. +// +// `..xs: Show` (no `[]`, no `$`) is a pack, not a slice: each call site +// monomorphizes with the concrete per-position arg types, and `xs[i]` IS the +// concrete element (comptime substitution, Decision 1) — so its own fields / +// methods dispatch statically. `xs.len` is a comptime constant. Elements may +// be DIFFERENT concrete types as long as each conforms to the protocol. +// +// (Conformance checking and projection `xs.T` / `xs.value` are still to come; +// this locks in the per-shape binding + concrete-element access.) + +#import "modules/std.sx"; + +Show :: protocol(T: Type) { + get :: (self: *Self) -> T; +} + +IntBox :: struct { v: s64; } +StrBox :: struct { s: string; } +impl Show(s64) for IntBox { get :: (self: *IntBox) -> s64 => self.v; } +impl Show(string) for StrBox { get :: (self: *StrBox) -> string => self.s; } + +howmany :: (..xs: Show) -> s64 { + return xs.len; +} + +sum2 :: (..xs: Show) -> s64 { + return xs[0].v + xs[1].v; +} + +describe :: (..xs: Show) -> void { + // Heterogeneous: xs[0] is an IntBox, xs[1] a StrBox. + print("describe len={} first={} second={}\n", xs.len, xs[0].v, xs[1].s); +} + +main :: () -> s32 { + a := IntBox.{ v = 42 }; + b := IntBox.{ v = 7 }; + print("count0={}\n", howmany()); // N=0 pack + print("count2={}\n", howmany(a, b)); // N=2 + print("sum={}\n", sum2(a, b)); // concrete field access via pack + + c := StrBox.{ s = "cool" }; + describe(a, c); // heterogeneous IntBox + StrBox + 0; +} diff --git a/src/ir/lower.zig b/src/ir/lower.zig index 9d40608..63433c8 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -5806,10 +5806,12 @@ pub const Lowering = struct { break :blk scoped; }; if (self.fn_ast_map.get(early_name)) |fd| { + if (isPackFn(fd)) { + // Protocol packs (`..xs: P`) and comptime type-packs + // (`..$args`) both monomorphize per call shape. + return self.lowerPackFnCall(fd, c); + } if (hasComptimeParams(fd)) { - if (isPackFn(fd)) { - return self.lowerPackFnCall(fd, c); - } return self.lowerComptimeCall(fd, c); } // Early detection of generic function calls — skip arg lowering for type params @@ -8586,7 +8588,7 @@ pub const Lowering = struct { var pack_start: usize = call_node.args.len; var fi: usize = 0; for (fd.params) |p| { - if (p.is_variadic and p.is_comptime) { + if (isPackParam(p)) { pack_start = fi; break; } @@ -8613,7 +8615,7 @@ pub const Lowering = struct { // Comptime values first (deterministic by fd.params order). var ct_fi: usize = 0; for (fd.params) |p| { - if (p.is_variadic and p.is_comptime) break; + if (isPackParam(p)) break; if (ct_fi >= call_node.args.len) break; if (p.is_comptime) { name_buf.appendSlice(self.alloc, "__ct_") catch return self.builder.constInt(0, .void); @@ -8638,7 +8640,7 @@ pub const Lowering = struct { defer args.deinit(self.alloc); var ri: usize = 0; for (fd.params) |p| { - if (p.is_variadic and p.is_comptime) break; + if (isPackParam(p)) break; if (ri >= call_node.args.len) break; if (!p.is_comptime) { args.append(self.alloc, self.lowerExpr(call_node.args[ri])) catch return self.builder.constInt(0, .void); @@ -8713,7 +8715,7 @@ pub const Lowering = struct { var pack_name: []const u8 = ""; var pack_param_idx: usize = std.math.maxInt(usize); for (fd.params, 0..) |p, i| { - if (p.is_variadic and p.is_comptime) { + if (isPackParam(p)) { pack_name = p.name; pack_param_idx = i; break; @@ -10168,11 +10170,19 @@ pub const Lowering = struct { /// bare-name body references). fn isPackFn(fd: *const ast.FnDecl) bool { for (fd.params) |p| { - if (p.is_comptime and p.is_variadic) return true; + if (isPackParam(p)) return true; } return false; } + /// A trailing pack parameter: the comptime type-pack `..$args` + /// (`is_comptime`) or the protocol-constrained pack `..xs: P` (`is_pack`). + /// Both monomorphize per call shape via `lowerPackFnCall`; the slice + /// variadic (`..xs: []T`) is neither and stays a runtime slice. + fn isPackParam(p: ast.Param) bool { + return p.is_variadic and (p.is_comptime or p.is_pack); + } + /// Creates a temporary function marked `is_comptime = true` that wraps /// the given expression as its return value. Returns the FuncId. pub fn createComptimeFunction(self: *Lowering, prefix: []const u8, expr: *const Node, ret_ty: TypeId) FuncId { diff --git a/tests/expected/191-protocol-pack.exit b/tests/expected/191-protocol-pack.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/tests/expected/191-protocol-pack.exit @@ -0,0 +1 @@ +0 diff --git a/tests/expected/191-protocol-pack.txt b/tests/expected/191-protocol-pack.txt new file mode 100644 index 0000000..bc7aed5 --- /dev/null +++ b/tests/expected/191-protocol-pack.txt @@ -0,0 +1,4 @@ +count0=0 +count2=2 +sum=49 +describe len=2 first=42 second=cool