Files
sx/examples/0404-protocols-dot-shorthand-protocol-field.sx
agra 4e942b5373 test: migrate examples to XXXX-category-name layout + split expected streams
Rename all example tests/companions to the XXXX-category-test-name scheme
(per-category 100-blocks: basic 0010, types 0100, ... errors 1000,
diagnostics 1100, ffi 1200, ffi-objc 1300, ffi-jni 1400, vectors 1500,
platform 1600). Companions and dir/C fixtures move in lockstep with their
parent test; #import/#source/#include paths rewritten to match.

Expected output now lives in examples/expected/ (a sibling dir of the
tests) split into three streams per the new convention:
  <name>.exit / <name>.stdout / <name>.stderr  (+ optional <name>.ir)

run_examples.sh rewritten: scans examples/ and issues/ for an
expected/<name>.exit marker, captures stdout and stderr separately (no
more 2>&1), compares each stream + exit + optional IR snapshot.

Behavior validated unchanged: every renamed test reproduces its prior
merged output + exit (diffs limited to file paths/basenames embedded in
diagnostics + traces, which correctly reflect the new names). Suite:
292 passed, 0 failed. 50-smoke.sx split + issue relocation + docs follow
in subsequent commits.
2026-06-01 19:05:15 +03:00

80 lines
2.2 KiB
Plaintext

// Dot-shorthand `.{ child = d }` for a struct whose first field is a protocol
// value, used as the argument to `List(Container).append` from two distinct
// container types. Exercises the cross-callsite path of dot-shorthand inference.
#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");
}