✓
Passing This code compiles and runs correctly.
Code
// TEST: Try to close a connection without using it
//
// connect() produces Connection[connected!]
// close() requires Connection[!active]
// These states don't match! You can't close a fresh connection.
//
// This is THE KEY INSIGHT: you MUST use the connection meaningfully
// before you can close it. Rust's Drop can't express this.
//
// EXPECTED: Compiler error - type mismatch (connected vs active)
~import "$app/db"
~app.db:connect(host: "localhost")
| ok c |>
// Try to close immediately - this should FAIL!
// close() needs [!active], but we have [connected!]
app.db:close(conn: c.conn)
| ok _ |> _
| err _ |> _
| err _ |> _
Error Verification
Expected Error Pattern
state mismatchActual Compiler Output
error[KORU030]: Resource '__type_ref' [connected!] was not disposed. Call: begin
--> auto_discharge:15:0
❌ Compiler coordination error: Auto-discharge failed (multiple disposal options or no disposal event)
error: CompilerCoordinationFailed
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_08_close_without_transaction/backend.zig:9687:17: 0x102f8a4af in emit (backend)
return error.CompilerCoordinationFailed;
^
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_08_close_without_transaction/backend.zig:9771:28: 0x102f8b2b7 in main (backend)
const generated_code = try RuntimeEmitter.emit(compile_allocator, final_ast);
^Imported Files
// Database module demonstrating phantom obligation semantics
//
// State machine:
// connect() → Connection[connected!] -- must use connection
// begin() → Transaction[started!] -- consumes conn, must exec
// exec() → Transaction[active!] -- now can commit/rollback
// commit() → Connection[active!] -- returns conn, can reuse or close
// rollback() → Connection[active!] -- returns conn, can reuse or close
// close() → void -- only accepts [!active], not [!connected]
//
// Key insight: close() requires [!active], which only comes from commit/rollback.
// This FORCES you to use the connection meaningfully before closing.
const std = @import("std");
const Connection = struct { handle: i32 };
const Transaction = struct { conn_handle: i32 };
// connect: creates connection with [connected!] obligation
~pub event connect { host: []const u8 }
| ok *Connection[connected!]
| err []const u8
~proc connect {
const c = std.heap.page_allocator.create(Connection) catch unreachable;
c.* = Connection{ .handle = 42 };
return .{ .ok = c };
}
// begin: consumes [!connected] OR [!active], produces Transaction[started!]
~pub event begin { conn: *Connection[!connected|!active] }
| ok *Transaction[started!]
| err []const u8
~proc begin {
const tx = std.heap.page_allocator.create(Transaction) catch unreachable;
tx.* = Transaction{ .conn_handle = conn.handle };
return .{ .ok = tx };
}
// exec: consumes [!started] OR [!active], produces Transaction[active!]
~pub event exec { tx: *Transaction[!started|!active], sql: []const u8 }
| ok *Transaction[active!]
| err []const u8
~proc exec {
std.debug.print("Executing: {s}\n", .{sql});
return .{ .ok = tx };
}
// commit: consumes [!active], returns Connection[active!]
~pub event commit { tx: *Transaction[!active] }
| ok *Connection[active!]
| err []const u8
~proc commit {
std.debug.print("COMMIT\n", .{});
const c = std.heap.page_allocator.create(Connection) catch unreachable;
c.* = Connection{ .handle = tx.conn_handle };
std.heap.page_allocator.destroy(tx);
return .{ .ok = c };
}
// rollback: consumes [!active], returns Connection[active!]
~pub event rollback { tx: *Transaction[!active] }
| ok *Connection[active!]
| err []const u8
~proc rollback {
std.debug.print("ROLLBACK\n", .{});
const c = std.heap.page_allocator.create(Connection) catch unreachable;
c.* = Connection{ .handle = tx.conn_handle };
std.heap.page_allocator.destroy(tx);
return .{ .ok = c };
}
// close: ONLY accepts [!active] - forces meaningful use before close
~pub event close { conn: *Connection[!active] }
| ok
| err []const u8
~proc close {
std.debug.print("Connection closed\n", .{});
std.heap.page_allocator.destroy(conn);
return .{ .ok = .{} };
}
Test Configuration
MUST_FAIL