You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
with fix-ceremony: a Sema-driven rewriter that removes ceremony the compiler can prove unnecessary #3061
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:
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.
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.
var never rebound — a var whose name is never assigned again and never passed to a mut parameter (§3.8a): let.
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.
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.
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).
.clone() on a str (D111) and explicit &x at a &T parameter — already typed census patterns; the rewriter removes them.
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.
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.
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.
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 Twithout 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:
A pub function's signature is its contract (law 6). I would leave every pub return type written and rewrite private functions only.
the tool counted code this target never checked as proof a name was unused: a var assigned only in a comptime if Target.os == .Windows arm became let and the Mac re-check passed. It now withholds those rewrites;
a var written through &raw mut was "never rebound";
the compile-time evaluator refused a field store into a let, which Sema accepts and run time performs.
Proven on macOS, linux-aarch64 and windows-x86_64 before merge.
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.
Activity
D194 (#3010): retained pointer cases of a variadic contract; census p…
#3061 slice 1: with fix-ceremony; #3068: a global named like an impor…
ehartford commentedon Nov 7, 2026
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.#3061 slice 2: fix-ceremony removes widening casts and collection con…
#3061 slice 2: fix-ceremony removes widening casts and collection con…
ehartford commentedon Nov 7, 2026
Direction (Eric, 2026-11-08): the rewriter is ultimately exposed to users as
with fmtor 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--applystays), 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.#3061 slice 3: fix-ceremony turns an index loop that only reads its l…
#3061 slice 4: fix-ceremony rewrites the census's typed patterns (#3075)
fix-ceremony: facts proven on the compiler, --check-with, and the src…
§16.3 words and `runs threads`; by-value producer field (§16.2b.4); a…
The compiler's own source takes the rewrites the compiler proves: 1,2…
with fix-ceremony: --help, a named refusal for an option it does not …
ehartford commentedon Nov 24, 2026
State after #3594, against the issue's list of rewrites:
varnever reboundsrc/in #3576List.new()/HashMap.new()where a literal is typed by useletwith its type written; not for struct-literal fields and arguments.clone()on a str,&xat a&Tparameter,Some(x)at an Option demandAlso fixed in #3594:
--helpwas read as the entry file.What item 1 needs before any code. Removing
-> Tis only safe when the body's type isTwithout the return's demand:fn f() -> i64: 1, an implicitOkwrap 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 everyreturn e, and a recursive function needs the annotation to be checked at all. Two policy points are not mine:pubfunction's signature is its contract (law 6). I would leave everypubreturn type written and rewrite private functions only.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.
with fix-ceremony: List.new() / HashMap.new() where a field, an argum…
The compiler's own source takes the empty literal where the type is d…
with fix-ceremony: a trait impl method's return type that restates th…
The standard library takes fix-ceremony's rewrites; the tool no longe…
ehartford commentedon Nov 25, 2026
Progress (2026-11-24), all merged:
A trait impl method that omits its return type is checked against the trait's as a written one is; one report per return mismatch #3598 a trait impl method that omits its return type is checked against the trait's as a written one is (it was an internal compiler error for a
&Selftail); one report per return mismatch.with fix-ceremony: a trait impl method's return type that restates the trait's is removed (part of #3061) #3599 the tool removes a trait impl method's return type that restates the trait's. This is item 1 for the case that needs no policy answer.
The standard library takes fix-ceremony's rewrites; the tool no longer trusts code this target did not check; the compile-time evaluator stores into a field of a let (part of #3061) #3601
lib/stdswept, file by file (38 files; 42 trait returns, 199var, 186 widening casts, 148 empty constructors). The sweep found three defects, fixed in the same PR:varassigned only in acomptime if Target.os == .Windowsarm becameletand the Mac re-check passed. It now withholds those rewrites;varwritten through&raw mutwas "never rebound";let, which Sema accepts and run time performs.Proven on macOS, linux-aarch64 and windows-x86_64 before merge.
Left: item 1 for free functions (needs the body's type without the annotation's demand, and the two answers above: do
pubsignatures 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.with fix-ceremony: a private function's return type that its body alr…