09 empty transaction

✓ Passing This code compiles and runs correctly.

Code

// TEST: Try to commit a transaction without executing anything
//
// begin() produces Transaction[started!]
// commit() requires Transaction[!active]
// These states don't match! You can't commit an empty transaction.
//
// This prevents wasteful "BEGIN; COMMIT;" patterns.
// If you start a transaction, you MUST do something with it.
//
// EXPECTED: Compiler error - type mismatch (started vs active)

~import "$app/db"

~app.db:connect(host: "localhost")
| ok c |>
    app.db:begin(conn: c.conn)
    | ok t |>
        // Try to commit immediately without exec - this should FAIL!
        // commit() needs [!active], but we have [started!]
        app.db:commit(tx: t.tx)
        | ok conn |>
            app.db:close(conn: conn.conn)
| err _ |> _
input.kz

Error Verification

Expected Error Pattern

state mismatch

Actual Compiler Output ✓ Pattern matched

[PHASE 2.4] Calling run_pass for transforms\n[PHASE 2.5] Executing comptime_main() - running comptime flows
[PHASE 2.5] Comptime flows complete (27 items)
[PHASE 2.6] Rescanning transformed AST (27 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]: Phantom state mismatch: expected 'app.db:active' but got 'app.db:started!' for argument 'tx'
  --> phantom_semantic_check:15:0

❌ Compiler coordination error: Phantom semantic validation failed
error: CompilerCoordinationFailed
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_09_empty_transaction/backend.zig:9647:17: 0x10212a433 in emit (backend)
                return error.CompilerCoordinationFailed;
                ^
/Users/larsde/src/koru/tests/regression/900_EXAMPLES_SHOWCASE/910_LANGUAGE_SHOOTOUT/2104_09_empty_transaction/backend.zig:9731:28: 0x10212b1bf 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 { conn: *Connection[connected!] }
| 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 [!connected] OR [!active], produces Transaction[started!]
~pub event begin { conn: *Connection[!connected|!active] }
| ok { tx: *Transaction[started!] }

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

// exec: consumes [!started] OR [!active], produces Transaction[active!]
~pub event exec { tx: *Transaction[!started|!active], sql: []const u8 }
| ok { tx: *Transaction[active!] }

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

// commit: consumes [!active], returns Connection[active!]
~pub event commit { tx: *Transaction[!active] }
| ok { conn: *Connection[active!] }

~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 = .{ .conn = c } };
}

// rollback: consumes [!active], returns Connection[active!]
~pub event rollback { tx: *Transaction[!active] }
| ok { conn: *Connection[active!] }

~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 = .{ .conn = c } };
}

// close: ONLY accepts [!active] - forces meaningful use before close
~pub event close { conn: *Connection[!active] }

~proc close {
    std.debug.print("Connection closed\n", .{});
    std.heap.page_allocator.destroy(conn);
}
db.kz

Test Configuration

MUST_FAIL