Appearance
Go append and make on owned slices
Status: accepted · Date: 2026-11-30
The slices decision put slices on the proved path as lists, and refused everything that changes a slice. This record adds xs = append(xs, …) and make([]T, 0, c) for a slice that the function owns, and says why the list stays exact.
The decision
- The interpreter (
tolean/Go/Spec/Eval.lean) takesappend(s, a, b, …): it evaluatess, then the others left to right, and gives the elements ofsfollowed by them (Go specification, "Appending to and copying slices"). It takesmake([]T, n)andmake([]T, n, c):nzero values, with Go's panics for a negative length and a capacity below the length ("Making slices, maps and channels"). - The translator writes
xs ++ [a, b]forappend(xs, a, b), and[]formake([]T, 0)and formake([]T, 0, c)whencis a literal from 0 up to 2^20. These sizes cannot panic, and the runtime can always allocate them. translate_soundcovers both: the IR operationsliceAppendconcatenates two lists, andmakewith those sizes evaluates to an empty slice in the interpreter.
Ownership
append writes into the backing array of s when it has room, so a slice that shares that array sees the write:
go
xs := make([]int, 1, 4)
ys := xs
xs = append(xs, 1)
ys = append(ys, 2) // xs[1] is now 2The list model cannot show that. tolean/Go/Translate/Owned.lean therefore checks each function before translating it. A local x that the function appends to must be owned:
xis not a parameter, whose array the caller holds;- every assignment to
xgives it a slice literal, amake, orappend(x, …), soxnever takes another slice's array; xis read only bylen(x),x[i]andreturn x. It is never passed to a call, assigned to another variable, used as an element, or appended to another slice.return xends the function, so nothing in it can see a later write.
Every call of append must be the right side of x = append(x, …) for an owned x. A function that fails the check is left out of the proved path, along with the functions that call it, as for any other refusal.
The check is syntactic and refuses more than it must: an inner scope that declares the same name counts as the same local, and a slice that a function returns and its caller appends to is refused in the caller, since the caller got it from a call. The interpreter's append is a list concatenation too, so the proof does not depend on the check. The check is what makes that list model agree with Go, and the differential test compares both with the Go runtime on random programs that build and append to slice locals.
Still refused
appendanywhere else:ys := append(xs, …),len(append(xs, …)),append(xs, ys...).makewith a nonzero or computed length, or a computed capacity, whose panics and allocation the translator does not test yet.xs[i] = e, slice expressionsxs[a:b](xs[a:]came later, decision),copy,xs == nil, keyed elements, slices of structs and of slices, andrangeover a slice, as in the slices decision.
This page is docs/decisions/2026-11-30-go-append.md at commit 5f1d031.
