✓
Passing This code compiles and runs correctly.
Code
// Test 520: Multiple resources with cleanup obligations
// Tests that the compiler tracks EACH resource separately
//
// Key points:
// - open_two() returns TWO files, each with [opened!]
// - We must close BOTH files
// - Each obligation is tracked separately by binding.field path
// - f.file1 and f.file2 are separate obligations
~import "$app/fs"
~app.fs:open_two(path1: "test1.txt", path2: "test2.txt")
| opened f |> app.fs:close(file: f.file1) // Close first file
| closed |> app.fs:close(file: f.file2) // Close second file
| closed |> _ // Both cleaned up!
Imported Files
const std = @import("std");
const File = struct { handle: i32 };
~pub event open_two { path1: []const u8, path2: []const u8 }
| opened { file1: *File[opened!], file2: *File[opened!] } // TWO obligations!
~proc open_two {
std.debug.print("Opening two files: {s}, {s}\n", .{path1, path2});
const allocator = std.heap.page_allocator;
const f1 = allocator.create(File) catch unreachable;
const f2 = allocator.create(File) catch unreachable;
f1.* = File{ .handle = 42 };
f2.* = File{ .handle = 43 };
return .{ .opened = .{ .file1 = f1, .file2 = f2 } };
}
~pub event close { file: *File[!opened] } // Consumes one obligation
| closed {}
~proc close {
std.debug.print("Closing file\n", .{});
return .{ .closed = .{} };
}