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

@@ -76,6 +76,12 @@ pub const TypeInfo = union(enum) {
pub const StructInfo = struct {
name: StringId,
fields: []const Field,
// True iff this struct backs a protocol value (registered via
// `registerProtocolDecl`). Used by the optional code path: a
// `?Protocol` is sentinel-shaped (the protocol struct itself,
// null ctx == none) rather than the standard `{T, i1}` discriminated
// layout — matching how `?Closure` works.
is_protocol: bool = false,
pub const Field = struct {
name: StringId,
@@ -370,7 +376,16 @@ pub const TypeTable = struct {
.string => 16, // {ptr, len}
.pointer, .many_pointer, .function => 8,
.closure => 16, // {fn_ptr, env}
.optional => |opt| self.sizeOf(opt.child) + 8, // child + has_value flag (aligned)
.optional => |opt| blk: {
// Sentinel-shaped optionals (pointer/closure/protocol) cost
// no extra storage — null reuses the payload's null state.
const child_info = self.get(opt.child);
if (child_info == .pointer or child_info == .many_pointer or child_info == .function) break :blk 8;
if (child_info == .closure) break :blk 16;
if (child_info == .@"struct" and child_info.@"struct".is_protocol) break :blk self.sizeOf(opt.child);
// Discriminated form: payload + has_value flag (8-aligned).
break :blk self.sizeOf(opt.child) + 8;
},
.slice => 16, // {ptr, len}
.array => |arr| arr.length * self.sizeOf(arr.element),
.vector => |vec| vec.length * self.sizeOf(vec.element),
@@ -445,6 +460,8 @@ pub const TypeTable = struct {
break :blk ptr_size;
if (child_info == .closure)
break :blk 2 * ptr_size;
if (child_info == .@"struct" and child_info.@"struct".is_protocol)
break :blk self.typeSizeBytes(o.child);
const cs = self.typeSizeBytes(o.child);
const ca = self.typeAlignBytes(o.child);
// { T, i1 } — i1 goes right after T, then pad to struct alignment