cli: add explicit source generics and Copy option/result

Specialize explicit function, struct and enum templates into deterministic
ordinary AIR, with structural constraints and bounded expansion. Preserve
source provenance and readable names in specialization diagnostics.

Add Copy-only std.option/std.result and vertical specification, grammar,
Quick Reference, RFC and source coverage updates. Keep linear aggregates
and existing unsupported source from-returns explicit follow-up work.

Repair the finite-layout check through arrays under the existing E1003
contract, with positive and rejected source/AIR fixtures.

Validation: build, unit tests, 640 conformance cases, 82/82 source constructs,
32-seed generic task stress, agent-reference, formatting, hosted smoke,
20 generic and 36 package CLI checks. Record three-encoding documentation
budgets; AIR reference is unchanged.
This commit is contained in:
dev committed 2026-10-06 19:53:26 +03:00
1 parent c96199e7ec
commit 7eea31518d
93 files changed
+1587 -75

No files matched your search

+56 -4
View File
@@ -50,13 +50,18 @@ fn checkDeclarations(c: *Context) !void {
}
}
// E1003: an aggregate that contains itself by value has no finite size.
try checkFiniteLayouts(c);
}
/// E1003 applies through arrays as well as direct nominal members.
pub fn checkFiniteLayouts(c: *Context) !void {
const m = c.module;
const state = try c.arena.alloc(enum { white, grey, black }, m.types.len);
@memset(state, .white);
for (0..m.types.len) |i| try visitDecl(c, state, @intCast(i));
}
fn visitDecl(c: *Context, state: anytype, i: u32) !void {
fn visitDecl(c: *Context, state: anytype, i: u32) std.mem.Allocator.Error!void {
if (state[i] == .black) return;
if (state[i] == .grey) {
const d = c.module.types[i];
@@ -66,12 +71,20 @@ fn visitDecl(c: *Context, state: anytype, i: u32) !void {
}
state[i] = .grey;
switch (c.module.types[i].kind) {
.@"struct" => |fs| for (fs) |f| if (f.ty == .named) try visitDecl(c, state, f.ty.named),
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| if (t == .named) try visitDecl(c, state, t.named),
.@"struct" => |fs| for (fs) |f| try visitValue(c, state, f.ty),
.@"enum" => |vs| for (vs) |v| for (v.payload) |t| try visitValue(c, state, t),
}
state[i] = .black;
}
fn visitValue(c: *Context, state: anytype, t: Type) std.mem.Allocator.Error!void {
switch (t) {
.named => |i| try visitDecl(c, state, i),
.array => |a| try visitValue(c, state, a.elem),
else => {},
}
}
const Position = enum { member, param, ret, block_param, borrowed };
/// Where a type may appear (spec 07 §8.3, §8.8):
@@ -117,6 +130,16 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
}, owner);
}
/// Validate a phantom or used source generic argument by ordinary type rules.
pub fn checkTypeArgument(c: *Context, span: air.Span, t: Type) !void {
try wellFormed(c, span, t, .param, "generic argument");
try argumentLengths(c, span, t);
}
fn argumentLengths(c: *Context, span: air.Span, t: Type) !void {
if (t == .array and (t.array.len == 0 or t.array.len > (1 << 32))) try c.report("E1001", span, "array length outside format-0 range", &.{});
if (t.inner()) |inner| try argumentLengths(c, span, inner);
}
/// `local.alloc` results are checked separately: their own type is storage.
fn zeroable(m: air.Module, t: Type) bool {
return switch (t) {
@@ -338,6 +361,35 @@ fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")
}
}
/// Structural Copy query for source constraints; borrows remain second-class.
pub fn isCopy(m: air.Module, t: Type) bool {
return switch (t) {
.int, .bool, .builtin, .cap, .iref => true,
.array => |a| isCopy(m, a.elem),
.named => |i| aggregateAll(m, i, isCopy),
else => false,
};
}
/// Local is context-bound state, not the complement of Send (spec 15 §15).
pub fn isLocalType(m: air.Module, t: Type) bool {
return switch (t) {
.local, .task, .scope, .shield, .guard => true,
.ref, .mut, .slice, .slice_mut, .own, .own_array, .mutex => |inner| isLocalType(m, inner.*),
.array => |a| isLocalType(m, a.elem),
.named => |i| switch (m.types[i].kind) {
.@"struct" => |fs| for (fs) |f| {
if (isLocalType(m, f.ty)) break true;
} else false,
.@"enum" => |vs| blk: {
for (vs) |v| for (v.payload) |p| if (isLocalType(m, p)) break :blk true;
break :blk false;
},
},
else => false,
};
}
/// `Send` (spec 04 §35): may move to another task.
pub fn isSend(m: air.Module, t: Type) bool {
return switch (t) {