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

[open!] was not discharged. Call: app.db:begin

Actual Compiler Output ✓ Pattern matched

error[KORU030]: Resource '_' [open!] was not discharged. Call: app.db:begin
  --> 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:9568:17: 0x1046d24af in emit (backend)
                return error.CompilerCoordinationFailed;
                ^
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_01_unused_connection/backend.zig:9652:28: 0x1046d32b7 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 *Connection[open!]
| err []const u8

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

// begin: consumes connection obligation, creates transaction obligation
~pub event begin { conn: *Connection[!open] }
| ok *Transaction[active!]
| err []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 };
}

// 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