ADR 0046: Smart casts between assignments
Status: Accepted (2026-10-26). Amends the smart-cast rule of
ADR 0031 and types.md §5.3; makes the error of a
destructured call error?, as errors.md §5 always said.
Context
Section titled “Context”ADR 0031 let a local smart-cast only when the function never reassigns it.
The rule is simple and sound, but it rejects Go’s most common error idiom
once the error has its documented type error?:
x, err := strconv.Atoi(a)if err != null { throw err}y, err := strconv.Atoi(b) // := assigns err againif err != null { throw err.wrap("second") // err was not smart-cast: E0305}The compiler had kept the idiom working by typing the error as a platform
error!, which let throw err compile without a check and throw a nil
error. Walking a linked list (node = node.Next in while node != null)
and defaulting a value (if u == null { u = guest }) failed the same way.
Decision
Section titled “Decision”A check smart-casts a reassigned local until its next assignment:
- Every assignment (
=, a:=list that reuses the name,++/--) ends the smart casts of the variable. An assignment of a value that is never null smart-casts it to non-null, and so does a declarationx: T? = nonNull. - A loop ends, at its start, the smart casts of the variables its body
assigns, since the next iteration runs after those assignments; the
condition of
while node != nullsmart-castsnodeagain each time. - After an
if/elseor awhen, a variable stays smart-cast when every branch that continues leaves it non-null; anifwithoutelseand awhenwithout one count the path that runs no branch. - The rule applies only where the compiler sees every assignment in order.
A local whose address is taken (
&x), that a lambda,goblock, ordeferblock assigns, or that anif,when, orcatchused as a value assigns (such a block runs before the plain reads of the expression around it, ADR 0045) is never smart-cast between assignments. A smart cast made outside a lambda does not hold inside it. - Locals the function never reassigns keep ADR 0031’s rule, including inside lambdas.
v, err := f() binds err as error? for Go calls too, and a success
value that has no zero value as T?, null when the call fails (errors.md
§5). E0401 names the call in that case and suggests v := f()?.
Consequences
Section titled “Consequences”- The Go idiom, list walks, and lazy defaults compile without copies, and
throw erron an unchecked error is E0305. - E0401 says why a check did not hold: an assignment after it, a loop, a
closure,
&x, or a lambda. - Comparing a smart-cast variable with
nullis legal and warns (W0004); it compiles to Go’sx != nilon the variable. run/flowcastsandcheck/flowcasts_negpin the rule.