issue-0028: ?Protocol = null sentinel-shaped optional protocols

Protocol structs registered via registerProtocolDecl carry a new
is_protocol flag; the ?T paths in sizeOf/typeSizeBytes/toLLVMType
recognise it and lay out ?Protocol as the protocol struct itself
(ctx == null IS the "none" state), matching how ?Closure / ?*T are
sentinel-shaped — no extra storage.

Method dispatch on ?Protocol auto-unwraps in lowerCall's field-access
path; the unwrap is structurally a no-op so we just rebind obj_ty to
the payload type. resolveCallParamTypes extended for optional-protocol
receivers so enum-literal args (gpu.create_texture(.r8, ...)) get the
right target_type and don't silently collapse to tag=0 : s32 — same
issue-0031-class bug closed in Session 66, one type-system layer
deeper.

Library: UIRenderer / UIPipeline / GlyphCache migrated from the verbose
gpu: GPU = ---; has_gpu: bool pattern to gpu: ?GPU = null. set_gpu no
longer maintains a parallel bool flag.

Bundled: dock.sx threads delta_time as a struct field rather than via
a global pointer (cleanup unrelated to issue-0028, committed alongside).

Verified: 85/85 regression tests pass; iOS-sim chess + macOS chess
both render correctly post-migration.
This commit is contained in:
agra
2026-05-18 18:32:55 +03:00
parent f9ecf9d00e
commit 79419b99bd
11 changed files with 117 additions and 93 deletions

View File

@@ -4216,6 +4216,23 @@ pub const Lowering = struct {
return self.emitProtocolDispatch(obj, proto_info, fa.field, args.items, obj_ty);
}
// Check if receiver is `?Protocol` — for sentinel-shaped
// optionals (Protocol has ctx as first ptr field, and a
// null ctx is the "none" state) the unwrap is a no-op
// structurally. Treat the optional value as the protocol
// value and dispatch. Calling a method on a null protocol
// is undefined (same as derefing a null pointer); user
// guards with `if x != null` first.
if (!obj_ty.isBuiltin()) {
const opt_info = self.module.types.get(obj_ty);
if (opt_info == .optional) {
const pay_ty = opt_info.optional.child;
if (self.getProtocolInfo(pay_ty)) |proto_info| {
return self.emitProtocolDispatch(obj, proto_info, fa.field, args.items, pay_ty);
}
}
}
var method_args = std.ArrayList(Ref).empty;
defer method_args.deinit(self.alloc);
method_args.append(self.alloc, obj) catch unreachable;
@@ -6680,6 +6697,18 @@ pub const Lowering = struct {
if (std.mem.eql(u8, m.name, fa.field)) return m.param_types;
}
}
// Optional-protocol receiver (`?GPU`): same as above but the
// protocol type sits inside the optional's payload.
if (!obj_ty.isBuiltin()) {
const opt_info = self.module.types.get(obj_ty);
if (opt_info == .optional) {
if (self.getProtocolInfo(opt_info.optional.child)) |proto_info| {
for (proto_info.methods) |m| {
if (std.mem.eql(u8, m.name, fa.field)) return m.param_types;
}
}
}
}
// Closure-typed struct field: `c.on(args)` lowers to call_closure on
// the field value. Pick up the callee's param types from the closure
// type so each arg gets the right target_type during lowering.
@@ -7646,7 +7675,7 @@ pub const Lowering = struct {
}) catch unreachable;
}
const struct_info: types.TypeInfo = .{ .@"struct" = .{ .name = name_id, .fields = fields.items } };
const struct_info: types.TypeInfo = .{ .@"struct" = .{ .name = name_id, .fields = fields.items, .is_protocol = true } };
const id = if (table.findByName(name_id)) |existing| existing else table.intern(struct_info);
table.update(id, struct_info);