cli: lower place bases iteratively
This commit is contained in:
5 files changed
+529
-54
No files matched your search
+326
-48
@@ -3208,7 +3208,88 @@ const Lowerer = struct {
|
||||
return p;
|
||||
}
|
||||
|
||||
/// Descend field/index bases without a native call per projection. The
|
||||
/// caller arena owns frames and the one ancestor list until lowering ends.
|
||||
/// Expression callbacks (dereference and index bounds) remain separate.
|
||||
fn placeRaw(l: *Lowerer, place: ast.Expr, mutable: bool, span: Span) Error!Placed {
|
||||
const Frame = struct { place: ast.Expr, unchecked: bool = false };
|
||||
var frames: std.ArrayList(Frame) = .empty;
|
||||
defer frames.deinit(l.arena);
|
||||
var e = place;
|
||||
while (true) switch (e.kind) {
|
||||
.field => |f| {
|
||||
try frames.append(l.arena, .{ .place = e });
|
||||
e = f.e.*;
|
||||
},
|
||||
.index => |x| {
|
||||
try frames.append(l.arena, .{ .place = e, .unchecked = l.unchecked });
|
||||
l.unchecked = false;
|
||||
e = x.e.*;
|
||||
},
|
||||
else => break,
|
||||
};
|
||||
var base = try l.placeLeaf(e, mutable, span);
|
||||
if (frames.items.len == 0) return base;
|
||||
var ends: std.ArrayList([]const u8) = .empty;
|
||||
defer ends.deinit(l.arena);
|
||||
try ends.appendSlice(l.arena, base.ends);
|
||||
while (frames.pop()) |frame| {
|
||||
const projection = frame.place;
|
||||
switch (projection.kind) {
|
||||
.field => |f| {
|
||||
const st = base.v.ty.inner() orelse return l.fail("E7005", projection.span, "field of a value that is not a struct", &.{});
|
||||
const d = l.decl(st) orelse return l.fail("E7005", projection.span, "field of a value that is not a struct", &.{.{ .name = "found", .value = try l.diagnosticTyText(st) }});
|
||||
if (d.kind != .@"struct") return l.fail("E7005", projection.span, "field of an enum; use `match`", &.{});
|
||||
const fd = for (d.kind.@"struct") |x| {
|
||||
if (std.mem.eql(u8, x.name, f.name)) break x;
|
||||
} else return l.fail("E7002", projection.span, "no such field", &.{ .{ .name = "type", .value = d.display_name orelse d.name }, .{ .name = "field", .value = f.name } });
|
||||
const ft = try l.typeOf(fd.ty);
|
||||
const v = try l.defBorrow(f.name, if (base.v.ty.isMutBorrow()) try l.wrap(.mut, ft) else try l.wrap(.ref, ft), try l.fmt("borrow.field @{s}.{s}, %{s}", .{ d.air_name orelse d.name, f.name, base.v.name }), base.v.name, span);
|
||||
try ends.append(l.arena, v.name);
|
||||
base.v = v;
|
||||
},
|
||||
.index => |x| {
|
||||
l.unchecked = frame.unchecked;
|
||||
const bt = base.v.ty;
|
||||
const elem = elemOf(bt) orelse return l.fail("E7005", projection.span, "indexing needs an array or a slice", &.{.{ .name = "found", .value = try l.diagnosticTyText(bt) }});
|
||||
if (x.hi) |hi| {
|
||||
// `s[a..b]`: slice.sub with start a and length b - a (checked).
|
||||
var sl = base.v;
|
||||
if (bt == .ref or bt == .mut) {
|
||||
sl = try l.defBorrow("s", (if (bt == .mut) try l.wrap(.slice_mut, elem) else try l.wrap(.slice, elem)), try l.fmt("slice.make %{s}", .{base.v.name}), base.v.name, span);
|
||||
try ends.append(l.arena, sl.name);
|
||||
}
|
||||
const a = try l.expr(x.lo.*, .{ .int = "usize" });
|
||||
try l.checkOperandTy(x.lo.span, a.ty, .{ .int = "usize" });
|
||||
const b = try l.expr(hi.*, .{ .int = "usize" });
|
||||
try l.checkOperandTy(hi.span, b.ty, .{ .int = "usize" });
|
||||
const len = try l.def("len", .{ .int = "usize" }, try l.fmt("sub.checked.usize %{s}, %{s}", .{ b.name, a.name }), span);
|
||||
const sub_ = try l.defBorrow("s", sl.ty, try l.fmt("slice.sub %{s}, %{s}, %{s}", .{ sl.name, a.name, len.name }), sl.name, span);
|
||||
try ends.append(l.arena, sub_.name);
|
||||
base.v = sub_;
|
||||
continue;
|
||||
}
|
||||
// Only the outermost index of an unchecked place is unchecked.
|
||||
const unchecked = l.unchecked;
|
||||
l.unchecked = false;
|
||||
const i = try l.expr(x.lo.*, .{ .int = "usize" });
|
||||
try l.checkOperandTy(x.lo.span, i.ty, .{ .int = "usize" });
|
||||
const proven = for (l.facts.items) |f| {
|
||||
if (std.mem.eql(u8, f.index, i.name) and std.mem.eql(u8, f.slice, base.v.name)) break true;
|
||||
} else false;
|
||||
if (unchecked) l.uses.insert(.unsafe);
|
||||
const index_op = if (unchecked) "borrow.index.unchecked" else if (proven) "borrow.index.proven" else "borrow.index";
|
||||
const v = try l.defBorrow("e", if (bt.isMutBorrow()) try l.wrap(.mut, elem) else try l.wrap(.ref, elem), try l.fmt("{s} %{s}, %{s}", .{ index_op, base.v.name, i.name }), base.v.name, span);
|
||||
try ends.append(l.arena, v.name);
|
||||
base.v = v;
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
return .{ .v = base.v, .ends = try ends.toOwnedSlice(l.arena) };
|
||||
}
|
||||
|
||||
fn placeLeaf(l: *Lowerer, place: ast.Expr, mutable: bool, span: Span) Error!Placed {
|
||||
const op = if (mutable) "borrow.mut" else "borrow.ref";
|
||||
switch (place.kind) {
|
||||
.name => |n| {
|
||||
@@ -3249,54 +3330,6 @@ const Lowerer = struct {
|
||||
if (!r.ty.isBorrow()) return l.fail("E7005", place.span, "`*` needs a borrow", &.{});
|
||||
return .{ .v = r, .ends = &.{} };
|
||||
},
|
||||
.field => |f| {
|
||||
const base = try l.placeRaw(f.e.*, mutable, span);
|
||||
const st = base.v.ty.inner() orelse return l.fail("E7005", place.span, "field of a value that is not a struct", &.{});
|
||||
const d = l.decl(st) orelse return l.fail("E7005", place.span, "field of a value that is not a struct", &.{.{ .name = "found", .value = try l.diagnosticTyText(st) }});
|
||||
if (d.kind != .@"struct") return l.fail("E7005", place.span, "field of an enum; use `match`", &.{});
|
||||
const fd = for (d.kind.@"struct") |x| {
|
||||
if (std.mem.eql(u8, x.name, f.name)) break x;
|
||||
} else return l.fail("E7002", place.span, "no such field", &.{ .{ .name = "type", .value = d.display_name orelse d.name }, .{ .name = "field", .value = f.name } });
|
||||
const ft = try l.typeOf(fd.ty);
|
||||
const v = try l.defBorrow(f.name, if (base.v.ty.isMutBorrow()) try l.wrap(.mut, ft) else try l.wrap(.ref, ft), try l.fmt("borrow.field @{s}.{s}, %{s}", .{ d.air_name orelse d.name, f.name, base.v.name }), base.v.name, span);
|
||||
return .{ .v = v, .ends = try l.concat(base.ends, v.name) };
|
||||
},
|
||||
.index => |x| {
|
||||
const outer_unchecked = l.unchecked;
|
||||
l.unchecked = false;
|
||||
const base = try l.placeRaw(x.e.*, mutable, span);
|
||||
l.unchecked = outer_unchecked;
|
||||
const bt = base.v.ty;
|
||||
const elem = elemOf(bt) orelse return l.fail("E7005", place.span, "indexing needs an array or a slice", &.{.{ .name = "found", .value = try l.diagnosticTyText(bt) }});
|
||||
if (x.hi) |hi| {
|
||||
// `s[a..b]`: slice.sub with start a and length b - a (checked).
|
||||
var sl = base.v;
|
||||
var ends = base.ends;
|
||||
if (bt == .ref or bt == .mut) {
|
||||
sl = try l.defBorrow("s", (if (bt == .mut) try l.wrap(.slice_mut, elem) else try l.wrap(.slice, elem)), try l.fmt("slice.make %{s}", .{base.v.name}), base.v.name, span);
|
||||
ends = try l.concat(ends, sl.name);
|
||||
}
|
||||
const a = try l.expr(x.lo.*, .{ .int = "usize" });
|
||||
try l.checkOperandTy(x.lo.span, a.ty, .{ .int = "usize" });
|
||||
const b = try l.expr(hi.*, .{ .int = "usize" });
|
||||
try l.checkOperandTy(hi.span, b.ty, .{ .int = "usize" });
|
||||
const len = try l.def("len", .{ .int = "usize" }, try l.fmt("sub.checked.usize %{s}, %{s}", .{ b.name, a.name }), span);
|
||||
const sub_ = try l.defBorrow("s", sl.ty, try l.fmt("slice.sub %{s}, %{s}, %{s}", .{ sl.name, a.name, len.name }), sl.name, span);
|
||||
return .{ .v = sub_, .ends = try l.concat(ends, sub_.name) };
|
||||
}
|
||||
// Only the outermost index of an unchecked place is unchecked.
|
||||
const unchecked = l.unchecked;
|
||||
l.unchecked = false;
|
||||
const i = try l.expr(x.lo.*, .{ .int = "usize" });
|
||||
try l.checkOperandTy(x.lo.span, i.ty, .{ .int = "usize" });
|
||||
const proven = for (l.facts.items) |f| {
|
||||
if (std.mem.eql(u8, f.index, i.name) and std.mem.eql(u8, f.slice, base.v.name)) break true;
|
||||
} else false;
|
||||
if (unchecked) l.uses.insert(.unsafe);
|
||||
const index_op = if (unchecked) "borrow.index.unchecked" else if (proven) "borrow.index.proven" else "borrow.index";
|
||||
const v = try l.defBorrow("e", if (bt.isMutBorrow()) try l.wrap(.mut, elem) else try l.wrap(.ref, elem), try l.fmt("{s} %{s}, %{s}", .{ index_op, base.v.name, i.name }), base.v.name, span);
|
||||
return .{ .v = v, .ends = try l.concat(base.ends, v.name) };
|
||||
},
|
||||
else => return l.invalid(.invalid_place, place.span, "not a place: borrow a variable, a field `x.f`, an element `s[i]` or a range `s[a..b]`", &.{}),
|
||||
}
|
||||
}
|
||||
@@ -9791,3 +9824,248 @@ test "grown index continuation stack propagates every allocation failure" {
|
||||
}
|
||||
try std.testing.expect(later_prefix_failures > 0);
|
||||
}
|
||||
|
||||
// This oracle retains the former recursive raw-place body independently.
|
||||
// Expression children use ReferenceOperands; shared leaf/type/emission helpers
|
||||
// are deliberately outside this comparison's independence claim.
|
||||
fn expectRawPlaceMatches(a: Allocator, m: ast.Module, e: ast.Expr, mutable: bool, unchecked: bool) !void {
|
||||
var old = try indexTestLowerer(a, m, e.span);
|
||||
var current = try indexTestLowerer(a, m, e.span);
|
||||
old.unchecked = unchecked;
|
||||
current.unchecked = unchecked;
|
||||
const span = unaryTestSpan(123_456);
|
||||
const expected = ReferenceIndexPlaces.placeRaw(&old, e, mutable, span);
|
||||
const actual = current.placeRaw(e, mutable, span);
|
||||
if (expected) |p| {
|
||||
const result = try actual;
|
||||
try std.testing.expectEqualStrings(p.v.name, result.v.name);
|
||||
try std.testing.expect(p.v.ty.eql(result.v.ty));
|
||||
try std.testing.expectEqualDeep(p.ends, result.ends);
|
||||
} 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.unchecked, current.unchecked);
|
||||
try std.testing.expectEqualDeep(old.facts.items, current.facts.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 "raw place continuations preserve base range error loan and unchecked ordering" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const expressions = [_][]const u8{
|
||||
"xs", "ss", "sm", "ra", "n", "o", "oa", "unknown", "b\"xy\"", "*ra", "*n",
|
||||
"indexed.data", "ir.data", "matrix.data[0usize][0usize]", "box.n", "box.missing", "box.n.missing", "xs[0usize]", "xs[unknown]", "xs[true]", "ss[ix]", "sm[ix]",
|
||||
"n[unknown]", "unknown[unknown]", "xs[0usize..1usize]", "ss[0usize..1usize]", "sm[0usize..1usize]", "xs[unknown..true]", "xs[true..unknown]", "xs[0usize..unknown]", "xs[0usize..true]", "xs[0usize..1usize][0usize]", "indexed.data[0usize..1usize][0usize]",
|
||||
"ss[0usize..1usize][0usize..1usize]", "xs[xs[0usize]..xs[0usize]]", "ss[0usize..1usize].missing", "matrix.data[unknown][true]", "matrix.data[true][unknown]", "(*ra)[0usize]", "(*ra)[0usize..1usize]", "xs[if true { 0usize } else { 0usize }]", "xs[xs[0usize]]", "xs[stop(b\"x\")]", "xs[void_call(b\"x\")]",
|
||||
"xs[ref n]", "xs[move o]", "matrix.data[0usize].missing", "indexed.missing[unknown]", "1usize", "true", "[0usize]",
|
||||
};
|
||||
for (expressions) |text| {
|
||||
const parsed = try parse_mod.parse(a, try std.fmt.allocPrint(a, "{s}fn f() -> void effects [] {{ return {s}; }}", .{ indexTestDeclarations, text }));
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const e = parsed.ok.functions[0].body.stmts[0].kind.@"return".?;
|
||||
for ([_]bool{ false, true }) |mutable| for ([_]bool{ false, true }) |unchecked| try expectRawPlaceMatches(a, parsed.ok, e, mutable, unchecked);
|
||||
}
|
||||
}
|
||||
|
||||
fn rawPlaceArrayChain(a: Allocator, count: usize) !ast.Expr {
|
||||
var e: ast.Expr = .{ .span = unaryTestSpan(90_000), .kind = .{ .name = "deep" } };
|
||||
for (0..count) |i| {
|
||||
const items = try a.dupe(ast.Expr, &.{ e, .{ .span = unaryTestSpan(@intCast(100_000 + i)), .kind = .{ .int = .{ .text = "0usize" } } } });
|
||||
e = .{ .span = unaryTestSpan(@intCast(i + 1)), .kind = .{ .index = .{ .e = &items[0], .lo = &items[1], .hi = null } } };
|
||||
}
|
||||
return e;
|
||||
}
|
||||
|
||||
fn bindRawPlaceArray(l: *Lowerer, count: usize) !void {
|
||||
var t: Ty = .{ .int = "usize" };
|
||||
for (0..count) |_| t = try l.arrayOf(t, 1);
|
||||
try l.bindings.append(l.arena, .{ .name = "deep", .value = "deep_storage", .mutable = true, .ty = t, .kind = .storage });
|
||||
}
|
||||
|
||||
test "deep raw index bases preserve complete ancestor chain spans and reverse ends" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const m = (try parse_mod.parse(a, indexTestDeclarations)).ok;
|
||||
const count = 32_768;
|
||||
const e = try rawPlaceArrayChain(a, count);
|
||||
for ([_]bool{ false, true }) |mutable| {
|
||||
var l = try indexTestLowerer(a, m, e.span);
|
||||
try bindRawPlaceArray(&l, count);
|
||||
l.unchecked = true;
|
||||
const span = unaryTestSpan(200_000);
|
||||
const p = try l.placeRaw(e, mutable, span);
|
||||
try std.testing.expectEqual(count + 1, p.ends.len);
|
||||
try std.testing.expectEqual(mutable, p.v.ty.isMutBorrow());
|
||||
try std.testing.expect(p.v.ty.inner().?.eql(.{ .int = "usize" }));
|
||||
try std.testing.expectEqualStrings(p.v.name, p.ends[count]);
|
||||
const insts = l.blocks.items[0].insts.items;
|
||||
try std.testing.expectEqual(2 * count + 1, insts.len);
|
||||
try std.testing.expect(std.mem.indexOf(u8, insts[0].text, if (mutable) "borrow.mut %deep_storage" else "borrow.ref %deep_storage") != null);
|
||||
for (0..count) |i| {
|
||||
try std.testing.expectEqualDeep(@as(?Span, unaryTestSpan(@intCast(100_000 + i))), insts[1 + 2 * i].span);
|
||||
try std.testing.expectEqualDeep(@as(?Span, span), insts[2 + 2 * i].span);
|
||||
try std.testing.expect(std.mem.indexOf(u8, insts[2 + 2 * i].text, if (i + 1 == count) "borrow.index.unchecked %" else "borrow.index %") != null);
|
||||
try std.testing.expectEqualStrings(p.ends[i], l.borrow_parents.get(p.ends[i + 1]).?);
|
||||
}
|
||||
try l.endNow(p.ends, span);
|
||||
const after = l.blocks.items[0].insts.items;
|
||||
for (0..count + 1) |i| try std.testing.expectEqualStrings(try l.fmt("borrow.end %{s}", .{p.ends[count - i]}), after[2 * count + 1 + i].text);
|
||||
}
|
||||
}
|
||||
|
||||
test "grown raw place worklists propagate every host allocation failure and release backing" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const m = (try parse_mod.parse(a, indexTestDeclarations)).ok;
|
||||
const count = 64;
|
||||
const e = try rawPlaceArrayChain(a, count);
|
||||
for ([_]bool{ false, true }) |persistent| {
|
||||
var fail_at: usize = 0;
|
||||
var late_failures: usize = 0;
|
||||
while (true) : (fail_at += 1) {
|
||||
try std.testing.expect(fail_at < 4096);
|
||||
var backing: std.testing.FailingAllocator = .init(std.testing.allocator, .{});
|
||||
var output: std.heap.ArenaAllocator = .init(backing.allocator());
|
||||
var l = try indexTestLowerer(output.allocator(), m, e.span);
|
||||
try bindRawPlaceArray(&l, count);
|
||||
var failing: QueryAllocationFailure = .{ .backing = output.allocator(), .fail_at = fail_at };
|
||||
var sticky: std.testing.FailingAllocator = .init(output.allocator(), .{ .fail_index = fail_at });
|
||||
l.arena = if (persistent) sticky.allocator() else failing.allocator();
|
||||
const result = l.placeRaw(e, true, e.span);
|
||||
const failed = if (persistent) sticky.has_induced_failure else failing.failed;
|
||||
if (failed) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
if (!persistent) try std.testing.expectEqual(fail_at + 1, failing.attempts);
|
||||
try std.testing.expect(l.problem == null and l.unimpl == null);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
if (l.blocks.items[0].insts.items.len > count) late_failures += 1;
|
||||
} else try std.testing.expectEqual(count + 1, (try result).ends.len);
|
||||
output.deinit();
|
||||
try std.testing.expectEqual(backing.allocated_bytes, backing.freed_bytes);
|
||||
if (!failed) break;
|
||||
}
|
||||
try std.testing.expect(late_failures > 0);
|
||||
}
|
||||
}
|
||||
|
||||
test "deep nominal raw fields keep parents and innermost error positions" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const count = 32_768;
|
||||
var m = (try parse_mod.parse(a, indexTestDeclarations)).ok;
|
||||
const declarations = try a.alloc(ast.TypeDecl, m.types.len + count);
|
||||
@memcpy(declarations[0..m.types.len], m.types);
|
||||
for (declarations[m.types.len..], 0..) |*d, i| {
|
||||
const span = unaryTestSpan(@intCast(100_000 + i));
|
||||
const fields = try a.dupe(ast.Field, &.{.{ .name = "data", .span = span, .ty = .{ .span = span, .kind = .{ .name = if (i + 1 == count) "usize" else try std.fmt.allocPrint(a, "Deep{d}", .{i + 1}) } } }});
|
||||
d.* = .{ .name = try std.fmt.allocPrint(a, "Deep{d}", .{i}), .span = span, .kind = .{ .@"struct" = fields } };
|
||||
}
|
||||
m.types = declarations;
|
||||
var e: ast.Expr = .{ .span = unaryTestSpan(90_000), .kind = .{ .name = "deep" } };
|
||||
for (0..count) |i| {
|
||||
const child = try a.dupe(ast.Expr, &.{e});
|
||||
e = .{ .span = unaryTestSpan(@intCast(i + 1)), .kind = .{ .field = .{ .e = &child[0], .name = "data" } } };
|
||||
}
|
||||
var l = try indexTestLowerer(a, m, e.span);
|
||||
try l.bindings.append(a, .{ .name = "deep", .value = "nominal_storage", .mutable = true, .ty = l.enumOrStruct("Deep0", e.span).?, .kind = .storage });
|
||||
const p = try l.placeRaw(e, true, e.span);
|
||||
try std.testing.expectEqual(count + 1, p.ends.len);
|
||||
try std.testing.expect(p.v.ty.inner().?.eql(.{ .int = "usize" }));
|
||||
for (p.ends[1..], 0..) |name, i| try std.testing.expectEqualStrings(p.ends[i], l.borrow_parents.get(name).?);
|
||||
for (l.blocks.items[0].insts.items) |inst| try std.testing.expectEqualDeep(@as(?Span, e.span), inst.span);
|
||||
// The same long projection spine fails at its first field when the base
|
||||
// has been replaced with a scalar stored value; outer spans do not win.
|
||||
var bad = try indexTestLowerer(a, m, e.span);
|
||||
try bad.bindings.append(a, .{ .name = "deep", .value = "scalar_storage", .mutable = true, .ty = .{ .int = "usize" }, .kind = .storage });
|
||||
try std.testing.expectError(error.Lower, bad.placeRaw(e, true, e.span));
|
||||
try std.testing.expectEqualStrings("E7005", bad.problem.?.code);
|
||||
try std.testing.expectEqualDeep(unaryTestSpan(1), bad.problem.?.span);
|
||||
try std.testing.expectEqual(@as(usize, 1), bad.blocks.items[0].insts.items.len);
|
||||
}
|
||||
|
||||
test "deep raw ranges and dereference leaves preserve every temporary ancestor" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const m = (try parse_mod.parse(a, indexTestDeclarations)).ok;
|
||||
const count = 32_768;
|
||||
const leaf = try a.dupe(ast.Expr, &.{.{ .span = unaryTestSpan(90_000), .kind = .{ .name = "ra" } }});
|
||||
var e: ast.Expr = .{ .span = unaryTestSpan(90_001), .kind = .{ .deref = &leaf[0] } };
|
||||
for (0..count) |i| {
|
||||
const items = try a.dupe(ast.Expr, &.{ e, .{ .span = unaryTestSpan(@intCast(100_000 + i)), .kind = .{ .int = .{ .text = "0usize" } } }, .{ .span = unaryTestSpan(@intCast(200_000 + i)), .kind = .{ .int = .{ .text = "1usize" } } } });
|
||||
e = .{ .span = unaryTestSpan(@intCast(i + 1)), .kind = .{ .index = .{ .e = &items[0], .lo = &items[1], .hi = &items[2] } } };
|
||||
}
|
||||
var l = try indexTestLowerer(a, m, e.span);
|
||||
const p = try l.placeRaw(e, false, e.span);
|
||||
try std.testing.expectEqual(count + 1, p.ends.len); // one make, count sub
|
||||
try std.testing.expect(p.v.ty == .slice);
|
||||
try std.testing.expectEqual(4 * count + 1, l.blocks.items[0].insts.items.len);
|
||||
try std.testing.expectEqualStrings("array_loan", l.borrow_parents.get(p.ends[0]).?);
|
||||
for (p.ends[1..], 0..) |name, i| try std.testing.expectEqualStrings(p.ends[i], l.borrow_parents.get(name).?);
|
||||
const insts = l.blocks.items[0].insts.items;
|
||||
for (0..count) |i| {
|
||||
try std.testing.expectEqualDeep(@as(?Span, unaryTestSpan(@intCast(100_000 + i))), insts[1 + 4 * i].span);
|
||||
try std.testing.expectEqualDeep(@as(?Span, unaryTestSpan(@intCast(200_000 + i))), insts[2 + 4 * i].span);
|
||||
try std.testing.expect(std.mem.indexOf(u8, insts[4 + 4 * i].text, "slice.sub %") != null);
|
||||
}
|
||||
}
|
||||
test "public raw place field range lowering OOM stays host error with arena cleanup" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const programs = [_][]const u8{
|
||||
"module oom\nstruct S { data: [[usize; 1]; 1] }\nfn main() -> usize effects [] { var s: S = S { data: [[0usize]] }; return s.data[0usize][0usize]; }\n",
|
||||
"module oom\nfn main() -> usize effects [] { var a: [usize; 1] = zero; ref s = a[0usize..1usize][0usize..1usize]; return s[0usize]; }\n",
|
||||
"module oom\nfn main() -> usize effects [] { var a: [usize; 1] = zero; ref s = a[unknown..1usize]; return 0usize; }\n",
|
||||
"module oom\nfn main() -> usize effects [] { var a: [usize; 1] = zero; ref s = a[0usize..true]; return 0usize; }\n",
|
||||
"module oom\nstruct S { data: usize }\nfn main() -> usize effects [] { var s: S = S { data: 0usize }; return s.missing[unknown]; }\n",
|
||||
"module oom\nfn main() -> usize effects [] { var a: [usize; 1] = zero; return a[0usize..1usize]; }\n",
|
||||
};
|
||||
for (programs, 0..) |program, index| {
|
||||
const parsed = try parse_mod.parse(fixture.allocator(), program);
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const baseline = try lower(fixture.allocator(), parsed.ok);
|
||||
if (index < 2) try std.testing.expect(baseline == .ok) else {
|
||||
try std.testing.expect(baseline == .err);
|
||||
try std.testing.expectEqualStrings(switch (index) {
|
||||
2, 4 => "E7002",
|
||||
5 => "E7006",
|
||||
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) == std.meta.activeTag(baseline));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user