cli: restrict source scope names to lexical task operands

This commit is contained in:
dev committed 2026-10-07 19:50:49 +03:00
1 parent 364eb57bbc
commit 9001e56325
32 files changed
+307 -6

No files matched your search

+15 -4
View File
@@ -1509,7 +1509,7 @@ const Lowerer = struct {
},
.boolean => |b| return l.def("c", .bool, if (b) "const.bool true" else "const.bool false", e.span),
.name => |n| {
const b = l.lookup(n) orelse return l.unknown(e.span, n);
const b = try l.valueBinding(e.span, n);
if (b.kind == .storage) return l.def(n, b.ty, try l.fmt("local.load %{s}", .{b.value}), e.span);
return .{ .name = b.value, .ty = b.ty };
},
@@ -1608,7 +1608,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);
const b = try l.valueBinding(e.span, n);
if (!l.isLinear(b.ty) and !(e.generic_transfer and b.ty != .void and b.ty != .never and !b.ty.isBorrow() and b.ty != .task and b.ty != .scope and b.ty != .guard and b.ty != .shield)) return l.invalid(if (b.ty.isBorrow()) .move_borrow else if (b.ty == .void or b.ty == .never) .invalid_move else .move_copy, e.span, "`move` applies to linear values; copyable values are passed plainly", &.{ .{ .name = "name", .value = n }, .{ .name = "type", .value = l.diagnosticTyText(b.ty) } });
if (b.kind == .storage) return l.def(n, b.ty, try l.fmt("local.load %{s}", .{b.value}), e.span);
return .{ .name = b.value, .ty = b.ty };
@@ -2184,12 +2184,23 @@ const Lowerer = struct {
}
}
/// Scope identifiers are lexical operands, not ordinary source values
/// (spec 15 §7). Do not suggest moving their retained completion handle.
fn valueBinding(l: *Lowerer, span: Span, name: []const u8) Error!*Binding {
const b = l.lookup(name) orelse return l.unknown(span, name);
if (b.ty == .scope) return l.invalid(.invalid_expression, span, "a scope name is only the first operand of `task.spawn`; it cannot be used as a value", &.{ .{ .name = "name", .value = name }, .{ .name = "type", .value = "scope" } });
return b;
}
/// An operand that consumes its value: a named linear value must be
/// 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 (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}) } });
if (e.kind == .name) {
const b = try l.valueBinding(e.span, e.kind.name);
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);
}