Skip to content

Go constant expressions on the proved path ​

Status: accepted · Date: 2026-12-06

Follows Go package constants, which read only literal constants.

The decision ​

  • The interpreter. Eval.constExpr evaluates a constant expression (Go specification, "Constant expressions", "Constant declarations", "Iota"): literals, true and false, iota, constants declared before it, and -, !, +, -, *, /, %, <<, >>, comparisons, && and ||. Eval.packageConsts walks the declarations in order. Within a const group the ith spec has iota = i, and a spec without type and values repeats the last one with values.
  • Exact arithmetic. Go evaluates constants exactly. A typed constant must be representable at each step, or go build refuses the package, so for every package that compiles exact Int arithmetic gives Go's value. / truncates and % follows the dividend, as Go does for integer constants. >> is a floor division by a power of two. The final value must still fit int64 where the translator uses it.
  • The proof. The proved path already inlines a constant's value from Program.consts, so the proof needs no change: the interpreter and the translator read the same value.
  • The output. The string translation writes def X : T := v with the computed value, since a spec that repeats the one before has no expression of its own. It writes every constant the interpreter reads, so an unused untyped one outside int64, such as 1 << 70, becomes a def of type Int with its exact value that nothing uses.
  • Variables. An unexported var that nothing changes may start at a constant expression. Without a type, an integer variable takes the type of its expression, which can be a typed constant's such as int64; the subset then accepts only a literal, which gives int.
  • Left out: constants from later ones (Go allows any order), floats, runes, &, |, ^, &^, len and conversions in constants, shifts by more than 512, and variables whose value is no constant expression, which depend on the order of package initialization.
  • Types. Eval.constExpr gives each value its type name, none when untyped. A constant without a type of its own takes the type of its expression: Mon = Sun + 1 for a Sun of type Day is a Day, as a spec that repeats a typed one is. A comparison is an untyped boolean. Two typed operands must have the same type name, so constants of type D int and type W int do not mix, and an alias and its target are refused the same way, conservatively. Every typed result, also an intermediate one, must fit its type: A * 4 / 8 for A Day = 1 << 62 leaves the subset, as go build refuses it. The untyped operand of a mixed operation is converted to the typed one's type and must fit it too, so A < 1 << 70 leaves the subset. A constant or variable with a type takes only an untyped value or one of that type, and it must fit.
  • Testing.
    • tests/go/Consts.lean checks the translation against the interpreter and the refusals, and pins the types above.
    • The certified corpus package tests/go/corpus/translate/constexpr has iota, a repeated typed spec, 1 << 70 >> 68, / and % of a negative constant, and string and bool constants built from others. go run ./check requires Lean to accept its certificate, in CI.
    • The differential generator writes constants with iota, the constant before and operators, which go run checks. It leaves them out under TOLEAN_CERTIFY_ONLY=1, so the fixed certificate sample (seed 20261130) keeps the programs of the rows in tests/certificates/expected-failures.tsv. Its certificates therefore do not cover these forms; the constexpr package does.

This page is docs/decisions/2026-12-06-go-constant-expressions.md at commit 5f1d031.