auto type erasure for protocols & struct literal init bloc

This commit is contained in:
agra
2026-02-25 12:25:06 +02:00
parent bfc784734c
commit 4abc7abb54
5 changed files with 196 additions and 1 deletions

View File

@@ -1034,10 +1034,17 @@ pub const Parser = struct {
}
try self.expect(.r_brace);
// Optional init block: T.{ fields } { stmts }
const init_block: ?*Node = if (self.current.tag == .l_brace)
try self.parseBlock()
else
null;
return try self.createNode(start_pos, .{ .struct_literal = .{
.struct_name = struct_name,
.type_expr = type_expr,
.field_inits = try field_inits.toOwnedSlice(self.allocator),
.init_block = init_block,
} });
}
@@ -2017,7 +2024,17 @@ pub const Parser = struct {
self.advance(); // skip 'push'
const context_expr = try self.parseExpr();
const body = try self.parseBlock();
// push Context.{ ... } { body } — if parseExpr consumed the push body
// as a struct init block, steal it back as the push body.
// (if/while don't have this issue — they require bool/optional conditions)
const body = if (context_expr.data == .struct_literal and
context_expr.data.struct_literal.init_block != null)
body_blk: {
const ib = context_expr.data.struct_literal.init_block.?;
context_expr.data.struct_literal.init_block = null;
break :body_blk ib;
} else try self.parseBlock();
return try self.createNode(start, .{ .push_stmt = .{
.context_expr = context_expr,