056 union per member consume marker

✓ Passing This code compiles and runs correctly.

Code

// Test 330_056: Union with per-member consume markers
// Tests that [!opened|!closing] discharges the correct obligation from either state
// Demonstrates: per-member ! syntax where each union member carries its own discharge marker

~import "$app/handle"

// Test 1: finalize() from opened state - discharges opened! obligation
~app.handle:open()
| opened h1 |> app.handle:finalize(h: h1.h)
    | done |> _

// Test 2: finalize() from closing state - discharges closing! obligation
~app.handle:open()
| opened h1 |> app.handle:start_close(h: h1.h)
    | closing h2 |> app.handle:finalize(h: h2.h)
        | done |> _
input.kz

Expected

Opening handle
Finalizing handle: 42
Opening handle
Starting close: 42
Finalizing handle: 42

Actual

Opening handle
Finalizing handle: 42
Opening handle
Starting close: 42
Finalizing handle: 42

Imported Files

// Library module: handle
// Demonstrates per-member consume markers in phantom state unions
// Each union member carries its own ! prefix to indicate obligation discharge

const std = @import("std");

// Handle type with phantom states
const Handle = struct {
    id: i32,
};

// Open a handle - produces obligation (must be finalized)
~pub event open {}
| opened { h: *Handle[opened!] }

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

// Start closing - explicitly discharges opened! and produces closing! obligation
~pub event start_close { h: *Handle[!opened] }
| closing { h: *Handle[closing!] }

~proc start_close {
    std.debug.print("Starting close: {d}\n", .{h.id});
    return .{ .closing = .{ .h = h } };
}

// Finalize - per-member consume markers: !opened discharges opened!, !closing discharges closing!
~pub event finalize { h: *Handle[!opened|!closing] }
| done {}

~proc finalize {
    std.debug.print("Finalizing handle: {d}\n", .{h.id});
    return .{ .done = .{} };
}
handle.kz

Test Configuration

MUST_RUN