Skip to content

#3061 slice 1: with fix-ceremony; #3068: a global named like an imported enum's variant - #3067

Merged
ehartford merged 3 commits into
mainfrom
fix-ceremony
Nov 7, 2026
Merged

ehartford merged 3 commits into
mainfrom
fix-ceremony

Conversation

@ehartford

@ehartford ehartford commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

#3061, slice 1: with fix-ceremony (Eric, 2026-11-07: "we need to make a sema based tool to automatically reduce bad ceremony"). The census counts; this rewrites, on facts Sema recorded while it checked the program, never on the text's shape.

with fix-ceremony --report <entry.w>          # every rewrite, by file and line
with fix-ceremony --apply  <entry.w>          # write them; the entry is checked again, and a file that no longer checks is restored
    --exclude <path-prefix>  --std            # std is rewritten only on request; a migrated corpus never (D112)

Passes in this slice.

  • Redundant annotation: let x: T = e where Sema typed e as T and e is typed without the annotation's demand (a read of a place, a string or f-string, a cast, a call of a non-generic function, operators over those): : T goes. A literal, a collection literal, a comprehension, a variant shorthand, an if/match, a closure or a generic call may take its type from the annotation (§4.3c, D93, D22), so those stay.
  • var never rebound: a var whose binding is never assigned again (x = …, x += …) and never passed to a mut parameter (§3.8a): let.

Sema owns the facts (D65). check_let_binding records each local let's file, and, when annotated with an initializer, the annotation's type and type node and the initializer; check_assign and check_mut_param_arg record the bindings they rebind. check_fstring now records its expression type in typed_expr_types like every other expression (it was the one initializer kind the rewriter could not see).

Proof. test/internals/ceremony_fix_test.w: a sample with every shape the two passes decide yields exactly six rewrites, the rewritten text is the expected one, and the rewritten program checks. --apply re-checks the entry and restores every file if it no longer does.

Found by the test and fixed: #3068. Its sample globals were BEFORE and AFTER, and the compiler's FnFlags enum has members of those names: MirLower's identifier lowering checked "is this a bare variant name" (variant_lookup) before the module-variable path, so the program's own str global was lowered as the enum's integer (an f-string, assert and assert_eq all saw 16384). Sema had typed the read as the global and recorded it (global_value_ident_nodes); MirLower now reads that fact and takes the variant path only when Sema did not resolve a global, as the place path already did. Pinned by behav_3068_global_named_like_imported_variant.w with lib/flags_mod_3068.w.

Not yet (the issue's later items): widening casts, List.new() → [], index loops, .clone() on str, &x at &T. Running the tool over src/ is its own PR, after this one.

Eric Hartford added 3 commits November 7, 2026 03:13
…ary (slice 1)

`with fix-ceremony --report|--apply [--std] [--exclude p] <entry.w>`
(src/CeremonyFix.w): a `let x: T = e` whose initializer Sema typed as `T`
without the annotation's demand loses `: T`; a `var` never assigned again
and never passed to a `mut` parameter becomes `let`. Sema records the
facts where it decides them (check_let_binding, check_assign,
check_mut_param_arg); check_fstring records its expression type like every
other expression. `--apply` re-checks the entry and restores every file if
it no longer checks. A migrated corpus is never rewritten (D112); std only
with --std. test/internals/ceremony_fix_test.w pins six rewrites over a
sample and that the rewritten program checks.
…/AFTER collide with FnFlags members through an imported enum's bare variant names, filed separately)
…s the global

MirLower's identifier lowering checked `variant_lookup` before the
module-variable path, so `let AFTER = "…"` beside an import whose enum has
an `AFTER` member was lowered as the enum's integer; Sema had typed the
read as the global and recorded it (global_value_ident_nodes), and MirLower
reads that fact now, as the place path already did. Pinned by
behav_3068_global_named_like_imported_variant.w with lib/flags_mod_3068.w.
@ehartford ehartford changed the title #3061: with fix-ceremony — remove the ceremony Sema proves unnecessary (slice 1) #3061 slice 1: with fix-ceremony; #3068: a global named like an imported enum's variant Nov 7, 2026
@ehartford
ehartford marked this pull request as ready for review November 7, 2026 12:14
@ehartford
ehartford merged commit 8e33ab2 into main Nov 7, 2026
@ehartford
ehartford deleted the fix-ceremony branch November 7, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A module's own global is shadowed by a bare variant name of an enum an imported module declares

1 participant