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

+47
View File
@@ -36,7 +36,36 @@ pub fn checkFile(arena: std.mem.Allocator, file_name: []const u8, source: []cons
.err => |d| return .{ .status = .rejected, .diagnostics = try arena.dupe(diag.Diagnostic, &.{d}) },
.unimplemented => |what| return .{ .status = .unimplemented, .diagnostics = &.{}, .missing_stage = what },
};
return checkLowered(arena, file_name, lowered);
}
/// AIR diagnostics that can only mean the frontend produced malformed AIR
/// (spec 15 §10): parse errors and invalid SSA or CFG. Semantic codes,
/// `E0006` included, are the program's.
const malformed_air = [_][]const u8{ "E0001", "E0002", "E0003", "E0004", "E0005", "E5001", "E5002" };
/// The AIR pipeline on lowered `.aji`, with every diagnostic mapped to its
/// source location; malformed AIR is one internal compiler error (`E7900`).
pub fn checkLowered(arena: std.mem.Allocator, file_name: []const u8, lowered: frontend.lower_mod.Output) !Report {
var r = try checkAir(arena, file_name, lowered.air);
for (r.diagnostics) |d| {
const internal = for (malformed_air) |c| {
if (std.mem.eql(u8, c, d.code.id)) break true;
} else false;
if (!internal) continue;
const m = try frontend.mapDiagnostic(arena, d, file_name, lowered.lines);
var fields: std.ArrayList(diag.Field) = .empty;
try fields.append(arena, .{ .name = "air_code", .value = .{ .str = d.code.id } });
try fields.append(arena, .{ .name = "air_message", .value = .{ .str = d.message } });
for (m.fields) |f| if (std.mem.eql(u8, f.name, "air_line") or std.mem.eql(u8, f.name, "air_col")) try fields.append(arena, f);
const ice: diag.Diagnostic = .{
.code = diag.lookup("E7900").?,
.span = m.span,
.message = "the frontend produced invalid AIR for this construct: a compiler bug, not an error in the program",
.fields = fields.items,
};
return .{ .status = .rejected, .diagnostics = try arena.dupe(diag.Diagnostic, &.{ice}), .lowered = lowered };
}
// One source construct often lowers to several AIR instructions that the
// verifier reports separately: keep one diagnostic per code and source
// location (the first).
@@ -62,3 +91,21 @@ pub fn checkAir(arena: std.mem.Allocator, file_name: []const u8, source: []const
if (ds.len != 0) return .{ .status = .rejected, .diagnostics = ds };
return .{ .status = .accepted, .diagnostics = &.{}, .module = module };
}
test "malformed lowered AIR is an internal compiler error" {
var a = std.heap.ArenaAllocator.init(std.testing.allocator);
defer a.deinit();
const arena = a.allocator();
const src = "module t\nfn main() -> void\neffects []\n{\n let x: u8 = 1;\n}\n";
var lowered = (try frontend.compile(arena, "t.aji", src)).ok;
// Inject the defect of the M checkpoint: a void `ret` with an empty operand.
const at = std.mem.indexOf(u8, lowered.air, " ret").?;
lowered.air = try std.mem.concat(arena, u8, &.{ lowered.air[0..at], " ret %", lowered.air[at + " ret".len ..] });
const r = try checkLowered(arena, "t.aji", lowered);
try std.testing.expectEqual(Status.rejected, r.status);
try std.testing.expectEqual(@as(usize, 1), r.diagnostics.len);
const d = r.diagnostics[0];
try std.testing.expectEqualStrings("E7900", d.code.id);
try std.testing.expectEqualStrings("E0001", d.fields[0].value.str);
try std.testing.expect(d.span.?.line == 5 or d.span.?.line == 4);
}
+1
View File
@@ -71,6 +71,7 @@ pub const registry = [_]Code{
.{ .id = "E7005", .kind = "source.type_mismatch", .meaning = "Expression has the wrong type in a source position" },
.{ .id = "E7006", .kind = "source.invalid_construct", .meaning = "Construct not allowed here" },
.{ .id = "E7007", .kind = "source.ambiguous", .meaning = "Source meaning cannot be determined" },
.{ .id = "E7900", .kind = "source.internal_compiler_error", .meaning = "The frontend produced invalid AIR for this construct: a compiler bug, not a program error; fields `air_code`, `air_message`" },
// T0xxx — Runtime traps (reported in trap records, not by the verifier)
.{ .id = "T0001", .kind = "trap.explicit", .meaning = "`trap` instruction executed" },
.{ .id = "T0002", .kind = "trap.assert", .meaning = "Assertion failed" },
+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");