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
+51
-42
@@ -30,6 +30,7 @@ const parse_mod = @import("parse.zig");
|
||||
const authority = @import("../air/authority.zig");
|
||||
const features_mod = @import("../air/features.zig");
|
||||
const air_module = @import("../air/module.zig");
|
||||
const properties = @import("../air/properties.zig");
|
||||
const verifier_types = @import("../verifier/types.zig");
|
||||
const type_positions = @import("type_positions.zig");
|
||||
const type_properties = @import("type_properties.zig");
|
||||
@@ -328,12 +329,9 @@ const Lowerer = struct {
|
||||
linear_enabled: bool = false,
|
||||
/// Complete declaration snapshot, with finite layouts checked before bodies.
|
||||
property_module: ?air_module.Module = null,
|
||||
/// Count witness visits in unit tests, never used to accept/reject source.
|
||||
property_visits: usize = 0,
|
||||
|
||||
fn probeType(l: *Lowerer, t: ast.Type) ?Ty {
|
||||
const previous = l.problem;
|
||||
defer l.problem = previous;
|
||||
return l.typeOf(t) catch null;
|
||||
}
|
||||
fn needsLinearType(l: *Lowerer, t: Ty, depth: usize) bool {
|
||||
if (depth > l.m.types.len + 32) return false;
|
||||
if (t == .named) return l.isLinear(t);
|
||||
@@ -344,23 +342,12 @@ const Lowerer = struct {
|
||||
return false;
|
||||
}
|
||||
fn isLinear(l: *Lowerer, t: Ty) bool {
|
||||
return l.linearAt(t, 0);
|
||||
}
|
||||
|
||||
fn linearAt(l: *Lowerer, t: Ty, depth: usize) bool {
|
||||
// Signatures/declarations resolve before checkRequirements; all
|
||||
// structural ownership queries run after its immutable snapshot.
|
||||
if (t.isLinear()) return true;
|
||||
if (depth > l.m.types.len + 32) return false;
|
||||
return switch (t) {
|
||||
.array => |a| l.linearAt(a.elem, depth + 1),
|
||||
.named => |i| switch (l.m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (l.probeType(f.ty)) |ty| if (l.linearAt(ty, depth + 1)) break true;
|
||||
} else false,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (l.probeType(p)) |ty| if (l.linearAt(ty, depth + 1)) break :blk true;
|
||||
break :blk false;
|
||||
},
|
||||
},
|
||||
.array => |a| l.isLinear(a.elem),
|
||||
.named => |i| l.property_module.?.nominal_properties.?[i].linear,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@@ -553,9 +540,6 @@ const Lowerer = struct {
|
||||
},
|
||||
}
|
||||
}
|
||||
for (l.m.types, 0..) |_, i| if (l.isLinear(.{ .named = @intCast(i) })) {
|
||||
l.linear_enabled = true;
|
||||
};
|
||||
const m: air_module.Module = .{ .name = l.m.name, .format_version = 0, .lang = .{ 0, 1 }, .features = &.{"linear_aggregates"}, .types = types, .functions = &.{} };
|
||||
var layout_context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = m };
|
||||
try verifier_types.checkFiniteLayouts(&layout_context);
|
||||
@@ -563,18 +547,22 @@ const Lowerer = struct {
|
||||
const d = l.m.types[layout_context.diags.items[0].span.?.line - 1];
|
||||
return l.fail("E1003", d.span, "aggregate type contains itself by value", &.{.{ .name = "type", .value = d.display_name orelse d.name }});
|
||||
}
|
||||
l.property_module = m;
|
||||
l.property_module = try properties.prepare(l.arena, m, null);
|
||||
for (l.m.types, 0..) |_, i| if (l.isLinear(.{ .named = @intCast(i) })) {
|
||||
l.linear_enabled = true;
|
||||
};
|
||||
for (l.m.requirements) |r| {
|
||||
const ty = try l.typeOf(r.ty);
|
||||
const t = try l.airType(ty);
|
||||
if (!boundedType(m, t, 0)) return l.invalid(.implementation_limit, r.span, "constraint type traversal is cyclic or exceeds depth 64", &.{});
|
||||
if (!properties.bounded(l.property_module.?, t, 0)) return l.invalid(.implementation_limit, r.span, "constraint type traversal is cyclic or exceeds depth 64", &.{});
|
||||
if (r.property.len == 0) {
|
||||
var context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = m };
|
||||
var context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = l.property_module.? };
|
||||
try verifier_types.checkTypeArgument(&context, .{ .line = 1, .col = 1 }, t);
|
||||
if (context.diags.items.len != 0) return l.fail("E7005", r.span, "generic argument is not a well-formed concrete type", &.{ .{ .name = "parameter", .value = r.parameter }, .{ .name = "found", .value = l.diagnosticTyText(ty) } });
|
||||
continue;
|
||||
}
|
||||
const ok = if (std.mem.eql(u8, r.property, "Copy")) verifier_types.isCopy(m, t) else if (std.mem.eql(u8, r.property, "Send")) verifier_types.isSend(m, t) else if (std.mem.eql(u8, r.property, "Share")) verifier_types.isShare(m, t) else if (std.mem.eql(u8, r.property, "Freeze")) verifier_types.isIslandSafe(m, t) else verifier_types.isLocalType(m, t);
|
||||
const cached = l.property_module.?;
|
||||
const ok = if (std.mem.eql(u8, r.property, "Copy")) verifier_types.isCopy(cached, t) else if (std.mem.eql(u8, r.property, "Send")) verifier_types.isSend(cached, t) else if (std.mem.eql(u8, r.property, "Share")) verifier_types.isShare(cached, t) else if (std.mem.eql(u8, r.property, "Freeze")) verifier_types.isIslandSafe(cached, t) else verifier_types.isLocalType(cached, t);
|
||||
if (!ok) return l.fail("E7005", r.span, "generic argument does not satisfy structural constraint", &.{
|
||||
.{ .name = "parameter", .value = r.parameter },
|
||||
.{ .name = "constraint", .value = r.property },
|
||||
@@ -3123,7 +3111,9 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn forbiddenIn(l: *Lowerer, t: Ty, depth: u32) Error!?Ty {
|
||||
if (@import("builtin").is_test) l.property_visits += 1;
|
||||
if (depth > 32) return null;
|
||||
if (t == .named and properties.sourceForbiddenDepth(l.property_module.?, .{ .named = t.named }) > 32 - depth) return null;
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => t,
|
||||
.array => |a| l.forbiddenIn(a.elem, depth + 1),
|
||||
@@ -4026,21 +4016,6 @@ fn expressionPath(arena: Allocator, e: ast.Expr) Allocator.Error!?[]const u8 {
|
||||
};
|
||||
}
|
||||
|
||||
// Bound property queries before invoking recursive verifier derivations.
|
||||
fn boundedType(m: air_module.Module, t: air_module.Type, depth: usize) bool {
|
||||
if (depth >= 64) return false;
|
||||
if (t == .named) return switch (m.types[t.named].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!boundedType(m, f.ty, depth + 1)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| blk: {
|
||||
for (vs) |v| for (v.payload) |p| if (!boundedType(m, p, depth + 1)) break :blk false;
|
||||
break :blk true;
|
||||
},
|
||||
};
|
||||
return if (t.inner()) |inner| boundedType(m, inner, depth + 1) else true;
|
||||
}
|
||||
|
||||
test "terminated blocks reject further emission with a source diagnostic" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
@@ -4055,3 +4030,37 @@ test "terminated blocks reject further emission with a source diagnostic" {
|
||||
try std.testing.expectEqual(@as(usize, 1), l.blocks.items[0].insts.items.len);
|
||||
try std.testing.expect(l.blocks.items[0].terminated);
|
||||
}
|
||||
|
||||
test "source property queries prune deep diamonds without changing witnesses" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
var text: std.ArrayList(u8) = .empty;
|
||||
try text.appendSlice(a, "module t\n");
|
||||
for (0..41) |i| try text.print(a, "struct N{d} {{ a: N{d}, b: N{d} }}\n", .{ i, i + 1, i + 1 });
|
||||
try text.appendSlice(a, "struct N41 { a: ref<u64>, b: task<u64> }\nfn main() -> void effects [] {}\n");
|
||||
var l: Lowerer = .{ .arena = a, .m = (try parse_mod.parse(a, text.items)).ok };
|
||||
try l.checkRequirements();
|
||||
try std.testing.expect(l.isLinear(.{ .named = 0 }));
|
||||
try std.testing.expectEqual(@as(?Ty, null), try l.forbiddenIn(.{ .named = 0 }, 0));
|
||||
try std.testing.expectEqual(@as(usize, 1), l.property_visits);
|
||||
l.property_visits = 0;
|
||||
const bad = (try l.forbiddenIn(.{ .named = 10 }, 0)).?;
|
||||
try std.testing.expect(bad == .ref and bad.inner().? == .int);
|
||||
try std.testing.expect(l.property_visits <= 33);
|
||||
}
|
||||
|
||||
test "cyclic source diamonds reject before property construction" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
var text: std.ArrayList(u8) = .empty;
|
||||
try text.appendSlice(a, "module t\n");
|
||||
for (0..41) |i| try text.print(a, "struct N{d} {{ a: N{d}, b: N{d} }}\n", .{ i, (i + 1) % 41, (i + 1) % 41 });
|
||||
try text.appendSlice(a, "fn main() -> void effects [] {}\n");
|
||||
var l: Lowerer = .{ .arena = a, .m = (try parse_mod.parse(a, text.items)).ok };
|
||||
try std.testing.expectError(error.Lower, l.checkRequirements());
|
||||
try std.testing.expectEqualStrings("E1003", l.problem.?.code);
|
||||
try std.testing.expectEqual(@as(u32, 2), l.problem.?.span.start.line);
|
||||
try std.testing.expect(l.property_module == null);
|
||||
}
|
||||
Reference in new issue
Block a user