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
+50
-12
@@ -18,8 +18,10 @@ pub const Span = ast.Span;
|
||||
pub const Compiled = union(enum) {
|
||||
/// Canonical AIR, its line map and the annotated form.
|
||||
ok: lower_mod.Output,
|
||||
/// One source diagnostic (`E7xxx`): the first problem only.
|
||||
/// A single source diagnostic; declaration-wide failures stop the phase.
|
||||
err: diag.Diagnostic,
|
||||
/// Independent source diagnostics; no partial AIR is exposed.
|
||||
errors: []const diag.Diagnostic,
|
||||
unimplemented: []const u8,
|
||||
};
|
||||
|
||||
@@ -32,6 +34,7 @@ pub fn compileWithModules(arena: std.mem.Allocator, file: []const u8, source: []
|
||||
const m = switch (parsed) {
|
||||
.ok => |m| m,
|
||||
.err => |f| return .{ .err = try syntaxDiagnostic(arena, file, f) },
|
||||
.errors => |fs| return diagnosticResult(try syntaxDiagnostics(arena, file, fs)),
|
||||
};
|
||||
const linked = try modules.link(arena, m, sources);
|
||||
const result = switch (linked) {
|
||||
@@ -40,25 +43,60 @@ pub fn compileWithModules(arena: std.mem.Allocator, file: []const u8, source: []
|
||||
.err => |p| lower_mod.Result{ .err = p },
|
||||
},
|
||||
.err => |p| lower_mod.Result{ .err = p },
|
||||
.errors => |ps| lower_mod.Result{ .errors = ps },
|
||||
};
|
||||
return switch (result) {
|
||||
.ok => |o| .{ .ok = o },
|
||||
.err => |p| blk: {
|
||||
const fields = try arena.alloc(diag.Field, p.fields.len + @intFromBool(p.reason != null));
|
||||
for (p.fields, fields[0..p.fields.len]) |pf, *f| f.* = .{ .name = pf.name, .value = .{ .str = pf.value } };
|
||||
if (p.reason) |reason| fields[p.fields.len] = .{ .name = "reason", .value = .{ .str = @tagName(reason) } };
|
||||
break :blk .{ .err = .{
|
||||
.code = diag.lookup(p.code).?,
|
||||
.span = .{ .file = p.span.file orelse file, .line = p.span.start.line, .col = p.span.start.col },
|
||||
.message = p.message,
|
||||
.fields = fields,
|
||||
.repair_candidates = p.repair_candidates,
|
||||
} };
|
||||
.err => |p| .{ .err = try problemDiagnostic(arena, file, p) },
|
||||
.errors => |ps| blk: {
|
||||
const ds = try arena.alloc(diag.Diagnostic, ps.len);
|
||||
for (ps, ds) |p, *d| d.* = try problemDiagnostic(arena, file, p);
|
||||
break :blk diagnosticResult(deduplicate(ds));
|
||||
},
|
||||
.unimplemented => |u| .{ .unimplemented = try std.fmt.allocPrint(arena, "{s}:{d}:{d}: {s}", .{ u.span.file orelse file, u.span.start.line, u.span.start.col, u.construct }) },
|
||||
};
|
||||
}
|
||||
|
||||
fn problemDiagnostic(arena: std.mem.Allocator, file: []const u8, p: lower_mod.Problem) std.mem.Allocator.Error!diag.Diagnostic {
|
||||
const fields = try arena.alloc(diag.Field, p.fields.len + @intFromBool(p.reason != null));
|
||||
for (p.fields, fields[0..p.fields.len]) |pf, *f| f.* = .{ .name = pf.name, .value = .{ .str = pf.value } };
|
||||
if (p.reason) |reason| fields[p.fields.len] = .{ .name = "reason", .value = .{ .str = @tagName(reason) } };
|
||||
return .{
|
||||
.code = diag.lookup(p.code).?,
|
||||
.span = .{ .file = p.span.file orelse file, .line = p.span.start.line, .col = p.span.start.col },
|
||||
.message = p.message,
|
||||
.fields = fields,
|
||||
.repair_candidates = if (std.mem.eql(u8, p.code, "E7900")) &.{} else p.repair_candidates,
|
||||
};
|
||||
}
|
||||
|
||||
fn diagnosticResult(ds: []const diag.Diagnostic) Compiled {
|
||||
for (ds) |d| if (std.mem.eql(u8, d.code.id, "E7900")) return .{ .err = d };
|
||||
return if (ds.len == 1) .{ .err = ds[0] } else .{ .errors = ds };
|
||||
}
|
||||
|
||||
fn deduplicate(ds: []diag.Diagnostic) []const diag.Diagnostic {
|
||||
var count: usize = 0;
|
||||
for (ds) |d| {
|
||||
const duplicate = for (ds[0..count]) |prior| {
|
||||
const a = d.span.?;
|
||||
const b = prior.span.?;
|
||||
if (d.code == prior.code and a.line == b.line and a.col == b.col and std.mem.eql(u8, a.file, b.file)) break true;
|
||||
} else false;
|
||||
if (!duplicate) {
|
||||
ds[count] = d;
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
return ds[0..count];
|
||||
}
|
||||
|
||||
pub fn syntaxDiagnostics(arena: std.mem.Allocator, file: []const u8, failures: []const parse.Failure) std.mem.Allocator.Error![]const diag.Diagnostic {
|
||||
const ds = try arena.alloc(diag.Diagnostic, failures.len);
|
||||
for (failures, ds) |f, *d| d.* = try syntaxDiagnostic(arena, file, f);
|
||||
return deduplicate(ds);
|
||||
}
|
||||
|
||||
pub fn syntaxDiagnostic(arena: std.mem.Allocator, file: []const u8, f: parse.Failure) std.mem.Allocator.Error!diag.Diagnostic {
|
||||
return .{
|
||||
.code = diag.lookup("E7001").?,
|
||||
|
||||
Reference in new issue
Block a user