053 for loop obligation escape

✓ Passing This code compiles and runs correctly.

Code

// ============================================================================
// REGRESSION TEST - Obligation escape when no disposal event exists
// ============================================================================
// Test: Cleanup obligation created but NO disposal event available
//
// BUG DISCOVERED: 2026-01-12
// Without a disposal event, auto-discharge cannot fix the leak.
// Should produce KORU031 error: no disposal event found.
//
// Expected: Should FAIL with "no disposal event found" error
// Actual: (after fix) Correctly fails

~import "$std/control"
~import "$std/build"

// Link libc for c_allocator
~std.build:requires { exe.linkLibC(); }

const std = @import("std");

const Resource = struct {
    id: usize,
};

const allocator = std.heap.c_allocator;

~event create { id: usize }
| created *Resource[active!]

~proc create {
    const r = allocator.create(Resource) catch unreachable;
    r.* = Resource{ .id = id };
    return .{ .created = r };
}

// NOTE: NO destroy event exists - auto-discharge cannot fix this!

~event use { r: *Resource[active] }

~proc use {
    std.debug.print("using {}\n", .{r.id});
}

// ERROR: Obligation [active!] has no disposal event [!active]
~for(0..10)
| each i |>
    create(id: i)
    | created r |>
        use(r)  // use it, but can't dispose - no destroy event exists!
| done |>
    _

pub fn main() void {}
input.kz

Error Verification

Actual 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 (41 items)
[PHASE 2.6] Rescanning transformed AST (41 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.build:requires
  [10] std.build:variants
  [11] std.build:config
  [12] std.build:command.sh
  [13] std.build:command.zig
  [14] std.build:step
  [15] std.testing:test
  [16] std.testing:validate_mocks
  [17] std.testing:test.with_harness
  [18] std.testing:test.harness
  [19] std.testing:assert
  [20] std.testing:test.property.equivalent
  [21] std.deps:deps
  [22] std.deps:requires.system
  [23] std.deps:requires.zig
  [24] std.template:define
error[KORU030]: No disposal event found for resource 'r' with state 'input:active!'.
  --> auto_discharge:46: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_053_for_loop_obligation_escape/backend.zig:9468:17: 0x104f52433 in emit (backend)
                return error.CompilerCoordinationFailed;
                ^
/Users/larsde/src/koru/tests/regression/300_ADVANCED_FEATURES/330_PHANTOM_TYPES/330_053_for_loop_obligation_escape/backend.zig:9552:28: 0x104f531bf in main (backend)
    const generated_code = try RuntimeEmitter.emit(compile_allocator, final_ast);
                           ^

Test Configuration

MUST_FAIL

Expected Behavior:

BACKEND_COMPILE_ERROR