cli: search expected source types iteratively
This commit is contained in:
1 parent
e862e03f2b
commit
e3f4695efe
5 files changed
+504
-57
No files matched your search
+425
-44
@@ -1771,58 +1771,171 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn usesInStmt(l: *Lowerer, n: []const u8, s: ast.Stmt) ?Ty {
|
||||
return switch (s.kind) {
|
||||
.let => |x| l.useIn(n, x.e, if (x.ty) |t| l.typeOfNoFail(t) else null),
|
||||
.@"var" => |x| if (x.init) |e| l.useIn(n, e, l.typeOfNoFail(x.ty)) else null,
|
||||
.assign => |x| l.useIn(n, x.e, if (x.place.kind == .name) (if (l.lookup(x.place.kind.name)) |b| b.ty else null) else null),
|
||||
.@"return" => |e| if (e) |x| l.useIn(n, x, l.ret) else null,
|
||||
.expr => |e| l.useIn(n, e, null),
|
||||
.@"while" => |x| l.useIn(n, x.cond, .bool) orelse l.usesInBlock(n, x.body),
|
||||
.@"for" => |x| l.useIn(n, x.lo, l.peekType(x.hi)) orelse l.useIn(n, x.hi, l.peekType(x.lo)) orelse l.usesInBlock(n, x.body),
|
||||
.loop, .shield => |b| l.usesInBlock(n, b),
|
||||
.scope => |x| l.usesInBlock(n, x.body),
|
||||
else => null,
|
||||
};
|
||||
return l.queryUse(n, .{ .statement = s }) catch null;
|
||||
}
|
||||
|
||||
fn usesInBlock(l: *Lowerer, n: []const u8, b: ast.Block) ?Ty {
|
||||
for (b.stmts) |s| if (l.usesInStmt(n, s)) |t| return t;
|
||||
if (b.tail) |t| return l.useIn(n, t.*, null);
|
||||
return null;
|
||||
return l.queryUse(n, .{ .block = b }) catch null;
|
||||
}
|
||||
|
||||
/// The expected type of `n` inside `e`, evaluated where `e` is expected
|
||||
/// to have type `want`.
|
||||
/// to have type `want`. Like peekType, this is a best-effort query.
|
||||
fn useIn(l: *Lowerer, n: []const u8, e: ast.Expr, want: ?Ty) ?Ty {
|
||||
return switch (e.kind) {
|
||||
.name => |x| if (std.mem.eql(u8, x, n)) want else null,
|
||||
.neg => |x| l.useIn(n, x.*, want),
|
||||
.not => |x| l.useIn(n, x.*, .bool),
|
||||
.cast => |x| l.useIn(n, x.e.*, null),
|
||||
.binary => |x| switch (x.op) {
|
||||
.@"and", .@"or" => l.useIn(n, x.l.*, .bool) orelse l.useIn(n, x.r.*, .bool),
|
||||
.eq, .ne, .lt, .le, .gt, .ge => l.useIn(n, x.l.*, l.peekType(x.r.*)) orelse l.useIn(n, x.r.*, l.peekType(x.l.*)),
|
||||
.shl, .shr => l.useIn(n, x.l.*, want) orelse l.useIn(n, x.r.*, l.peekType(x.l.*) orelse want),
|
||||
else => blk: {
|
||||
const t = want orelse l.peekType(x.l.*) orelse l.peekType(x.r.*);
|
||||
break :blk l.useIn(n, x.l.*, t) orelse l.useIn(n, x.r.*, t);
|
||||
return l.queryUse(n, .{ .expression = .{ .e = e, .want = .{ .known = want } } }) catch null;
|
||||
}
|
||||
|
||||
// Right-hand hints are resolved only after an earlier use was unknown.
|
||||
// In particular, do not eagerly query a comparison/shift/for left operand.
|
||||
const UseExpected = union(enum) {
|
||||
known: ?Ty,
|
||||
peek: ast.Expr,
|
||||
peek_or: struct { e: ast.Expr, fallback: ?Ty },
|
||||
};
|
||||
const UseRequest = union(enum) {
|
||||
expression: struct { e: ast.Expr, want: UseExpected },
|
||||
statement: ast.Stmt,
|
||||
block: ast.Block,
|
||||
statements: []const ast.Stmt,
|
||||
arguments: struct { args: []const ast.Expr, params: []const Ty, at: usize },
|
||||
};
|
||||
|
||||
fn queryUse(l: *Lowerer, n: []const u8, initial: UseRequest) Allocator.Error!?Ty {
|
||||
var pending: std.ArrayList(UseRequest) = .empty;
|
||||
defer pending.deinit(l.arena);
|
||||
var request = initial;
|
||||
search: while (true) {
|
||||
switch (request) {
|
||||
.expression => |x| {
|
||||
const e = x.e;
|
||||
const want: ?Ty = switch (x.want) {
|
||||
.known => |t| t,
|
||||
.peek => |other| l.peekType(other),
|
||||
.peek_or => |other| l.peekType(other.e) orelse other.fallback,
|
||||
};
|
||||
switch (e.kind) {
|
||||
.name => |name| if (std.mem.eql(u8, name, n)) {
|
||||
if (want) |t| return t;
|
||||
},
|
||||
.neg, .not, .cast => {
|
||||
const operand = switch (e.kind) {
|
||||
.neg => |v| v.*,
|
||||
.not => |v| v.*,
|
||||
.cast => |v| v.e.*,
|
||||
else => unreachable,
|
||||
};
|
||||
const hint: ?Ty = switch (e.kind) {
|
||||
.neg => want,
|
||||
.not => .bool,
|
||||
.cast => null,
|
||||
else => unreachable,
|
||||
};
|
||||
request = .{ .expression = .{ .e = operand, .want = .{ .known = hint } } };
|
||||
continue :search;
|
||||
},
|
||||
.binary => |b| {
|
||||
const left: ?Ty = switch (b.op) {
|
||||
.@"and", .@"or" => .bool,
|
||||
.eq, .ne, .lt, .le, .gt, .ge => l.peekType(b.r.*),
|
||||
.shl, .shr => want,
|
||||
else => want orelse l.peekType(b.l.*) orelse l.peekType(b.r.*),
|
||||
};
|
||||
const right: UseExpected = switch (b.op) {
|
||||
.eq, .ne, .lt, .le, .gt, .ge => .{ .peek = b.l.* },
|
||||
.shl, .shr => .{ .peek_or = .{ .e = b.l.*, .fallback = want } },
|
||||
else => .{ .known = left },
|
||||
};
|
||||
try pending.append(l.arena, .{ .expression = .{ .e = b.r.*, .want = right } });
|
||||
request = .{ .expression = .{ .e = b.l.*, .want = .{ .known = left } } };
|
||||
continue :search;
|
||||
},
|
||||
.call => |c| {
|
||||
const sig = l.calleeSig(c.callee.*) orelse {
|
||||
request = pending.pop() orelse return null;
|
||||
continue :search;
|
||||
};
|
||||
request = .{ .arguments = .{ .args = c.args, .params = sig.params, .at = 0 } };
|
||||
continue :search;
|
||||
},
|
||||
.if_expr => |branch| {
|
||||
if (branch.@"else") |otherwise| switch (otherwise) {
|
||||
.block => |b| try pending.append(l.arena, .{ .block = b }),
|
||||
.@"if" => |inner| try pending.append(l.arena, .{ .expression = .{
|
||||
.e = .{ .span = e.span, .kind = .{ .if_expr = inner } },
|
||||
.want = .{ .known = want },
|
||||
} }),
|
||||
};
|
||||
try pending.append(l.arena, .{ .block = branch.then });
|
||||
request = .{ .expression = .{ .e = branch.cond, .want = .{ .known = .bool } } };
|
||||
continue :search;
|
||||
},
|
||||
.block => |b| {
|
||||
request = .{ .block = b.* };
|
||||
continue :search;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
},
|
||||
},
|
||||
.call => |x| blk: {
|
||||
const sig = l.calleeSig(x.callee.*) orelse break :blk null;
|
||||
for (x.args, 0..) |a, i| {
|
||||
const pt: ?Ty = if (i < sig.params.len) sig.params[i] else null;
|
||||
if (l.useIn(n, a, pt)) |t| break :blk t;
|
||||
}
|
||||
break :blk null;
|
||||
},
|
||||
.if_expr => |x| l.useIn(n, x.cond, .bool) orelse l.usesInBlock(n, x.then) orelse if (x.@"else") |els| switch (els) {
|
||||
.block => |b| l.usesInBlock(n, b),
|
||||
.@"if" => |inner| l.useIn(n, .{ .span = e.span, .kind = .{ .if_expr = inner } }, want),
|
||||
} else null,
|
||||
.block => |b| l.usesInBlock(n, b.*),
|
||||
else => null,
|
||||
};
|
||||
.statement => |s| switch (s.kind) {
|
||||
.let => |x| {
|
||||
request = .{ .expression = .{ .e = x.e, .want = .{ .known = if (x.ty) |t| l.typeOfNoFail(t) else null } } };
|
||||
continue :search;
|
||||
},
|
||||
.@"var" => |x| if (x.init) |e| {
|
||||
request = .{ .expression = .{ .e = e, .want = .{ .known = l.typeOfNoFail(x.ty) } } };
|
||||
continue :search;
|
||||
},
|
||||
.assign => |x| {
|
||||
const hint: ?Ty = if (x.place.kind == .name) (if (l.lookup(x.place.kind.name)) |b| b.ty else null) else null;
|
||||
request = .{ .expression = .{ .e = x.e, .want = .{ .known = hint } } };
|
||||
continue :search;
|
||||
},
|
||||
.@"return" => |value| if (value) |e| {
|
||||
request = .{ .expression = .{ .e = e, .want = .{ .known = l.ret } } };
|
||||
continue :search;
|
||||
},
|
||||
.expr => |e| {
|
||||
request = .{ .expression = .{ .e = e, .want = .{ .known = null } } };
|
||||
continue :search;
|
||||
},
|
||||
.@"while" => |x| {
|
||||
try pending.append(l.arena, .{ .block = x.body });
|
||||
request = .{ .expression = .{ .e = x.cond, .want = .{ .known = .bool } } };
|
||||
continue :search;
|
||||
},
|
||||
.@"for" => |x| {
|
||||
try pending.append(l.arena, .{ .block = x.body });
|
||||
try pending.append(l.arena, .{ .expression = .{ .e = x.hi, .want = .{ .peek = x.lo } } });
|
||||
request = .{ .expression = .{ .e = x.lo, .want = .{ .peek = x.hi } } };
|
||||
continue :search;
|
||||
},
|
||||
.loop, .shield => |b| {
|
||||
request = .{ .block = b };
|
||||
continue :search;
|
||||
},
|
||||
.scope => |x| {
|
||||
request = .{ .block = x.body };
|
||||
continue :search;
|
||||
},
|
||||
else => {},
|
||||
},
|
||||
.block => |b| {
|
||||
if (b.tail) |e| try pending.append(l.arena, .{ .expression = .{ .e = e.*, .want = .{ .known = null } } });
|
||||
request = .{ .statements = b.stmts };
|
||||
continue :search;
|
||||
},
|
||||
.statements => |stmts| if (stmts.len != 0) {
|
||||
if (stmts.len > 1) try pending.append(l.arena, .{ .statements = stmts[1..] });
|
||||
request = .{ .statement = stmts[0] };
|
||||
continue :search;
|
||||
},
|
||||
.arguments => |args| if (args.at < args.args.len) {
|
||||
if (args.at + 1 < args.args.len) try pending.append(l.arena, .{ .arguments = .{ .args = args.args, .params = args.params, .at = args.at + 1 } });
|
||||
request = .{ .expression = .{ .e = args.args[args.at], .want = .{ .known = if (args.at < args.params.len) args.params[args.at] else null } } };
|
||||
continue :search;
|
||||
},
|
||||
}
|
||||
request = pending.pop() orelse return null;
|
||||
}
|
||||
}
|
||||
|
||||
fn typeOfNoFail(l: *Lowerer, annotation: ast.Type) ?Ty {
|
||||
@@ -4407,6 +4520,274 @@ test "terminated blocks reject further emission with a source diagnostic" {
|
||||
try std.testing.expect(l.blocks.items[0].terminated);
|
||||
}
|
||||
|
||||
// Independent old expected-use descent, including the outer rest/shadow
|
||||
// cursor. Type/name/signature queries are shared, not independently derived.
|
||||
const ReferenceUseQuery = struct {
|
||||
fn infer(l: *Lowerer, n: []const u8) ?Ty {
|
||||
var i = l.rest.items.len;
|
||||
while (i > 0) {
|
||||
i -= 1;
|
||||
const r = l.rest.items[i];
|
||||
for (r.stmts) |s| {
|
||||
if (statement(l, n, s)) |t| return t;
|
||||
if (s.kind == .let) for (s.kind.let.names) |bn| if (std.mem.eql(u8, bn, n)) return null;
|
||||
}
|
||||
if (r.tail) |t| if (expression(l, n, t.*, if (i == 0) l.ret else null)) |ty| return ty;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
fn statement(l: *Lowerer, n: []const u8, s: ast.Stmt) ?Ty {
|
||||
return switch (s.kind) {
|
||||
.let => |x| expression(l, n, x.e, if (x.ty) |t| l.typeOfNoFail(t) else null),
|
||||
.@"var" => |x| if (x.init) |e| expression(l, n, e, l.typeOfNoFail(x.ty)) else null,
|
||||
.assign => |x| expression(l, n, x.e, if (x.place.kind == .name) (if (l.lookup(x.place.kind.name)) |b| b.ty else null) else null),
|
||||
.@"return" => |e| if (e) |x| expression(l, n, x, l.ret) else null,
|
||||
.expr => |e| expression(l, n, e, null),
|
||||
.@"while" => |x| expression(l, n, x.cond, .bool) orelse block(l, n, x.body),
|
||||
.@"for" => |x| expression(l, n, x.lo, l.peekType(x.hi)) orelse expression(l, n, x.hi, l.peekType(x.lo)) orelse block(l, n, x.body),
|
||||
.loop, .shield => |b| block(l, n, b),
|
||||
.scope => |x| block(l, n, x.body),
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
fn block(l: *Lowerer, n: []const u8, b: ast.Block) ?Ty {
|
||||
for (b.stmts) |s| if (statement(l, n, s)) |t| return t;
|
||||
if (b.tail) |t| return expression(l, n, t.*, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// The expected type of `n` inside `e`, evaluated where `e` is expected
|
||||
/// to have type `want`.
|
||||
fn expression(l: *Lowerer, n: []const u8, e: ast.Expr, want: ?Ty) ?Ty {
|
||||
return switch (e.kind) {
|
||||
.name => |x| if (std.mem.eql(u8, x, n)) want else null,
|
||||
.neg => |x| expression(l, n, x.*, want),
|
||||
.not => |x| expression(l, n, x.*, .bool),
|
||||
.cast => |x| expression(l, n, x.e.*, null),
|
||||
.binary => |x| switch (x.op) {
|
||||
.@"and", .@"or" => expression(l, n, x.l.*, .bool) orelse expression(l, n, x.r.*, .bool),
|
||||
.eq, .ne, .lt, .le, .gt, .ge => expression(l, n, x.l.*, l.peekType(x.r.*)) orelse expression(l, n, x.r.*, l.peekType(x.l.*)),
|
||||
.shl, .shr => expression(l, n, x.l.*, want) orelse expression(l, n, x.r.*, l.peekType(x.l.*) orelse want),
|
||||
else => blk: {
|
||||
const t = want orelse l.peekType(x.l.*) orelse l.peekType(x.r.*);
|
||||
break :blk expression(l, n, x.l.*, t) orelse expression(l, n, x.r.*, t);
|
||||
},
|
||||
},
|
||||
.call => |x| blk: {
|
||||
const sig = l.calleeSig(x.callee.*) orelse break :blk null;
|
||||
for (x.args, 0..) |a, i| {
|
||||
const pt: ?Ty = if (i < sig.params.len) sig.params[i] else null;
|
||||
if (expression(l, n, a, pt)) |t| break :blk t;
|
||||
}
|
||||
break :blk null;
|
||||
},
|
||||
.if_expr => |x| expression(l, n, x.cond, .bool) orelse block(l, n, x.then) orelse if (x.@"else") |els| switch (els) {
|
||||
.block => |b| block(l, n, b),
|
||||
.@"if" => |inner| expression(l, n, .{ .span = e.span, .kind = .{ .if_expr = inner } }, want),
|
||||
} else null,
|
||||
.block => |b| block(l, n, b.*),
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
fn expectUseQueryEqual(expected: ?Ty, actual: ?Ty) !void {
|
||||
if (expected) |t| try std.testing.expect(actual != null and t.eql(actual.?)) else try std.testing.expect(actual == null);
|
||||
}
|
||||
|
||||
test "expected-use queries match independent shallow expression and statement descent" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const m: ast.Module = .{ .name = "uses", .types = &.{}, .functions = &.{} };
|
||||
var l: Lowerer = .{ .arena = a, .m = m, .ret = .{ .int = "i64" } };
|
||||
try l.bindings.appendSlice(a, &.{
|
||||
.{ .name = "known", .value = "known", .mutable = false, .ty = .{ .int = "u32" } },
|
||||
.{ .name = "flag", .value = "flag", .mutable = false, .ty = .bool },
|
||||
.{ .name = "x", .value = "x", .mutable = true, .ty = .{ .int = "u16" } },
|
||||
});
|
||||
for ([_][]const u8{ "f", "ns.f" }) |name| try l.sigs.put(a, name, .{ .params = &.{ .{ .int = "u8" }, .bool, .{ .int = "u64" } }, .ret = .bool, .air_name = name, .display_name = name });
|
||||
const leaves = [_][]const u8{ "n", "unknown", "known", "flag", "1", "1u8", "move n", "ref n", "* n", "n.len", "[n; 2]", "match 0u8 { _ => n }" };
|
||||
const forms = [_][]const u8{
|
||||
"{s}", "- {s}", "not {s}", "({s}) as! u8", "({s}) + known", "known + ({s})", "({s}) + unknown", "unknown + ({s})",
|
||||
"({s}) == known", "known == ({s})", "({s}) << known", "known >> ({s})", "({s}) and flag", "flag or ({s})", "f({s}, n, n)", "f(unknown, {s}, n)",
|
||||
"f(unknown, unknown, {s}, n)", "ns.f(unknown, {s})", "unknown({s})", "if {s} {{ return n; }} else {{ let y: u16 = n; }}", "if false {{ {s} }} else if n {{ return n; }}", "{{ let y: u16 = {s}; n }}",
|
||||
};
|
||||
inline for (forms) |form| for (leaves) |leaf| {
|
||||
const text = try std.fmt.allocPrint(a, form, .{leaf});
|
||||
const source = try std.fmt.allocPrint(a, "module uses\nfn probe() -> i64 effects [] {{ return {s}; }}", .{text});
|
||||
const parsed = try parse_mod.parse(a, source);
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const e = parsed.ok.functions[0].body.stmts[0].kind.@"return".?;
|
||||
for ([_]?Ty{ null, .bool, .{ .int = "u64" } }) |want| try expectUseQueryEqual(ReferenceUseQuery.expression(&l, "n", e, want), l.useIn("n", e, want));
|
||||
};
|
||||
const statements = [_][]const u8{
|
||||
"let y: u8 = n;", "let y = n;", "let n: u8 = n;", "let (n, y) = f(n);", "var y: u32 = n;", "var y: u32 = zero;",
|
||||
"x = n;", "unknown = n;", "x.len = n;", "return n;", "return;", "n;",
|
||||
"f(unknown, n);", "while n { return n; }", "while false { let y: u16 = n; }", "for i in n..5u8 { return n; }", "for i in 0u16..n { return n; }", "for i in 0u16..5u8 { return n; }",
|
||||
"loop { return n; }", "shield { let y: u8 = n; }", "scope s { let y: u16 = n; }", "ref r = n;", "mut r = n;", "break;",
|
||||
"continue;", "trap;", "lock ref n as guard { return n; } cancelled { return n; }",
|
||||
};
|
||||
for (statements) |text| {
|
||||
const source = try std.fmt.allocPrint(a, "module uses\nfn probe() -> i64 effects [] {{ {s} }}", .{text});
|
||||
const parsed = try parse_mod.parse(a, source);
|
||||
if (parsed != .ok) std.debug.print("failed expected-use fixture: {s}\n", .{text});
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const body = parsed.ok.functions[0].body;
|
||||
try expectUseQueryEqual(ReferenceUseQuery.block(&l, "n", body), l.usesInBlock("n", body));
|
||||
for (body.stmts) |stmt| try expectUseQueryEqual(ReferenceUseQuery.statement(&l, "n", stmt), l.usesInStmt("n", stmt));
|
||||
}
|
||||
try std.testing.expect(l.problem == null);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.blocks.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
}
|
||||
|
||||
test "expected-use rest queries preserve scope order tails and top-level let rebinding" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const fixtures = [_][]const u8{
|
||||
"return n;", "let y: u8 = n; return n;", "let n = 0u8; return n;", "let (x, n) = unknown; return n;",
|
||||
"let n: u8 = n; return n;", "var n: u8 = 0u8; return n;", "{ let n = 0u8; return n; };", "n",
|
||||
"let y: u8 = unknown; n", "{ n };", "if false { return n; } else { let y: u8 = n; };",
|
||||
};
|
||||
const m: ast.Module = .{ .name = "uses", .types = &.{}, .functions = &.{} };
|
||||
for (fixtures) |inner| for (fixtures) |outer| {
|
||||
var l: Lowerer = .{ .arena = a, .m = m, .ret = .{ .int = "i64" } };
|
||||
for ([_][]const u8{ outer, inner }) |text| {
|
||||
const source = try std.fmt.allocPrint(a, "module uses\nfn probe() -> i64 effects [] {{ {s} }}", .{text});
|
||||
const parsed = try parse_mod.parse(a, source);
|
||||
try std.testing.expect(parsed == .ok);
|
||||
const b = parsed.ok.functions[0].body;
|
||||
try l.rest.append(a, .{ .stmts = b.stmts, .tail = b.tail });
|
||||
}
|
||||
try expectUseQueryEqual(ReferenceUseQuery.infer(&l, "n"), l.inferUses("n"));
|
||||
};
|
||||
}
|
||||
|
||||
test "expected-use queries do not evaluate deferred right-hand type hints after success" {
|
||||
const span = unaryTestSpan(1);
|
||||
const n: ast.Expr = .{ .span = span, .kind = .{ .name = "n" } };
|
||||
const value: ast.Expr = .{ .span = span, .kind = .{ .boolean = true } };
|
||||
const array: ast.Expr = .{ .span = span, .kind = .{ .array_rep = .{ .e = &value, .len = 2 } } };
|
||||
const left: ast.Expr = .{ .span = span, .kind = .{ .binary = .{ .op = .add, .l = &n, .r = &array } } };
|
||||
const right: ast.Expr = .{ .span = span, .kind = .{ .int = .{ .text = "1u16" } } };
|
||||
for ([_]ast.BinOp{ .eq, .shl }) |op| {
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 1 });
|
||||
var l: Lowerer = .{ .arena = failing.allocator(), .m = .{ .name = "uses", .types = &.{}, .functions = &.{} } };
|
||||
const e: ast.Expr = .{ .span = span, .kind = .{ .binary = .{ .op = op, .l = &left, .r = &right } } };
|
||||
try expectUseQueryEqual(.{ .int = "u16" }, l.useIn("n", e, .{ .int = "u16" }));
|
||||
// One worklist allocation is allowed. An eager peekType(left) would
|
||||
// construct the array type on a second request and induce host OOM.
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
}
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 1 });
|
||||
var l: Lowerer = .{ .arena = failing.allocator(), .m = .{ .name = "uses", .types = &.{}, .functions = &.{} } };
|
||||
const loop: ast.Stmt = .{ .span = span, .kind = .{ .@"for" = .{
|
||||
.name = "i",
|
||||
.lo = left,
|
||||
.hi = right,
|
||||
.body = .{ .span = span, .stmts = &.{}, .tail = null },
|
||||
} } };
|
||||
try expectUseQueryEqual(.{ .int = "u16" }, l.usesInStmt("n", loop));
|
||||
try std.testing.expect(!failing.has_induced_failure);
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
}
|
||||
|
||||
test "expected-use queries handle deep unary branches calls and statement blocks" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const span = unaryTestSpan(1);
|
||||
const m: ast.Module = .{ .name = "uses", .types = &.{}, .functions = &.{} };
|
||||
const name: ast.Expr = .{ .span = span, .kind = .{ .name = "n" } };
|
||||
const unknown: ast.Expr = .{ .span = span, .kind = .{ .name = "unknown" } };
|
||||
const callee: ast.Expr = .{ .span = span, .kind = .{ .name = "f" } };
|
||||
const empty: ast.Block = .{ .span = span, .stmts = &.{}, .tail = null };
|
||||
var l: Lowerer = .{ .arena = a, .m = m, .ret = .{ .int = "u64" } };
|
||||
try l.sigs.put(a, "f", .{ .params = &.{.bool}, .ret = .bool, .air_name = "f", .display_name = "f" });
|
||||
var unary = name;
|
||||
var mixed: ast.Expr = .{ .span = span, .kind = .{ .not = &name } };
|
||||
for (0..32_768) |i| {
|
||||
const operand = try a.create(ast.Expr);
|
||||
operand.* = unary;
|
||||
unary = .{ .span = span, .kind = .{ .neg = operand } };
|
||||
const inner = try a.create(ast.Expr);
|
||||
inner.* = mixed;
|
||||
switch (i % 3) {
|
||||
0 => mixed = .{ .span = span, .kind = .{ .binary = .{ .op = .@"or", .l = &unknown, .r = inner } } },
|
||||
1 => {
|
||||
const args = try a.alloc(ast.Expr, 1);
|
||||
args[0] = inner.*;
|
||||
mixed = .{ .span = span, .kind = .{ .call = .{ .callee = &callee, .type_arg = null, .args = args } } };
|
||||
},
|
||||
else => {
|
||||
const branch = try a.create(ast.If);
|
||||
const next = try a.create(ast.If);
|
||||
next.* = .{ .cond = unknown, .then = .{ .span = span, .stmts = &.{}, .tail = inner }, .@"else" = null };
|
||||
branch.* = .{ .cond = unknown, .then = empty, .@"else" = .{ .@"if" = next } };
|
||||
mixed = .{ .span = span, .kind = .{ .if_expr = branch } };
|
||||
},
|
||||
}
|
||||
}
|
||||
try expectUseQueryEqual(.{ .int = "u16" }, l.useIn("n", unary, .{ .int = "u16" }));
|
||||
try expectUseQueryEqual(.bool, l.useIn("n", mixed, null));
|
||||
// Plain unary traversal does not need host metadata allocation.
|
||||
var none: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = 0 });
|
||||
var no_alloc: Lowerer = .{ .arena = none.allocator(), .m = m };
|
||||
try expectUseQueryEqual(.bool, no_alloc.useIn("n", unary, .bool));
|
||||
try std.testing.expect(!none.has_induced_failure);
|
||||
var body: ast.Block = .{ .span = span, .stmts = &.{.{ .span = span, .kind = .{ .@"return" = name } }}, .tail = null };
|
||||
for (0..32_768) |_| {
|
||||
const stmts = try a.alloc(ast.Stmt, 3);
|
||||
stmts[0] = .{ .span = span, .kind = .{ .expr = unknown } };
|
||||
stmts[1] = .{ .span = span, .kind = .{ .shield = body } };
|
||||
stmts[2] = .{ .span = span, .kind = .{ .@"return" = unknown } };
|
||||
// The first successful statement wins over every enclosing tail.
|
||||
const tail = try a.create(ast.Expr);
|
||||
tail.* = .{ .span = span, .kind = .{ .not = &name } };
|
||||
body = .{ .span = span, .stmts = stmts, .tail = tail };
|
||||
}
|
||||
try expectUseQueryEqual(.{ .int = "u64" }, l.usesInBlock("n", body));
|
||||
try std.testing.expect(l.problem == null);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.blocks.items.len);
|
||||
}
|
||||
|
||||
test "expected-use worklist OOM is unknown and releases all backing allocations" {
|
||||
var fixture: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer fixture.deinit();
|
||||
const a = fixture.allocator();
|
||||
const span = unaryTestSpan(1);
|
||||
const name: ast.Expr = .{ .span = span, .kind = .{ .name = "n" } };
|
||||
const unknown: ast.Expr = .{ .span = span, .kind = .{ .name = "unknown" } };
|
||||
var e = name;
|
||||
for (0..128) |_| {
|
||||
const inner = try a.create(ast.Expr);
|
||||
inner.* = e;
|
||||
// Descend left with a pending right request, forcing worklist growth.
|
||||
e = .{ .span = span, .kind = .{ .binary = .{ .op = .@"and", .l = inner, .r = &unknown } } };
|
||||
}
|
||||
var fail_index: usize = 0;
|
||||
while (true) : (fail_index += 1) {
|
||||
try std.testing.expect(fail_index < 128);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
|
||||
var arena: std.heap.ArenaAllocator = .init(failing.allocator());
|
||||
var l: Lowerer = .{ .arena = arena.allocator(), .m = .{ .name = "uses", .types = &.{}, .functions = &.{} } };
|
||||
const result = l.useIn("n", e, null);
|
||||
if (failing.has_induced_failure) try std.testing.expect(result == null) else try expectUseQueryEqual(.bool, result);
|
||||
try std.testing.expect(l.problem == null);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.blocks.items.len);
|
||||
arena.deinit();
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
if (!failing.has_induced_failure) break;
|
||||
}
|
||||
}
|
||||
|
||||
// Independent pre-repair expression query and built-in result derivation.
|
||||
// Name/signature/type lookup helpers are shared; the descent is not.
|
||||
const ReferenceExpressionQuery = struct {
|
||||
|
||||
Reference in new issue
Block a user