Skip to content

MIR: an owned temporary in an f-string hole is a statement temp, dropped once the string is built (#1390) - #1549

Merged
ehartford merged 1 commit into
mainfrom
fix-1390-fstring-temp-leak
Sep 24, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1390-fstring-temp-leak

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Draft: layer 1 of 7 in the ownership/leaks stack (batch battery pending; iterate tier + :drop-audit/:move-audit on the layer's stage1 done locally). Closes #1390.

Audits (tools/drop_audit.w + tools/move_audit.w, candidate = this layer's stage1, baseline = the pinned seed): drop-audit 190 cells, 0 regressions (the 3 new f-string cells FIXED vs seed); move-audit 0 failures on this layer's cells.

MIR: an owned temporary in an f-string hole is a statement temp, dropped once the string is built (#1390)

Formatting observes its interpolant. lower_fstring turned every OK_MOVE
interpolant into a copy (#1394's guard for move u.name), which is right
for a place an owner holds and wrong for a call or operator result: the
temporary then had no owner and nothing dropped it.

Route: leak. Evidence on origin/main ba2d5d1's stage1:

  • run --debug-alloc of the issue repro: LEAK size=16, leak count=1.
  • WITH_DEBUG_ALLOC_TRAP_FREE on the block under lldb: one trap-alloc hit,
    no free; the dbg_trap_alloc_check backtrace is alloc_str <- check at
    i1390.w:2:25, the t.slice(0, 2) call.
  • MIR of check: call slice(...) -> _3, fmt_buf_write_str(copy _2, copy _3), and no drop of _3 anywhere in the body.

Exact line: MirLower.lower_fstring, the OK_MOVE -> OK_COPY rewrite after
lower_expr(expr_node). The spec'd/debug view path had the same hole: the
str_clone_ref it makes for a &str hole was an unregistered OK_MOVE the
observing writer never consumed.

Fix: observe_interpolant reads a place in place (the #1394 case) and
materializes an owned compiler temporary (operand_moves_owned_temp: a
plain non-user, non-global local) into a registered statement temp; both
hole paths go through it.

Matrix (18 hole shapes, check(&s, c) called twice; leak count
base -> fixed, stdout identical): slice 2->0, spec 2->0, :? 2->0, ++
2->0, two holes 4->0, to_upper 2->0, f"{..}" ++ 2->0, spec'd &str view
2->0, :? &str view 2->0; unchanged 0: print(temp), .len() receiver, int
hole, if-join hole, field of temp, bound local, element, named local with
spec, move x, clone in a join arm.

Fixture: test/debug_alloc/da_fstring_owned_temp_interpolant.w (base: leak
count=20; fixed: 0).

The drop audit had no f-string cell: tools/drop_audit.w gains
fstring_hole_temp_{field,method,concat,slice,spec} (concat/slice/spec
LEAK on the base, PASS fixed). --validate-all said ok over the leaking
body; that validator has no leak check (filed separately).

@ehartford
ehartford added this pull request to stack #1556 September 23, 2026 19:42
…ped once the string is built (#1390)

Formatting observes its interpolant. lower_fstring turned every OK_MOVE
interpolant into a copy (#1394's guard for `move u.name`), which is right
for a place an owner holds and wrong for a call or operator result: the
temporary then had no owner and nothing dropped it.

Route: leak. Evidence on origin/main ba2d5d1's stage1:
- `run --debug-alloc` of the issue repro: `LEAK size=16`, leak count=1.
- WITH_DEBUG_ALLOC_TRAP_FREE on the block under lldb: one trap-alloc hit,
  no free; the dbg_trap_alloc_check backtrace is alloc_str <- check at
  i1390.w:2:25, the `t.slice(0, 2)` call.
- MIR of `check`: `call slice(...) -> _3`, `fmt_buf_write_str(copy _2,
  copy _3)`, and no drop of _3 anywhere in the body.

Exact line: MirLower.lower_fstring, the `OK_MOVE -> OK_COPY` rewrite after
`lower_expr(expr_node)`. The spec'd/debug view path had the same hole: the
str_clone_ref it makes for a `&str` hole was an unregistered OK_MOVE the
observing writer never consumed.

Fix: observe_interpolant reads a place in place (the #1394 case) and
materializes an owned compiler temporary (operand_moves_owned_temp: a
plain non-user, non-global local) into a registered statement temp; both
hole paths go through it.

Matrix (18 hole shapes, `check(&s, c)` called twice; leak count
base -> fixed, stdout identical): slice 2->0, spec 2->0, :? 2->0, `++`
2->0, two holes 4->0, to_upper 2->0, `f"{..}" ++` 2->0, spec'd &str view
2->0, :? &str view 2->0; unchanged 0: print(temp), .len() receiver, int
hole, if-join hole, field of temp, bound local, element, named local with
spec, `move x`, clone in a join arm.

Fixture: test/debug_alloc/da_fstring_owned_temp_interpolant.w (base: leak
count=20; fixed: 0).

The drop audit had no f-string cell: tools/drop_audit.w gains
fstring_hole_temp_{field,method,concat,slice,spec} (concat/slice/spec
LEAK on the base, PASS fixed). --validate-all said ok over the leaking
body; that validator has no leak check (filed separately).
@ehartford
ehartford force-pushed the fix-1390-fstring-temp-leak branch from dbf4eb8 to 7d9d90c Compare September 23, 2026 21:39
@ehartford
ehartford marked this pull request as ready for review September 23, 2026 22:32
@ehartford
ehartford merged commit d5a2d05 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1390-fstring-temp-leak branch September 24, 2026 16:55
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.

An owned str temporary interpolated in an f-string leaks: f"{t.slice(0, 2)}"

1 participant