007 use after disposal

✓ Passing This code compiles and runs correctly.

Code

// Test 516: Use-after-disposal error
// Tests that using a resource after disposal is caught
//
// Key points:
// - open() returns *File[opened!]
// - close() accepts *File[!opened] and marks it as disposed
// - use_file() expects *File[opened]
// - ERROR: Cannot use f.file after it was disposed by close()

~import "$app/fs"

~app.fs:open(path: "test.txt")
| opened f |> app.fs:close(file: f.file)
    | closed |> app.fs:use_file(file: f.file)  // ERROR: f.file was disposed!
        | used |> _
input.kz

Imported Files

const std = @import("std");
const File = struct { handle: i32 };

~pub event open { path: []const u8 }
| opened { file: *File[opened!] }

~proc open {
    std.debug.print("Opening file: {s}\n", .{path});
    const allocator = std.heap.page_allocator;
    const f = allocator.create(File) catch unreachable;
    f.* = File{ .handle = 42 };
    return .{ .opened = .{ .file = f } };
}

~pub event close { file: *File[!opened] }  // Consumes obligation
| closed {}

~proc close {
    std.debug.print("Closing file\n", .{});
    return .{ .closed = .{} };
}

~pub event use_file { file: *File[opened] }  // Expects opened file
| used {}

~proc use_file {
    std.debug.print("Using file\n", .{});
    return .{ .used = .{} };
}
fs.kz

Test Configuration

MUST_FAIL