Skip to content

with fmt: configurable line width and symmetric folding of plain single-expression bodies (Law 13) #2519

Done

Description

@ehartford

Under Law 13 (D238: Meaning belongs to the compiler; presentation belongs to the formatter), semantic/Sema-backed ceremony rewrites live in with fix-ceremony (#3061, closed). What belongs in with fmt is strictly presentation:

  • Configurable line width (never enforced by the compiler): defaults to 100 columns, configurable via --max-width N and [fmt] max_width in with.toml (CLI overrides with.toml).
  • Symmetric folding of single-expression bodies: fold a comment-free block body holding a single plain (single-line, non-block) expression onto the header line when the full rendered line (including leading indentation) is at most the configured width; unfold an inline single-expression body into a multi-line body when it exceeds the configured width (preserving any trailing comment and staying idempotent).
  • Spec (§29.13): updated with Eric's blessed wording (2026-11-26), clearly separating with fmt presentation behavior from compiler syntax rules.
  • Tests: 100-col boundary (100 folds, 101 unfolds), custom width override (--max-width 60, with.toml, CLI override), indented method near the boundary, comment-bearing body untouched, over-width inline body with trailing comment remaining idempotent, and compiler accepting >100-col lines without complaint.

Activity

  1. ehartford commented on Oct 21, 2026

    @ehartford
    ContributorAuthor

    What with fmt should fix automatically

    The rule: with fmt fixes something automatically only when Sema can prove the program means exactly the same thing afterward (Law 1 applied backwards: the compiler already knows the answer, so it removes what the programmer spelled out). Anything that changes ownership, the ABI or a signature can get a suggested fix in a diagnostic, but is never applied silently. Counts are main's census bar (ceremony-bar.tsv, 2026-10-20).

    A. Remove automatically (with fmt, on by default)

    # Pattern Proof Sema needs Count on main
    1 Return type that matches what Sema infers (fn one() -> f64: 1.0 → fn one(): 1.0) The trait declares the return type, or the tail expression's type is known not counted yet (new typed pattern)
    2 Block body holding one expression → inlined after the colon Token check (inline_colon_body_convertible) plus no comments inside not counted yet
    3 Local type annotation equal to the initializer's type Re-inferring without the annotation gives the same type; annotations that drive inference stay (var xs: List[i32] = List.new()) not counted yet
    4 .clone() on a str D111: passing a str copies it, so the clone does nothing 505 (src 502, lib 3)
    5 Explicit &x passed to a &T parameter Auto-referencing produces the same call 1857 (src 1852, lib 5)
    6 move x passed to a consuming T parameter The signature already consumes it; field moves (D82 vacate) and moves outside call arguments are excluded 1139 (all areas)
    7 move x on a str binding; copy x on a Copy binding D111; for Copy types a copy is a copy 3 + 2
    8 Some(x) where an Option is expected Sema already applies the coercion 8
    9 let _ = expr where discarding the value does nothing → expr The value isn't a Task (dropping a Task cancels it, so that let _ = is a real choice) 727 (+602 empty-body let _ = 0 in rt)
    10 Casts that change nothing or only widen D114: widening is implicit; narrowing casts stay part of len-cast (see C)
    11 Trailing 0 in a function returning i32; Ok(()) / Ok(v) tails Language rule: the tail is implied not counted yet
    12 Enum.Variant where the expected type is known → .Variant The expected type is the enum not counted yet
    13 unsafe block containing no unsafe operation R4c already rejects a block that only calls a runtime function; extend to any block with nothing unsafe in it not counted yet

    B. Suggest in a diagnostic, never apply silently

    • A plain T parameter the function only reads → &T. The signature is authoritative; rewriting it changes ownership, destructor timing and the ABI for every caller. The exact &T fix may be suggested, never applied.
    • lit-ptr-len ("…" as *const u8, N; rt 46, src 36): fixing it means changing the callee to accept a str. A cleanup sweep, not a formatter job.
    • vec-name (D118, target 0): Vec names no type. A rename to List that needs a type check; a one-time migration, not fmt.

    C. Don't fix: the language is the problem

    Constraints on the fixer

    1. Never touch corpus directories (a corpus.stamp, D112), test/ fixtures, UAT fixtures or examples/ — examples are contracts; changing them is Eric's call even when meaning is unchanged.
    2. A file that fails Sema still gets the token-level pass; fmt never refuses to format.
    3. Idempotence gate: a second with fmt over lib/std and src is a no-op; each census count falls by exactly what fmt removed, and each fixed pattern's bar goes to 0 so the census keeps it out.
    4. No new warnings for any of these (not a nanny): the formatter fixes them quietly instead.

    Biggest wins: #5, #6, #9 and #4 — about 4,200 removed tokens, each proven by facts with analyze already produces.

  2. ehartford commented on Oct 21, 2026

    @ehartford
    ContributorAuthor

    Addition to section A: an annotated empty List.new() → []

    # Pattern Proof Sema needs Count on main
    14 var ends: List[i32] = List.new() → var ends = [] Inferring the literal's element type from its uses (D93) gives the annotated type. Where nothing in the body decides the element type, the annotation stays (var ends = [] with no deciding use is refused: "empty sequence literal requires expected type") 3861 (var|let) x: List[T] = List.new() sites, rough lexical count over src/ lib/ tools/ build/ rt/ examples/

    Checked with the installed compiler (2026-10-21) — the element type is decided by the uses:

    body after var ends = [] result
    ends.push(3), then take(&ends) where take(xs: &List[i32]) ok, List[i32]
    let n: i32 = 4, ends.push(n), let y: i32 = ends[0] ok
    ends.push(3), then S { ends: ends } with ends: List[i32] ok
    ends.push(3), then let y: i32 = ends[0] refused: implicit integer narrowing or sign change from isize to i32
    nothing refused: empty sequence literal requires expected type

    The fourth row looks wrong: the untyped constant 3 fixes the element type as isize before the later i32 demand gets a say, while the same literal passed to a &List[i32] parameter works. Until that's settled, the fixer can only rewrite a site when re-checking without the annotation gives the same type, which covers the rows that work.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced: not built. with fmt (src/Fmt.w) is still token-level; no pass removes a proven-redundant return type or folds a one-expression body.

    post-release: a Sema-backed formatter is a new subsystem.

  4. added this to the post-v0.16.0 milestone on Oct 28, 2026
  5. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    State (2026-11-25, main ce3899b). Two of the three items here exist, in with fix-ceremony (#3061) rather than in with fmt:

    item state
    1. a written return type equal to what the trait declares is dropped done for trait impls: the rewrite landed in #3599 and lib/std was swept in #3601 (42 sites). impl One for f64: fn one(): 1.0, the example this issue opens with, is how traits.with reads today. For a free function it is the open half of #3061 item 1.
    3. a local annotation equal to the initializer's type, and a widening as, are dropped done: both are fix-ceremony rewrites, each proved by a Sema fact, and the compiler and lib/std are swept.
    2. a block body holding one expression is folded onto the colon line not done.

    So what is left of this issue is item 2, and it is different in kind: it removes no characters the compiler can prove redundant; it changes layout. That makes it with fmt's, and it needs a rule that is a matter of taste, which is yours:

    • fold always, or only when the folded line fits a width (which width);
    • fold a body that is an if/match expression, or only a plain expression;
    • on by default in with fmt (every file reformats once), or behind a flag.

    Prediction: fold only a plain one-expression body, only when the line stays within 100 columns, on by default, 60%.

    One decision I am making by the laws rather than asking: the Sema-driven rewrites stay in fix-ceremony, one owner for them, and with fmt stays the layout tool. That answers the "which issue owns it" question I had left on #3061.

    Labelling human attn needed for the folding rule.

  6. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    Ruled by Eric, 2026-11-25 (answers to briefs.md).

    Fold only a plain one-expression body, only when the folded line fits with fmt's existing width (no new number), on by default. The rule is symmetric: a folded line that no longer fits is unfolded, so the output does not depend on history. A body that carries a comment is not folded. Label removed; this is mine.

  7. ehartford commented on Nov 26, 2026

    @ehartford
    ContributorAuthor

    Ruled 2026-11-25: fold only a plain one-expression body, when the folded line fits with fmt's existing width, on by default, symmetric (unfold what no longer fits), never a body that carries a comment.

    One thing stops the implementation: with fmt has no width. src/Fmt.with (509 lines) never measures a line, and neither the spec nor the formatter names a column limit. So "fits the existing width" has no number to use.

    Question for Eric (one choice): (a) fold whenever the body is one plain expression, with no width rule at all (then nothing is ever unfolded, and the symmetric rule is moot); or (b) a width of 100 columns, stated in §29.13 as the formatter's one number. My earlier prediction was (b), 60%.

    Labelled human attn needed for that choice.

  8. ehartford commented on Nov 26, 2026

    @ehartford
    ContributorAuthor

    Ruled by Eric, 2026-11-26:

    • Line width is strictly a with fmt concern, never enforced by the compiler, and is configurable.
    • Default width: 100 columns, configurable via CLI flag (--max-width N) and [fmt] in with.toml (CLI overrides with.toml).
    • Symmetric folding rule: fold a comment-free block body holding a single plain (single-line, non-block) expression onto the header line when the full rendered line (including leading indentation) is at most the configured width; unfold an inline single-expression body into a multi-line body when it exceeds the configured width (preserving any trailing comment and staying idempotent).
    • Blessed §29.13 wording: "Line width is not enforced by the compiler. with fmt defaults to 100 columns and is configurable. A block body holding a single plain expression with no comments is folded onto the header line when the folded line fits within the configured width, and an inline single-expression body that exceeds the configured width is unfolded into a multi-line body."
    • Update the existing with fmt (default) bullet so it does not contradict this rule.
    • Tests: 100-col boundary (100 folds, 101 unfolds), custom width override (--max-width 60), indented method near boundary, comment-bearing body untouched, over-width inline body with trailing comment remaining idempotent.

    Removing human attn needed.

  9. changed the title [-]with fmt: a Sema-backed pass that removes proven-redundant return types, folds one-expression bodies and drops redundant annotations[/-] [+]with fmt: configurable line width and symmetric folding of plain single-expression bodies (Law 13)[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions