Files
aji/src/verifier/effects.zig
T
dev d405d4ea50 Phase 2: I/O intrinsics, linear resources, deterministic profile (RFC 0007)
- air: resource types file<read|write|append>, listener, stream; io_error;
  intrinsic registry (19 intrinsics) synced with reference/intrinsics.md;
  fallible intrinsics as ok/err terminators; fs prefix narrowing;
  linear_aggregates gating
- verifier: intrinsic form, operand, result and capability-kind checks
  (E1001, E3201, E5002); intrinsic effects (E3101); resource linearity
  in the borrow walker with per-edge moves: E3002 leaks and join
  mismatches, E3001, E2001, E2002, E2101 for close while borrowed;
  resources as borrow roots
- interp: deterministic world (captured console, VFS, loopback TCP,
  instruction clock, pinned xoshiro256 PRNG), blocked outcome, run
  record with stdout/stderr/files
- CLI: --stdin, --vfs, --seed, --result-file, --profile (hosted: exit 3),
  exit code 7; runner: stdin/vfs/seed/stdout/stderr/file/blocked headers
- conformance: hello, file copy and TCP echo exit programs; 37 new tests;
  4000 mutants without a crash
2026-10-04 14:02:17 +03:00

57 lines
2.7 KiB
Zig

//! Effect checking (spec 05 §9.3). Reports E3101.
//!
//! Applies to modules that declare the feature `effects`. Effects are flat
//! atoms: a call needs every effect of its callee in the caller's list.
//! Checking each function against its direct callees makes the rule hold
//! transitively, including through recursion.
const std = @import("std");
const air = @import("../air.zig");
const verifier = @import("../verifier.zig");
const Context = verifier.Context;
pub fn check(c: *Context) !void {
const m = c.module;
if (!m.declares("effects")) return;
for (m.functions) |f| {
for (f.blocks) |b| for (b.insts) |inst| switch (inst.op) {
.call => |x| {
const callee = m.functions[x.callee];
for (callee.effects) |e| if (!contains(f.effects, e)) {
try c.report("E3101", inst.span, "callee effect not declared by the caller", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "callee", .value = .{ .str = callee.name } },
.{ .name = "effect", .value = .{ .str = e } },
});
};
},
.intrinsic => |x| if (air.authority.intrinsics[x.index].effect) |e| {
if (!contains(f.effects, e)) try c.report("E3101", inst.span, "intrinsic effect not declared by the caller", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "intrinsic", .value = .{ .str = air.authority.intrinsics[x.index].name } },
.{ .name = "effect", .value = .{ .str = e } },
});
},
else => {},
};
}
}
fn contains(list: []const []const u8, name: []const u8) bool {
for (list) |x| if (std.mem.eql(u8, x, name)) return true;
return false;
}
const h = "module t\nair 0\nlang 0.1\nfeatures [effects]\n";
test "callee effects must be declared" {
const g = "fn @g() -> void\neffects [io.write]\nentry:\n ret\n";
try verifier.expectCodes(h ++ "fn @f() -> void\neffects [io.write]\nentry:\n call @g()\n ret\n" ++ g, &.{});
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n call @g()\n ret\n" ++ g, &.{"E3101"});
// Flat: atomic does not cover atomic.weak.
try verifier.expectCodes(h ++ "fn @f() -> void\neffects [atomic]\nentry:\n call @w()\n ret\nfn @w() -> void\neffects [atomic.weak]\nentry:\n ret\n", &.{"E3101"});
// Without the feature, lists are recorded but not checked.
try verifier.expectCodes("module t\nair 0\nlang 0.1\nfeatures []\nfn @f() -> void\neffects []\nentry:\n call @g()\n ret\n" ++ g, &.{});
}