lang F1 6: generic-struct -> parameterized-protocol erasure (issue 0054 FIXED)

Two fixes, root-caused from xx Combined -> VL(s64) trapping:
- instantiateGenericStruct binds the template name to the concrete instance
  (tb.put(tmpl.name, id)), so an impl method self: *Combined resolves self.field
  to the instance (Combined__s64_s64), not the 0-field generic stub. This was a
  general pre-existing bug: self.x on ANY generic-struct impl method failed.
- createProtocolThunk monomorphizes the template method for a generic-struct
  instance (Combined.get -> Combined__s64_s64.get with the instance bindings),
  so the erasure vtable dispatches instead of hitting an unreachable thunk.

xx c on a generic Combined now dispatches correctly (examples/212 -> 99).
247 examples + unit green.
This commit is contained in:
agra
2026-05-30 03:25:04 +03:00
parent e96e76f6b0
commit 1f6e27d8f2
4 changed files with 55 additions and 2 deletions

View File

@@ -11604,6 +11604,11 @@ pub const Lowering = struct {
const id = if (table.findByName(name_id)) |existing| existing else table.intern(info);
table.update(id, info);
// Bind the template name to this concrete instance so a method's
// `self: *Combined` (the template name) resolves to `*Combined__s64_s64`
// — otherwise `self.field` hits the 0-field generic stub.
tb.put(tmpl.name, id) catch {};
// Store the type bindings and template name for method resolution
const owned_mangled = self.alloc.dupe(u8, mangled_name) catch return id;
self.struct_instance_bindings.put(owned_mangled, tb) catch {};
@@ -12879,8 +12884,21 @@ pub const Lowering = struct {
// Ensure the concrete method is lowered
const qualified = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ concrete_type_name, method.name }) catch method.name;
if (self.fn_ast_map.contains(qualified) and !self.lowered_functions.contains(qualified)) {
self.lazyLowerFunction(qualified);
if (!self.lowered_functions.contains(qualified)) {
if (self.fn_ast_map.contains(qualified)) {
self.lazyLowerFunction(qualified);
} else if (self.struct_instance_template.get(concrete_type_name)) |tmpl_name| {
// Generic-struct instance (`Combined__s64_s64`): the impl method
// is registered under the template name (`Combined.get`).
// Monomorphize it for this instance's bindings so the thunk has a
// concrete `Combined__s64_s64.get` to call.
const tmpl_qualified = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ tmpl_name, method.name }) catch method.name;
if (self.fn_ast_map.get(tmpl_qualified)) |fd| {
if (self.struct_instance_bindings.getPtr(concrete_type_name)) |bindings| {
self.monomorphizeFunction(fd, qualified, bindings);
}
}
}
}
// Call the concrete method: ConcreteType.method(__sx_ctx?, ctx, args...).