041 auto discharge single

✓ Passing This code compiles and runs correctly.

Code

// TEST: Auto-discharge with single consumer (using discard _)
//
// When a flow terminates with an unsatisfied cleanup obligation [state!],
// and there's exactly ONE event that consumes it [!state], auto-insert it.
//
// Here: open() returns *File[opened!], close() consumes *File[!opened]
// Since close() is the ONLY consumer, it should be auto-inserted.
//
// Using _ as the binding means we discard the value but auto-discharge
// synthesizes a binding (_auto_0) and inserts the disposal call.
//
// Expected: Compiles successfully with auto-inserted disposal

~import "$app/fs"

// Flow terminates without explicit close - should auto-insert close()
// Using _ to discard - auto-discharge will synthesize a binding
~app.fs:open(path: "test.txt")
| opened _ |> _

pub fn main() void {}
input.kz

Expected Output

Opening file
Closing file (auto-discharged)

Imported Files

// Library module: fs
// Single disposal event - auto-discharge should work

const std = @import("std");

const File = struct { handle: i32 };

// Open a file - returns opened! state (requires cleanup)
~pub event open { path: []const u8 }
| opened { file: *File[opened!] }

~proc open {
    std.debug.print("Opening file\n", .{});
    const f = std.heap.page_allocator.create(File) catch unreachable;
    f.* = File{ .handle = 42 };
    return .{ .opened = .{ .file = f } };
}

// Close - the ONLY consumer of [!opened]
~pub event close { file: *File[!opened] }
| closed {}

~proc close {
    std.debug.print("Closing file (auto-discharged)\n", .{});
    return .{ .closed = .{} };
}
fs.kz

Test Configuration

MUST_RUN