Files
sx/examples/issue-0018.sx
2026-03-05 16:20:36 +02:00

90 lines
2.6 KiB
Plaintext

// issue-0018: Dot-shorthand `.{...}` for struct with protocol field causes
// LLVM verification error when used in List(T).append from 2+ different
// struct methods.
//
// Trigger: TWO or more structs each with `List(Container)` calling
// `.append(.{ child = d })` — using dot-shorthand.
//
// Works: Only 1 struct doing it, or using explicit `Container.{ child = d }`.
// Fails: 2+ structs → `Invalid InsertValueInst operands!`
// `%si = insertvalue i64 undef, { ptr, ptr } %load, 0`
//
// Likely a monomorphization issue in `List(T).append` when the dot-shorthand
// type inference is resolved from multiple call sites.
#import "modules/std.sx";
Drawable :: protocol {
draw :: () -> s32;
name :: () -> string;
layout :: (x: s32) -> s32;
handle :: (event: s32) -> bool;
}
Circle :: struct { radius: s32; }
impl Drawable for Circle {
draw :: (self: *Circle) -> s32 { self.radius; }
name :: (self: *Circle) -> string { "circle"; }
layout :: (self: *Circle, x: s32) -> s32 { x + self.radius; }
handle :: (self: *Circle, event: s32) -> bool { event > 0; }
}
Square :: struct { side: s32; }
impl Drawable for Square {
draw :: (self: *Square) -> s32 { self.side * self.side; }
name :: (self: *Square) -> string { "square"; }
layout :: (self: *Square, x: s32) -> s32 { x + self.side; }
handle :: (self: *Square, event: s32) -> bool { event > 1; }
}
Rect :: struct {
x: f32;
y: f32;
w: f32;
h: f32;
zero :: () -> Rect { Rect.{ x = 0.0, y = 0.0, w = 0.0, h = 0.0 }; }
}
Container :: struct {
child: Drawable;
computed_frame: Rect = .zero();
}
// Two different structs, each with List(Container), both calling .append(.{...})
// This mirrors VStack/HStack in the game.
StackA :: struct {
children: List(Container);
add :: (self: *StackA, d: Drawable) {
// BUG: `.{ child = d }` causes LLVM error when 2+ structs do this
self.children.append(.{ child = d });
}
}
StackB :: struct {
children: List(Container);
add :: (self: *StackB, d: Drawable) {
// BUG: second struct doing `.{ child = d }` triggers the error
self.children.append(.{ child = d });
// FIX: explicit `Container.{ child = d }` works
// self.children.append(Container.{ child = d });
}
}
main :: () -> void {
c := Circle.{ radius = 42 };
s := Square.{ side = 5 };
a : StackA = .{};
a.add(c);
print("StackA: draw={}\n", a.children.items[0].child.draw());
b : StackB = .{};
b.add(s);
print("StackB: draw={}\n", b.children.items[0].child.draw());
print("OK\n");
}