01 unused connection

✓ Passing This code compiles and runs correctly.

Code

// TEST: Connection opened but never used meaningfully
//
// This is the core thesis: acquiring a resource creates an OBLIGATION
// to use it meaningfully. We connect but never start a transaction -
// the [open!] obligation isn't satisfied.
//
// EXPECTED: Compiler error - Connection[open!] obligation not satisfied
//
// Compare to Rust:
//   let conn = Connection::open_in_memory().unwrap();
//   // conn drops here - Rust says "fine, cleanup happened"
//   // Koru says "ERROR: you didn't DO anything meaningful with this connection"

~import "$app/db"

~app.db:connect(host: "localhost")
| ok _ |>
    // We got a connection with phantom obligation [open!]
    // But we never call begin() to start a transaction!
    // The phantom obligation is NOT satisfied - should error.
    _
| err _ |>
    _
input.kz

Error Verification

Expected Error Pattern

Phantom obligation not discharged

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 (29 items)
[PHASE 2.6] Rescanning transformed AST (29 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.testing:test
  [6] std.testing:validate_mocks
  [7] std.testing:test.with_harness
  [8] std.testing:test.harness
  [9] std.testing:assert
  [10] std.testing:test.property.equivalent
  [11] std.deps:deps
  [12] std.deps:requires.system
  [13] std.deps:requires.zig
  [14] std.control:if
  [15] std.control:for
  [16] std.control:capture
  [17] std.control:const
  [18] std.build:requires
  [19] std.build:variants
  [20] std.build:config
  [21] std.build:command.sh
  [22] std.build:command.zig
  [23] std.build:step
  [24] std.template:define

[PHANTOM-KORU] Starting phantom check proc...
error[KORU030]: Resource '_.conn' with cleanup obligation 'app.db:open!' was not disposed. Call the disposal event explicitly.
  --> phantom_semantic_check:17:0

❌ Compiler coordination error: Phantom semantic validation failed
error: CompilerCoordinationFailed
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_01_unused_connection/backend.zig:9562:17: 0x100742433 in emit (backend)
                return error.CompilerCoordinationFailed;
                ^
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_01_unused_connection/backend.zig:9646:28: 0x1007431bf in main (backend)
    const generated_code = try RuntimeEmitter.emit(compile_allocator, final_ast);
                           ^

Imported Files

// Database module demonstrating phantom obligation semantics
//
// The key insight: acquiring a resource creates an OBLIGATION to USE it meaningfully.
// Automatic cleanup (RAII, IDisposable, use statements) doesn't satisfy this -
// you must actually DO something with the resource.
//
// State machine:
//   connect()  → Connection[open!]           -- creates obligation to use connection
//   begin()    → Transaction[active!]        -- consumes connection, creates tx obligation  
//   execute()  → Transaction[active]         -- uses tx without consuming (can execute many)
//   commit()   → void                        -- consumes tx obligation (meaningful end)
//   rollback() → void                        -- consumes tx obligation (meaningful end)

const std = @import("std");

const Connection = struct { handle: i32 };
const Transaction = struct { conn_handle: i32 };
const QueryResult = struct { rows: i32 };

// connect: creates connection with obligation to use it
~pub event connect { host: []const u8 }
| ok { conn: *Connection[open!] }
| err { msg: []const u8 }

~proc connect {
    const c = std.heap.page_allocator.create(Connection) catch unreachable;
    c.* = Connection{ .handle = 42 };
    return .{ .ok = .{ .conn = c } };
}

// begin: consumes connection obligation, creates transaction obligation
~pub event begin { conn: *Connection[!open] }
| ok { tx: *Transaction[active!] }
| err { msg: []const u8 }

~proc begin {
    const tx = std.heap.page_allocator.create(Transaction) catch unreachable;
    tx.* = Transaction{ .conn_handle = conn.handle };
    std.heap.page_allocator.destroy(conn);
    return .{ .ok = .{ .tx = tx } };
}

// execute: uses transaction WITHOUT consuming it (can call multiple times)
~pub event execute { tx: *Transaction[active], sql: []const u8 }
| ok { tx: *Transaction[active], result: QueryResult }
| err { tx: *Transaction[active], msg: []const u8 }

~proc execute {
    std.debug.print("Executing: {s}\n", .{sql});
    return .{ .ok = .{ .tx = tx, .result = .{ .rows = 1 } } };
}

// commit: consumes transaction obligation - this is a MEANINGFUL END
// [!] marks this as the default discharge for Transaction[active!]
// Void event (no branches) so [!] works correctly
~[!]pub event commit { tx: *Transaction[!active] }

~proc commit {
    std.debug.print("COMMIT\n", .{});
    std.heap.page_allocator.destroy(tx);
}

// rollback: consumes transaction obligation - this is also a MEANINGFUL END
// Also marked [!] as alternative discharge
~[!]pub event rollback { tx: *Transaction[!active] }

~proc rollback {
    std.debug.print("ROLLBACK\n", .{});
    std.heap.page_allocator.destroy(tx);
}
db.kz

Test Configuration

MUST_FAIL

Compiler Flags:

--auto-discharge=disable