P6A-11, P6A-12: malformed lowering is an internal compiler error; source type-check completeness

P6A-11: when a .aji module passed the frontend but its AIR does not parse
(E0001-E0005) or has invalid SSA/CFG (E5001, E5002), the result is one
E7900 source.internal_compiler_error at the source construct, with the
AIR code in air_code; semantic codes (E0006 included) are unaffected.
Unit test injects the checkpoint's `ret %` defect.

P6A-12: source-decidable type errors are E7005 at the source:
- `as` checks lossless widening with the verifier's own rule (shared
  function), and that both sides are integers
- slice.copy element types; own.free / region.destroy operand types
- element rules of channel.make<T> and mutex.make (no borrow, scope,
  task, shield or guard, at any depth)
Probing every source position found no other E1001/E1002 leak. The old
test encoding the previous rule (verifier_widen, E1002) is replaced by
typecheck/lossy_widen (E7005); 9 typecheck tests. Spec 15 §10 states the
frontend/verifier boundary.
This commit is contained in:
dev committed 2026-10-05 18:10:53 +03:00
1 parent 154c39865f
commit 748eddf4e5
16 files changed
+249 -14

No files matched your search

+42
View File
@@ -28,6 +28,8 @@ const ast = @import("ast.zig");
const parse_mod = @import("parse.zig");
const authority = @import("../air/authority.zig");
const features_mod = @import("../air/features.zig");
const air_module = @import("../air/module.zig");
const verifier_types = @import("../verifier/types.zig");
const Allocator = std.mem.Allocator;
const Span = ast.Span;
@@ -1265,6 +1267,14 @@ const Lowerer = struct {
if (to != .int) return l.fail("E7005", x.ty.span, "casts convert between integer types", &.{});
const from = l.peekType(x.e.*) orelse to;
const v = try l.expr(x.e.*, from);
if (v.ty != .int) return l.fail("E7005", x.e.span, "casts convert between integer types", &.{.{ .name = "found", .value = l.tyText(v.ty) }});
// `as` widens without loss, by the verifier's own rule (spec 02 §2).
if (x.mode == .widen) {
const a = std.meta.stringToEnum(air_module.IntType, v.ty.int).?;
const b = std.meta.stringToEnum(air_module.IntType, to.int).?;
if (a != b and !verifier_types.lossless(a, b))
return l.fail("E7005", e.span, "`as` only widens without loss; use `as!` (checked, traps T0006 when the value does not fit) or `as%` (wrapping)", &.{ .{ .name = "from", .value = v.ty.int }, .{ .name = "to", .value = to.int } });
}
const mode = switch (x.mode) {
.widen => "widen",
.checked => "checked",
@@ -1372,6 +1382,8 @@ const Lowerer = struct {
if (std.mem.eql(u8, p, "own.free") or std.mem.eql(u8, p, "region.destroy")) {
if (args.len != 1) return l.fail("E7005", e.span, "this operation takes one linear value", &.{});
const o = try l.consuming(args[0], null);
const ok = if (std.mem.eql(u8, p, "own.free")) (o.ty == .own or o.ty == .own_array) else o.ty == .region;
if (!ok) return l.fail("E7005", args[0].span, if (std.mem.eql(u8, p, "own.free")) "`own.free` takes an `own<T>` or `own_array<T>`" else "`region.destroy` takes a region", &.{.{ .name = "found", .value = l.tyText(o.ty) }});
try l.emit(try l.fmt("{s} %{s}", .{ p, o.name }), e.span);
return .{ .name = "", .ty = .void };
}
@@ -1994,6 +2006,9 @@ const Lowerer = struct {
var temps: std.ArrayList([]const u8) = .empty;
const d = try l.argument(args[0], null, &temps);
const src = try l.argument(args[1], null, &temps);
if (d.ty != .slice_mut) return l.fail("E7005", args[0].span, "`slice.copy` writes to `mut dst` (a `slice_mut<T>`)", &.{.{ .name = "found", .value = l.tyText(d.ty) }});
if (src.ty != .slice and src.ty != .slice_mut) return l.fail("E7005", args[1].span, "`slice.copy` reads from `ref src` (a `slice<T>`)", &.{.{ .name = "found", .value = l.tyText(src.ty) }});
if (!d.ty.inner().?.eql(src.ty.inner().?)) return l.fail("E7005", args[1].span, "`slice.copy` copies between slices of the same element type", &.{ .{ .name = "expected", .value = l.tyText(d.ty.inner().?) }, .{ .name = "found", .value = l.tyText(src.ty.inner().?) } });
try l.emit(try l.fmt("slice.copy %{s}, %{s}", .{ d.name, src.name }), e.span);
try l.endNow(temps.items, e.span);
return .{ .name = "", .ty = .void };
@@ -2194,6 +2209,7 @@ const Lowerer = struct {
if (eq(p, "mutex.make")) {
if (args.len != 1) return l.fail("E7005", e.span, "`mutex.make` takes the initial value", &.{});
const v = try l.consuming(args[0], null);
try l.elementRule(v.ty, args[0].span, "a mutex's content");
return try l.def("m", try l.wrap(.mutex, v.ty), try l.fmt("mutex.make %{s}", .{v.name}), e.span);
}
if (eq(p, "mutex.take")) {
@@ -2395,6 +2411,31 @@ const Lowerer = struct {
_ = try l.mergeArms(s.span, "select", ends.items, vars, before, "endselect");
}
/// Element rules of channels and mutexes (spec 04 §38.1): no borrow,
/// scope, task, shield or guard, at any depth. Endpoints inside are the
/// reserved `channel_transfer`, left to the verifier (`E0006`).
fn elementRule(l: *Lowerer, t: Ty, span: Span, what: []const u8) Error!void {
if (try l.forbiddenIn(t, 0)) |bad| return l.fail("E7005", span, try l.fmt("{s} contains no borrow, scope, task, shield or guard", .{what}), &.{ .{ .name = "type", .value = l.tyText(t) }, .{ .name = "found", .value = l.tyText(bad) } });
}
fn forbiddenIn(l: *Lowerer, t: Ty, depth: u32) Error!?Ty {
if (depth > 32) return null;
return switch (t) {
.ref, .mut, .slice, .slice_mut, .scope, .task, .shield, .guard => t,
.array => |a| l.forbiddenIn(a.elem, depth + 1),
.own, .own_array, .mutex => |x| l.forbiddenIn(x.*, depth + 1),
.named => |i| blk: {
const d = l.m.types[i];
switch (d.kind) {
.@"struct" => |fs| for (fs) |f| if (try l.forbiddenIn(try l.typeOf(f.ty), depth + 1)) |bad| break :blk bad,
.@"enum" => |vs| for (vs) |v| for (v.payload) |pt| if (try l.forbiddenIn(try l.typeOf(pt), depth + 1)) |bad| break :blk bad,
}
break :blk null;
},
else => null,
};
}
fn resultTy(r: authority.ResultType) Ty {
return switch (r) {
.usize => .{ .int = "usize" },
@@ -2550,6 +2591,7 @@ const Lowerer = struct {
try want(l, e.span, args, 2, path);
const ta = e.kind.call.type_arg orelse return l.fail("E7005", e.span, "`channel.make` names its element type: `channel.make<T>(n, mem)`", &.{});
const et = try l.memberType(ta);
try l.elementRule(et, ta.span, "a channel element");
const n = try l.expr(args[0], usize_ty);
try l.checkTy(args[0].span, n.ty, usize_ty);
const c = try l.expectCap(args[1], "mem.alloc");