verifier: cache structural properties and nominal size queries
This commit is contained in:
1 parent
41f3afbf05
commit
48b9b42fed
31 files changed
+1399
-166
No files matched your search
+33
-2
@@ -73,16 +73,26 @@ pub const Context = struct {
|
||||
/// Runs all verifier stages. Returns the diagnostics of the first failing
|
||||
/// stage, or an empty slice if the module verifies.
|
||||
pub fn verify(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error![]const diag.Diagnostic {
|
||||
return (try verifyModule(arena, file, module)).diagnostics;
|
||||
}
|
||||
|
||||
pub const Result = struct { diagnostics: []const diag.Diagnostic, module: air.Module };
|
||||
|
||||
/// Retain implementation caches in the checked module without trusting caches
|
||||
/// supplied by a caller. Neither cache enters AIR text or changes a stage.
|
||||
pub fn verifyModule(arena: std.mem.Allocator, file: []const u8, module: air.Module) std.mem.Allocator.Error!Result {
|
||||
var c: Context = .{ .arena = arena, .file = file, .module = module };
|
||||
c.module.nominal_properties = null;
|
||||
c.module.accounted_sizes = null;
|
||||
const stages = .{ budget.check, cfg.check, ssa.check, types.check, effects.check, unsafe_.check, init.check, borrow.check, bounds.check };
|
||||
inline for (stages) |stage| {
|
||||
try stage(&c);
|
||||
if (c.diags.items.len != 0) {
|
||||
std.mem.sort(diag.Diagnostic, c.diags.items, {}, bySpan);
|
||||
return c.diags.items;
|
||||
return .{ .diagnostics = c.diags.items, .module = c.module };
|
||||
}
|
||||
}
|
||||
return &.{};
|
||||
return .{ .diagnostics = &.{}, .module = c.module };
|
||||
}
|
||||
|
||||
fn bySpan(_: void, a: diag.Diagnostic, b: diag.Diagnostic) bool {
|
||||
@@ -127,3 +137,24 @@ pub fn expectCodes(src: []const u8, codes: []const []const u8) !void {
|
||||
}
|
||||
|
||||
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";
|
||||
|
||||
test "verification rebuilds caller-supplied property and size caches" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const source = test_header ++ "type @P = struct { x: u64 }\nfn @f(%p: @P) -> @P\neffects []\nentry:\n %q = copy %p\n ret %q\n";
|
||||
var m = (try air.parse.parse(a, "t.air", source)).module.?;
|
||||
const fake = try a.alloc(@import("air/module.zig").NominalProperties, 1);
|
||||
fake[0] = .{ .linear = true, .copy = false };
|
||||
m.nominal_properties = fake;
|
||||
const result = try verifyModule(a, "t.air", m);
|
||||
try std.testing.expectEqual(@as(usize, 0), result.diagnostics.len);
|
||||
try std.testing.expect(!result.module.isLinearType(.{ .named = 0 }));
|
||||
try std.testing.expect(result.module.accounted_sizes != null);
|
||||
var large = (try air.parse.parse(a, "t.air", test_header ++ "type @P = struct { x: array<u8, 1048577> }\nfn @f() -> void\neffects []\nentry:\n ret\n")).module.?;
|
||||
var sizes: @import("air/module.zig").SizeCache = .{};
|
||||
try sizes.put(a, .{ .index = 0, .depth = 0 }, 0);
|
||||
large.accounted_sizes = &sizes;
|
||||
const rejected = try verifyModule(a, "t.air", large);
|
||||
try std.testing.expectEqualStrings("E5101", rejected.diagnostics[0].code.id);
|
||||
}
|
||||
Reference in new issue
Block a user