031 tap binding substitution

✓ Passing This code compiles and runs correctly.

Code

// TEST: Tap binding substitution
//
// Verifies that taps can use bindings to access payload data.
// The tap transform correctly rewrites binding references when splicing.
//
// Example: The tap declares `| opened f |> log_open(handle: f.file.handle)`
//          The main flow has `| opened _ |>` (auto-discharge synthesizes binding)
//
// The tap transform:
// 1. Synthesizes a unique binding (_tap_N) when original binding is "_"
// 2. Rewrites tap's binding references: f.file.handle → _tap_N.file.handle
// 3. Sets the synthesized binding on the continuation
//
// Result: Tap can observe and access payload data without breaking codegen!

~import "$std/taps"
~import "$app/fs"

// Tap that USES the binding to access file data
// This should work: the tap reads f.file.handle and passes it to log_open
~tap(app.fs:open -> *)
| opened f |> app.fs:log_open(handle: f.file.handle)
    | done |> _

// Main flow with auto-discharge
~app.fs:open(path: "test.txt")
| opened _ |> _

pub fn main() void {}
input.kz

Expected Output

Opening file
TAP: Observed file open!
Closing file (auto-discharged)

Imported Files

// Library module: fs
// Phantom-tracked file operations for testing tap binding substitution

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 = .{} };
}

// A logging event for taps - receives the file handle from tap binding
~pub event log_open { handle: i32 }
| done {}

~proc log_open {
    std.debug.print("TAP: Observed file open!\n", .{});
    return .{ .done = .{} };
}
fs.kz

Test Configuration

MUST_RUN