cli: validate structural source type positions before AIR

This commit is contained in:
dev committed 2026-10-07 23:54:55 +03:00
1 parent 5f76e92ff3
commit caf3b86e80
48 files changed
+423 -33

No files matched your search

+22 -8
View File
@@ -31,6 +31,7 @@ const authority = @import("../air/authority.zig");
const features_mod = @import("../air/features.zig");
const air_module = @import("../air/module.zig");
const verifier_types = @import("../verifier/types.zig");
const type_positions = @import("type_positions.zig");
const Allocator = std.mem.Allocator;
const Span = ast.Span;
@@ -462,7 +463,7 @@ const Lowerer = struct {
if (l.sigs.contains(f.name)) return l.invalid(.duplicate_declaration, f.span, "function defined twice", &.{.{ .name = "name", .value = f.name }});
const ps = try l.arena.alloc(Ty, f.params.len);
for (f.params, ps) |p, *t| t.* = try l.typeOf(p.ty);
const ret = try l.typeOf(f.ret);
const ret = try l.typeAt(f.ret, .ret);
var origin: ?usize = null;
if (f.from) |name| {
for (f.params, 0..) |param, i| if (std.mem.eql(u8, param.name, name)) {
@@ -669,12 +670,22 @@ const Lowerer = struct {
}
fn memberType(l: *Lowerer, t: ast.Type) Error!Ty {
const ty = try l.typeOf(t);
if (ty == .void or ty == .never) return l.fail("E7005", t.span, "a field or payload needs a value type", &.{});
return ty;
return l.typeAt(t, .member);
}
fn typeOf(l: *Lowerer, t: ast.Type) Error!Ty {
return l.typeAt(t, .param);
}
fn typeAt(l: *Lowerer, t: ast.Type, position: type_positions.Position) Error!Ty {
if (type_positions.firstInvalid(t, position)) |bad| return l.fail("E7005", bad.span, "type constructor not allowed in this position", &.{
.{ .name = "position", .value = @tagName(bad.position) },
.{ .name = "type_constructor", .value = bad.constructor },
});
return l.resolveType(t);
}
fn resolveType(l: *Lowerer, t: ast.Type) Error!Ty {
switch (t.kind) {
.applied => return l.fail("E7002", t.span, "unknown generic type", &.{}),
.function_ref => return l.invalid(.invalid_static_function, t.span, "static functions are not runtime types", &.{}),
@@ -692,12 +703,14 @@ const Lowerer = struct {
},
.array => |a| {
if (a.len == 0 or a.len > (@as(u64, 1) << 32)) return l.fail("E7005", t.span, "array length must be between 1 and 2^32", &.{});
return l.arrayOf(try l.memberType(a.elem.*), a.len);
const elem = try l.resolveType(a.elem.*);
if (elem == .void or elem == .never) return l.fail("E7005", a.elem.span, "an array element needs a value type", &.{});
return l.arrayOf(elem, a.len);
},
.wrapped => |w| {
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, buffer, task, sender, receiver, mutex, island, frozen, arc, iref, atomic };
const which = std.meta.stringToEnum(Wr, w.wrapper) orelse return l.notYet(t.span, "this type (later layers)");
const in = try l.typeOf(w.inner.*);
const in = try l.resolveType(w.inner.*);
return switch (which) {
.ref => l.wrap(.ref, in),
.mut => l.wrap(.mut, in),
@@ -1066,6 +1079,7 @@ const Lowerer = struct {
fn stmt(l: *Lowerer, s: ast.Stmt) Error!void {
switch (s.kind) {
.let => |x| {
const annotation: ?Ty = if (x.ty) |t| try l.typeOf(t) else null;
if (x.e.kind == .call) if (pathOf(x.e.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "struct.unpack") or std.mem.eql(u8, p, "array.unpack")) return l.unpackLet(s, x.names, x.e, x.tuple);
if (x.tuple and x.names.len == 1) return l.invalid(.invalid_binding, s.span, "a single parenthesized binding requires aggregate unpack", &.{});
if (x.names.len != 1) {
@@ -1079,7 +1093,7 @@ const Lowerer = struct {
try l.bindings.append(l.arena, .{ .name = x.names[0], .mutable = false, .value = st.name, .ty = st.ty, .kind = .storage });
return;
}
var want: ?Ty = if (x.ty) |t| try l.typeOf(t) else null;
var want: ?Ty = annotation;
// An untyped `let` takes its type from how the name is used
// later (`return`, arguments, operations, annotations).
if (want == null and l.peekType(x.e) == null) want = l.inferUses(x.names[0]);
@@ -3181,7 +3195,7 @@ const Lowerer = struct {
if (std.mem.eql(u8, path, "buffer.new")) {
try want(l, e.span, args, 2, path);
const source_ty = e.kind.call.type_arg orelse return l.fail("E7005", e.span, "buffer.new needs an explicit content type", &.{});
const ty = try l.typeOf(source_ty);
const ty = try l.memberType(source_ty);
const n = try l.expr(args[0], usize_ty);
try l.checkTy(args[0].span, n.ty, usize_ty);
const cap = try l.expectCap(args[1], "mem.alloc");