cli: preserve whole-array arguments for explicit array parameters

This commit is contained in:
dev committed 2026-10-07 22:29:45 +03:00
1 parent f733f7c7bb
commit dde801c653
44 files changed
+397 -9

No files matched your search

+17 -7
View File
@@ -1159,7 +1159,7 @@ 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, s.span);
const p = try l.uncheckedPlace(x.place, x.mutable, x.unchecked, 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);
@@ -2173,7 +2173,10 @@ const Lowerer = struct {
fn argument(l: *Lowerer, a: ast.Expr, want: ?Ty, temps: *std.ArrayList([]const u8)) Error!Value {
switch (a.kind) {
.borrow => |x| {
const p = try l.uncheckedPlace(x.place.*, x.mutable, x.unchecked, a.span);
// 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);
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);
@@ -2216,12 +2219,12 @@ const Lowerer = struct {
/// `ref unchecked s[i]`: `borrow.index.unchecked` (spec 13 §6). Only an
/// element place can be unchecked; the boundary (`effects [unsafe]`) is
/// the verifier's (`E6001`).
fn uncheckedPlace(l: *Lowerer, place: ast.Expr, mutable: bool, unchecked: bool, span: Span) Error!Placed {
if (!unchecked) return l.placeBorrow(place, mutable, true, span);
fn uncheckedPlace(l: *Lowerer, place: ast.Expr, mutable: bool, unchecked: bool, want_slice: bool, span: Span) Error!Placed {
if (!unchecked) return l.borrowShape(try l.placeRaw(place, mutable, span), mutable, want_slice, span);
if (place.kind != .index or place.kind.index.hi != null) return l.invalid(.invalid_place, place.span, "`unchecked` applies to one element `s[i]`", &.{});
l.unchecked = true;
defer l.unchecked = false;
return l.placeBorrow(place, mutable, true, span);
return l.borrowShape(try l.placeRaw(place, mutable, span), mutable, want_slice, span);
}
fn rootBinding(l: *Lowerer, e: ast.Expr) ?*Binding {
@@ -2239,10 +2242,17 @@ const Lowerer = struct {
/// array becomes a slice. A place that is itself a borrow binding is
/// returned as is (`ends` empty).
fn placeBorrow(l: *Lowerer, place: ast.Expr, mutable: bool, want_slice: bool, span: Span) Error!Placed {
var p = try l.placeRaw(place, mutable, span);
const p = try l.placeRaw(place, mutable, span);
return if (want_slice) l.borrowShape(p, mutable, true, span) else p;
}
/// 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 {
var p = raw;
// `ref place` reached through a `mut` borrow (`ref s.buf` with
// `s: mut<S>`) is a shared reborrow of it (spec 07a `borrow.ref`).
if (want_slice and !mutable and (p.v.ty == .mut or p.v.ty == .slice_mut)) {
if (!mutable and (p.v.ty == .mut or p.v.ty == .slice_mut)) {
const t = if (p.v.ty == .mut) try l.wrap(.ref, p.v.ty.inner().?) else try l.wrap(.slice, p.v.ty.inner().?);
const r = try l.defBorrow("r", t, try l.fmt("borrow.ref %{s}", .{p.v.name}), p.v.name, span);
p = .{ .v = r, .ends = try l.concat(p.ends, r.name) };