Introduce linear_buffers through the specification, AIR parser/serializer, verifier, deterministic interpreter and source lowering. A buffer owns an initialized prefix with explicit reserve, bounded push, moving pop/views and recoverable empty-only free. Growth charges old+new capacity; failed growth, full push and nonempty free preserve or return complete ownership. Implement compiler-pinned safe std.vec with nine operations for Copy values, owners, files and channel endpoints. Permit complete Copy moves inside specialized generic bodies without weakening monomorphic or borrow rules. Update RFC 0024, Q-036, STD-6a/b, reference grammar, Quick Reference, bundles, source coverage and CI. Remaining collections stay STD-6c. Validation: unit tests and AIR round-trip; 757 conformance cases; 56 dedicated buffer cases with per-instruction collection and 32-seed schedule stress; extended memory test; hosted smoke; 36 module, 20 generic, 49 linear and 118 buffer CLI checks; 91/91 source constructs; Zig fmt and reference bundles. Full source bundle: 49,589 -> 52,384 cl100k tokens (+5.6%); AIR 175,736 bytes. The reference interpreter's push copies its initialized prefix; no native performance or empirical MCC benefit is claimed.
136 lines
7.4 KiB
Zig
136 lines
7.4 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;
|
|
const diag = @import("../diag.zig");
|
|
|
|
pub fn check(c: *Context) !void {
|
|
const m = c.module;
|
|
if (!m.declares("effects")) return;
|
|
for (m.functions) |f| {
|
|
const first = c.diags.items.len;
|
|
defer suggest(c, f, first) catch {};
|
|
for (f.blocks) |b| for (b.insts) |inst| switch (inst.op) {
|
|
.task_spawn => |x| {
|
|
if (!contains(f.effects, "task.spawn")) try c.report("E3101", inst.span, "task.spawn requires the effect task.spawn", &.{
|
|
.{ .name = "function", .value = .{ .str = f.name } },
|
|
.{ .name = "effect", .value = .{ .str = "task.spawn" } },
|
|
});
|
|
const callee = m.functions[x.callee];
|
|
for (callee.effects) |e| if (!std.mem.eql(u8, e, "unsafe") and !contains(f.effects, e)) {
|
|
try c.report("E3101", inst.span, "task function effect not declared by the spawning function", &.{
|
|
.{ .name = "function", .value = .{ .str = f.name } },
|
|
.{ .name = "callee", .value = .{ .str = callee.name } },
|
|
.{ .name = "effect", .value = .{ .str = e } },
|
|
});
|
|
};
|
|
},
|
|
.call => |x| {
|
|
const callee = m.functions[x.callee];
|
|
// `unsafe` is checked by the unsafe stage, which knows about
|
|
// `trusted` boundaries (spec 13 §6.4).
|
|
for (callee.effects) |e| if (!std.mem.eql(u8, e, "unsafe") and !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 } },
|
|
});
|
|
};
|
|
},
|
|
// In a module declaring `cancellation`, every operation that may
|
|
// suspend requires `task.wait`, `task.join` included (spec 04 §37.4).
|
|
.task_join => if (m.declares("cancellation")) try need(c, f, inst, "task.wait"),
|
|
.task_join_cancel, .channel_send, .channel_recv, .select, .mutex_lock => try need(c, f, inst, "task.wait"),
|
|
.channel_make => try need(c, f, inst, "mem.alloc"),
|
|
// Atomics (spec 04 §25, spec 05 §13): every memory operation needs
|
|
// `atomic`; a non-`seq_cst` access also needs `atomic.weak`.
|
|
.atomic_fence => try need(c, f, inst, "atomic"),
|
|
.atomic_load => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
|
.atomic_store => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
|
.atomic_rmw => |x| try needAtomic(c, f, inst, x.ord != .seq_cst),
|
|
.atomic_cmpxchg => |x| try needAtomic(c, f, inst, x.success != .seq_cst or x.failure != .seq_cst),
|
|
.buffer_new, .buffer_reserve, .own_new, .own_pack, .own_array, .region_create, .region_new, .region_array, .island_create, .island_alloc, .arc_new => if (!contains(f.effects, "mem.alloc")) {
|
|
try c.report("E3101", inst.span, "allocation requires the effect mem.alloc", &.{
|
|
.{ .name = "function", .value = .{ .str = f.name } },
|
|
.{ .name = "effect", .value = .{ .str = "mem.alloc" } },
|
|
});
|
|
},
|
|
.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 needAtomic(c: *Context, f: air.Function, inst: air.Inst, weak: bool) !void {
|
|
try need(c, f, inst, "atomic");
|
|
if (weak) try need(c, f, inst, "atomic.weak");
|
|
}
|
|
|
|
/// Adds `suggested_effects` to every E3101 reported for `f` since diagnostic
|
|
/// `first`: the complete, sorted effect list the function needs (its declared
|
|
/// effects plus every missing one). Effects stay declared by the programmer;
|
|
/// the suggestion makes the repair mechanical.
|
|
fn suggest(c: *Context, f: air.Function, first: usize) !void {
|
|
var list: std.ArrayList([]const u8) = .empty;
|
|
for (f.effects) |e| if (!contains(list.items, e)) try list.append(c.arena, e);
|
|
const new = c.diags.items[first..];
|
|
for (new) |d| if (std.mem.eql(u8, d.code.id, "E3101")) for (d.fields) |fld| {
|
|
if (std.mem.eql(u8, fld.name, "effect") and !contains(list.items, fld.value.str)) try list.append(c.arena, fld.value.str);
|
|
};
|
|
if (list.items.len == f.effects.len) return;
|
|
std.mem.sort([]const u8, list.items, {}, struct {
|
|
fn lt(_: void, a: []const u8, b: []const u8) bool {
|
|
return std.mem.lessThan(u8, a, b);
|
|
}
|
|
}.lt);
|
|
var w: std.Io.Writer.Allocating = .init(c.arena);
|
|
w.writer.writeAll("[") catch return error.OutOfMemory;
|
|
for (list.items, 0..) |e, i| w.writer.print("{s}{s}", .{ if (i == 0) "" else ", ", e }) catch return error.OutOfMemory;
|
|
w.writer.writeAll("]") catch return error.OutOfMemory;
|
|
for (new) |*d| if (std.mem.eql(u8, d.code.id, "E3101")) {
|
|
const fields = try c.arena.alloc(diag.Field, d.fields.len + 1);
|
|
@memcpy(fields[0..d.fields.len], d.fields);
|
|
fields[d.fields.len] = .{ .name = "suggested_effects", .value = .{ .str = w.written() } };
|
|
d.fields = fields;
|
|
};
|
|
}
|
|
|
|
fn need(c: *Context, f: air.Function, inst: air.Inst, effect: []const u8) !void {
|
|
if (contains(f.effects, effect)) return;
|
|
try c.report("E3101", inst.span, "instruction requires an effect the function does not declare", &.{
|
|
.{ .name = "function", .value = .{ .str = f.name } },
|
|
.{ .name = "effect", .value = .{ .str = effect } },
|
|
});
|
|
}
|
|
|
|
pub 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, &.{});
|
|
}
|