✓
Passing This code compiles and runs correctly.
Code
// TEST: Manual disposal at | each |> is NOT allowed
// STATUS: IMPLEMENTED
// MUST_FAIL: KORU032 - Cannot dispose outer-scope resource inside repeating loop body
//
// When an obligation is created OUTSIDE a for-loop, it CANNOT be
// disposed inside | each |> (would run N times).
//
// Here: Open file before loop, try to close inside each - ERROR!
//
// Expected: KORU032 error about disposing in repeating context
~import "$std/control"
~import "$app/fs"
~app.fs:open(path: "outer.txt")
| opened f |>
std.control:for(0..3)
| each _ |>
app.fs:close(file: f.file) // ERROR! Would run 3 times
| closed _ |> _
| done |> _
pub fn main() void {}
Error Verification
Expected Error Pattern
Test expects KORU032 error when trying to dispose outer-scope resource inside loop bodyActual Compiler Output
[PHASE 2.4] Calling run_pass for transforms\n[PHASE 2.5] Executing comptime_main() - running comptime flows
[PHASE 2.5] Comptime flows complete (28 items)
[PHASE 2.6] Rescanning transformed AST (28 items)
[PHASE 2.6] Rescan complete: 25 comptime events found
[0] std.compiler:requires
[1] std.compiler:flag.declare
[2] std.compiler:command.declare
[3] std.compiler:coordinate
[4] std.compiler:context_create
[5] std.control:if
[6] std.control:for
[7] std.control:capture
[8] std.control:const
[9] std.testing:test
[10] std.testing:validate_mocks
[11] std.testing:test.with_harness
[12] std.testing:test.harness
[13] std.testing:assert
[14] std.testing:test.property.equivalent
[15] std.deps:deps
[16] std.deps:requires.system
[17] std.deps:requires.zig
[18] std.template:define
[19] std.build:requires
[20] std.build:variants
[21] std.build:config
[22] std.build:command.sh
[23] std.build:command.zig
[24] std.build:step
error[KORU032]: Cannot dispose outer-scope resource 'f.file' inside @scope boundary. Handle outside the scope or escape via branch constructor.
--> auto_discharge:16:0
❌ Compiler coordination error: Auto-discharge failed (multiple disposal options or no disposal event)
error: CompilerCoordinationFailed
/Users/larsde/src/koru/tests/regression/300_ADVANCED_FEATURES/330_PHANTOM_TYPES/330_020_for_manual_each_error/backend.zig:9467:17: 0x1006a6433 in emit (backend)
return error.CompilerCoordinationFailed;
^
/Users/larsde/src/koru/tests/regression/300_ADVANCED_FEATURES/330_PHANTOM_TYPES/330_020_for_manual_each_error/backend.zig:9551:28: 0x1006a71bf in main (backend)
const generated_code = try RuntimeEmitter.emit(compile_allocator, final_ast);
^Imported Files
// Library module: fs
// Defines filesystem operations with cleanup obligations
const std = @import("std");
// File type with phantom states
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 allocator = std.heap.page_allocator;
const f = allocator.create(File) catch unreachable;
f.* = File{ .handle = 42 };
return .{ .opened = .{ .file = f } };
}
// Close a file - CONSUMES opened! state (disposes the resource)
~pub event close { file: *File[!opened] }
| closed {}
~proc close {
std.debug.print("Closing file\n", .{});
return .{ .closed = .{} };
}
Test Configuration
MUST_FAIL