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
This commit is contained in:
1 parent
571cc6e362
commit
d405d4ea50
49 files changed
+2325
-81
No files matched your search
+102
-3
@@ -84,7 +84,7 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
|
||||
const ok = switch (t) {
|
||||
.void, .never => pos == .ret,
|
||||
.local => false,
|
||||
.ref, .mut, .slice, .slice_mut => pos != .member,
|
||||
.ref, .mut, .slice, .slice_mut, .resource => pos != .member,
|
||||
else => true,
|
||||
};
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
@@ -182,11 +182,103 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
.enum_make => |x| .{ .named = x.decl },
|
||||
.call => |x| m.functions[x.callee].ret,
|
||||
.slice_len => .{ .int = .usize },
|
||||
.intrinsic => |x| blk: {
|
||||
const reg = air.authority.intrinsics[x.index];
|
||||
if (reg.fallible or reg.results.len == 0) break :blk .void;
|
||||
break :blk intrinsicResult(reg.results[0]);
|
||||
},
|
||||
.array_splat => |x| x.ty,
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn intrinsicResult(r: air.authority.ResultType) Type {
|
||||
return switch (r) {
|
||||
.usize => .{ .int = .usize },
|
||||
.u64 => .{ .int = .u64 },
|
||||
.resource => |res| .{ .resource = res },
|
||||
};
|
||||
}
|
||||
|
||||
fn operandText(arena: std.mem.Allocator, o: air.authority.Operand) ![]const u8 {
|
||||
return switch (o) {
|
||||
.cap => |k| std.fmt.allocPrint(arena, "cap<{s}>", .{k}),
|
||||
.bytes => "slice<u8>",
|
||||
.buffer => "slice_mut<u8>",
|
||||
.borrowed => |rs| blk: {
|
||||
var w: std.Io.Writer.Allocating = .init(arena);
|
||||
for (rs, 0..) |r, i| w.writer.print("{s}mut<{s}>", .{ if (i == 0) "" else " or ", r.text() }) catch return error.OutOfMemory;
|
||||
break :blk w.written();
|
||||
},
|
||||
.consumed => |rs| blk: {
|
||||
var w: std.Io.Writer.Allocating = .init(arena);
|
||||
for (rs, 0..) |r, i| w.writer.print("{s}{s}", .{ if (i == 0) "" else " or ", r.text() }) catch return error.OutOfMemory;
|
||||
break :blk w.written();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn operandMatches(o: air.authority.Operand, t: Type) bool {
|
||||
return switch (o) {
|
||||
.cap => t == .cap, // the kind is checked separately (E3201)
|
||||
.bytes => t == .slice and t.slice.* == .int and t.slice.int == .u8,
|
||||
.buffer => t == .slice_mut and t.slice_mut.* == .int and t.slice_mut.int == .u8,
|
||||
.borrowed => |rs| t == .mut and t.mut.* == .resource and std.mem.indexOfScalar(air.authority.Resource, rs, t.mut.resource) != null,
|
||||
.consumed => |rs| t == .resource and std.mem.indexOfScalar(air.authority.Resource, rs, t.resource) != null,
|
||||
};
|
||||
}
|
||||
|
||||
fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")) !void {
|
||||
const f = fc.f;
|
||||
const span = inst.span;
|
||||
const reg = air.authority.intrinsics[x.index];
|
||||
if (reg.fallible != (x.ok != null)) {
|
||||
return fc.c.report("E1001", span, if (reg.fallible) "intrinsic is fallible: use the `, ok -> @a, err -> @b` form" else "intrinsic is infallible: it has no `ok`/`err` targets", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "intrinsic", .value = .{ .str = reg.name } },
|
||||
});
|
||||
}
|
||||
if (x.args.len != reg.operands.len) {
|
||||
return fc.c.report("E1001", span, "wrong number of intrinsic operands", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "intrinsic", .value = .{ .str = reg.name } },
|
||||
.{ .name = "expected", .value = .{ .int = @intCast(reg.operands.len) } },
|
||||
.{ .name = "found", .value = .{ .int = @intCast(x.args.len) } },
|
||||
});
|
||||
}
|
||||
for (x.args, reg.operands) |a, o| {
|
||||
const t = fc.typeOf(a);
|
||||
if (!operandMatches(o, t)) {
|
||||
try fc.kindError(span, a, try operandText(fc.c.arena, o));
|
||||
continue;
|
||||
}
|
||||
if (o == .cap and !std.mem.eql(u8, air.authority.kinds[t.cap].name, o.cap)) {
|
||||
try fc.c.report("E3201", span, "capability of the wrong kind for this intrinsic", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "intrinsic", .value = .{ .str = reg.name } },
|
||||
.{ .name = "expected", .value = .{ .str = o.cap } },
|
||||
.{ .name = "found", .value = .{ .str = air.authority.kinds[t.cap].name } },
|
||||
});
|
||||
}
|
||||
}
|
||||
if (x.ok) |ok| {
|
||||
if (inst.result != null) return fc.c.report("E1001", span, "a fallible intrinsic delivers results to its `ok` block parameters", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
});
|
||||
for (f.blocks[ok].params, reg.results) |p, r| {
|
||||
const want = intrinsicResult(r);
|
||||
if (!p.ty.eql(want)) try fc.mismatch(span, try fc.c.valueName(f, p.value), want, p.ty);
|
||||
}
|
||||
const e = f.blocks[x.err.?].params[0];
|
||||
if (!e.ty.eql(.{ .builtin = .io_error })) try fc.mismatch(span, try fc.c.valueName(f, e.value), .{ .builtin = .io_error }, e.ty);
|
||||
} else if (inst.result != null and reg.results.len == 0) {
|
||||
try fc.c.report("E1001", span, "intrinsic has no result to bind", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "intrinsic", .value = .{ .str = reg.name } },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn boxed(arena: std.mem.Allocator, t: Type) !*const Type {
|
||||
const b = try arena.create(Type);
|
||||
b.* = t;
|
||||
@@ -215,11 +307,13 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.local_load => if (t == .local) t.local.* else invalid,
|
||||
.borrow_ref => switch (t) {
|
||||
.local, .ref, .mut => .{ .ref = try boxed(a, t.inner().?) },
|
||||
.resource => .{ .ref = try boxed(a, t) },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
else => invalid,
|
||||
},
|
||||
.borrow_mut => switch (t) {
|
||||
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
|
||||
.resource => .{ .mut = try boxed(a, t) },
|
||||
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
|
||||
else => invalid,
|
||||
},
|
||||
@@ -322,8 +416,8 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
});
|
||||
},
|
||||
.local_load => |v| if (fc.typeOf(v) != .local) try fc.kindError(span, v, "local<T>"),
|
||||
.borrow_ref => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, ref<T>, mut<T>, slice<T> or slice_mut<T>"),
|
||||
.borrow_mut => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, mut<T> or slice_mut<T>"),
|
||||
.borrow_ref => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, a resource, ref<T>, mut<T>, slice<T> or slice_mut<T>"),
|
||||
.borrow_mut => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, a resource, mut<T> or slice_mut<T>"),
|
||||
.borrow_field => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.borrow, "ref or mut of the named struct"),
|
||||
.borrow_end => |v| if (!fc.typeOf(v).isBorrow()) try fc.kindError(span, v, "a borrow"),
|
||||
.load => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<T> or mut<T>"),
|
||||
@@ -427,6 +521,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
}
|
||||
if (x.default) |d| try fc.expectTarget(span, d);
|
||||
},
|
||||
.intrinsic => |x| try checkIntrinsic(fc, inst, x),
|
||||
.cap_narrow => |x| {
|
||||
const t = fc.typeOf(x.cap);
|
||||
if (t != .cap) return fc.kindError(span, x.cap, "cap<K>");
|
||||
@@ -437,6 +532,10 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
.{ .name = "kind", .value = .{ .str = air.authority.kinds[t.cap].name } },
|
||||
.{ .name = "form", .value = .{ .str = x.form } },
|
||||
});
|
||||
if (std.mem.eql(u8, x.form, "prefix")) {
|
||||
if (x.args.len != 1) return fc.c.report("E1001", span, "cap.narrow.prefix takes one path operand", &.{.{ .name = "function", .value = .{ .str = f.name } }});
|
||||
if (!operandMatches(.bytes, fc.typeOf(x.args[0]))) try fc.kindError(span, x.args[0], "slice<u8>");
|
||||
}
|
||||
const ok = f.blocks[x.ok].params[0];
|
||||
if (!ok.ty.eql(t)) try fc.mismatch(span, try fc.c.valueName(f, ok.value), t, ok.ty);
|
||||
const err = f.blocks[x.err].params[0];
|
||||
|
||||
Reference in new issue
Block a user