On this page

0060. A trailing semicolon on a block tail is a no-op

Context

It was reported that harness fmt could change meaning by dropping a ; after a tail expression, on the belief that ; turns a tail into a unit statement. The parser does not record a trailing ;: StmtKind::Expr is the same with or without it. The type checker and lowering treat the last expression of a used block as its value, and LANGUAGE.md already says "a block's value is its last statement if an expression (even with trailing ;)". Function bodies are never used as values, so fn f() -> i64 { 3; } and { 3 } both give E0306 (only the reported column differs: 1:20 versus 1:19, not the code). The formatter still kept ; after a non-if tail inside parentheses.

Release verification (cargo build --release -q): for each /tmp/opencode/fmt2-<case>.hn, run target/release/harness check <file> and target/release/harness run <file>, then target/release/harness fmt <file> and repeat check/run. Both commands report the same diagnostic in each phase:

Case Original tail Before fmt After fmt Run stdout (both)
nonunit-if fn f() -> i64 { if true { 1 } else { 2 }; } E0306 at 1:43 E0306 at 3:1 empty; exit 1
nonunit-value fn f() -> i64 { 3; } E0306 at 1:20 E0306 at 3:1 empty; exit 1
unit-value fn h() { 3; } (called by main) clean clean empty; exit 0
map v.map { x -> x + 1; } on [1, 2] clean clean 2\n3\n; exit 0
parens-else println(if false { if true { 1 } else { 2 } } else { 3; }) clean clean 3\n; exit 0

The exact error is error[E0306]: function can reach its end: expected i64, found unit; help: return a value on every path, for example `return 0` . Clean cases have empty stderr. Each helper case also has fn main() {} unless noted. The reported E0306-to-clean change was not reproduced in either direction.

Decision

Option (b) as already implemented: a ; after the last statement of a block is a no-op; no language or diagnostics change. The formatter therefore never emits a ; after the last statement of a block, inside parentheses or not. It still emits ; between statements inside parentheses (ADR-0018).

Alternatives considered

Consequences

Tests compare the full front-end check results (diagnostic codes) of every tests/programs input before and after formatting, plus targeted cases with explicit expected codes and idempotence. LANGUAGE.md and diagnostics are unchanged. If a future change makes ; semantic, it needs a new ADR and the formatter must keep the token.