lang 2.4: bind protocol-constrained packs (per-shape mono, concrete elements)
`..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.
This commit is contained in:
46
examples/191-protocol-pack.sx
Normal file
46
examples/191-protocol-pack.sx
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
1
tests/expected/191-protocol-pack.exit
Normal file
1
tests/expected/191-protocol-pack.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
4
tests/expected/191-protocol-pack.txt
Normal file
4
tests/expected/191-protocol-pack.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
count0=0
|
||||
count2=2
|
||||
sum=49
|
||||
describe len=2 first=42 second=cool
|
||||
Reference in New Issue
Block a user