✓
Passing This code compiles and runs correctly.
Code
// TEST: std/fmt:ln spliced MID-CHAIN between branch-outcome handler calls.
//
// Pins the emitter result_N collision: the flow emitter names handler
// results result_<counter>, and the transform/inline-expansion splice
// (std/fmt:ln's inline_body) misaligned the counter so an inner handler
// result collapsed onto an outer one:
//
// error: local constant 'result_1' shadows local constant from outer scope
//
// Shape mirrors openssl/examples/02_interp_write.kz (the live repro) but
// with a dependency-free sibling module: connect -> fmt:ln -> write ->
// read -> shutdown, each returning | ok / | err outcomes so the emitter
// nests switches. Without the fmt:ln hop the same chain numbers uniquely.
import app/net
import std/fmt
import std/io
app/net:connect(host: "localhost")
| ok c |> std/fmt:ln("GET /conn/{{ c:d }}"): req
|> app/net:write(conn: c, data: req.text)
| ok w |> app/net:read(conn: w)
| ok r |> app/net:shutdown(r.conn)
| ok |> _
| err _ |> _
| err _ |> _
| err _ |> _
| err _ |> _Supporting Files
~proc connect|cs {
_ = host;
return new Output { tag = "ok", ok = 7 };
}
~proc write|cs {
__koru_stdout_write($"write: {data}\n");
return new Output { tag = "ok", ok = conn };
}
~proc read|cs {
__koru_stdout_write($"read from conn {conn}\n");
return new Output { tag = "ok", ok = new ok_payload { conn = conn, n = 42 } };
}
~proc shutdown|cs {
__koru_stdout_write($"shutdown conn {conn}\n");
return new Output { tag = "ok", ok = new { } };
}// JavaScript implementation facet. The events are declared in net.kz (merged
// at import); this file holds the |js proc bodies.
~proc connect|js {
return { tag: "ok", ok: 7 };
}
~proc write|js {
console.log(`write: ${data}`);
return { tag: "ok", ok: conn };
}
~proc read|js {
console.log(`read from conn ${conn}`);
return { tag: "ok", ok: { conn: conn, n: 42 } };
}
~proc shutdown|js {
console.log(`shutdown conn ${conn}`);
return { tag: "ok", ok: {} };
}
// Minimal network-shaped module: a chain of branch-outcome events
// (connect -> write -> read -> shutdown) with | ok / | err branches,
// used to pin the emitter result_N collision when a std/fmt:ln hop
// is spliced into the chain.
const std = @import("std");
~pub tor connect { host: string }
| ok i32
| err string
~proc connect|zig {
_ = host;
return .{ .ok = 7 };
}
~pub tor write { conn: i32, data: string<*@scoped> }
| ok i32
| err string
~proc write|zig {
std.debug.print("write: {s}\n", .{data});
return .{ .ok = conn };
}
~pub tor read { conn: i32 }
| ok { conn: i32, n: i32 }
| err string
~proc read|zig {
std.debug.print("read from conn {d}\n", .{conn});
return .{ .ok = .{ .conn = conn, .n = 42 } };
}
~pub tor shutdown { conn: i32 }
| ok
| err string
~proc shutdown|zig {
std.debug.print("shutdown conn {d}\n", .{conn});
return .{ .ok = .{} };
}
Actual
write: GET /conn/7
read from conn 7
shutdown conn 7
Expected output
✓ Zig✓ C#write: GET /conn/7
read from conn 7
shutdown conn 7
Emitted Zig source
// Access compiler flags via the per-user compiler_env module
const CompilerEnv = @import("compiler_env").CompilerEnv;
pub const panic = if (@import("builtin").mode == .Debug)
@import("std").debug.FullPanic(@import("std").debug.defaultPanic)
else
@import("std").debug.simple_panic;
const __koru_bare = struct {
extern fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
extern fn free(ptr: ?*anyopaque) void;
fn bareAlloc(_: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, _: usize) ?[*]u8 {
var p: ?*anyopaque = null;
const a = @max(alignment.toByteUnits(), @sizeOf(usize));
if (posix_memalign(&p, a, len) != 0) return null;
return @ptrCast(p);
}
fn bareResize(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) bool { return false; }
fn bareRemap(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) ?[*]u8 { return null; }
fn bareFree(_: *anyopaque, memory: []u8, _: @import("std").mem.Alignment, _: usize) void { free(@ptrCast(memory.ptr)); }
const vtable = @import("std").mem.Allocator.VTable{ .alloc = bareAlloc, .resize = bareResize, .remap = bareRemap, .free = bareFree };
const allocator = @import("std").mem.Allocator{ .ptr = undefined, .vtable = &vtable };
};
const __koru_backing = if (@import("builtin").link_libc) @import("std").heap.c_allocator else if (@import("builtin").os.tag == .freestanding and !@import("builtin").cpu.arch.isWasm()) __koru_bare.allocator else @import("std").heap.page_allocator;
var __koru_leak_count: @import("std").atomic.Value(usize) = .init(0);
fn __koru_alloc(ctx: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, ret_addr: usize) ?[*]u8 {
_ = ctx;
const r = __koru_backing.rawAlloc(len, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
if (r != null) _ = __koru_leak_count.fetchAdd(1, .monotonic);
}
return r;
}
fn __koru_resize(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) bool {
_ = ctx;
return __koru_backing.rawResize(memory, alignment, new_len, ret_addr);
}
fn __koru_remap(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
_ = ctx;
return __koru_backing.rawRemap(memory, alignment, new_len, ret_addr);
}
fn __koru_free(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, ret_addr: usize) void {
_ = ctx;
__koru_backing.rawFree(memory, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
_ = __koru_leak_count.fetchSub(1, .monotonic);
}
}
const __koru_vtable = @import("std").mem.Allocator.VTable{ .alloc = __koru_alloc, .resize = __koru_resize, .remap = __koru_remap, .free = __koru_free };
pub fn koru_allocator() @import("std").mem.Allocator {
return .{ .ptr = undefined, .vtable = &__koru_vtable };
}
pub inline fn __koru_intcast(comptime T: type, x: anytype) T {
if (comptime (@import("builtin").mode == .Debug or @import("builtin").mode == .ReleaseSafe))
return @as(T, @intCast(x));
const dst = @typeInfo(T);
const src = @typeInfo(@TypeOf(x));
if (comptime (dst == .int and src == .int and dst.int.bits == src.int.bits and dst.int.signedness != src.int.signedness))
return @as(T, @bitCast(x));
return @as(T, @intCast(x));
}
pub fn koru_leak_check() void {
if (comptime @import("builtin").mode != .Debug) return;
if (__koru_leak_count.load(.acquire) == 0) return;
if (comptime @import("builtin").target.os.tag == .freestanding) {
if (comptime @import("builtin").cpu.arch == .wasm32 or @import("builtin").cpu.arch == .wasm64) {
@panic("KORU LEAK CHECK FAILED: the produced program leaked");
} else {
const __klc = struct { extern var stdout: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; };
var __lb: [128]u8 = undefined;
const __lm = "KORU LEAK CHECK FAILED: allocations still outstanding at end of run: ";
@memcpy(__lb[0..__lm.len], __lm);
var __ln: usize = __lm.len;
var __lv = __koru_leak_count.load(.acquire);
var __ld: [20]u8 = undefined;
var __lk: usize = 0;
while (__lv > 0) : (__lk += 1) { __ld[__lk] = @intCast('0' + __lv % 10); __lv /= 10; }
for (0..__lk) |__li| { __lb[__ln] = __ld[__lk - 1 - __li]; __ln += 1; }
__lb[__ln] = '\n'; __ln += 1; __lb[__ln] = 0;
_ = __klc.fputs(@as([*:0]const u8, @ptrCast(&__lb)), __klc.stdout);
@trap();
}
} else {
@import("std").debug.print("KORU LEAK CHECK FAILED: the produced program leaked (trace above)\n", .{});
@import("std").process.exit(1);
}
}
pub const main_module = struct {
// >>> FLOW: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:20 ~app.net:connect()
pub fn flow0() void {
const result_0 = koru_app.koru_net.connect_event.handler(.{ .host = "localhost" });
switch (result_0) {
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:21 | ok c |>
.ok => |c| {
const req = __koru_inline_0__: {
const __S = struct { threadlocal var __buf: [4096]u8 = undefined; };
const __text = @import("std").fmt.bufPrint(&__S.__buf, "GET /conn/{d}", .{c}) catch @panic("std/fmt:ln output exceeds the 4096-byte scratch buffer — use fmt.blk (allocates, no cap) or [size(Nk)] for long lines");
break :__koru_inline_0__ .{ .text = __text };
};
const result_1 = koru_app.koru_net.write_event.handler(.{ .conn = c, .data = req.text });
_ = &result_1;
switch (result_1) {
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:23 | ok w |>
.ok => |w| {
const result_2 = koru_app.koru_net.read_event.handler(.{ .conn = w });
_ = &result_2;
switch (result_2) {
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:24 | ok r |>
.ok => |r| {
const result_3 = koru_app.koru_net.shutdown_event.handler(.{ .conn = r.conn });
_ = &result_3;
switch (result_3) {
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:25 | ok |>
.ok => {
},
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:26 | err _auto_6 |>
.err => |_| {
},
}
},
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:27 | err _auto_7 |>
.err => |_| {
},
}
},
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:28 | err _auto_8 |>
.err => |_| {
},
}
},
// >>> BRANCH: tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/input.k:29 | err _auto_9 |>
.err => |_| {
},
}
}
pub fn koru_start_flow() void {
const result_0 = koru_koru.start_event.handler(.{ });
const result_0_done = result_0.done;
_ = &result_0_done;
}
pub fn koru_end_flow() void {
const result_0 = koru_koru.end_event.handler(.{ });
const result_0_done = result_0.done;
_ = &result_0_done;
}
};
pub const koru_app = struct {
pub const koru_net = struct {
const std = @import("std");
pub const connect_event = struct {
pub const Input = struct {
host: []const u8,
};
pub const Output = union(enum(u8)) {
ok: i32,
err: []const u8,
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: connect [/Users/larsde/src/koru/tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/net.kz:12]
const host = __koru_event_input.host;
_ = &host;
_ = &__koru_event_input;
_ = &host;
return .{ .ok = 7 };
}
};
pub const write_event = struct {
pub const Input = struct {
conn: i32,
data: []const u8,
};
pub const Output = union(enum(u8)) {
ok: i32,
err: []const u8,
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: write [/Users/larsde/src/koru/tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/net.kz:21]
const conn = __koru_event_input.conn;
const data = __koru_event_input.data;
_ = &conn;
_ = &data;
_ = &__koru_event_input;
std.debug.print("write: {s}\n", .{data});
return .{ .ok = conn };
}
};
pub const read_event = struct {
pub const Input = struct {
conn: i32,
};
pub const Output = union(enum(u8)) {
ok: struct {
conn: i32,
n: i32,
},
err: []const u8,
};
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.conn);
}
fn __koru_handler_impl(__koru_p_0: i32) @This().Output {
const __koru_event_input: @This().Input = .{ .conn = __koru_p_0 };
// >>> PROC: read [/Users/larsde/src/koru/tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/net.kz:30]
const conn = __koru_event_input.conn;
_ = &conn;
_ = &__koru_event_input;
std.debug.print("read from conn {d}\n", .{conn});
return .{ .ok = .{ .conn = conn, .n = 42 } };
}
};
pub const shutdown_event = struct {
pub const Input = struct {
conn: i32,
};
pub const Output = union(enum(u8)) {
ok: struct {
},
err: []const u8,
};
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.conn);
}
fn __koru_handler_impl(__koru_p_0: i32) @This().Output {
const __koru_event_input: @This().Input = .{ .conn = __koru_p_0 };
// >>> PROC: shutdown [/Users/larsde/src/koru/tests/regression/600_STDLIB/620_FMT/620_003_fmt_ln_mid_chain_result_collision/net.kz:39]
const conn = __koru_event_input.conn;
_ = &conn;
_ = &__koru_event_input;
std.debug.print("shutdown conn {d}\n", .{conn});
return .{ .ok = .{} };
}
};
};
};
pub const koru_koru = struct {
pub const start_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
done: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
_ = &__koru_event_input;
return .{ .done = .{} };
}
};
pub const end_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
done: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
_ = &__koru_event_input;
return .{ .done = .{} };
}
};
};
pub fn main() void {
main_module.koru_start_flow();
main_module.flow0();
main_module.koru_end_flow();
if (comptime @import("builtin").mode == .Debug) koru_leak_check();
}
test {
@import("std").testing.refAllDeclsRecursive(@This());
}
Emitted C# source
static class main_module {
public static void __koru_stdout_write(object s) => global::System.Console.Out.Write(s);
public static void __koru_stderr_write(object s) => global::System.Console.Error.Write(s);
// The mutable handle carrier — `*String`-style resources are
// `new __KoruBox { data = … }` because C# anonymous types are
// read-only: `s.data = …` (std/string append/clear, handle
// mutation generally) needs a settable member. `object`, not
// `dynamic`: the field is storage — every `x.data` read reaches
// it through a dynamic-typed handle (`*T` Input fields, the
// `dynamic` borrow locals below), so the member itself only
// needs to hold the value, and `object` keeps the DLR out of
// the store/load path.
public class __KoruBox { public object data; }
// Textification for `{{ … }}` operands: C# bool ToStrings as
// `True` where Koru prints `true`, and the operand's static type
// is unknown at this boundary — a `(x) is bool` inline test would
// be a compile error on statically-typed operands instead. Generic
// on purpose: `dynamic` boxed every value-type operand — measured
// ~1s/10M elements on 012_threat_scanner — while T specializes to
// the operand's own ToString() with no box.
public static string __koru_str<T>(T v) => v is bool b ? (b ? "true" : "false") : v?.ToString();
public static class app_net_connect_event {
public struct Input {
public string host;
}
public struct Output {
public string tag;
public int ok;
public string err;
}
public static Output handler(Input __koru_input) {
var host = __koru_input.host;
_ = host;
return new Output { tag = "ok", ok = 7 };
return default;
}
}
public static class app_net_write_event {
public struct Input {
public int conn;
public string data;
}
public struct Output {
public string tag;
public int ok;
public string err;
}
public static Output handler(Input __koru_input) {
var conn = __koru_input.conn;
var data = __koru_input.data;
__koru_stdout_write($"write: {data}\n");
return new Output { tag = "ok", ok = conn };
return default;
}
}
public static class app_net_read_event {
public struct Input {
public int conn;
}
public struct Output {
public string tag;
public ok_payload ok;
public string err;
}
public struct ok_payload {
public int conn;
public int n;
}
public static Output handler(Input __koru_input) {
var conn = __koru_input.conn;
__koru_stdout_write($"read from conn {conn}\n");
return new Output { tag = "ok", ok = new ok_payload { conn = conn, n = 42 } };
return default;
}
}
public static class app_net_shutdown_event {
public struct Input {
public int conn;
}
public struct Output {
public string tag;
public object ok;
public string err;
}
public static Output handler(Input __koru_input) {
var conn = __koru_input.conn;
__koru_stdout_write($"shutdown conn {conn}\n");
return new Output { tag = "ok", ok = new { } };
return default;
}
}
public static class koru_start_event {
public struct Input {
}
public struct Output {
public string tag;
public object done;
}
public static Output handler(Input __koru_input) => new Output { tag = "done" };
}
public static class koru_end_event {
public struct Input {
}
public struct Output {
public string tag;
public object done;
}
public static Output handler(Input __koru_input) => new Output { tag = "done" };
}
public static void flow0() {
var result_0 = main_module.app_net_connect_event.handler(new app_net_connect_event.Input { host = (string)("localhost")});
if (result_0.tag == "ok") {
var c = result_0.ok;
var req = new { text = $"GET /conn/{(c)}" };
var result_1 = main_module.app_net_write_event.handler(new app_net_write_event.Input { conn = (int)(c), data = (string)(req.text)});
if (result_1.tag == "ok") {
var w = result_1.ok;
var result_2 = main_module.app_net_read_event.handler(new app_net_read_event.Input { conn = (int)(w)});
if (result_2.tag == "ok") {
var r = result_2.ok;
var result_3 = main_module.app_net_shutdown_event.handler(new app_net_shutdown_event.Input { conn = (int)(r.conn)});
if (result_3.tag == "ok") {
}
if (result_3.tag == "err") {
var _auto_6 = result_3.err;
}
}
if (result_2.tag == "err") {
var _auto_7 = result_2.err;
}
}
if (result_1.tag == "err") {
var _auto_8 = result_1.err;
}
}
if (result_0.tag == "err") {
var _auto_9 = result_0.err;
}
}
public static void flow1() {
main_module.koru_start_event.handler(new koru_start_event.Input { });
}
public static void flow2() {
main_module.koru_end_event.handler(new koru_end_event.Input { });
}
}
static class Program {
static void Main() {
main_module.flow1();
main_module.flow0();
main_module.flow2();
}
}
Flows
flow ~connect click a branch to expand · @labels scroll to their anchor
connect (host: "localhost")
Test Configuration
MUST_RUN LANGUAGES: zig cs