cli: add whole-value linear aggregates

Implement STD-5 through source, AIR, verifier and deterministic interpreter.
Constructors consume nested owners; exhaustive unpack/take, explicit swap and
own.pack preserve ownership on success and failure. Support owning Option and
Result while keeping classification helpers Copy-only. Preserve borrow identity
across whole replacement and collection; distinguish individual owned arrays
from repeated heap arrays.

Specify feature contracts, invariants, diagnostics, compatibility and measured
source/AIR budgets in RFC 0023 and normative docs. Add canonical round trips,
AIR/source acceptance and rejection fixtures, CLI integration and CI checks.

Validate build, unit/extended tests, full conformance, forced-collection schedule
stress (32 seeds per quantum), hosted smoke, CLI suites, source coverage (88/88),
formatting and reference bundle generation. Source bundle grows 3.2%; Quick
Reference grows 4.0%. No MCC improvement is claimed.

Dynamic initialized linear buffers and explicit drain remain Q-036/STD-6;
frame/region/repeated heap storage is Copy-only. RFC remains Proposed.
This commit is contained in:
dev committed 2026-10-06 21:23:52 +03:00
1 parent 7eea31518d
commit 4f084decce
109 files changed
+1814 -142

No files matched your search

+2 -1
View File
@@ -79,6 +79,7 @@ pub const Expr = struct {
index: struct { e: *const Expr, lo: *const Expr, hi: ?*const Expr },
struct_lit: struct { name: []const u8, fields: []const FieldInit, type_args: []const Type = &.{} },
array_rep: struct { e: *const Expr, len: u64 },
array_list: []const Expr,
or_trap: *const Expr,
if_expr: *const If,
match_expr: *const Match,
@@ -141,7 +142,7 @@ pub const Select = struct {
pub const Stmt = struct {
span: Span,
kind: union(enum) {
let: struct { names: []const []const u8, ty: ?Type, e: Expr },
let: struct { names: []const []const u8, ty: ?Type, e: Expr, tuple: bool = false },
/// `init == null` is `= zero`.
@"var": struct { name: []const u8, ty: Type, init: ?Expr },
assign: struct { place: Expr, e: Expr },
+146 -23
View File
@@ -315,6 +315,43 @@ const Lowerer = struct {
scope_binding_start: usize = 0,
fns: std.ArrayList(FnOut) = .empty,
linear_enabled: bool = false,
fn probeType(l: *Lowerer, t: ast.Type) ?Ty {
const previous = l.problem;
defer l.problem = previous;
return l.typeOf(t) catch null;
}
fn needsLinearType(l: *Lowerer, t: Ty, depth: usize) bool {
if (depth > l.m.types.len + 32) return false;
if (t == .named) return l.isLinear(t);
if (t.inner()) |inner| {
if ((t == .array or t == .own or t == .own_array or t == .slice or t == .slice_mut) and l.isLinear(inner)) return true;
return l.needsLinearType(inner, depth + 1);
}
return false;
}
fn isLinear(l: *Lowerer, t: Ty) bool {
return l.linearAt(t, 0);
}
fn linearAt(l: *Lowerer, t: Ty, depth: usize) bool {
if (t.isLinear()) return true;
if (depth > l.m.types.len + 32) return false;
return switch (t) {
.array => |a| l.linearAt(a.elem, depth + 1),
.named => |i| switch (l.m.types[i].kind) {
.@"struct" => |fs| for (fs) |f| {
if (l.probeType(f.ty)) |ty| if (l.linearAt(ty, depth + 1)) break true;
} else false,
.@"enum" => |vs| blk: {
for (vs) |v| for (v.payload) |p| if (l.probeType(p)) |ty| if (l.linearAt(ty, depth + 1)) break :blk true;
break :blk false;
},
},
else => false,
};
}
fn fail(l: *Lowerer, code: []const u8, span: Span, message: []const u8, fields: []const Problem.Field) Error {
// `fields` usually points at a temporary array: keep a copy.
l.problem = .{ .code = code, .span = span, .message = message, .fields = try l.arena.dupe(Problem.Field, fields) };
@@ -465,7 +502,6 @@ const Lowerer = struct {
}
fn checkRequirements(l: *Lowerer) Error!void {
if (l.m.requirements.len == 0) return;
const types = try l.arena.alloc(air_module.TypeDecl, l.m.types.len);
for (l.m.types, types, 0..) |d, *out, index| {
out.* = .{ .name = d.air_name orelse d.name, .span = .{ .line = @intCast(index + 1), .col = 1 }, .kind = undefined };
@@ -486,7 +522,10 @@ const Lowerer = struct {
},
}
}
const m: air_module.Module = .{ .name = l.m.name, .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = types, .functions = &.{} };
for (l.m.types, 0..) |_, i| if (l.isLinear(.{ .named = @intCast(i) })) {
l.linear_enabled = true;
};
const m: air_module.Module = .{ .name = l.m.name, .format_version = 0, .lang = .{ 0, 1 }, .features = &.{"linear_aggregates"}, .types = types, .functions = &.{} };
var layout_context: @import("../verifier.zig").Context = .{ .arena = l.arena, .file = "<generic>", .module = m };
try verifier_types.checkFiniteLayouts(&layout_context);
if (layout_context.diags.items.len != 0) {
@@ -528,10 +567,19 @@ const Lowerer = struct {
}
var roots: std.ArrayList([]const u8) = .empty;
try roots.append(l.arena, "effects");
var signatures = l.sigs.valueIterator();
while (signatures.next()) |f| {
for (f.params) |t| if (l.needsLinearType(t, 0)) {
l.linear_enabled = true;
};
if (l.needsLinearType(f.ret, 0)) l.linear_enabled = true;
}
if (l.linear_enabled) try roots.append(l.arena, "linear_aggregates");
const Rule = struct { []const u8, []const []const u8 };
const rules = [_]Rule{
.{ "arrays", &.{ "array<", "slice", "array.splat", "borrow.index" } },
.{ "ownership", &.{ "local.", "borrow.", " load ", "= load ", "store ", "ref<", "mut<" } },
.{ "linear_aggregates", &.{ "array.make ", "struct.unpack ", "array.unpack ", "enum.take ", "value.swap ", "own.pack " } },
.{ "heap", &.{ "own.", "own<", "own_array<", "alloc_error" } },
.{ "regions", &.{"region"} },
.{ "capabilities", &.{ "cap<", "cap.narrow", "cap_error" } },
@@ -953,7 +1001,10 @@ const Lowerer = struct {
fn stmt(l: *Lowerer, s: ast.Stmt) Error!void {
switch (s.kind) {
.let => |x| {
if (x.names.len == 2) {
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.fail("E7006", s.span, "a single parenthesized binding requires aggregate unpack", &.{});
if (x.names.len != 1) {
if (x.e.kind != .call or x.names.len != 2) return l.fail("E7006", s.span, "this multi-binding requires aggregate unpack", &.{});
if (pathOf(x.e.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "atomic.compare_exchange")) return l.compareExchangeLet(s, x.names, x.e);
return l.splitLet(s, x.names, x.e);
}
@@ -983,7 +1034,7 @@ const Lowerer = struct {
if (v.ty.isBorrow()) return l.fail("E7006", x.e.span, "bind a borrow with `ref name = place;` or `mut name = place;`", &.{});
// Linear values (owners, regions, resources, endpoints, tasks,
// mutexes) are borrowed in place, never moved to frame storage.
if (l.addressable.contains(x.names[0]) and !v.ty.isLinear()) {
if (l.addressable.contains(x.names[0]) and !l.isLinear(v.ty)) {
const st = try l.storageFor(x.names[0], v.ty, s.span);
try l.emit(try l.fmt("local.store %{s}, %{s}", .{ st, v.name }), s.span);
try l.bindings.append(l.arena, .{ .name = x.names[0], .mutable = false, .value = st, .ty = v.ty, .kind = .storage });
@@ -992,7 +1043,7 @@ const Lowerer = struct {
.@"var" => |x| {
const ty = try l.typeOf(x.ty);
if (ty.isBorrow()) return l.fail("E7006", s.span, "a `var` holds a value; bind a borrow with `ref` or `mut`", &.{});
if (l.addressable.contains(x.name) and !ty.isLinear()) {
if (l.addressable.contains(x.name) and !l.isLinear(ty)) {
const st = try l.storageFor(x.name, ty, s.span);
if (x.init) |e| {
const v = try l.consuming(e, ty);
@@ -1238,6 +1289,7 @@ const Lowerer = struct {
.deref => |x| if (l.peekType(x.*)) |t| t.inner() else null,
.index => |x| if (x.hi == null) (if (l.peekType(x.e.*)) |t| elemOf(t) else null) else null,
.array_rep => |x| if (l.peekType(x.e.*)) |t| (l.arrayOf(t, x.len) catch null) else null,
.array_list => |xs| if (xs.len != 0) (if (l.peekType(xs[0])) |t| (l.arrayOf(t, xs.len) catch null) else null) else null,
.neg => |x| l.peekType(x.*),
.not => .bool,
.cast => |x| l.typeOfNoFail(x.ty),
@@ -1396,6 +1448,18 @@ const Lowerer = struct {
try l.endNow(p.ends, e.span);
return v;
},
.array_list => |xs| {
if (xs.len == 0 or xs.len > 64) return l.fail("E7006", e.span, "array literal requires 1..64 complete elements", &.{});
var ty: ?Ty = if (want) |w| if (w == .array) w.array.elem else null else null;
var names: std.ArrayList(u8) = .empty;
for (xs, 0..) |x, i| {
const v = try l.consuming(x, ty);
if (ty) |t| try l.checkTy(x.span, v.ty, t) else ty = v.ty;
try names.appendSlice(l.arena, try l.fmt("{s}%{s}", .{ if (i == 0) "" else ", ", v.name }));
}
const at = try l.arrayOf(ty.?, xs.len);
return l.def("arr", at, try l.fmt("array.make {s}({s})", .{ l.tyText(at), names.items }), e.span);
},
.array_rep => |x| {
const want_elem: ?Ty = if (want) |w| (if (w == .array) w.array.elem else null) else null;
const v = try l.expr(x.e.*, want_elem);
@@ -1464,7 +1528,7 @@ const Lowerer = struct {
.or_trap => |x| return l.orTrap(e, x.*),
.move => |n| {
const b = l.lookup(n) orelse return l.unknown(e.span, n);
if (!b.ty.isLinear()) return l.fail("E7006", e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
if (!l.isLinear(b.ty)) return l.fail("E7006", e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
return .{ .name = b.value, .ty = b.ty };
},
}
@@ -1543,6 +1607,19 @@ const Lowerer = struct {
}
fn call(l: *Lowerer, e: ast.Expr, callee: ast.Expr, args: []const ast.Expr) Error!Value {
if (pathOf(callee)) |path| if (std.mem.eql(u8, path, "value.swap")) {
if (args.len != 2 or args[0].kind != .borrow or !args[0].kind.borrow.mutable) return l.fail("E7006", e.span, "value.swap requires mut place and replacement", &.{});
const b = args[0].kind.borrow;
const p = try l.placeBorrow(b.place.*, true, false, e.span);
if (p.v.ty != .mut) return l.fail("E7005", e.span, "value.swap requires mut<T>", &.{});
const ty = p.v.ty.inner().?;
const replacement = try l.consuming(args[1], ty);
try l.checkTy(args[1].span, replacement.ty, ty);
const result = try l.def("old", ty, try l.fmt("value.swap %{s}, %{s}", .{ p.v.name, replacement.name }), e.span);
try l.endNow(p.ends, e.span);
l.linear_enabled = true;
return result;
};
if (callee.kind == .variant) return l.variantValue(e, callee.kind.variant.@"enum", callee.kind.variant.name, args);
if (try l.moduleCall(callee)) |name| {
// Reuse ordinary call typing/borrowing/lowering, including E7002.
@@ -1652,7 +1729,7 @@ const Lowerer = struct {
if (std.mem.eql(u8, x.name, f.name)) break x;
} else return l.fail("E7006", e.span, "missing field", &.{ .{ .name = "type", .value = d.display_name orelse name }, .{ .name = "field", .value = f.name } });
const ft = try l.typeOf(f.ty);
const v = try l.expr(fi.e, ft);
const v = try l.consuming(fi.e, ft);
try l.checkTy(fi.e.span, v.ty, ft);
try args.appendSlice(l.arena, try l.fmt("{s}%{s}", .{ if (i == 0) "" else ", ", v.name }));
}
@@ -1669,7 +1746,7 @@ const Lowerer = struct {
var names: std.ArrayList(u8) = .empty;
for (args, v.payload, 0..) |a, pt, i| {
const ty = try l.typeOf(pt);
const av = try l.expr(a, ty);
const av = try l.consuming(a, ty);
try l.checkTy(a.span, av.ty, ty);
try names.appendSlice(l.arena, try l.fmt("{s}%{s}", .{ if (i == 0) "" else ", ", av.name }));
}
@@ -1743,7 +1820,7 @@ const Lowerer = struct {
if (m.kind == .outcome) return l.outcomeMatch(m, want);
const st = l.peekType(m.scrutinee) orelse return l.fail("E7007", m.scrutinee.span, "the type of this value cannot be determined", &.{});
if (st == .builtin) return l.builtinMatch(m, st.builtin, want);
const sv = try l.expr(m.scrutinee, st);
const sv = try l.consuming(m.scrutinee, st);
const depth = l.bindings.items.len;
const vars = try l.carriedLive(depth, m.span, .{ .match = m });
const before = try l.snapshot();
@@ -1754,7 +1831,7 @@ const Lowerer = struct {
const vs = d.kind.@"enum";
const blocks = try l.arena.alloc(usize, m.arms.len);
var head: std.ArrayList(u8) = .empty;
try head.appendSlice(l.arena, try l.fmt("enum.switch {s} %{s}", .{ l.tyText(st), sv.name }));
try head.appendSlice(l.arena, try l.fmt("{s} {s} %{s}", .{ if (l.isLinear(st)) "enum.take" else "enum.switch", l.tyText(st), sv.name }));
for (m.arms, blocks, 0..) |arm, *blk, i| {
if (arm.pattern.kind != .ctor or arm.pattern.kind.ctor.qualifier == null)
return l.fail("E7006", arm.pattern.span, "an arm of a `match` over an enum names one variant as `Enum::Variant`; `_` is not allowed", &.{.{ .name = "enum", .value = d.display_name orelse d.name }});
@@ -1995,7 +2072,7 @@ const Lowerer = struct {
/// written `move x` (spec 15 §5). Use after move, double consumption and
/// leaks are the verifier's (E2001, E2002, E3001, E3002).
fn consuming(l: *Lowerer, e: ast.Expr, want: ?Ty) Error!Value {
if (e.kind == .name) if (l.lookup(e.kind.name)) |b| if (b.ty.isLinear() and b.kind == .ssa)
if (e.kind == .name) if (l.lookup(e.kind.name)) |b| if (l.isLinear(b.ty) and b.kind == .ssa)
return l.fail("E7003", e.span, "a linear value is consumed here: write `move` to transfer it, or borrow it with `ref`/`mut`", &.{ .{ .name = "name", .value = e.kind.name }, .{ .name = "suggestion", .value = try l.fmt("move {s}", .{e.kind.name}) } });
return l.expr(e, want);
}
@@ -2059,6 +2136,7 @@ const Lowerer = struct {
.ssa => {
// A linear value (owner, region) is borrowed in place.
const bt: ?Ty = switch (b.ty) {
.named, .array => if (l.isLinear(b.ty)) (if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty)) else null,
.own => |in| if (mutable) try l.wrap(.mut, in.*) else try l.wrap(.ref, in.*),
.own_array => |in| if (mutable) try l.wrap(.slice_mut, in.*) else try l.wrap(.slice, in.*),
.region => if (mutable) try l.wrap(.mut, .region) else try l.wrap(.ref, .region),
@@ -2186,6 +2264,30 @@ const Lowerer = struct {
/// `let (left, right) = s.split_at(k);`: two disjoint borrows of `s`,
/// bound for the rest of the block (spec 15 §5).
fn unpackLet(l: *Lowerer, s: ast.Stmt, names: []const []const u8, e: ast.Expr, tuple: bool) Error!void {
if (!tuple or s.kind.let.ty != null or e.kind.call.args.len != 1) return l.fail("E7006", s.span, "unpack requires a parenthesized complete binding and one argument", &.{});
const v = try l.consuming(e.kind.call.args[0], null);
const operation = pathOf(e.kind.call.callee.*).?;
var components: std.ArrayList(Ty) = .empty;
if (std.mem.eql(u8, operation, "struct.unpack") and v.ty == .named and l.m.types[v.ty.named].kind == .@"struct") {
for (l.m.types[v.ty.named].kind.@"struct") |f| try components.append(l.arena, try l.typeOf(f.ty));
} else if (std.mem.eql(u8, operation, "array.unpack") and v.ty == .array) {
if (v.ty.array.len > 64) return l.fail("E7006", s.span, "unpack exceeds 64 block parameters", &.{});
for (0..@intCast(v.ty.array.len)) |_| try components.append(l.arena, v.ty.array.elem);
} else return l.fail("E7005", e.span, "unpack needs the corresponding struct or array", &.{});
if (names.len != components.items.len or names.len > 64) return l.fail("E7005", s.span, "unpack must bind every component", &.{});
const next = try l.newBlock("unpacked");
for (names, components.items, 0..) |n, t, i| {
for (names[0..i]) |previous| if (!std.mem.eql(u8, n, "_") and std.mem.eql(u8, n, previous)) return l.fail("E7006", s.span, "duplicate unpack binding", &.{});
const pn = try l.freshName(if (std.mem.eql(u8, n, "_")) "unused" else n);
try l.blocks.items[next].params.append(l.arena, .{ .name = pn, .ty = t });
if (!std.mem.eql(u8, n, "_")) try l.bindings.append(l.arena, .{ .name = n, .value = pn, .ty = t, .mutable = false });
}
try l.term(try l.fmt("{s} {s} %{s}, next -> @{s}", .{ operation, l.tyText(v.ty), v.name, l.blocks.items[next].name }), s.span);
l.cur = next;
l.linear_enabled = true;
}
fn splitLet(l: *Lowerer, s: ast.Stmt, names: []const []const u8, e: ast.Expr) Error!void {
if (e.kind != .call or e.kind.call.callee.kind != .field or !std.mem.eql(u8, e.kind.call.callee.kind.field.name, "split_at") or e.kind.call.args.len != 1)
return l.fail("E7006", e.span, "`let (a, b) = …` is only for `s.split_at(k)`", &.{});
@@ -2293,6 +2395,7 @@ const Lowerer = struct {
.field => |x| try l.markExpr(x.e.*),
.struct_lit => |x| for (x.fields) |f| try l.markExpr(f.e),
.array_rep => |x| try l.markExpr(x.e.*),
.array_list => |xs| for (xs) |x| try l.markExpr(x),
.if_expr => |x| {
try l.markExpr(x.cond);
try l.markBlock(x.then);
@@ -2322,7 +2425,7 @@ const Lowerer = struct {
fn fallible(l: *Lowerer, path: []const u8) ?void {
_ = l;
const ops = [_][]const u8{ "own.new", "own.array", "region.create", "region.new", "region.array", "cap.narrow.budget", "cap.narrow.prefix", "channel.make", "channel.send", "channel.recv", "channel.close_receiver", "task.check_cancel", "task.join_cancel", "island.create", "island.alloc", "arc.new" };
const ops = [_][]const u8{ "own.new", "own.pack", "own.array", "region.create", "region.new", "region.array", "cap.narrow.budget", "cap.narrow.prefix", "channel.make", "channel.send", "channel.recv", "channel.close_receiver", "task.check_cancel", "task.join_cancel", "island.create", "island.alloc", "arc.new" };
for (ops) |o| if (std.mem.eql(u8, o, path)) return {};
if (authority.intrinsicIndex(path)) |i| if (authority.intrinsics[i].fallible) return {};
return null;
@@ -2893,6 +2996,13 @@ const Lowerer = struct {
}.n;
const alloc_err: Ty = .{ .builtin = .alloc_error };
const usize_ty: Ty = .{ .int = "usize" };
if (std.mem.eql(u8, path, "own.pack")) {
try want(l, e.span, args, 2, path);
const v = try l.consuming(args[0], null);
const c = try l.expectCap(args[1], "mem.alloc");
l.linear_enabled = true;
return .{ .text = try l.fmt("own.pack {s} %{s}, %{s}", .{ l.tyText(v.ty), v.name, c.name }), .outcomes = try l.outs(&.{ .{ .role = "ok", .params = &.{try l.wrap(.own, v.ty)} }, .{ .role = "err", .params = &.{ alloc_err, v.ty } } }) };
}
if (std.mem.eql(u8, path, "own.new")) {
try want(l, e.span, args, 2, path);
const v = try l.consuming(args[0], null);
@@ -3186,17 +3296,21 @@ fn elemOf(t: Ty) ?Ty {
/// `cap.narrow.budget`; null for anything else.
fn pathOf(e: ast.Expr) ?[]const u8 {
const known = [_][]const u8{
"slice.copy", "own.new", "own.array", "own.take", "own.free",
"region.create", "region.new", "region.array", "region.reset", "region.destroy",
"cap.narrow.budget", "cap.narrow.prefix", "task.spawn", "task.join", "task.cancel",
"task.cancelled", "channel.make", "channel.dup", "channel.send", "channel.recv",
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take", "task.check_cancel",
"task.join_cancel", "island.create", "island.alloc", "island.root", "island.at",
"island.freeze", "island.destroy", "frozen.root", "frozen.at", "frozen.release",
"arc.from_frozen", "arc.new", "arc.clone", "arc.release", "arc.root",
"arc.at", "atomic.new", "atomic.take", "atomic.load", "atomic.store",
"atomic.swap", "atomic.add.wrap", "atomic.sub.wrap", "atomic.and", "atomic.or",
"atomic.xor", "atomic.compare_exchange", "atomic.fence",
"struct.unpack", "array.unpack", "value.swap", "own.pack",
"slice.copy", "own.new", "own.array", "own.take",
"own.free", "region.create", "region.new", "region.array",
"region.reset", "region.destroy", "cap.narrow.budget", "cap.narrow.prefix",
"task.spawn", "task.join", "task.cancel", "task.cancelled",
"channel.make", "channel.dup", "channel.send", "channel.recv",
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take",
"task.check_cancel", "task.join_cancel", "island.create", "island.alloc",
"island.root", "island.at", "island.freeze", "island.destroy",
"frozen.root", "frozen.at", "frozen.release", "arc.from_frozen",
"arc.new", "arc.clone", "arc.release", "arc.root",
"arc.at", "atomic.new", "atomic.take", "atomic.load",
"atomic.store", "atomic.swap", "atomic.add.wrap", "atomic.sub.wrap",
"atomic.and", "atomic.or", "atomic.xor", "atomic.compare_exchange",
"atomic.fence",
};
var buf: [64]u8 = undefined;
const text = dotted(e, &buf) orelse return null;
@@ -3314,6 +3428,9 @@ fn exprReads(e: ast.Expr, name: []const u8, skip: ?Span) bool {
break :blk false;
},
.array_rep => |x| exprReads(x.e.*, name, skip),
.array_list => |xs| for (xs) |x| {
if (exprReads(x, name, skip)) break true;
} else false,
.if_expr => |x| ifReads(x.*, name, skip),
.match_expr => |x| blk: {
if (exprReads(x.scrutinee, name, skip)) break :blk true;
@@ -3375,6 +3492,9 @@ const After = struct {
break :blk false;
},
.array_rep => |x| w.expr(x.e.*),
.array_list => |xs| for (xs) |x| {
if (w.expr(x)) break true;
} else false,
.if_expr => |x| w.ifExpr(x.*),
.match_expr => |x| blk: {
if (w.expr(x.scrutinee)) break :blk true;
@@ -3533,6 +3653,9 @@ fn exprAssigns(e: ast.Expr, name: []const u8) bool {
.field => |x| exprAssigns(x.e.*, name),
.index => |x| exprAssigns(x.e.*, name) or exprAssigns(x.lo.*, name) or (if (x.hi) |h| exprAssigns(h.*, name) else false),
.array_rep => |x| exprAssigns(x.e.*, name),
.array_list => |xs| for (xs) |x| {
if (exprAssigns(x, name)) break true;
} else false,
else => false,
};
}
+20 -6
View File
@@ -331,11 +331,14 @@ const Parser = struct {
if (isWord(t, "let")) {
_ = p.next();
var names: std.ArrayList([]const u8) = .empty;
if (p.peek().tag == .l_paren) {
const tuple = p.peek().tag == .l_paren;
if (tuple) {
_ = p.next();
try names.append(p.arena, try p.ident("a name"));
_ = try p.expect(.comma, "`,`");
try names.append(p.arena, try p.ident("a name"));
while (p.peek().tag != .r_paren) {
try names.append(p.arena, if (p.peek().tag == .underscore) p.next().text else try p.ident("a name"));
if (p.peek().tag != .comma) break;
_ = p.next();
}
_ = try p.expect(.r_paren, "`)`");
} else try names.append(p.arena, try p.ident("a name"));
var ty: ?ast.Type = null;
@@ -346,7 +349,7 @@ const Parser = struct {
_ = try p.expect(.eq, "`=`");
const e = try p.expr();
_ = try p.expect(.semicolon, "`;`");
return .{ .span = p.span(start), .kind = .{ .let = .{ .names = names.items, .ty = ty, .e = e } } };
return .{ .span = p.span(start), .kind = .{ .let = .{ .names = names.items, .ty = ty, .e = e, .tuple = tuple } } };
}
const unchecked_borrow = isWord(p.peekAt(1), "unchecked") and p.peekAt(2).tag == .ident and p.peekAt(3).tag == .eq;
if ((isWord(t, "ref") or isWord(t, "mut")) and ((p.peekAt(1).tag == .ident and p.peekAt(2).tag == .eq) or unchecked_borrow)) {
@@ -822,6 +825,17 @@ const Parser = struct {
.l_bracket => {
_ = p.next();
const e = try p.expr();
if (p.peek().tag != .semicolon) {
var items: std.ArrayList(ast.Expr) = .empty;
try items.append(p.arena, e);
while (p.peek().tag == .comma) {
_ = p.next();
if (p.peek().tag == .r_bracket) break;
try items.append(p.arena, try p.expr());
}
_ = try p.expect(.r_bracket, "`]`");
return .{ .span = p.span(start), .kind = .{ .array_list = items.items } };
}
_ = try p.expect(.semicolon, "`;` in `[e; N]`");
const n = try p.expect(.int, "array length");
_ = try p.expect(.r_bracket, "`]`");
@@ -853,7 +867,7 @@ const Parser = struct {
};
return .{ .span = p.span(start), .kind = .{ .match_expr = try p.box(ast.Match, .{ .scrutinee = scrut, .arms = as, .span = p.span(start), .kind = kind }) } };
}
if (isKeyword(t.text)) return p.fail(t, "an expression");
if (isKeyword(t.text) and !(isWord(t, "struct") and p.peekAt(1).tag == .dot and isWord(p.peekAt(2), "unpack"))) return p.fail(t, "an expression");
_ = p.next();
if (p.peek().tag == .colon_colon and p.peekAt(1).tag != .lt) {
_ = p.next();