Appearance
Go package-level variables that the package changes
Status: accepted · Date: 2026-11-29
The proved path now translates a function that reads or assigns an unexported package-level variable that some code of the package changes. This record settles the two questions that the record on variables that nothing changes left open, and splits the work into three changes.
The decision
- Explicit passing. The variables that change are the package's state. A function that uses one takes all of them as its first parameters, in the order of their declarations. A function that assigns one, a writer, also returns them, after its own results. A writer with no results returns the variables alone. The output uses plain parameters and tuples, not
StateM, so the IR and its evaluator need no new construct, andtranslate_soundcovers the output as it covers any other function. - A writer call inside an expression is outside the model. In
x + f(), wherefassignsx, Go does not say whetherxis read before or after the call (Go specification, "Order of evaluation"). The interpreter gives no result for a function with such a call (Eval.Stmt.callsOK), and the translator refuses the function with a message that names the writer. A program that needs one stays on the string translation. - A writer call that stands alone takes the new state. A function that calls a writer is a writer too. Before it runs, its body is rewritten (
Eval.Stmt.thread):g(…)becomes_, …, v₁, …, vₙ = g(…),a = g(…)becomesa, v₁, …, vₙ = g(…),a := g(…)becomesa, v₁', …, vₙ' := g(…)followed byvᵢ = vᵢ', andreturn g(…)binds the results to fresh names in the same way and returns them. The fresh names contain', which no Go name has. The translator translates the rewritten body, so the proof needs no new case. Avar x T = g(…)and a call in the head of anif,fororswitchstay refused. - Every writer must be proved. When a reached function uses a variable that changes, every function of the package that assigns one must be on the proved path. If one is not, because the roots do not reach it or the proved path refuses it, the translator refuses the package with a message that names it and gives the reason. Otherwise the string translation would read the variable as its initial value. If the proved path threads the variables, a function that it leaves out may not use them either.
- A function that declares a parameter, result or local with the name of a variable is refused, because the variable is a parameter in the output.
- A method with a value receiver is the function
T.Mhere too, so one that uses a variable takes the variables. A callx.M(…)of such a method is refused for now, and the interpreter gives no result for one that assigns a variable. - A variable's type is its declared type, or
boolorstringfor a literal of that kind, orintfor an integer literal.
Three changes
- Interpreter state, and proved functions that read or assign the variables directly and call no writer.
- Writer calls as a statement, after
:=or=, and afterreturn, with the tuple taken apart into the variables again. This change also moves the refusal of a nested writer call from the interpreter's call to the whole function, because the interpreter's call of a writer now gives the new state. - The refusal rule for a writer call nested in an expression in the Go guide's subset rules, and the check that every writer is proved. The rule itself landed with the second change, so this change adds the check.
This page is docs/decisions/2026-11-29-go-package-state.md at commit 5f1d031.
