Skip to content

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) takes append(s, a, b, …): it evaluates s, then the others left to right, and gives the elements of s followed by them (Go specification, "Appending to and copying slices"). It takes make([]T, n) and make([]T, n, c): n zero 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] for append(xs, a, b), and [] for make([]T, 0) and for make([]T, 0, c) when c is a literal from 0 up to 2^20. These sizes cannot panic, and the runtime can always allocate them.
  • translate_sound covers both: the IR operation sliceAppend concatenates two lists, and make with 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 2

The 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:

  • x is not a parameter, whose array the caller holds;
  • every assignment to x gives it a slice literal, a make, or append(x, …), so x never takes another slice's array;
  • x is read only by len(x), x[i] and return x. It is never passed to a call, assigned to another variable, used as an element, or appended to another slice. return x ends 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 ​

  • append anywhere else: ys := append(xs, …), len(append(xs, …)), append(xs, ys...).
  • make with a nonzero or computed length, or a computed capacity, whose panics and allocation the translator does not test yet.
  • xs[i] = e, slice expressions xs[a:b] (xs[a:] came later, decision), copy, xs == nil, keyed elements, slices of structs and of slices, and range over a slice, as in the slices decision.

This page is docs/decisions/2026-11-30-go-append.md at commit 5f1d031.