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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user