Skip to content

Go: the order of slice bounds and other checks ​

Status: accepted · Date: 2026-12-04

The differential test (seed 3, on the CI of #371) found a function where the translation and the interpreter panicked with index out of range [3] with length 0, but Go panicked with slice bounds out of range [1:0]. The statement returned (v[0:][3] + v[2]) + (("lean" + v[1:][3]) + …) with v empty.

Go's rule ​

The Go specification ("Order of evaluation") fixes the order of calls, method calls, receive operations and binary logical operations only. gc evaluates each slice expression xs[a:] of an expression, with the checks that its value needs, before the other checks. Checked on go1.26 with s empty and t := []string{"a"}:

  • s[3] + t[2:][0], s[3] == t[2:][0] and ("x" + s[3]) + t[2:][0] panic with slice bounds out of range [2:1].
  • t[1:][2] + s[3] panics with index out of range [2] with length 0: the slice is fine, and its index comes first left to right.

The interpreter and the output test the checks left to right, so the translator from #361 accepted statements where gc's order differs. This is a translator bug, not a generator bug: the generator wrote valid Go.

The decision ​

Pure.slicesFirst is a new condition of orderKnown. A statement with a slice bound is kept only when:

  • the checks of its outermost slices (sliceChecks: those of the base and the bound, then the bound's test) come first in divisors, so gc's order and the interpreter's agree;
  • no other slice bound comes later.

The existing rule of orderKnown still applies: a statement may not mix checks inside the arguments of a call, len or append with checks outside them. A slice counts as a check where it sits.

The first version of this rule (#374) also refused a slice next to another check when both were inside the arguments of one call. That refusal was not needed, and the generator writes such calls: with seed 103, v, _ := G(s[:][3], …) was refused. It is now allowed.

translate_sound is unchanged, because the translator only refuses more.

The generator now writes xs[a:] only when no other check has been written in the statement so far. Its bound is a constant or a computed bound without a check, and it never appears in an argument of append.

Refused ​

  • A statement with a check before a slice bound, such as s[3] + t[2:][0] or 1 / x + s[2:][0].
  • A statement with two slices that have an index between them, such as s[3:][0] + t[2:][0].
  • A slice inside the argument of a call, len or append, with a check outside those arguments, or the reverse, such as len(s[2:]) + s[0].

Modelling gc's hoisting in the interpreter would tie the semantics to one compiler, in an area that the specification leaves open. tests/go/EvalOrder.lean holds the regression.

Follow-up: nested slices and several values (#394, #398) ​

The rule above only looked at the outermost slices of a statement. It took all the checks of an outermost slice as one ordered group, without looking inside its base and bound. In len(xs[1/x + xs[2:][0]:]) with x = 0, and in len(u[s[3] + t[2:][0]:]), gc evaluates the inner xs[2:] first and panics with slice bounds out of range [2:1]. The interpreter and the output tested the division or the index first.

Pure.slicesNested now applies slicesFirst again to the base and the bound of every slice, at every depth, and orderKnown requires it. Any slice whose base or bound has a check before an inner slice is refused.

The rule was also skipped for several values. return a, b and a, b := … with several expressions translated their values with translateArgs, which never calls orderKnown. So return s[3] + t[2:][0], x was kept. Both now use translateTopArgs, which applies the same rule as one value.

The regressions are in tests/go/EvalOrder.lean. Four more cases in tests/go/differential run under go run: each checks Go's panic message and checks that the proved path refuses the function.

Follow-up: the base and the bound of a slice (#424 review) ​

Putting inner slices first does not order the other checks of a slice's base against those of its bound. With x = 0 and ss empty, len(ss[:][3][1/x:]) panics with integer divide by zero on go1.26: gc tests the division in the bound before the index [3] in the base. The interpreter tests the base first and reports index out of range [3]. The same thing happens with an index in the bound (ss[s[3]][1/x:], ss[1/x][s[3]:]) and with a high bound (ss[:][3][0:1/x]). It also happens inside return a, b, a, b := …, a, b = … and call arguments.

Pure.baseBoundApart now refuses a slice whose base and bounds both have a check other than an inner slice's bound. A check in only one of them keeps the slice. We refuse every such slice rather than model gc's order. gc's choice depends on how it lowers the operands, and the specification leaves it open.

Among the review's 707 Go-vs-interpreter cases, 12 accepted slices disagreed with Go before this change. After it, all 12 are refused and none disagree. tests/go/EvalOrder.lean holds the refusals. Seven cases in tests/go/differential cover each statement form under go run.

This page is docs/decisions/2026-12-04-go-slice-check-order.md at commit 5f1d031.