015 scope for loop outer

✓ Passing This code compiles and runs correctly.

Code

// TEST: Scope - obligation OUTSIDE for-loop is auto-discharged in done branch
// STATUS: IMPLEMENTED
//
// When an obligation is created OUTSIDE a for-loop:
// - It CANNOT be auto-discharged inside the `each` branch (@scope)
// - It CAN be auto-discharged in the `done` branch (no @scope)
//
// Here: We open a file BEFORE the loop, loop runs, then auto_discharge
// inserts close in the `done` branch. This is valid code.
//
// Expected: Compiles successfully - file is auto-closed after loop

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

~app.fs:open(path: "outer.txt")
| opened outer_f |>
    std.control:for(0..3)
    | each _ |> _  // outer_f is suspended here (@scope), disposed in done
    | done |> _

pub fn main() void {}
input.kz

Imported Files

// Library module: fs
// Single disposal event

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: {s}\n", .{path});
    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\n", .{});
    return .{ .closed = .{} };
}
fs.kz