cli: validate exclusive source places before AIR

This commit is contained in:
dev committed 2026-10-08 12:37:22 +03:00
1 parent dcd5f1f702
commit 41f3afbf05
54 files changed
+541 -2

No files matched your search

+17 -2
View File
@@ -1230,7 +1230,11 @@ const Lowerer = struct {
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "buffer.view")) return l.bufferView(s, x.name, x.mutable, x.place);
if (x.place.kind == .call) if (pathOf(x.place.kind.call.callee.*)) |p| if (std.mem.endsWith(u8, p, ".at"))
return l.graphAccess(s, x.name, x.mutable, x.place);
const p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, true, s.span);
// Check the original mode before a whole array becomes a
// slice: shape conversion must not upgrade shared access.
var p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, false, s.span);
if (x.mutable) try l.requireExclusivePlace(x.place.span, p.v.ty);
p = try l.borrowShape(p, x.mutable, true, s.span);
if (p.ends.len == 0) {
// Borrowing a borrow binding: a reborrow, ended at scope end.
const rb = try l.defBorrow(x.name, p.v.ty, try l.fmt("{s} %{s}", .{ if (x.mutable) "borrow.mut" else "borrow.ref", p.v.name }), p.v.name, s.span);
@@ -2262,7 +2266,9 @@ const Lowerer = struct {
// Only a known array-borrow parameter preserves the array
// (spec 15 §5). Bindings and all other arguments keep slices.
const want_slice = if (want) |t| !(t == .ref or t == .mut) or t.inner().? != .array else true;
const p = try l.uncheckedPlace(x.place.*, x.mutable, x.unchecked, want_slice, a.span);
var p = try l.uncheckedPlace(x.place.*, x.mutable, x.unchecked, false, a.span);
if (x.mutable) try l.requireExclusivePlace(a.span, p.v.ty);
p = try l.borrowShape(p, x.mutable, want_slice, a.span);
if (p.ends.len == 0) {
const rb = try l.defBorrow("b", p.v.ty, try l.fmt("{s} %{s}", .{ if (x.mutable) "borrow.mut" else "borrow.ref", p.v.name }), p.v.name, a.span);
try temps.append(l.arena, rb.name);
@@ -2332,6 +2338,14 @@ const Lowerer = struct {
return if (want_slice) l.borrowShape(p, mutable, true, span) else p;
}
/// Explicit `mut` cannot upgrade shared access, including before array
/// shape conversion. Aliasing and lifetime legality stay with AIR.
fn requireExclusivePlace(l: *Lowerer, span: Span, t: Ty) Error!void {
if (t.isMutBorrow()) return;
const expected = if (t == .slice) try l.wrap(.slice_mut, t.inner().?) else try l.wrap(.mut, t.inner().?);
return l.fail("E7005", span, "mut needs an exclusive place", &.{ .{ .name = "expected", .value = l.diagnosticTyText(expected) }, .{ .name = "found", .value = l.diagnosticTyText(t) } });
}
/// Explicit argument/binding modes apply independently of array shape.
/// In particular, `ref` through `mut<[T; N]>` still reborrows shared.
fn borrowShape(l: *Lowerer, raw: Placed, mutable: bool, want_slice: bool, span: Span) Error!Placed {
@@ -2469,6 +2483,7 @@ const Lowerer = struct {
const p = try l.placeBorrow(place, true, false, span);
const in = p.v.ty.inner() orelse return l.fail("E7005", place.span, "assignment needs a mutable place", &.{});
if (in == .array and place.kind == .deref) return l.invalid(.invalid_assignment, place.span, "write array elements with `s[i] = …`", &.{});
if (p.v.ty != .mut) return l.fail("E7005", place.span, "assignment needs a mutable place", &.{ .{ .name = "expected", .value = l.diagnosticTyText(try l.wrap(.mut, in)) }, .{ .name = "found", .value = l.diagnosticTyText(p.v.ty) } });
try l.checkOperandTy(value.span, v.ty, in);
try l.emit(try l.fmt("store %{s}, %{s}", .{ p.v.name, v.name }), span);
try l.endNow(p.ends, span);