frontend: collect independent source diagnostics without cascades
This commit is contained in:
1 parent
2c6c7a3dcd
commit
4cda44823d
47 files changed
+697
-44
No files matched your search
+37
-12
@@ -7,7 +7,7 @@ const stdlib = @import("stdlib.zig");
|
||||
const authority = @import("../air/authority.zig");
|
||||
|
||||
pub const Source = stdlib.Source;
|
||||
pub const Result = union(enum) { ok: ast.Module, err: lower.Problem };
|
||||
pub const Result = union(enum) { ok: ast.Module, err: lower.Problem, errors: []const lower.Problem };
|
||||
const Error = error{ OutOfMemory, InvalidSource };
|
||||
const Node = struct { module: ast.Module, active: bool, height: usize = 0 };
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn link(arena: std.mem.Allocator, entry: ast.Module, sources: []const Source
|
||||
const span: ast.Span = if (entry.functions.len != 0) entry.functions[0].span else .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 1 } };
|
||||
const combined = g.run(entry, span) catch |err| switch (err) {
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
error.InvalidSource => return .{ .err = g.problem.? },
|
||||
error.InvalidSource => return if (g.problems.len > 1) .{ .errors = g.problems } else .{ .err = g.problem.? },
|
||||
};
|
||||
return .{ .ok = combined };
|
||||
}
|
||||
@@ -38,6 +38,22 @@ const Graph = struct {
|
||||
sources: []const Source,
|
||||
nodes: std.ArrayList(Node) = .empty,
|
||||
problem: ?lower.Problem = null,
|
||||
problems: []const lower.Problem = &.{},
|
||||
|
||||
fn syntaxFailures(g: *Graph, source: Source, failures: []const parse.Failure) Error {
|
||||
const internal = std.mem.startsWith(u8, source.name, "std.");
|
||||
const problems = try g.arena.alloc(lower.Problem, if (internal) 1 else failures.len);
|
||||
for (failures[0..problems.len], problems) |failure, *problem| problem.* = .{
|
||||
.code = if (internal) "E7900" else "E7001",
|
||||
.span = .{ .start = failure.pos, .end = failure.pos, .file = source.file, .namespace = source.name },
|
||||
.message = try std.fmt.allocPrint(g.arena, "expected {s}", .{failure.expected}),
|
||||
.fields = try g.arena.dupe(lower.Problem.Field, &.{ .{ .name = "expected", .value = failure.expected }, .{ .name = "found", .value = failure.found } }),
|
||||
.repair_candidates = if (internal) &.{} else failure.repair_candidates,
|
||||
};
|
||||
g.problem = problems[0];
|
||||
g.problems = problems;
|
||||
return error.InvalidSource;
|
||||
}
|
||||
|
||||
fn fail(g: *Graph, comptime code: []const u8, span: ast.Span, message: []const u8, name: []const u8) Error {
|
||||
if (comptime std.mem.eql(u8, code, "E7006")) @compileError("E7006 requires an explicit reason via invalid()");
|
||||
@@ -117,16 +133,8 @@ const Graph = struct {
|
||||
} else return g.fail("E7002", imp.span, "unknown module in explicit source mapping", imp.path);
|
||||
const m = switch (try parse.parseWithFile(g.arena, source.source, source.file)) {
|
||||
.ok => |m| m,
|
||||
.err => |failure| {
|
||||
g.problem = .{
|
||||
.code = if (std.mem.startsWith(u8, source.name, "std.")) "E7900" else "E7001",
|
||||
.span = .{ .start = failure.pos, .end = failure.pos, .file = source.file, .namespace = source.name },
|
||||
.message = try std.fmt.allocPrint(g.arena, "expected {s}", .{failure.expected}),
|
||||
.fields = try g.arena.dupe(lower.Problem.Field, &.{ .{ .name = "expected", .value = failure.expected }, .{ .name = "found", .value = failure.found } }),
|
||||
.repair_candidates = if (std.mem.startsWith(u8, source.name, "std.")) &.{} else failure.repair_candidates,
|
||||
};
|
||||
return error.InvalidSource;
|
||||
},
|
||||
.err => |failure| return g.syntaxFailures(source, &.{failure}),
|
||||
.errors => |failures| return g.syntaxFailures(source, failures),
|
||||
};
|
||||
if (!std.mem.eql(u8, m.name, source.name)) return g.invalid(.module_header_mismatch, .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 1 }, .file = source.file, .namespace = source.name }, "module header differs from its mapped name", source.name);
|
||||
const index = g.nodes.items.len;
|
||||
@@ -157,3 +165,20 @@ fn used(types: []const ast.TypeDecl, functions: []const ast.FnDecl, name: []cons
|
||||
for (functions) |f| if (std.mem.eql(u8, f.air_name orelse f.name, name)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
test "std syntax lists become exactly one internal error without advice" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const failures: []const parse.Failure = &.{
|
||||
.{ .pos = .{ .line = 2, .col = 3 }, .expected = "an expression", .found = "!", .repair_candidates = @import("../diag.zig").SourceRepairKind.one(.boolean_negation) },
|
||||
.{ .pos = .{ .line = 3, .col = 4 }, .expected = "an expression", .found = "!", .repair_candidates = @import("../diag.zig").SourceRepairKind.one(.boolean_negation) },
|
||||
};
|
||||
var g: Graph = .{ .arena = a, .sources = &.{} };
|
||||
try std.testing.expectEqual(error.InvalidSource, g.syntaxFailures(.{ .name = "std.bytes", .file = "<std.bytes>", .source = "" }, failures));
|
||||
try std.testing.expectEqual(@as(usize, 1), g.problems.len);
|
||||
try std.testing.expectEqualStrings("E7900", g.problem.?.code);
|
||||
try std.testing.expectEqualStrings("<std.bytes>", g.problem.?.span.file.?);
|
||||
try std.testing.expectEqual(failures[0].pos, g.problem.?.span.start);
|
||||
try std.testing.expectEqual(@as(usize, 0), g.problem.?.repair_candidates.len);
|
||||
}
|
||||
Reference in new issue
Block a user