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
+142
-2
@@ -175,6 +175,7 @@ pub const Result = union(enum) {
|
||||
ok: Output,
|
||||
/// A source problem (`E7xxx`).
|
||||
err: Problem,
|
||||
errors: []const Problem,
|
||||
/// A construct of a layer that is not implemented yet.
|
||||
unimplemented: struct { span: Span, construct: []const u8 },
|
||||
};
|
||||
@@ -184,7 +185,7 @@ const Error = error{ Lower, Unimplemented, OutOfMemory };
|
||||
pub fn lower(arena: Allocator, m: ast.Module) Allocator.Error!Result {
|
||||
var l: Lowerer = .{ .arena = arena, .m = m };
|
||||
l.run() catch |e| switch (e) {
|
||||
error.Lower => return .{ .err = l.problem.? },
|
||||
error.Lower => return if (l.problems.items.len > 1) .{ .errors = l.problems.items } else .{ .err = if (l.problems.items.len == 1) l.problems.items[0] else l.problem.? },
|
||||
error.Unimplemented => return .{ .unimplemented = l.unimpl.? },
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
};
|
||||
@@ -286,6 +287,7 @@ const Lowerer = struct {
|
||||
/// Set while a byte string is materialized: the tag for its instructions.
|
||||
in_constant: u32 = 0,
|
||||
problem: ?Problem = null,
|
||||
problems: std.ArrayList(Problem) = .empty,
|
||||
unimpl: ?@FieldType(Result, "unimplemented") = null,
|
||||
sigs: std.StringHashMapUnmanaged(FnSig) = .empty,
|
||||
/// `@Name` of each declared type.
|
||||
@@ -493,7 +495,43 @@ const Lowerer = struct {
|
||||
var type_lines: std.ArrayList(Inst) = .empty;
|
||||
for (l.m.types) |t| try type_lines.append(l.arena, .{ .text = try l.typeDeclText(t), .span = t.span });
|
||||
try l.checkRequirements();
|
||||
for (l.m.functions) |f| try l.fns.append(l.arena, try l.function(f));
|
||||
var failed_declarations: std.ArrayList(Span) = .empty;
|
||||
for (l.m.functions) |f| {
|
||||
const failed = for (failed_declarations.items) |span| {
|
||||
if (sameDeclaration(span, f.span)) break true;
|
||||
} else false;
|
||||
if (failed) continue;
|
||||
// Bodies read only validated signatures/declarations. Restore all
|
||||
// module accumulations on failure, including byte-string constants,
|
||||
// features and linearity flags; no failed FnOut is ever appended.
|
||||
const checkpoint = .{ .uses = l.uses, .linear_enabled = l.linear_enabled, .constants_len = l.constants.items.len };
|
||||
const output = l.function(f) catch |err| switch (err) {
|
||||
error.OutOfMemory => return error.OutOfMemory,
|
||||
error.Unimplemented => {
|
||||
if (l.problems.items.len == 0) return error.Unimplemented;
|
||||
l.uses = checkpoint.uses;
|
||||
l.linear_enabled = checkpoint.linear_enabled;
|
||||
l.constants.items.len = checkpoint.constants_len;
|
||||
l.in_constant = 0;
|
||||
l.unimpl = null;
|
||||
continue;
|
||||
},
|
||||
error.Lower => {
|
||||
const problem = l.problem.?;
|
||||
l.uses = checkpoint.uses;
|
||||
l.linear_enabled = checkpoint.linear_enabled;
|
||||
l.constants.items.len = checkpoint.constants_len;
|
||||
l.in_constant = 0;
|
||||
l.problem = null;
|
||||
try l.problems.append(l.arena, problem);
|
||||
try failed_declarations.append(l.arena, f.span);
|
||||
if (l.problems.items.len == @import("../diag.zig").source_diagnostic_limit) return error.Lower;
|
||||
continue;
|
||||
},
|
||||
};
|
||||
try l.fns.append(l.arena, output);
|
||||
}
|
||||
if (l.problems.items.len != 0) return error.Lower;
|
||||
|
||||
var features: std.ArrayList(u8) = .empty;
|
||||
const roots = try l.featureRoots(type_lines.items);
|
||||
@@ -809,6 +847,16 @@ const Lowerer = struct {
|
||||
l.allocs = .empty;
|
||||
l.facts = .empty;
|
||||
l.addressable = .empty;
|
||||
l.cur = null;
|
||||
l.rest = .empty;
|
||||
l.pending_loop = null;
|
||||
l.pending_bindings = .empty;
|
||||
l.pending_ends = .empty;
|
||||
l.unchecked = false;
|
||||
l.in_constant = 0;
|
||||
l.scope_binding_start = 0;
|
||||
l.problem = null;
|
||||
l.unimpl = null;
|
||||
l.counter = 0;
|
||||
l.params = f.params;
|
||||
try l.findAddressable(f.body);
|
||||
@@ -3625,6 +3673,12 @@ const Lowerer = struct {
|
||||
}
|
||||
};
|
||||
|
||||
fn sameDeclaration(a: Span, b: Span) bool {
|
||||
return a.start.line == b.start.line and a.start.col == b.start.col and
|
||||
std.mem.eql(u8, a.file orelse "", b.file orelse "") and
|
||||
std.mem.eql(u8, a.namespace orelse "", b.namespace orelse "");
|
||||
}
|
||||
|
||||
fn elemOf(t: Ty) ?Ty {
|
||||
return switch (t) {
|
||||
.array => |a| a.elem,
|
||||
@@ -4083,3 +4137,89 @@ test "cyclic source diamonds reject before property construction" {
|
||||
try std.testing.expectEqual(@as(u32, 2), l.problem.?.span.start.line);
|
||||
try std.testing.expect(l.property_module == null);
|
||||
}
|
||||
|
||||
test "failed body discards constants and features and never emits partial AIR" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const source =
|
||||
\\module collection
|
||||
\\fn good() -> void effects [] { let text = b"good"; }
|
||||
\\fn bad() -> void effects [unsafe] { let text = b"bad"; let x = missing; }
|
||||
\\fn main() -> void effects [] {}
|
||||
;
|
||||
const m = (try parse_mod.parse(a, source)).ok;
|
||||
var l: Lowerer = .{ .arena = a, .m = m };
|
||||
try std.testing.expectError(error.Lower, l.run());
|
||||
try std.testing.expectEqual(@as(usize, 1), l.problems.items.len);
|
||||
try std.testing.expectEqualStrings("E7002", l.problems.items[0].code);
|
||||
try std.testing.expectEqual(@as(usize, 2), l.fns.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 1), l.constants.items.len);
|
||||
try std.testing.expectEqualStrings("good", l.constants.items[0].bytes);
|
||||
try std.testing.expect(!l.uses.contains(.unsafe));
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.annotated.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.lines.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.constant_of.items.len);
|
||||
}
|
||||
|
||||
test "later function output matches fresh lowering after failed borrow and scope bodies" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const stable =
|
||||
\\fn stable() -> u64 effects [] {
|
||||
\\ let inferred = 7;
|
||||
\\ let text = b"ok";
|
||||
\\ var bytes: [u8; 4] = zero;
|
||||
\\ var total: u64 = inferred;
|
||||
\\ for i in 0usize..2usize { total = total + (bytes[i] as u64); }
|
||||
\\ return total + (text.len as! u64);
|
||||
\\}
|
||||
\\fn main() -> void effects [] {}
|
||||
;
|
||||
const baseline_source = try std.fmt.allocPrint(a, "module collection\n{s}", .{stable});
|
||||
const baseline_module = (try parse_mod.parse(a, baseline_source)).ok;
|
||||
var baseline: Lowerer = .{ .arena = a, .m = baseline_module };
|
||||
try baseline.run();
|
||||
var baseline_emission: Lowerer = .{ .arena = a, .m = baseline_module };
|
||||
try baseline_emission.emitFunction(baseline.fns.items[0]);
|
||||
for ([_][]const u8{
|
||||
"fn bad() -> void effects [] { let shadow = 1u64; let x = missing; }",
|
||||
"fn bad() -> void effects [] { var bytes: [u8; 4] = zero; ref unchecked view = bytes[missing]; }",
|
||||
"fn bad() -> void effects [unsafe] { scope s { let text = b\"bad\"; loop { let x = missing; } } }",
|
||||
}) |bad| {
|
||||
const source = try std.fmt.allocPrint(a, "module collection\n{s}\n{s}", .{ bad, stable });
|
||||
const m = (try parse_mod.parse(a, source)).ok;
|
||||
var recovered: Lowerer = .{ .arena = a, .m = m };
|
||||
try std.testing.expectError(error.Lower, recovered.run());
|
||||
try std.testing.expectEqual(@as(usize, 1), recovered.problems.items.len);
|
||||
try std.testing.expectEqualStrings("E7002", recovered.problems.items[0].code);
|
||||
try std.testing.expectEqual(@as(usize, 2), recovered.fns.items.len);
|
||||
try std.testing.expectEqual(@as(usize, 0), recovered.out.items.len);
|
||||
var emission: Lowerer = .{ .arena = a, .m = m };
|
||||
try emission.emitFunction(recovered.fns.items[0]);
|
||||
try std.testing.expectEqualStrings(baseline_emission.out.items, emission.out.items);
|
||||
try std.testing.expectEqualSlices(u32, baseline_emission.constant_of.items, emission.constant_of.items);
|
||||
try std.testing.expectEqual(@as(usize, 1), recovered.constants.items.len);
|
||||
try std.testing.expectEqualStrings("ok", recovered.constants.items[0].bytes);
|
||||
}
|
||||
}
|
||||
|
||||
test "defensive nonmaterializable moves retain invalid_move even in generic transfers" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const span: Span = .{ .start = .{ .line = 2, .col = 3 }, .end = .{ .line = 2, .col = 9 } };
|
||||
// Annotation/binding checks normally reject these values earlier. Keep
|
||||
// the lower-level guard stable without inventing accepted void parameters.
|
||||
for ([_]Ty{ .void, .never }) |ty| for ([_]bool{ false, true }) |generic| {
|
||||
var l: Lowerer = .{ .arena = a, .m = .{ .name = "defensive", .types = &.{}, .functions = &.{} } };
|
||||
try l.bindings.append(a, .{ .name = "value", .value = "v", .ty = ty, .mutable = false });
|
||||
try std.testing.expectError(error.Lower, l.expr(.{ .span = span, .generic_transfer = generic, .kind = .{ .move = "value" } }, null));
|
||||
try std.testing.expectEqualStrings("E7006", l.problem.?.code);
|
||||
try std.testing.expectEqual(InvalidReason.invalid_move, l.problem.?.reason.?);
|
||||
try std.testing.expectEqual(span, l.problem.?.span);
|
||||
try std.testing.expectEqual(@as(usize, 0), l.out.items.len);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user