Skip to content

with fix-ceremony: a Sema-driven rewriter that removes ceremony the compiler can prove unnecessary #3061

Done

Description

@ehartford

Eric, 2026-11-07: "we need to make a sema based tool to automatically reduce bad ceremony." The ceremony census (tools/ceremony_census.w) counts; this tool fixes.

Shape. A built-in with fix-ceremony <files> [--report | --apply] (src/CeremonyFix.w), after with migrate-receivers (src/ReceiverMigration.w): selection and contracts come from compiler_analyze_file facts and Sema's tables, never from text; the Lexer applies byte edits; --report lists each site with the rewrite, --apply writes it, and a rewritten file must still check (the tool re-checks before it keeps an edit).

Rewrites, each proved by a Sema fact:

  1. Inferable return type — fn f(...) -> T: where Sema's inferred body type equals T and the type is not a view, a Result error type, or a trait declaration (D43): drop -> T.
  2. Redundant binding annotation — let x: T = e where e's type is exactly T and the annotation changes no demand (not a []/{}/None/integer-literal demand, not a D22 owned-copy demand, not a D179 str-view copy): drop : T.
  3. var never rebound — a var whose name is never assigned again and never passed to a mut parameter (§3.8a): let.
  4. Widening cast — e as T where e's type widens into T at that operand (i32 into i64 arithmetic or comparison, u8 into i32): drop the cast. A narrowing stays.
  5. List.new() / HashMap.new() where a literal is typed by use — [] / {} when Sema has an expected type or a later use that types it (D93), including struct-literal fields and arguments.
  6. Index loop over a list — for i in 0..xs.len(): whose body uses i only as xs[i]: for x in xs: (a mut element write or a second list indexed by i keeps the loop).
  7. .clone() on a str (D111) and explicit &x at a &T parameter — already typed census patterns; the rewriter removes them.
  8. Numbered pub const KIND_X: i32 = n sets — report only (the enum needs a name).

Order of work. 2, 3, 4, 7 first (one fact each); 1 and 5 next (demand-sensitive); 6 last. Each rewrite ships with a fixture pair (before / after) and is run over src/ as its own PR, which lowers the census bar.

Context: the D194 stack (#3010) added list-new, list-annotation, index-loop and numbered-kind-const as lexical census patterns, and the day's review (every cast, annotation and List.new() I wrote by copying neighbours) is the motivation.

Activity

  1. added
    enhancement
    on Nov 7, 2026
  2. added 2 commits that reference this issue on Nov 7, 2026
  3. ehartford commented on Nov 7, 2026

    @ehartford
    ContributorAuthor

    Slice 1 landed in #3067: with fix-ceremony --report|--apply [--std] [--exclude p] <entry.w> with the redundant-annotation and var-never-rebound passes, the Sema facts they read (ceremony_let_*, ceremony_rebound_lets), and test/internals/ceremony_fix_test.w. Under a :dev stage1 the report over a small program's std imports listed 1936 candidate rewrites in 27 std files, which gives the --std sweep its first number. Next: widening casts, List.new() → [], index loops, then the typed census patterns (.clone() on str, &x at &T), and a src/ sweep as its own PR.

  4. added 2 commits that reference this issue on Nov 7, 2026
  5. ehartford commented on Nov 7, 2026

    @ehartford
    ContributorAuthor

    Direction (Eric, 2026-11-08): the rewriter is ultimately exposed to users as with fmt or similar. Consequences for the passes: every fact comes from Sema on the user's own program (no compiler-internal assumptions), a rewrite never changes meaning (the re-check-and-restore after --apply stays), and the std/corpora exclusions become the defaults of a user command. The command name and whether layout formatting joins it are open until the passes are complete.

  6. added 4 commits that reference this issue on Nov 7, 2026
  7. ehartford commented on Nov 24, 2026

    @ehartford
    ContributorAuthor

    State after #3594, against the issue's list of rewrites:

    # rewrite state
    2 redundant binding annotation done (#3067)
    3 var never rebound done (#3067)
    4 widening cast done; swept over src/ in #3576
    5 List.new() / HashMap.new() where a literal is typed by use done for a let with its type written; not for struct-literal fields and arguments
    6 index loop over one list done; swept in #3576
    7 .clone() on a str, &x at a &T parameter, Some(x) at an Option demand done
    8 numbered constant sets, report only done (#3594)
    1 inferable return type not done

    Also fixed in #3594: --help was read as the entry file.

    What item 1 needs before any code. Removing -> T is only safe when the body's type is T without the return's demand: fn f() -> i64: 1, an implicit Ok wrap and an empty literal all get their type from the annotation. The binding pass solves the same problem by classifying the initializer (cf_demand_free); a function needs it for the tail and for every return e, and a recursive function needs the annotation to be checked at all. Two policy points are not mine:

    1. A pub function's signature is its contract (law 6). I would leave every pub return type written and rewrite private functions only.
    2. with fmt: configurable line width and symmetric folding of plain single-expression bodies (Law 13) #2519 (with fmt) lists the same rewrite. One of the two issues should own it.

    Not labelled: items 1 and 5's extension are implementable once those two points are answered, and the second is a question of bookkeeping.

  8. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    Progress (2026-11-24), all merged:

    Left: item 1 for free functions (needs the body's type without the annotation's demand, and the two answers above: do pub signatures always stay written, and which of this issue and #2519 owns it). Four standard-library files do not check as their own entry (builtins, result, string, traits), so the tool cannot reach them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions