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.
112 lines
5.5 KiB
Zig
112 lines
5.5 KiB
Zig
//! Front door used by the CLI, the conformance runner and (later) `ajid`:
|
|
//! parse → verify, collecting diagnostics (spec 07 §8.6).
|
|
|
|
const std = @import("std");
|
|
const air = @import("air.zig");
|
|
const diag = @import("diag.zig");
|
|
const verifier = @import("verifier.zig");
|
|
const frontend = @import("frontend.zig");
|
|
|
|
pub const Status = enum {
|
|
/// Module verified; no error diagnostics.
|
|
accepted,
|
|
/// At least one error diagnostic.
|
|
rejected,
|
|
/// The pipeline cannot yet decide this input.
|
|
unimplemented,
|
|
};
|
|
|
|
pub const Report = struct {
|
|
status: Status,
|
|
diagnostics: []const diag.Diagnostic,
|
|
/// Present when `status == .accepted`.
|
|
module: ?air.Module = null,
|
|
/// Pipeline stage that is missing when `status == .unimplemented`.
|
|
missing_stage: []const u8 = "",
|
|
/// For `.aji` input: the lowered AIR and its line map (spec 15 §10).
|
|
lowered: ?frontend.lower_mod.Output = null,
|
|
};
|
|
|
|
/// Checks a `.aji` module (frontend, then the AIR pipeline, with every
|
|
/// diagnostic at its source location) or a text AIR module.
|
|
pub fn checkFile(arena: std.mem.Allocator, file_name: []const u8, source: []const u8) !Report {
|
|
if (!std.mem.endsWith(u8, file_name, ".aji")) return checkAir(arena, file_name, source);
|
|
const lowered = switch (try frontend.compile(arena, file_name, source)) {
|
|
.ok => |o| o,
|
|
.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).
|
|
var mapped: std.ArrayList(diag.Diagnostic) = .empty;
|
|
for (r.diagnostics) |d| {
|
|
const m = try frontend.mapDiagnostic(arena, d, file_name, lowered.lines);
|
|
const dup = for (mapped.items) |prev| {
|
|
if (prev.code == m.code and prev.span.?.line == m.span.?.line and prev.span.?.col == m.span.?.col) break true;
|
|
} else false;
|
|
if (!dup) try mapped.append(arena, m);
|
|
}
|
|
r.diagnostics = mapped.items;
|
|
r.lowered = lowered;
|
|
return r;
|
|
}
|
|
|
|
/// Checks a text AIR module. All allocations go into `arena`.
|
|
pub fn checkAir(arena: std.mem.Allocator, file_name: []const u8, source: []const u8) !Report {
|
|
const parsed = try air.parse.parse(arena, file_name, source);
|
|
if (parsed.diagnostics.len != 0) return .{ .status = .rejected, .diagnostics = parsed.diagnostics };
|
|
const module = parsed.module.?;
|
|
const ds = try verifier.verify(arena, file_name, module);
|
|
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);
|
|
}
|