frontend: validate unknown effect names before body lowering

This commit is contained in:
dev committed 2026-10-08 14:49:49 +03:00
1 parent 4c9c5626c9
commit 762c5abe7b
40 files changed
+335 -9

No files matched your search

+1 -1
View File
@@ -67,7 +67,7 @@ pub const registry = [_]Code{
.{ .id = "E6001", .kind = "unsafe.required", .meaning = "Operation requires explicit unsafe boundary; field `reason`: `outside_boundary`, `trusted_declares_unsafe`" },
.{ .id = "E6101", .kind = "abi.unaligned_reference", .meaning = "Normal reference requested from insufficiently aligned storage" },
.{ .id = "E7001", .kind = "source.syntax", .meaning = "Malformed `.aji` source" },
.{ .id = "E7002", .kind = "source.unknown_name", .meaning = "Undefined variable, function, type, field or variant" },
.{ .id = "E7002", .kind = "source.unknown_name", .meaning = "Undefined variable, function, type, field, variant or effect" },
.{ .id = "E7003", .kind = "source.missing_move", .meaning = "Named linear value consumed without `move`" },
.{ .id = "E7004", .kind = "source.not_exhaustive", .meaning = "`match` misses an outcome, variant or default" },
.{ .id = "E7005", .kind = "source.type_mismatch", .meaning = "Expression has the wrong type in a source position" },
+30
View File
@@ -187,3 +187,33 @@ test canonicalBytes {
defer a.deinit();
try std.testing.expectEqualStrings("b\"hi\\n\\\"\\x01\"", try canonicalBytes(a.allocator(), "hi\n\"\x01"));
}
test "unknown effect names are source errors before AIR in ordinary and template contracts" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
for ([_][]const u8{
"fn bad() -> void effects [bogus.effect] {}",
"fn bad<T>(x: T) -> T effects [bogus.effect] { return x; }",
"fn bad<F: fn(u64) -> u64 effects [bogus.effect]>() -> void effects [] {}",
"struct Bad<F: fn(u64) -> u64 effects [bogus.effect]> { value: u64 }",
}, 0..) |declaration, i| {
const source = try std.fmt.allocPrint(a, "module test.effects\n{s}\nfn main() -> u64 effects [] {{ return 42; }}", .{declaration});
const r = try compile(a, "effects.aji", source);
try std.testing.expect(r == .err);
try std.testing.expectEqualStrings("E7002", r.err.code.id);
try std.testing.expectEqualStrings("effects.aji", r.err.span.?.file);
try std.testing.expectEqual(@as(u32, 2), r.err.span.?.line);
const col: u32 = if (i == 2) 8 else if (i == 3) 12 else 1;
try std.testing.expectEqual(col, r.err.span.?.col);
try std.testing.expectEqual(@as(usize, 2), r.err.fields.len);
for (r.err.fields) |field| {
try std.testing.expect(std.mem.eql(u8, field.name, "name") or std.mem.eql(u8, field.name, "effect"));
try std.testing.expectEqualStrings("bogus.effect", field.value.str);
}
}
for (@import("air/authority.zig").effects) |effect| {
const source = try std.fmt.allocPrint(a, "module test.effects\nfn unused() -> void effects [{s}] {{}}\nfn main() -> u64 effects [] {{ return 42; }}", .{effect.name});
try std.testing.expect((try compile(a, "effects.aji", source)) == .ok);
}
}
+10 -2
View File
@@ -35,6 +35,14 @@ const Generator = struct {
return error.InvalidGeneric;
}
fn unknownEffect(g: *Generator, span: ast.Span, effect: []const u8) Error {
g.problem = .{ .code = "E7002", .span = span, .message = "unknown effect", .fields = try g.arena.dupe(lower.Problem.Field, &.{
.{ .name = "name", .value = effect },
.{ .name = "effect", .value = effect },
}) };
return error.InvalidGeneric;
}
fn invalid(g: *Generator, reason: lower.InvalidReason, span: ast.Span, message: []const u8, name: []const u8) Error {
g.problem = .{ .code = "E7006", .reason = reason, .span = span, .message = message, .fields = try g.arena.dupe(lower.Problem.Field, &.{.{ .name = "name", .value = name }}) };
return error.InvalidGeneric;
@@ -171,7 +179,7 @@ const Generator = struct {
if (std.mem.eql(u8, w, "ref") or std.mem.eql(u8, w, "mut") or std.mem.eql(u8, w, "slice") or std.mem.eql(u8, w, "slice_mut")) return g.invalid(.invalid_static_function, c.ret.span, "static function contracts do not support borrowed returns", p.name);
}
for (c.effects, 0..) |effect, j| {
if (authority.effectIndex(effect) == null) return g.fail("E0006", p.span, "unknown effect in static function contract", effect);
if (authority.effectIndex(effect) == null) return g.unknownEffect(p.span, effect);
for (c.effects[0..j]) |prev| if (std.mem.eql(u8, prev, effect)) return g.invalid(.invalid_static_function, p.span, "duplicate effect in static function contract", effect);
}
}
@@ -301,7 +309,7 @@ const Generator = struct {
try g.validate(f.name, f.generics, f.span);
if (f.generics.len != 0) {
if (f.trusted != null) return g.invalid(.trusted_unavailable, f.span, "trusted is not available in source templates", f.name);
for (f.effects) |effect| if (authority.effectIndex(effect) == null) return g.fail("E0006", f.span, "unknown effect in template contract", effect);
for (f.effects) |effect| if (authority.effectIndex(effect) == null) return g.unknownEffect(f.span, effect);
for (f.params, 0..) |p, j| {
try g.checkBinding(p.name, p.span, .{ .params = f.generics });
for (f.params[0..j]) |prev| if (std.mem.eql(u8, p.name, prev.name)) return g.invalid(.duplicate_declaration, p.span, "duplicate value parameter", p.name);
+4
View File
@@ -466,6 +466,10 @@ const Lowerer = struct {
if (ret.isMutBorrow() and !ps[i].isMutBorrow()) return l.fail("E7005", f.span, "an exclusive return requires an exclusive `from` parameter", &.{});
} else if (ret.isBorrow()) return l.invalid(.invalid_borrow_return, f.span, "a borrow return requires `from parameter`", &.{});
if (std.mem.eql(u8, f.name, "main") and ret.isBorrow()) return l.invalid(.invalid_borrow_return, f.span, "main cannot return a borrow", &.{});
for (f.effects) |effect| if (authority.effectIndex(effect) == null) return l.fail("E7002", f.span, "unknown effect", &.{
.{ .name = "name", .value = effect },
.{ .name = "effect", .value = effect },
});
try l.sigs.put(l.arena, f.name, .{ .params = ps, .ret = ret, .from = origin, .air_name = f.air_name orelse f.name, .display_name = f.display_name orelse f.name });
}
if (!l.sigs.contains("main")) return l.fail("E7002", .{ .start = .{ .line = 1, .col = 1 }, .end = .{ .line = 1, .col = 1 } }, "the module has no `main` function", &.{.{ .name = "name", .value = "main" }});