On this page

0028. Canonical formatting of declarations and arms

Context

New declarations and record literals need one canonical layout consistent with ADR-0015's comment preservation and ADR-0018's semicolon-free statements.

Decision

Declarations have one field or variant per line, with a trailing comma, four spaces of indentation and a blank line between items. Record literals stay on one line if the entire enclosing output line fits 100 columns; otherwise each field has its own line and trailing comma. Preserve written field order: it is evaluation order, not a cosmetic choice. Comments force multiline literals and retain their source order/text. Same-line trailing comments stay at the end of their own field or variant line, after the generated comma; standalone comments stay standalone. Trailing comments on match arms likewise stay on their arm, after its comma when parentheses require one. A comment after a declaration's closing brace stays on that brace's own line.

Reuse the inline-candidate pass; identify candidates by both span endpoints, since a literal and enclosing postfix access can share a starting offset. Postfix access on an if or match expression preserves the necessary parentheses. Type names use a dedicated printer, ready for nested arguments in M4.4. M4.3 amendment, before merge: match arms occupy one line per head/body, expanding braced bodies as blocks. Strip optional type qualifiers in patterns; preserve written alternative and binder order. Arms have no trailing comma outside parentheses. Inside parentheses every arm ends with a comma, since the lexer drops newlines there. A single-statement arm has no generated ;; statements inside its braced body retain ADR-0018's parenthesized separators. Keep empty payload parentheses when they protect an interior comment's newline.

Alternatives considered

Consequences

Formatting is syntax-only, idempotent and preserves the span-free AST. Statements remain semicolon-free except required separators inside parentheses. The formatter's source fixtures and comment tests cover declaration and literal layouts, nesting, grouping and the full-line width boundary.