✓
Passing This code compiles and runs correctly.
Code
// Test: Auto-discharge for phantom obligations
//
// Tests that the compiler automatically inserts cleanup
// when [allocated!] obligation is not manually discharged
~import std/io
const std = @import("std");
pub const Resource = struct {
data: []const u8,
allocator: std.mem.Allocator,
};
~pub event create-resource { name: []const u8 }
| created *Resource<allocated!>
~proc create-resource|zig {
const alloc = std.heap.page_allocator;
const data = std.fmt.allocPrint(alloc, "Resource: {s}", .{name}) catch unreachable;
const res = alloc.create(Resource) catch unreachable;
res.* = .{ .data = data, .allocator = alloc };
return .{ .created = res };
}
~pub event destroy-resource { res: *Resource<!allocated> }
~proc destroy-resource|zig {
std.debug.print(" Auto-discharge called!\n", .{});
res.allocator.free(res.data);
std.heap.page_allocator.destroy(res);
}
// Helper to use the resource
~pub event use-resource { res: *Resource }
~proc use-resource|zig {
std.debug.print(" Using: {s}\n", .{res.data});
}
// Test: Create resource, use it, but DON'T manually destroy
// Auto-discharge should insert destroy-resource
~std/io:print.ln("Test start")
~create-resource(name: "AutoDisposed")
| created r |> use-resource(res: r)
// Obligation [allocated!] should trigger auto-discharge here
~std/io:print.ln("Test done")
Actual
Test start
Using: Resource: AutoDisposed
Auto-discharge called!
Test done
Expected output
Test start
Using: Resource: AutoDisposed
Auto-discharge called!
Test done
Test Configuration
MUST_RUN