cli: validate inferred allocation content before AIR

This commit is contained in:
dev committed 2026-10-08 10:31:10 +03:00
1 parent 1bbfed24a8
commit bc5412dadd
67 files changed
+607 -8

No files matched your search

+33 -7
View File
@@ -775,6 +775,28 @@ const Lowerer = struct {
});
}
/// Constructors need a real content value before forming any result or
/// outcome. A never-returning operand has already terminated the block.
fn contentValue(l: *Lowerer, e: ast.Expr) Error!Value {
const v = try l.consuming(e, null);
if (v.ty == .void or v.ty == .never) return l.contentPositionError(e.span, v.ty);
return v;
}
fn contentPositionError(l: *Lowerer, span: Span, ty: Ty) Error {
return l.fail("E7005", span, "type constructor not allowed in this position", &.{
.{ .name = "position", .value = "member" },
.{ .name = "type_constructor", .value = @tagName(ty) },
});
}
fn copyContent(l: *Lowerer, span: Span, ty: Ty, operation: []const u8) Error!void {
if (!verifier_types.isCopy(l.property_module.?, try l.airType(ty))) return l.fail("E7005", span, "this allocation requires Copy content", &.{
.{ .name = "constraint", .value = "Copy" },
.{ .name = "operation", .value = operation },
});
}
fn function(l: *Lowerer, f: ast.FnDecl) Error!FnOut {
l.blocks = .empty;
l.bindings = .empty;
@@ -2849,7 +2871,7 @@ const Lowerer = struct {
return l.invalid(.invalid_borrow, e.span, "a graph access is a borrow: bind it with `ref x = island.at(g, r);` or `mut x = island.at(g, r);`", &.{});
if (eq(p, "mutex.make")) {
if (args.len != 1) return l.fail("E7005", e.span, "`mutex.make` takes the initial value", &.{});
const v = try l.consuming(args[0], null);
const v = try l.contentValue(args[0]);
try l.elementRule(v.ty, args[0].span, "a mutex's content");
return try l.def("m", try l.wrap(.mutex, v.ty), try l.fmt("mutex.make %{s}", .{v.name}), e.span);
}
@@ -3259,14 +3281,16 @@ const Lowerer = struct {
}
if (std.mem.eql(u8, path, "own.pack")) {
try want(l, e.span, args, 2, path);
const v = try l.consuming(args[0], null);
const v = try l.contentValue(args[0]);
if (v.ty.isBorrow()) return l.contentPositionError(args[0].span, v.ty);
const c = try l.expectCap(args[1], "mem.alloc");
l.linear_enabled = true;
return .{ .text = try l.fmt("own.pack {s} %{s}, %{s}", .{ l.tyText(v.ty), v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.own, v.ty)} }, .{ .role = "err", .params = &.{ alloc_err, v.ty } } }) };
}
if (std.mem.eql(u8, path, "own.new")) {
try want(l, e.span, args, 2, path);
const v = try l.consuming(args[0], null);
const v = try l.contentValue(args[0]);
try l.copyContent(args[0].span, v.ty, path);
const c = try l.expectCap(args[1], "mem.alloc");
return .{ .text = try l.fmt("own.new %{s}, %{s}", .{ v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.own, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
@@ -3287,7 +3311,8 @@ const Lowerer = struct {
if (std.mem.eql(u8, path, "region.new")) {
try want(l, e.span, args, 2, path);
const rb = try l.regionBorrow(args[0]);
const v = try l.consuming(args[1], null);
const v = try l.contentValue(args[1]);
try l.copyContent(args[1].span, v.ty, path);
return .{ .borrow_parent = rb.name, .text = try l.fmt("region.new %{s}, %{s}", .{ rb.name, v.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.mut, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
if (std.mem.eql(u8, path, "region.array")) {
@@ -3295,7 +3320,8 @@ const Lowerer = struct {
const rb = try l.regionBorrow(args[0]);
const n = try l.expr(args[1], usize_ty);
try l.checkTy(args[1].span, n.ty, usize_ty);
const v = try l.consuming(args[2], null);
const v = try l.contentValue(args[2]);
try l.copyContent(args[2].span, v.ty, path);
return .{ .borrow_parent = rb.name, .text = try l.fmt("region.array %{s}, %{s}, %{s}", .{ rb.name, n.name, v.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.slice_mut, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
if (authority.intrinsicIndex(path)) |ix| {
@@ -3314,7 +3340,7 @@ const Lowerer = struct {
}
if (std.mem.eql(u8, path, "island.create") or std.mem.eql(u8, path, "arc.new")) {
try want(l, e.span, args, 2, path);
const v = try l.consuming(args[0], null);
const v = try l.contentValue(args[0]);
const c = try l.expectCap(args[1], "mem.alloc");
const owner = if (std.mem.eql(u8, path, "arc.new")) try l.wrap(.arc, v.ty) else try l.wrap(.island, v.ty);
return .{ .text = try l.fmt("{s} %{s}, %{s}", .{ path, v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{owner} }, .{ .role = "err", .params = &.{alloc_err} } }) };
@@ -3322,7 +3348,7 @@ const Lowerer = struct {
if (std.mem.eql(u8, path, "island.alloc")) {
try want(l, e.span, args, 2, path);
const g = try l.graphBorrow(args[0], "island", true);
const v = try l.consuming(args[1], null);
const v = try l.contentValue(args[1]);
return .{ .text = try l.fmt("island.alloc %{s}, %{s}", .{ g.name, v.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.iref, v.ty)} }, .{ .role = "err", .params = &.{alloc_err} } }) };
}
if (std.mem.eql(u8, path, "task.check_cancel")) {