//! 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, &.{}); }