From e604868ffbcb97431d96ec0707c715a7e82490e1 Mon Sep 17 00:00:00 2001 From: agra Date: Fri, 29 May 2026 19:24:06 +0300 Subject: [PATCH] lang 2.4: parameterized-protocol method calls on pack elements MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `xs[i].get()` on a parameterised `..xs: Box(T)` pack now resolves — the canonical `ValueListenable` shape. registerParamImpl, for a CONCRETE-struct source, now also registers the impl's methods as `.` in fn_ast_map (like a non-parameterised impl), so UFCS finds them. Such methods are already fully concrete (`impl Box(s64) for IntCell` → `get(self: *IntCell) -> s64`), so there's nothing to monomorphize; generic/pack sources stay lazy in param_impl_map. First impl wins on a name collision. Heterogeneous parameterised packs work: each `xs[i]` binds a different T and dispatches to its own impl. Regression: examples/194-protocol-pack-parameterized.sx (Box(s64) IntCell + Box(string) StrCell, order-independent). --- examples/194-protocol-pack-parameterized.sx | 30 +++++++++++++++++++ src/ir/lower.zig | 24 +++++++++++++-- .../194-protocol-pack-parameterized.exit | 1 + .../194-protocol-pack-parameterized.txt | 2 ++ 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 examples/194-protocol-pack-parameterized.sx create mode 100644 tests/expected/194-protocol-pack-parameterized.exit create mode 100644 tests/expected/194-protocol-pack-parameterized.txt diff --git a/examples/194-protocol-pack-parameterized.sx b/examples/194-protocol-pack-parameterized.sx new file mode 100644 index 0000000..c0acdbc --- /dev/null +++ b/examples/194-protocol-pack-parameterized.sx @@ -0,0 +1,30 @@ +// Feature 1 — method calls on a PARAMETERIZED protocol pack (the canonical +// shape: `..xs: ValueListenable` where each element conforms with its own +// type-arg). Calling the protocol method `get()` on `xs[i]` resolves to the +// concrete element's impl, even though each element binds a different `T`. +// +// (Parameterised-protocol impl methods with a concrete source type are now +// registered as `.`, so UFCS — and thus `xs[i].get()` — +// resolves them.) + +#import "modules/std.sx"; + +Box :: protocol(T: Type) { + get :: () -> T; +} + +IntCell :: struct { v: s64; } +StrCell :: struct { s: string; } +impl Box(s64) for IntCell { get :: (self: *IntCell) -> s64 => self.v; } +impl Box(string) for StrCell { get :: (self: *StrCell) -> string => self.s; } + +describe :: (..xs: Box) -> void { + // xs[0] : Box(s64), xs[1] : Box(string) — different type-args per position. + print("first={} second={}\n", xs[0].get(), xs[1].get()); +} + +main :: () -> s32 { + describe(IntCell.{ v = 11 }, StrCell.{ s = "hi" }); + describe(StrCell.{ s = "x" }, IntCell.{ v = 99 }); + 0; +} diff --git a/src/ir/lower.zig b/src/ir/lower.zig index d0d21d0..87c7c61 100644 --- a/src/ir/lower.zig +++ b/src/ir/lower.zig @@ -11633,7 +11633,7 @@ pub const Lowering = struct { // Methods are NOT registered in fn_ast_map — they're monomorphised lazily // per (Source, Target) pair at the xx call site. if (ib.protocol_type_args.len > 0) { - self.registerParamImpl(ib, decl); + self.registerParamImpl(ib, decl, is_imported); return; } // Collect explicitly implemented method names @@ -11674,7 +11674,7 @@ pub const Lowering = struct { /// `pack_start != null`) are additionally registered into /// `param_impl_pack_map` keyed without the source suffix — the matching /// site walks that map to bind packs against any concrete closure shape. - fn registerParamImpl(self: *Lowering, ib: *const ast.ImplBlock, decl: *const Node) void { + fn registerParamImpl(self: *Lowering, ib: *const ast.ImplBlock, decl: *const Node, is_imported: bool) void { const table = &self.module.types; // Resolve the protocol's type-arg list to concrete TypeIds. @@ -11743,6 +11743,26 @@ pub const Lowering = struct { } gop.value_ptr.append(self.alloc, entry) catch return; + // Concrete-struct source: also register the impl's methods as + // `.` in fn_ast_map so UFCS resolves them (e.g. + // `xs[i].get()` on a pack element). For a concrete impl like + // `impl Box(s64) for IntCell`, the method is already fully concrete — + // nothing to monomorphize, unlike generic/pack sources (which stay + // lazy in param_impl_map and are handled below). + { + const si = table.get(src_ty); + if (!src_ty.isBuiltin() and si == .@"struct") { + const src_name = self.formatTypeName(src_ty); + for (methods.items) |mfd| { + const q = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ src_name, mfd.name }) catch continue; + if (self.fn_ast_map.contains(q)) continue; // first impl wins + self.fn_ast_map.put(q, mfd) catch {}; + self.import_flags.put(q, is_imported) catch {}; + self.declareFunction(mfd, q); + } + } + } + // Pack-shaped source: also register in the pack map. The source // closure carries `pack_start` set; matching binds the source's // tail param types to the pack-name and the source's return to diff --git a/tests/expected/194-protocol-pack-parameterized.exit b/tests/expected/194-protocol-pack-parameterized.exit new file mode 100644 index 0000000..573541a --- /dev/null +++ b/tests/expected/194-protocol-pack-parameterized.exit @@ -0,0 +1 @@ +0 diff --git a/tests/expected/194-protocol-pack-parameterized.txt b/tests/expected/194-protocol-pack-parameterized.txt new file mode 100644 index 0000000..a013fde --- /dev/null +++ b/tests/expected/194-protocol-pack-parameterized.txt @@ -0,0 +1,2 @@ +first=11 second=hi +first=x second=99