Files
aji/src/verifier.zig
T
dev 1ae6b485d8 Phase 1: frame storage, second-class borrows, arrays (P1-3..P1-10)
- air: types local/ref/mut/array/slice/slice_mut, from-returns,
  multi-result binding, 18 new instructions; feature gating reports
  E0006 feature_not_declared; borrow-typed block parameters gated by
  borrow_joins; printer and round-trip tests for all new forms
- verifier: second-class and zeroable type rules (E1001); local.alloc
  only in entry (E5002); frame size accounting (E5101); stages init
  (E1010), borrow (E2101, E2102, E2105-E2108) and bounds facts (E2103);
  value types shared between stages
- interp: frame cells, places, slices, deep copies between storage and
  SSA values, T0008 bounds traps
- features arrays and ownership supported; implemented_phase = 1
- conformance: 5 accept, 18 reject, 11 run tests for Phase 1, including
  the file-copy core exit program; 6000 mutants without a crash
2026-10-04 13:24:09 +03:00

111 lines
4.1 KiB
Zig

//! AIR verifier (spec 08). Part of the language semantics, not a lint.
//!
//! Passes are added in plan order (docs/plan/README.md). Each normative
//! rejection maps to a code in `diag.registry` and has accept and reject
//! conformance tests under tests/conformance/.
//!
//! Stages run in the order of spec 07 §8.6 and verification stops after the
//! first stage that reports an error, so later stages may assume the
//! invariants established by earlier ones.
const std = @import("std");
const air = @import("air.zig");
const diag = @import("diag.zig");
pub const budget = @import("verifier/budget.zig");
pub const cfg = @import("verifier/cfg.zig");
pub const ssa = @import("verifier/ssa.zig");
pub const types = @import("verifier/types.zig");
pub const init = @import("verifier/init.zig");
pub const borrow = @import("verifier/borrow.zig");
pub const bounds = @import("verifier/bounds.zig");
/// Shared state for one verification run.
pub const Context = struct {
arena: std.mem.Allocator,
file: []const u8,
module: air.Module,
diags: std.ArrayList(diag.Diagnostic) = .empty,
/// Type of every value, per function; filled by the types stage and
/// available to all later stages.
value_types: []const []const air.Type = &.{},
pub fn report(
c: *Context,
id: []const u8,
span: air.Span,
message: []const u8,
fields: []const diag.Field,
) std.mem.Allocator.Error!void {
try c.diags.append(c.arena, .{
.code = diag.lookup(id).?,
.span = .{ .file = c.file, .line = span.line, .col = span.col },
.message = message,
.fields = try c.arena.dupe(diag.Field, fields),
});
}
pub fn valueName(c: *Context, f: air.Function, v: air.ValueId) std.mem.Allocator.Error![]const u8 {
return std.fmt.allocPrint(c.arena, "%{s}", .{f.values[v].name});
}
pub fn typeName(c: *Context, t: air.Type) std.mem.Allocator.Error![]const u8 {
var w: std.Io.Writer.Allocating = .init(c.arena);
air.print.printType(c.module, t, &w.writer) catch return error.OutOfMemory;
return w.written();
}
};
/// 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 {
var c: Context = .{ .arena = arena, .file = file, .module = module };
const stages = .{ budget.check, cfg.check, ssa.check, types.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 &.{};
}
fn bySpan(_: void, a: diag.Diagnostic, b: diag.Diagnostic) bool {
const sa = a.span.?;
const sb = b.span.?;
if (sa.line != sb.line) return sa.line < sb.line;
if (sa.col != sb.col) return sa.col < sb.col;
return std.mem.lessThan(u8, a.code.id, b.code.id);
}
test {
_ = budget;
_ = cfg;
_ = ssa;
_ = types;
_ = init;
_ = borrow;
_ = bounds;
}
/// Test helper: parses `src` and verifies it, expecting exactly `codes`.
pub fn expectCodes(src: []const u8, codes: []const []const u8) !void {
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena_state.deinit();
const arena = arena_state.allocator();
const r = try air.parse.parse(arena, "t.air", src);
if (r.diagnostics.len != 0) {
for (r.diagnostics) |d| std.debug.print("parse: {s} {s}\n", .{ d.code.id, d.message });
return error.ParseFailed;
}
const ds = try verify(arena, "t.air", r.module.?);
if (ds.len != codes.len) {
for (ds) |d| std.debug.print("got {s} {s} at {d}:{d}\n", .{ d.code.id, d.message, d.span.?.line, d.span.?.col });
return error.TestExpectedEqual;
}
for (codes, ds) |c, d| try std.testing.expectEqualStrings(c, d.code.id);
}
pub const test_header = "module t\nair 0\nlang 0.1\nfeatures []\n";