Skip to content

Go: xs[i] = e on an owned slice ​

Status: accepted · Date: 2026-12-01

Step 4 of the Go plan lists xs[i] = e among the slice operations left to do. This record says when the proved path accepts it.

The decision ​

  • xs[i] = e, for a local slice xs, becomes let xs := Go.sliceSet xs i e. Go.sliceSet is List.set at a non-negative index. Go evaluates the operands of the index and the right side first, in the usual order, and only then tests the index and stores the value (Go specification, "Assignment statements"). The output does the same. It checks the divisors and indexes of i and then of e, then tests the index with Go.sliceFault, then sets the element. So a division by zero in e panics before an index out of range does.
  • The interpreter has the same rule (Eval.setIndex), and Proof.setIndex_sound shows that the code agrees with it.
  • The list model is exact only when no other slice shares the array of xs. So xs[i] = e counts as a use that needs ownership, like xs = append(xs, …) (Owned.check, see the append decision record). xs must not be a parameter. Nothing may read it except len(xs), xs[j] and return xs. It may be given only a new slice.

Counted loops over len(xs) ​

for i := 0; i < len(xs); i++ { xs[i] = … } is accepted. Go evaluates i < len(xs) before each iteration, and the interpreter evaluates the bound once, before the loop. The two agree because the body cannot change what the bound reads, and this is proved, not assumed:

  • Eval.boundReads gives len(xs) the reads len and Eval.lenKey xs, not xs. Eval.countedBodySafe refuses a body that assigns, declares or shadows xs, but allows xs[j] = e.
  • Proof.Bound.stmt_ok shows that a body that the check accepts keeps the value of each name the bound reads, and keeps the length of each slice whose len it reads. xs[j] = e keeps the length because Eval.setIndex gives a list of the same length (Proof.Bound.setIndex_slice).
  • Proof.Bound.bound_rel shows that the bound then has the same value, up to the contents of a slice that only len reads, and Proof.Bound.counted_bound_fixed concludes that an integer bound has the same value after each iteration as before it.
  • This needs len to be the predeclared function. A package may declare its own len, so the interpreter and the translator refuse such a bound (Eval.lenBuiltin).

The range loop over xs stays refused. It walks the elements, not just the length, so a set element would change what later iterations see.

Refused ​

  • xs[i] = e on a parameter, whose array the caller sees, or on a slice that another slice, call or struct may share.
  • xs[i] = e in a range loop over xs, and in a counted loop whose bound reads xs other than through len(xs), as in i < xs[0]. Both loops fix the names that their bound or slice reads (Eval.countedBodySafe).
  • A counted bound that calls len when the package declares the name len (Eval.lenBuiltin).
  • xs[i] op= e, xs[i]++, xs[i].f = e, xs[i][j] = e, and a statement with several targets.

This page is docs/decisions/2026-12-01-go-slice-assign.md at commit 5f1d031.