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

+2 -2
View File
@@ -33,8 +33,8 @@ Targets and the evaluation order are in `README.md`. Phase 6A is specified by RF
| P6A-8 | done | Source Reference Bundle (directory, `QUICK.md` front door, AIR reference only under `air/`), harness profile `aji_source` with per-group documentation metrics, Quick Reference examples checked against conformance programs, raw and normalized size metrics | P6A-6 | `docs/agent/source-bundle.txt`, `tools/eval/languages/aji_source.json` | bundle measured; every Quick Reference example is an excerpt of a checked program |
| P6A-9 | todo | Dev regression on tasks M and C: the frozen `Aji/AIR` runs vs new `Aji/source` runs, same agents (all three native agents) | P6A-8 | results note | criteria below reported |
| P6A-10 | todo | Phase 6A exit review | all | plan status | targets reported |
| P6A-12 | todo | Source type-check completeness: errors fully determined by the source type system are reported by the frontend as `E7xxx` at the source construct, never left to the verifier on the lowered AIR (checkpoint 2: `E1001` ×4 in a source run). Not a layer 7 blocker; to be closed before the Phase 6A freeze | P6A-4 | `src/frontend/lower.zig` | for each verifier type rule reachable from source, a source test expecting the `E7xxx` |
| P6A-11 | todo | Malformed generated AIR is a compiler bug: when a `.aji` module passed the frontend but its AIR fails to parse or is structurally invalid, report an internal compiler diagnostic (with the source construct), not the AIR parse error mapped back (found by the M checkpoint: void fall-through lowered to `ret %`) | P6A-4 | `src/check.zig`, error registry | a test that injects malformed lowering gets the internal diagnostic |
| P6A-12 | done | Source type-check completeness: errors fully determined by the source type system are reported by the frontend as `E7xxx` at the source construct, never left to the verifier on the lowered AIR (checkpoint 2: `E1001` ×4 in a source run). Not a layer 7 blocker; to be closed before the Phase 6A freeze | P6A-4 | `src/frontend/lower.zig` | for each verifier type rule reachable from source, a source test expecting the `E7xxx` |
| P6A-11 | done | Malformed generated AIR is a compiler bug: when a `.aji` module passed the frontend but its AIR fails to parse or is structurally invalid, report an internal compiler diagnostic (with the source construct), not the AIR parse error mapped back (found by the M checkpoint: void fall-through lowered to `ret %`) | P6A-4 | `src/check.zig`, error registry | a test that injects malformed lowering gets the internal diagnostic |
### P6A-9 criteria (owner decision, fixed before the runs)
+1
View File
@@ -91,6 +91,7 @@ Only problems that exist because of the frontend. Everything the verifier decide
| E7005 | source.type_mismatch | Expression has the wrong type in a source position |
| E7006 | source.invalid_construct | Construct not allowed here |
| E7007 | source.ambiguous | Source meaning cannot be determined |
| E7900 | source.internal_compiler_error | The frontend produced invalid AIR for this construct: a compiler bug, not a program error; fields `air_code`, `air_message` |
## T0xxx — Runtime traps
+24
View File
@@ -360,8 +360,31 @@ A byte string or array used where a `slice<u8>` is expected is materialized in f
| found by the verifier on the lowered AIR | the AIR code; the diagnostic's `file`, `line` and `col` are the source location of the construct the instruction came from, and `air_line`/`air_col` locate it in the lowered AIR (`aji lower`); the verifier's own fields (`function`, `block`, `inst`) are kept |
| syntax, name resolution, lowering and invalid source constructs | `E7xxx`, below |
| several AIR instructions of one construct reporting the same code | one diagnostic (the first) per code and source location |
| the lowered AIR is malformed: a parse error (`E0001`–`E0005`) or invalid SSA or CFG (`E5001`, `E5002`) | `E7900` (`source.internal_compiler_error`) at the source construct, with the AIR code in `air_code`. It is a compiler bug, never an error in the program, and replaces every other diagnostic of the module. Semantic codes (ownership, borrows, resources, effects, capabilities, concurrency, `E0006`) are not affected |
| trap at run time | the trap record also carries `source_location` (`file`, `line`, `col`) |
**Which errors the frontend reports.** An error that is fully decided by source types is `E7005` (or another `E7xxx`) at the source construct, and never reaches the verifier as `E1001`/`E1002`. The frontend checks:
- the types of expressions, operands and results;
- arity and argument types of functions and built-in operations;
- the existence of fields and variants;
- `ref`/`mut` compatibility;
- lossless widening by `as` (the verifier's own rule);
- slice element types of `slice.copy`;
- the element rules of channels and mutexes (no borrow, scope, task, shield or guard).
The verifier remains the authority for:
- ownership state, linearity and moves;
- borrow conflicts and lifetimes;
- resource state;
- effects and capabilities;
- concurrency (loans, `Send`/`Share`, the critical-section rule);
- reserved features;
- every other verifier invariant.
Its diagnostics keep their AIR codes, reported at the source location.
| Code | Kind | Meaning |
|---|---|---|
| E7001 | source.syntax | Malformed `.aji` source |
@@ -371,6 +394,7 @@ A byte string or array used where a `slice<u8>` is expected is materialized in f
| E7005 | source.type_mismatch | Expression has the wrong type in a source position |
| E7006 | source.invalid_construct | Construct not allowed here (e.g. `break` outside a loop, `move` of a non-linear value, `zero` for a non-zeroable type) |
| E7007 | source.ambiguous | Source meaning cannot be determined (e.g. an integer literal with no type from context) |
| E7900 | source.internal_compiler_error | The frontend produced invalid AIR for this construct (a compiler bug, not a program error) |
## 11. Tooling
+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");
@@ -1,12 +0,0 @@
// The frontend does not duplicate the verifier: a lossy `as` lowers to cast.widen and the verifier rejects it (E1002).
// expect: reject E1002
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.widen
fn main() -> u8
effects []
{
let big: u64 = 1;
return big as u8;
}
@@ -0,0 +1,13 @@
// Arithmetic operands have the same integer type; there is no implicit conversion.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.binary_mixed_widths
fn main() -> u64
effects []
{
let a = 1u8;
let b = 2u64;
b + a
}
@@ -0,0 +1,17 @@
// A call argument of the wrong type is E7005 at the argument, not E1001 after lowering.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.call_argument
fn f(x: u64) -> u64
effects []
{
x
}
fn main() -> u64
effects []
{
f(true)
}
@@ -0,0 +1,12 @@
// Casts convert between integer types.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.cast_non_integer
fn main() -> u64
effects []
{
let a = true;
a as u64
}
@@ -0,0 +1,18 @@
// A channel element contains no borrow (spec 04 §38.1), checked at the source.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.channel_borrow_element
fn main(heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
{
match channel.make<ref<u64>>(1, heap) {
ok(tx, rx) => {
channel.close_sender(move tx);
channel.close_receiver(move rx) or trap;
0
}
err(e) => { trap; }
}
}
@@ -0,0 +1,12 @@
// `as` only widens without loss (the verifier's rule, checked at the source): a narrowing `as` is E7005 at the cast.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.lossy_widen
fn main() -> u8
effects []
{
let big: u64 = 1;
return big as u8;
}
@@ -0,0 +1,14 @@
// A mutex's content contains no borrow, checked at the source.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.mutex_borrow_content
fn main() -> u64
effects []
{
var x: u64 = 0;
ref r = x;
let m = mutex.make(r);
0
}
@@ -0,0 +1,13 @@
// `own.free` takes an owner.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.own_free_non_owner
fn main() -> u64
effects []
{
let x = 1u64;
own.free(x);
0
}
@@ -0,0 +1,19 @@
// A shared borrow is passed where an exclusive one is expected.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.ref_for_mut
fn g(r: mut<u64>) -> void
effects []
{
*r = 1;
}
fn main() -> u64
effects []
{
var x: u64 = 0;
g(ref x);
x
}
@@ -0,0 +1,14 @@
// `slice.copy` copies between slices of the same element type.
// expect: reject E7005
// phase: 6
// spec: docs/spec/15-source-language.md
module demo.slice_copy_elements
fn main() -> u64
effects []
{
var a: [u8; 4] = zero;
var b: [u64; 4] = zero;
slice.copy(mut a, ref b);
0
}