Skip to content

Array fills and copies: N > 64 initializes and copies every element; a non-Copy fill evaluates per element (#1814 #1049 #1050) - #1821

Merged
ehartford merged 2 commits into
mainfrom
fix-fill
Sep 28, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-fill

Conversation

@ehartford

@ehartford ehartford commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Stack layer 4 of #1813. #1049+#1050 (one root cause: the >64-element array memcpy length was the element count, not the byte size), #1814 (a non-Copy fill evaluated once and byte-copied N times — N owners of one buffer; now a loop over the element places at every N, and the MIR validator refuses RK_ARRAY_FILL of a non-Copy element). Agent evidence: :gate, :drop-audit (213/0), :move-audit (105/0), :deep-debug-tool-tests green. Draft until the stack's battery is green.

@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top 3fddbecd (#1801 → #1811 → #1812 → #1821, on main 7fd175c), driven by the new seed nightly-20260927-local-1-99939352ffda: :gate rc=0 in 651 s; :battery-checks (seed-driver, fixpoint, test-with-audits incl. drop and move audits and every test lane, user-programs-safe, last-green) rc=0 in 2563 s. Earlier reds on this stack were each fixed in the layer that caused them (run 1: six lanes; run 3: the native runner skipped the missing-input check — fixed by bea5908, bottom of #1811). Merge: gh stack merge 1821 --yes --squash.

@ehartford
ehartford marked this pull request as ready for review September 28, 2026 05:11
@ehartford
ehartford force-pushed the fix-fill branch 2 times, most recently from c68e095 to 7fdb39b Compare September 28, 2026 05:12
Base automatically changed from fix-lang to main September 28, 2026 05:12
Eric Hartford added 2 commits September 28, 2026 01:12
…not its element count — the tail was stack garbage

`mir_emit_stmt`'s large-array `RK_USE` path copies an array with more than
64 elements with `llvm.memcpy`, and passed `wl_get_array_length` (the
element COUNT) as the byte length. A `[u32; 100]` copy moved 100 bytes =
25 elements; the rest of the destination kept stack garbage. `u8` was
right only because its count equals its byte size. The length is now
`wl_abi_size_of(data layout, array type)`.

The same line is #1049's root cause: a `[v; N > 64]` fill stores all N
elements into its temp (the pre-cleanup IR has every store), and the
copy of that temp into the binding went through this memcpy — `[7; 100]`
read back `7 37486657`, exactly floor(N / sizeof(T)) elements set. The
#1049 review located it here; so did this fix: with only this line
changed, every fill row of the #1049 table reads back whole. One root
cause, one change, both issues.

Evidence: the pre-fix stage1 on the new tests prints `u32 65 bad=48`,
`u32 100 bad=75`, `u32 1000 bad=750`, `u16 100 bad=50`, `u64 100
bad=88`, `pair 100 bad=94` (copy) and `u32 100 bad=75`, `u16 80
bad=40`, `u64 72 bad=63`, `i32 100 bad=75`, `pair 100 bad=94` (fill);
`u8` and `N = 64` are whole. After: every row `ok`. The C backend copies
with `sizeof` and is unaffected.

Fixes #1050
Fixes #1049
…nt at every N, as a loop over the element places — never one value copied into N slots

§4.3a / §2.3: a fill is its written form `[v, v, …]`; transport is not
duplication, and no second live value comes from one unless it is Copy.

- MirLower (the NK_ARRAY_LIT fill branch): over 64 elements, or with a
  const count, a non-Copy fill took `RK_ARRAY_FILL` — one `s.clone()`
  byte-copied into N slots, N owners of one buffer. `[s.clone(); 65]`
  segfaulted (rc 139). A non-Copy fill at any N is now
  `lower_non_copy_array_fill`: the array is zero-initialized storage
  (every element reset) registered as a statement temp, then a loop
  `arr[i] = move <v>` evaluates v per element; temps v creates drop per
  iteration, and an early exit out of v (`[f()?; 70]`) drops the
  elements already built. MIR size no longer grows with N.
- Sema (check_array_literal): a const-count fill held its value once and
  checked it once, so `[s; N]` moved `s` into N elements (a silent second
  owner). It is checked twice when N >= 2, as the literal-count desugar's
  N checks do, and a moved place is "use of moved value".
- The tool that stayed silent: `--validate-all` said ok over
  `_5 = array_fill(move _4, count=65)` of a `str`. The typed MIR
  validator now rejects an `array_fill` whose element type is not Copy
  (MirModule.sema_non_copy_fill_types, recorded from Sema beside the
  moved/dropped drop types); mir_test_non_copy_array_fill builds that MIR
  by hand.

Evidence: pre-fix stage1: `[s.clone(); 65]` → `--validate-all: ok`, run
rc 139, MIR `array_fill(move _4, count=65)`; the new test crashed after
`64 abc abc`. After: MIR `bb1: _5 = lt(_4, 65) … bb4: _3[_4] = move _7;
_4 = add(_4, 1)`; `run --debug-alloc` on the test (str fills at N = 64,
65, 100, 1000, a const 70, Drop-typed `Tok` fills at 65 and 3 with
made = dropped) → `debug-alloc: leak count=0`, no double free;
`--validate-all` and `--validate-ownership` ok. Early exit, discarded,
looped, zero-count, argument and return fills: leak count 0.

Fixes #1814
@ehartford
ehartford merged commit e26d915 into main Sep 28, 2026
@ehartford
ehartford deleted the fix-fill branch September 28, 2026 05: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.

1 participant