cli: lower consuming ownership builtin operands iteratively
This commit is contained in:
1 parent
5cc69393e4
commit
062fce5cfd
5 files changed
+333
-15
No files matched your search
+208
-14
@@ -2016,6 +2016,7 @@ const Lowerer = struct {
|
||||
.ordinary => |c| l.operandExprPrepared(e, null, .{ .call = c }),
|
||||
.variant => |c| l.operandExprPrepared(e, null, .{ .variant = c }),
|
||||
.boolean_builtin => |c| l.operandExprPrepared(e, null, .{ .boolean_builtin = c }),
|
||||
.consuming_builtin => |c| l.operandExprPrepared(e, null, .{ .consuming_builtin = c }),
|
||||
},
|
||||
.if_expr => |x| return l.ifExpr(e.span, x.*, want),
|
||||
.block => |b| {
|
||||
@@ -2059,6 +2060,7 @@ const Lowerer = struct {
|
||||
array_repeat: ast.Expr,
|
||||
variant_argument: *VariantConstruction,
|
||||
boolean_builtin: *BooleanBuiltin,
|
||||
consuming_builtin: *ConsumingBuiltin,
|
||||
};
|
||||
var pending: std.ArrayList(Frame) = .empty;
|
||||
defer pending.deinit(l.arena);
|
||||
@@ -2136,6 +2138,14 @@ const Lowerer = struct {
|
||||
want = .bool;
|
||||
continue;
|
||||
},
|
||||
.consuming_builtin => |c| {
|
||||
prepared = .none;
|
||||
try l.checkConsumingName(c.input);
|
||||
try pending.append(l.arena, .{ .consuming_builtin = c });
|
||||
e = c.input;
|
||||
want = null;
|
||||
continue;
|
||||
},
|
||||
}
|
||||
switch (e.kind) {
|
||||
.not, .deref => |x| {
|
||||
@@ -2191,6 +2201,7 @@ const Lowerer = struct {
|
||||
.ordinary => |c| prepared = .{ .call = c },
|
||||
.variant => |c| prepared = .{ .variant = c },
|
||||
.boolean_builtin => |c| prepared = .{ .boolean_builtin = c },
|
||||
.consuming_builtin => |c| prepared = .{ .consuming_builtin = c },
|
||||
},
|
||||
.variant => |x| switch (try l.prepareVariant(e, x.@"enum", x.name, &.{})) {
|
||||
.done => |v| break v,
|
||||
@@ -2312,6 +2323,7 @@ const Lowerer = struct {
|
||||
try l.emit(try l.fmt("{s} %{s}", .{ if (c.assume) "assume" else "assert", v.name }), c.span);
|
||||
v = .{ .name = "", .ty = .void };
|
||||
},
|
||||
.consuming_builtin => |c| v = try l.finishConsumingBuiltin(c, v),
|
||||
.field_value => |f| v = try l.finishField(f, v),
|
||||
.array_element => |c| {
|
||||
const input = c.inputs[c.index];
|
||||
@@ -2385,6 +2397,7 @@ const Lowerer = struct {
|
||||
array: *ArrayConstruction,
|
||||
variant: *VariantConstruction,
|
||||
boolean_builtin: *BooleanBuiltin,
|
||||
consuming_builtin: *ConsumingBuiltin,
|
||||
};
|
||||
|
||||
const OrdinaryCall = struct {
|
||||
@@ -2398,7 +2411,20 @@ const Lowerer = struct {
|
||||
temps: std.ArrayList([]const u8) = .empty,
|
||||
};
|
||||
const BooleanBuiltin = struct { span: Span, input: ast.Expr, assume: bool };
|
||||
const CallDispatch = union(enum) { done: Value, ordinary: *OrdinaryCall, variant: *VariantConstruction, boolean_builtin: *BooleanBuiltin };
|
||||
const ConsumingBuiltin = struct { span: Span, input: ast.Expr, operation: enum { take, free, destroy } };
|
||||
const CallDispatch = union(enum) { done: Value, ordinary: *OrdinaryCall, variant: *VariantConstruction, boolean_builtin: *BooleanBuiltin, consuming_builtin: *ConsumingBuiltin };
|
||||
|
||||
fn finishConsumingBuiltin(l: *Lowerer, c: *ConsumingBuiltin, o: Value) Error!Value {
|
||||
if (c.operation == .take) {
|
||||
if (o.ty != .own) return l.fail("E7005", c.input.span, "`own.take` takes an `own<T>`", &.{.{ .name = "found", .value = try l.diagnosticTyText(o.ty) }});
|
||||
return l.def("v", o.ty.own.*, try l.fmt("own.take %{s}", .{o.name}), c.span);
|
||||
}
|
||||
const free = c.operation == .free;
|
||||
const ok = if (free) (o.ty == .own or o.ty == .own_array) else o.ty == .region;
|
||||
if (!ok) return l.fail("E7005", c.input.span, if (free) "`own.free` takes an `own<T>` or `own_array<T>`" else "`region.destroy` takes a region", &.{.{ .name = "found", .value = try l.diagnosticTyText(o.ty) }});
|
||||
try l.emit(try l.fmt("{s} %{s}", .{ if (free) "own.free" else "region.destroy", o.name }), c.span);
|
||||
return .{ .name = "", .ty = .void };
|
||||
}
|
||||
|
||||
fn call(l: *Lowerer, e: ast.Expr, callee: ast.Expr, args: []const ast.Expr, borrow_allowed: bool) Error!Value {
|
||||
return switch (try l.prepareCall(e, callee, args, borrow_allowed)) {
|
||||
@@ -2406,6 +2432,7 @@ const Lowerer = struct {
|
||||
.ordinary => |c| l.operandExprPrepared(e, null, .{ .call = c }),
|
||||
.variant => |c| l.operandExprPrepared(e, null, .{ .variant = c }),
|
||||
.boolean_builtin => |c| l.operandExprPrepared(e, null, .{ .boolean_builtin = c }),
|
||||
.consuming_builtin => |c| l.operandExprPrepared(e, null, .{ .consuming_builtin = c }),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2446,19 +2473,13 @@ const Lowerer = struct {
|
||||
if (pathOf(callee)) |p| {
|
||||
if (std.mem.eql(u8, p, "slice.copy")) return .{ .done = try l.sliceCopy(e, args) };
|
||||
if (l.fallible(p) != null) return l.invalid(.invalid_operation, e.span, "a fallible operation is handled with `match` over its outcomes or with `or trap`", &.{.{ .name = "operation", .value = p }});
|
||||
if (std.mem.eql(u8, p, "own.take")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "`own.take` takes one owner", &.{});
|
||||
const o = try l.consuming(args[0], null);
|
||||
if (o.ty != .own) return l.fail("E7005", args[0].span, "`own.take` takes an `own<T>`", &.{.{ .name = "found", .value = try l.diagnosticTyText(o.ty) }});
|
||||
return .{ .done = try l.def("v", o.ty.own.*, try l.fmt("own.take %{s}", .{o.name}), e.span) };
|
||||
}
|
||||
if (std.mem.eql(u8, p, "own.free") or std.mem.eql(u8, p, "region.destroy")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "this operation takes one linear value", &.{});
|
||||
const o = try l.consuming(args[0], null);
|
||||
const ok = if (std.mem.eql(u8, p, "own.free")) (o.ty == .own or o.ty == .own_array) else o.ty == .region;
|
||||
if (!ok) return l.fail("E7005", args[0].span, if (std.mem.eql(u8, p, "own.free")) "`own.free` takes an `own<T>` or `own_array<T>`" else "`region.destroy` takes a region", &.{.{ .name = "found", .value = try l.diagnosticTyText(o.ty) }});
|
||||
try l.emit(try l.fmt("{s} %{s}", .{ p, o.name }), e.span);
|
||||
return .{ .done = .{ .name = "", .ty = .void } };
|
||||
const take = std.mem.eql(u8, p, "own.take");
|
||||
const free = std.mem.eql(u8, p, "own.free");
|
||||
if (take or free or std.mem.eql(u8, p, "region.destroy")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, if (take) "`own.take` takes one owner" else "this operation takes one linear value", &.{});
|
||||
const state = try l.arena.create(ConsumingBuiltin);
|
||||
state.* = .{ .span = e.span, .input = args[0], .operation = if (take) .take else if (free) .free else .destroy };
|
||||
return .{ .consuming_builtin = state };
|
||||
}
|
||||
if (std.mem.eql(u8, p, "region.reset")) {
|
||||
if (args.len != 1 or args[0].kind != .name) return l.fail("E7005", e.span, "`region.reset` takes the region by name", &.{});
|
||||
@@ -8416,3 +8437,176 @@ test "boolean builtin values preserve aggregate and field consumer failures" {
|
||||
for ([_]?Ty{ null, .bool, .{ .int = "u8" } }) |want| try expectFieldMatches(a, parsed.ok, e, want);
|
||||
};
|
||||
}
|
||||
|
||||
fn consumingTestLowerer(a: Allocator, m: ast.Module, span: Span) !Lowerer {
|
||||
var l = try fieldTestLowerer(a, m, span);
|
||||
const integer: Ty = .{ .int = "i64" };
|
||||
const owner = try l.wrap(.own, integer);
|
||||
const owner_array = try l.wrap(.own_array, .{ .int = "u8" });
|
||||
try l.bindings.appendSlice(a, &.{
|
||||
.{ .name = "r", .value = "region_input", .mutable = false, .ty = .region },
|
||||
.{ .name = "oa", .value = "array_owner", .mutable = false, .ty = owner_array },
|
||||
.{ .name = "oo", .value = "nested_owner", .mutable = false, .ty = try l.wrap(.own, owner) },
|
||||
.{ .name = "ob", .value = "borrow_owner", .mutable = false, .ty = try l.wrap(.own, try l.wrap(.ref, integer)) },
|
||||
.{ .name = "obool", .value = "boolean_owner", .mutable = false, .ty = try l.wrap(.own, .bool) },
|
||||
.{ .name = "aggregate_owner", .value = "aggregate_owner", .mutable = false, .ty = try l.wrap(.own, l.enumOrStruct("Owner", span).?) },
|
||||
});
|
||||
try l.sigs.put(a, "keep", .{ .params = try a.dupe(Ty, &.{owner}), .ret = owner, .air_name = "keep", .display_name = "keep" });
|
||||
try l.sigs.put(a, "keep_region", .{ .params = try a.dupe(Ty, &.{.region}), .ret = .region, .air_name = "keep_region", .display_name = "keep_region" });
|
||||
return l;
|
||||
}
|
||||
|
||||
fn expectConsumingMatches(a: Allocator, m: ast.Module, e: ast.Expr, want: ?Ty) !void {
|
||||
var old = try consumingTestLowerer(a, m, e.span);
|
||||
var current = try consumingTestLowerer(a, m, e.span);
|
||||
const expected = ReferenceOperands.expr(&old, e, want);
|
||||
const actual = current.expr(e, want);
|
||||
if (expected) |v| {
|
||||
const result = try actual;
|
||||
try std.testing.expectEqualStrings(v.name, result.name);
|
||||
try std.testing.expect(v.ty.eql(result.ty));
|
||||
} else |err| try std.testing.expectError(err, actual);
|
||||
try std.testing.expectEqualDeep(old.problem, current.problem);
|
||||
try std.testing.expectEqualDeep(old.unimpl, current.unimpl);
|
||||
try std.testing.expectEqualDeep(old.blocks.items, current.blocks.items);
|
||||
try std.testing.expectEqualDeep(old.allocs.items, current.allocs.items);
|
||||
try std.testing.expectEqualDeep(old.constants.items, current.constants.items);
|
||||
try std.testing.expectEqualDeep(old.bindings.items, current.bindings.items);
|
||||
try std.testing.expectEqual(old.counter, current.counter);
|
||||
try std.testing.expectEqual(old.cur, current.cur);
|
||||
try std.testing.expectEqualDeep(old.uses, current.uses);
|
||||
try std.testing.expectEqual(old.linear_enabled, current.linear_enabled);
|
||||
try std.testing.expectEqual(old.in_constant, current.in_constant);
|
||||
try std.testing.expectEqual(old.borrow_parents.count(), current.borrow_parents.count());
|
||||
var parents = old.borrow_parents.iterator();
|
||||
while (parents.next()) |item| try std.testing.expectEqualStrings(item.value_ptr.*, current.borrow_parents.get(item.key_ptr.*).?);
|
||||
}
|
||||
|
||||
test "consuming builtin continuations match former moves types aggregates CFG and errors" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const expressions = [_][]const u8{
|
||||
"own.take(move o)", "own.take(o)", "own.take(move oo)", "own.take(move ob)",
|
||||
"own.take(move oa)", "own.free(move oa)", "own.free(oa)", "own.free(move oo)",
|
||||
"own.free(move o)", "own.free(o)", "region.destroy(move r)", "region.destroy(r)",
|
||||
"own.take(keep(move o))", "own.free(keep(move o))", "region.destroy(keep_region(move r))", "own.take()",
|
||||
"own.free()", "region.destroy()", "own.take(unknown, true)", "own.free(unknown, false)",
|
||||
"region.destroy(unknown, false)", "own.take(unknown)", "own.free(unknown)", "region.destroy(unknown)",
|
||||
"own.take(1i64)", "own.free(true)", "region.destroy(false and bool_id(true))", "own.free(ref n)",
|
||||
"own.take(sc)", "own.free(move n)", "own.take(stop(b\"xy\"))", "region.destroy(void_call(b\"xy\"))",
|
||||
"own.take(Box { n: 1i64 }.n)", "own.free([1i64; 2])", "own.free(assert(true))", "own.free(own.take(move oo))",
|
||||
"own.take(own.take(move oo))", "own.take(move aggregate_owner)", "own.free(move aggregate_owner)", "own.take(move obool)",
|
||||
};
|
||||
const forms = [_][]const u8{ "{s}", "id({s})", "own.take({s})", "own.free({s})", "region.destroy({s})", "not ({s})", "({s}) as! u8", "[{s}]", "Box {{ n: {s} }}.n", "Choice::some({s})", "({s}) == 1i64", "1i64 == ({s})", "true and ({s})", "({s}) or false", "- ({s})" };
|
||||
inline for (forms) |form| for (expressions) |text| {
|
||||
const parsed = try parse_mod.parse(a, try std.fmt.allocPrint(a, "{s}fn f() -> void effects [] {{ return {s}; }}", .{ fieldTestDeclarations, try std.fmt.allocPrint(a, form, .{text}) }));
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const e = parsed.ok.functions[0].body.stmts[0].kind.@"return".?;
|
||||
for ([_]?Ty{ null, .bool, .{ .int = "i64" } }) |want| try expectConsumingMatches(a, parsed.ok, e, want);
|
||||
};
|
||||
}
|
||||
|
||||
fn consumingTestCallee(a: Allocator, name: []const u8) !*const ast.Expr {
|
||||
const dot = std.mem.indexOfScalar(u8, name, '.').?;
|
||||
const root = try a.create(ast.Expr);
|
||||
root.* = .{ .span = unaryTestSpan(70_001), .kind = .{ .name = name[0..dot] } };
|
||||
const callee = try a.create(ast.Expr);
|
||||
callee.* = .{ .span = unaryTestSpan(70_002), .kind = .{ .field = .{ .e = root, .name = name[dot + 1 ..] } } };
|
||||
return callee;
|
||||
}
|
||||
|
||||
test "deep consuming builtin operands and rejected nested operations avoid native descent" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const m = (try parse_mod.parse(a, fieldTestDeclarations)).ok;
|
||||
const count = 32_768;
|
||||
for ([_][]const u8{ "own.take", "own.free", "region.destroy" }) |operation| {
|
||||
const region = std.mem.eql(u8, operation, "region.destroy");
|
||||
const callee = try consumingTestCallee(a, operation);
|
||||
const keep: ast.Expr = .{ .span = unaryTestSpan(70_003), .kind = .{ .name = if (region) "keep_region" else "keep" } };
|
||||
var e: ast.Expr = .{ .span = unaryTestSpan(90_000), .kind = .{ .move = if (region) "r" else "o" } };
|
||||
for (0..count) |i| {
|
||||
const args = try a.alloc(ast.Expr, 1);
|
||||
args[0] = e;
|
||||
e = .{ .span = unaryTestSpan(@intCast(i + 1)), .kind = .{ .call = .{ .callee = &keep, .type_arg = null, .args = args } } };
|
||||
}
|
||||
const args = try a.alloc(ast.Expr, 1);
|
||||
args[0] = e;
|
||||
const span = unaryTestSpan(100_000);
|
||||
const root: ast.Expr = .{ .span = span, .kind = .{ .call = .{ .callee = callee, .type_arg = null, .args = args } } };
|
||||
var l = try consumingTestLowerer(a, m, span);
|
||||
const initial_counter = l.counter;
|
||||
const v = try l.expr(root, null);
|
||||
const take = std.mem.eql(u8, operation, "own.take");
|
||||
try std.testing.expect(if (take) v.ty.eql(.{ .int = "i64" }) else v.ty == .void);
|
||||
const insts = l.blocks.items[0].insts.items;
|
||||
try std.testing.expectEqual(count + 1, insts.len);
|
||||
try std.testing.expectEqual(initial_counter + count + @intFromBool(take), l.counter);
|
||||
for (0..count) |i| try std.testing.expectEqualDeep(@as(?Span, unaryTestSpan(@intCast(i + 1))), insts[i].span);
|
||||
try std.testing.expect(std.mem.indexOf(u8, insts[count].text, operation) != null);
|
||||
try std.testing.expectEqualDeep(@as(?Span, span), insts[count].span);
|
||||
|
||||
e = .{ .span = unaryTestSpan(90_000), .kind = .{ .move = if (region) "r" else "o" } };
|
||||
for (0..count) |i| {
|
||||
const inputs = try a.alloc(ast.Expr, 1);
|
||||
inputs[0] = e;
|
||||
e = .{ .span = unaryTestSpan(@intCast(i + 1)), .kind = .{ .call = .{ .callee = callee, .type_arg = null, .args = inputs } } };
|
||||
if (i > 0 and i % 3 == 0) {
|
||||
const input = try a.alloc(ast.Expr, 1);
|
||||
input[0] = e;
|
||||
e = .{ .span = unaryTestSpan(@intCast(i + 1)), .kind = .{ .call = .{ .callee = &keep, .type_arg = null, .args = input } } };
|
||||
}
|
||||
}
|
||||
l = try consumingTestLowerer(a, m, e.span);
|
||||
try std.testing.expectError(error.Lower, l.expr(e, null));
|
||||
try std.testing.expectEqualStrings("E7005", l.problem.?.code);
|
||||
try std.testing.expectEqualDeep(unaryTestSpan(1), l.problem.?.span);
|
||||
try std.testing.expectEqual(@as(usize, 1), l.blocks.items[0].insts.items.len);
|
||||
try std.testing.expectEqual(@as(usize, @intFromBool(take)), l.counter);
|
||||
try std.testing.expect(l.unimpl == null);
|
||||
}
|
||||
}
|
||||
|
||||
test "public consuming builtin lowering propagates OOM on success and rejection" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const programs = [_][]const u8{
|
||||
"module oom\nfn keep(o: own<i64>) -> own<i64> effects [] { return move o; }\nfn keep_region(r: region) -> region effects [] { return move r; }\nfn get(o: own<i64>) -> i64 effects [mem.alloc] { return own.take(keep(move o)); }\nfn release(o: own<i64>) -> void effects [mem.alloc] { own.free(keep(move o)); }\nfn kill(r: region) -> void effects [mem.alloc] { region.destroy(keep_region(move r)); }\n",
|
||||
"module oom\nfn bad(o: own<i64>) -> void effects [] { own.free(o); }\n",
|
||||
"module oom\nfn bad() -> void effects [] { own.take(1i64); }\n",
|
||||
"module oom\nfn bad() -> void effects [] { region.destroy(); }\n",
|
||||
};
|
||||
for (programs, 0..) |program, program_index| {
|
||||
const parsed = try parse_mod.parse(fixture.allocator(), try std.fmt.allocPrint(fixture.allocator(), "{s}fn main() -> i64 effects [] {{ return 42i64; }}\n", .{program}));
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const baseline = try lower(fixture.allocator(), parsed.ok);
|
||||
if (program_index == 0) {
|
||||
try std.testing.expect(baseline == .ok);
|
||||
} else {
|
||||
try std.testing.expect(baseline == .err);
|
||||
try std.testing.expectEqualStrings(if (program_index == 1) "E7003" else "E7005", baseline.err.code);
|
||||
}
|
||||
for ([_]bool{ false, true }) |persistent| {
|
||||
var fail_at: usize = 0;
|
||||
while (true) : (fail_at += 1) {
|
||||
try std.testing.expect(fail_at < 1024);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_at });
|
||||
var output: std.heap.ArenaAllocator = .init(if (persistent) failing.allocator() else std.testing.allocator);
|
||||
var recovering: QueryAllocationFailure = .{ .backing = output.allocator(), .fail_at = fail_at };
|
||||
const result = lower(if (persistent) output.allocator() else recovering.allocator(), parsed.ok);
|
||||
output.deinit();
|
||||
if (persistent) try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
const failed = if (persistent) failing.has_induced_failure else recovering.failed;
|
||||
if (failed) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
if (!persistent) try std.testing.expectEqual(fail_at + 1, recovering.attempts);
|
||||
} else {
|
||||
try std.testing.expect(std.meta.activeTag(try result) == @as(std.meta.Tag(Result), if (program_index == 0) .ok else .err));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user