lang 2.4: parameterized-protocol method calls on pack elements

`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 `<Source>.<method>` 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).
This commit is contained in:
agra
2026-05-29 19:24:06 +03:00
parent a67627a691
commit e604868ffb
4 changed files with 55 additions and 2 deletions

View File

@@ -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 `<Source>.<method>`, 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;
}

View File

@@ -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
// `<Source>.<method>` 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

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,2 @@
first=11 second=hi
first=x second=99