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

+19 -7
View File
@@ -2,6 +2,7 @@
const std = @import("std");
const ast = @import("ast.zig");
const lower = @import("lower.zig");
const type_positions = @import("type_positions.zig");
const authority = @import("../air/authority.zig");
const A = std.mem.Allocator;
const Error = error{ OutOfMemory, InvalidGeneric };
@@ -162,8 +163,8 @@ const Generator = struct {
for (params[0..i]) |previous| if (std.mem.eql(u8, p.name, previous.name)) return g.invalid(.duplicate_declaration, p.span, "duplicate type parameter", p.name);
if (p.callable) |c| {
if (p.constraints.len != 0 or g.function(p.name, p.span) != null) return g.invalid(.invalid_static_function, p.span, "invalid static function parameter", p.name);
for (c.params) |t| try g.headerType(t, params, 0);
try g.headerType(c.ret, params, 0);
for (c.params) |t| try g.headerAt(t, params, 0, .param);
try g.headerAt(c.ret, params, 0, .ret);
if (c.ret.kind == .wrapped) {
const w = c.ret.kind.wrapped.wrapper;
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);
@@ -180,6 +181,17 @@ const Generator = struct {
}
}
fn headerAt(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize, position: type_positions.Position) Error!void {
if (type_positions.firstInvalid(t, position)) |bad| {
g.problem = .{ .code = "E7005", .span = bad.span, .message = "type constructor not allowed in this position", .fields = try g.arena.dupe(lower.Problem.Field, &.{
.{ .name = "position", .value = @tagName(bad.position) },
.{ .name = "type_constructor", .value = bad.constructor },
}) };
return error.InvalidGeneric;
}
return g.headerType(t, params, depth);
}
fn headerType(g: *Generator, t: ast.Type, params: []const ast.TypeParam, depth: usize) Error!void {
if (depth >= 64) return g.invalid(.implementation_limit, t.span, "type traversal exceeds depth 64", "type");
switch (t.kind) {
@@ -212,7 +224,7 @@ const Generator = struct {
if (depth >= 64) return g.invalid(.implementation_limit, arg.span, "generic argument traversal exceeds depth 64", expected.name);
if (expected.callable == null) {
if (arg.kind == .function_ref) return g.fail("E7005", arg.span, "expected a type argument, found a static function", expected.name);
return g.headerType(arg, params, depth);
return g.headerAt(arg, params, depth, .param);
}
if (arg.kind != .function_ref) return g.fail("E7005", arg.span, "expected an explicit static function argument", expected.name);
const f = arg.kind.function_ref;
@@ -266,8 +278,8 @@ const Generator = struct {
},
}
if (t.generics.len != 0) switch (t.kind) {
.@"struct" => |fs| for (fs) |field| try g.headerType(field.ty, t.generics, 0),
.@"enum" => |vs| for (vs) |v| for (v.payload) |payload_type| try g.headerType(payload_type, t.generics, 0),
.@"struct" => |fs| for (fs) |field| try g.headerAt(field.ty, t.generics, 0, .member),
.@"enum" => |vs| for (vs) |v| for (v.payload) |payload_type| try g.headerAt(payload_type, t.generics, 0, .member),
};
generic = generic or t.generics.len != 0;
}
@@ -281,9 +293,9 @@ const Generator = struct {
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);
try g.headerType(p.ty, f.generics, 0);
try g.headerAt(p.ty, f.generics, 0, .param);
}
try g.headerType(f.ret, f.generics, 0);
try g.headerAt(f.ret, f.generics, 0, .ret);
try g.borrowReturnContract(f);
}
generic = generic or f.generics.len != 0;
+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");
+35
View File
@@ -0,0 +1,35 @@
//! Structural source type positions, using the frozen AIR predicate.
//! Unknown generic names remain unknown; graph-content properties stay in
//! the verifier. Traversal follows syntax, never nominal type definitions.
const std = @import("std");
const ast = @import("ast.zig");
const air = @import("../air/module.zig");
const types = @import("../verifier/types.zig");
pub const Position = types.Position;
pub const Failure = struct { span: ast.Span, position: Position, constructor: []const u8 };
pub fn firstInvalid(annotation: ast.Type, position: Position) ?Failure {
var t = annotation;
var pos = position;
while (true) {
const tag: std.meta.Tag(air.Type) = switch (t.kind) {
.name => |n| if (std.mem.eql(u8, n, "void")) .void else if (std.mem.eql(u8, n, "never")) .never else .named,
.array => .array,
.wrapped => |w| std.meta.stringToEnum(std.meta.Tag(air.Type), w.wrapper) orelse return null,
.cap => .cap,
.file => .resource,
.applied, .function_ref => return null, // Resolved by specialization/name checks.
};
// Published source linear aggregates permit complete linear members;
// their feature closure and property checks remain independently verified.
if (!types.allowedPosition(tag, pos, true)) return .{ .span = t.span, .position = pos, .constructor = @tagName(tag) };
if (tag == .island or tag == .frozen or tag == .arc) return null;
pos = types.innerPosition(tag);
t = switch (t.kind) {
.array => |a| a.elem.*,
.wrapped => |w| w.inner.*,
else => return null,
};
}
}
+22 -13
View File
@@ -90,7 +90,26 @@ fn visitValue(c: *Context, state: anytype, t: Type) std.mem.Allocator.Error!void
}
}
const Position = enum { member, param, ret, block_param, borrowed };
pub const Position = enum { member, param, ret, block_param, borrowed };
/// Shared with source annotation checking; no state or diagnostics.
pub fn allowedPosition(tag: std.meta.Tag(Type), pos: Position, linear_aggregates: bool) bool {
return switch (tag) {
.void, .never => pos == .ret,
.local => false,
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
.scope, .task, .shield, .guard => pos != .member,
.resource, .own, .own_array, .buffer, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or linear_aggregates,
else => true,
};
}
pub fn innerPosition(tag: std.meta.Tag(Type)) Position {
return switch (tag) {
.ref, .mut, .task, .sender, .receiver, .mutex, .guard => .borrowed,
else => .member,
};
}
/// Where a type may appear (spec 07 §8.3, §8.8):
/// - `void`/`never` only as return types;
@@ -99,14 +118,7 @@ const Position = enum { member, param, ret, block_param, borrowed };
/// never as members (fields, payloads, elements, locals);
/// - the inner type of every compound type is a value type.
fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []const u8) !void {
const ok = switch (t) {
.void, .never => pos == .ret,
.local => false,
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
.scope, .task, .shield, .guard => pos != .member,
.resource, .own, .own_array, .buffer, .region, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => pos != .member or c.module.declares("linear_aggregates"),
else => true,
};
const ok = allowedPosition(std.meta.activeTag(t), pos, c.module.declares("linear_aggregates"));
if (t == .own_array and c.module.declares("linear_aggregates") and c.module.isLinearType(t.own_array.*)) try c.report("E1001", span, "dynamic heap arrays require Copy content", &.{});
// An atomic cell holds an AtomicScalar (spec 04 §23).
if (t == .atomic and !isAtomicScalar(t.atomic.*)) try c.report("E1001", span, "atomic<T> needs bool or an integer of at most 64 bits", &.{
@@ -131,10 +143,7 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
// a task may return one; every other compound holds value types only.
// Channel elements and mutex contents may be linear (spec 04 §38.1);
// their remaining rules are the element rules of `checkElement`.
if (t.inner()) |inner| try wellFormed(c, span, inner, switch (t) {
.ref, .mut, .task, .sender, .receiver, .mutex, .guard => .borrowed,
else => .member,
}, owner);
if (t.inner()) |inner| try wellFormed(c, span, inner, innerPosition(std.meta.activeTag(t)), owner);
}
/// Validate a phantom or used source generic argument by ordinary type rules.