Skip to content

len-cast: .len() as i32 is the census' largest ceremony pattern; D108's isize length needs index/range positions or a checked narrowing so the cast disappears #2522

Open

Description

@ehartford

.len() as is the census' largest lexical ceremony pattern (len-cast), and with fmt cannot remove it (#2519, section C): under D108 len() returns isize, so most of these casts narrow to i32, and dropping one changes the program's meaning. The fix is in the language, not a formatter.

Counts today (.len() as <T>, tracked .w under src/ lib/ rt/ tools/ build/ examples/, corpora included in this rough count):

target src whole tree
i32 779 839
i64 55 65
u64 17 18
u32 4 5

(The census bar still records len-cast src 4487: ~/.local/with-green/ceremony-bar.tsv was last written 2026-10-08, so the bar is stale, see below.)

Two classes

  1. as i64 (65): removable now. isize is 64 bits on 64-bit hosts and 32 on wasm32 (D108), so isize → i64 always widens, and D114 makes widening implicit. These are Sema-provable and belong on with fmt: configurable line width and symmetric folding of plain single-expression bodies (Law 13) #2519's automatic list (item 10).
  2. as i32 (839): needs a ruling. The code reads for i in 0..xs.len() as i32, let n = s.len() as i32, an i32 field set from a length. The cast exists because the surrounding code holds counts and indices in i32 (v[i] takes an i32 index) while len() is pointer-width. A person reading it sees no meaning in the cast; it's representation leaking through (Law 1).

Directions to brief (the "do the thing" procedure: references, spec text, mission fit, prediction):

  • (a) Index and range positions accept isize directly, so for i in 0..xs.len() and xs[i] need no cast and the code that holds positions moves to isize (D114 already says no code depends on isize's width).
  • (b) An i32 context accepts len() with a checked narrowing (panic on overflow, like out-of-range indexing, D71), so the cast disappears where the value is genuinely small.
  • (c) Both: positions become isize, and the remaining i32 storage (AST fields, serialized counts) narrows with a checked conversion spelled once at the boundary.

Option (b) on its own risks a silent-truncation reading; whichever is chosen, a narrowing must never be silent wraparound.

Done when: a ruling exists, .len() as i32 falls to the sites where the narrowing is a real boundary (a C struct field, a serialized width), .len() as i64 is 0, and the census bar for len-cast is lowered to match.

Also found: the census bar file has not been refreshed since 2026-10-08, although :install-user is documented to publish it on every reseed. The gate is measuring against counts that are 12 days old, so a rise up to the old bar passes silently. build.w:4841 makes install-user depend on ceremony-bar-publish, yet ~/.local/bin/with was installed 2026-10-20 21:31 while the bar is from 10-08: the publish step is not landing where the census reads it (or is skipped). That needs its own look.

Related: D108, D114, D71, #2519, #2261 (the facade-side analogue: a count parameter takes a non-negative isize without a cast).

Activity

  1. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced, and the next step is a ruling. An index position takes isize today (for i in 0..xs.len(): s += xs[i] compiles), but passing a length to an i32 parameter still needs the cast:

    $ with run main.w   # let n = xs.len(); takes_i32(n)
    error: implicit integer narrowing or sign change from `isize` to `i32` (argument 1 of `takes_i32`); use an explicit `as` cast (§4.2.6)
    

    846 lines under src/ still contain .len() as.

    The question: which language change removes the cast: lengths stay isize and the receiving parameters widen (a sweep), a checked narrowing that panics out of range, or something else? §4.2.6 forbids implicit narrowing today.

  2. added this to the post-v0.16.0 milestone on Oct 28, 2026
  3. ehartford commented on Nov 2, 2026

    @ehartford
    ContributorAuthor

    Ceremony audit 2026-11-02: this is the one item that needs Eric's ruling. Verified today with the pinned seed:

    let xs = [1, 2, 3]
    let n = xs.len()
    let half: i32 = n / 2
    
    error: implicit integer narrowing or sign change from `isize` to `i32`; use an explicit `as` cast (§4.2.6)
    

    for i in 0..xs.len() and xs[xs.len() - 1] already work with no cast; the refusal is at an i32 demand. Current counts: .len() as i32 src 843 (census bar), whole tree 839+ as in the body above.

    Brief (D98 shape): Go (int everywhere, no cast), Zig (usize, @intCast at every narrowing), Rust (usize, as/try_into), Swift (Int everywhere, no cast). The spec (§4.2.6) refuses silent narrowing; D108 made len() isize and called these casts legacy; D114 says no code assumes a width. Mission: a narrowing that can lose data is two meanings (wrap or trap), so a silent narrowing is out; a checked one (panic on overflow, as xs[i] panics out of range, D71) is one meaning and fits. Prediction (80%): option (c) — positions are isize and an i32 demand accepts an isize with a checked narrowing, so the cast disappears everywhere the value is small and the remaining as i32 sites are real boundaries (a C struct field, a serialized width). Waiting on Eric.

  4. added
    ceremony
    on Nov 2, 2026
  5. ehartford commented on Nov 2, 2026

    @ehartford
    ContributorAuthor

    Conformance audit 2026-11-02: the spec's own §13.6 example fails on this: let sorted_index: BTreeMap[str, i32] = [w: i for (i, w) in vocab.enumerate()] → "implicit integer narrowing or sign change from isize to i32" (the enumerate index is isize, the map's value i32). A ruling here also makes the spec's example compile.

  6. ehartford commented on Nov 2, 2026

    @ehartford
    ContributorAuthor

    D183: ruled by Eric 2026-11-02 (spec v7.35, #2970): the general checked narrowing, as is the wrap, positions are isize, comptime out-of-range is an error, the lint names the narrow declaration. Implementation next.

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

    @ehartford
    ContributorAuthor

    D183's checked narrowing landed in #2989 (merged). What remains here is the .len() as i32 sweep: with positions isize (D108/D114) most casts can go; each site that stays a conversion now warns naming the narrow declaration, which is the worklist.

  9. ehartford commented on Nov 18, 2026

    @ehartford
    ContributorAuthor

    Not a ruling: D183's checked narrowing landed, so what is left is the sweep of .len() as i32 sites, which is mine. Label removed.

  10. ehartford commented on Nov 23, 2026

    @ehartford
    ContributorAuthor

    Measured 2026-11-23 on main (after D183 and the .with rename): .len() as i32 in src/ is 594 sites.

    shape sites what removing the cast needs
    let n = xs.len() as i32 (or var) 270 n is an i32 from there on, so the binding and what it flows into move to isize
    for i in 0..xs.len() as i32: 46 i becomes isize; each use of i where an i32 is demanded then narrows with a check inside the loop, unless that parameter or field moves too
    a comparison against a length 5 the other operand's type
    everything else (arguments, fields, arithmetic) 273 the i32 parameter or field it feeds

    By file: ComptimeEval 85, SemaCheck 77, Parser 59, Sema 44, MirLower 44, Codegen 40, MirCore 34, SemaDecl 32.

    So what is left is not a cast-by-cast rewrite. D183 said it: each site that stays a conversion is a place where an i32 field or parameter is what is wrong. Dropping the casts without moving those declarations would turn 594 wraps into 594 range checks, many inside the compiler's hot loops, which the ruling names as a cost. The work is the position-width migration of the compiler's own tables and indexes (the same ground as #666's id spaces), file by file, with with fix-ceremony extended to report the declaration each cast leads to.

    Order I would take: (1) teach fix-ceremony --report to print, for each len() cast, the i32 declaration that demands it, so the list is of declarations, not uses; (2) migrate the declarations with the most uses first; (3) the casts then fall away as redundant annotations do today.

  11. ehartford commented on Nov 23, 2026

    @ehartford
    ContributorAuthor

    Step one is merged (#3571): with fix-ceremony --report <entry> lists every narrowing cast of a length in two classes, from what Sema knows at the cast.

    On the compiler today: 621 length casts: 260 demanded by a declaration (a parameter, field, annotated binding or return of the narrow width; the declaration is what to widen) and 361 that set a width nothing asks for (a plain binding or a range bound; without the cast it is the length's own width).

    The pinned seed accepts D183's checked narrowing (verified with src/main), so the compiler's own source is no longer held back.

    Step two, in this order:

    1. The 361 undemanded casts: remove them file by file and re-check; put back any whose binding then meets a narrower demand (the compiler says so with the D183 lint). What stays removed is the set that never needed to be a conversion.
    2. The 260 demanded ones: widen the declaration behind them, most uses first.
  12. added
    blocked
    on Nov 24, 2026
  13. ehartford commented on Nov 24, 2026

    @ehartford
    ContributorAuthor

    Labelled blocked on #666. Measured in #3574: of the 621 length casts, the 31 that fed nothing narrower are gone; the other 590 feed a declaration written i32 (a node id, a symbol, a count field, a parameter), directly or through a binding. Removing one of those means widening that declaration, and which declarations are ids with their own width is exactly #666's question (distinct NodeId / Symbol / TypeId / FileId types). Until those types exist, widening by hand would be decided twice.

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

    blockedceremony

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions