Skip to content

Codegen: a built-in enum/struct display frees its parts once joined; a formatted str is a copy (#1392) - #1550

Merged
ehartford merged 1 commit into
fix-1390-fstring-temp-leakfrom
fix-1392-error-display-leak
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1390-fstring-temp-leakfrom
fix-1392-error-display-leak

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

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

Audits (tools/drop_audit.w + tools/move_audit.w, candidate = this layer's stage1, baseline = the pinned seed): drop-audit 190 cells, 0 regressions (display_parts_{enum,nested,struct} FIXED); move-audit unchanged.

Codegen: a built-in enum/struct display frees its parts once joined; a formatted str is a copy (#1392)

IoError.to_str() is the error declaration's generated Display, whose body
is f"{self}": the built-in display of the enum. That display builds
"Os(" ++ display(code) ++ ", " ++ display(path) ++ ")" with
with_str_concat_ref, which only reads its inputs, and dropped nothing: every
intermediate join and every formatted payload leaked. Any enum with a
payload (Option, Result, user enums, nested) and any struct under :?
leaked the same way.

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

  • run --debug-alloc of the issue repro: leak count=5 (four 16-byte, one
    32-byte blocks).
  • WITH_DEBUG_ALLOC_TRAP_FREE on each block under lldb, backtrace at
    dbg_trap_alloc_check: four are with_str_concat_ref and one is with_fmt_i32,
    all called from IoError.to_str at fs.w:64 (the generated body); no free.

Exact lines: CodegenDispatch.gen_display_enum_variant /
gen_display_nullable_option / gen_debug_struct, each
result = self.mir_str_concat(result, part). One more line made freeing
unsafe: coerce_typed_val_to_str returned a str value ITSELF
(if val_ty == str_ty: return val), so a formatted str payload aliased the
payload the enum still owns (and a formatted top-level str aliased the
value its FMT_TO_STR statement temp then drops).

Fix: coerce_typed_val_to_str always returns the caller's own str (a str is
copied; an identity result of the scalar path is copied), and the compound
displays join through mir_fmt_join, which frees both inputs — literals are
left alone by with_str_free's ownership check.

Matrix (base leaks -> fixed, stdout identical in every cell): the issue's
read_file/IoError.to_str 5->0; an error type's to_str, display(),
debug_str(), f"{e}", f"{e:?}" 11->0 each; plain enum {v}/{v:?}
5->0; struct :? 12->0; formatting then reading the str payload 33->0;
nested enum with Option[str] 38->0; Option[str] 8->0; Option[&str] view
4->0 (the map's value still reads after); Result 14->0; struct holding an
enum 45->0; Option[i32] 12->0.

Fixture: test/debug_alloc/da_enum_display_payload_parts.w (base: leak
count=152; fixed: 0). The drop audit had no display cell:
tools/drop_audit.w gains display_parts_{enum,nested,struct} (LEAK on the
base, PASS fixed).

@ehartford
ehartford added this pull request to stack #1556 September 23, 2026 19:42
…a formatted str is a copy (#1392)

IoError.to_str() is the `error` declaration's generated Display, whose body
is `f"{self}"`: the built-in display of the enum. That display builds
"Os(" ++ display(code) ++ ", " ++ display(path) ++ ")" with
with_str_concat_ref, which only reads its inputs, and dropped nothing: every
intermediate join and every formatted payload leaked. Any enum with a
payload (Option, Result, user enums, nested) and any struct under `:?`
leaked the same way.

Route: leak. Evidence on origin/main ba2d5d1's stage1:
- `run --debug-alloc` of the issue repro: leak count=5 (four 16-byte, one
  32-byte blocks).
- WITH_DEBUG_ALLOC_TRAP_FREE on each block under lldb, backtrace at
  dbg_trap_alloc_check: four are with_str_concat_ref and one is with_fmt_i32,
  all called from IoError.to_str at fs.w:64 (the generated body); no free.

Exact lines: CodegenDispatch.gen_display_enum_variant /
gen_display_nullable_option / gen_debug_struct, each
`result = self.mir_str_concat(result, part)`. One more line made freeing
unsafe: coerce_typed_val_to_str returned a str value ITSELF
(`if val_ty == str_ty: return val`), so a formatted str payload aliased the
payload the enum still owns (and a formatted top-level str aliased the
value its FMT_TO_STR statement temp then drops).

Fix: coerce_typed_val_to_str always returns the caller's own str (a str is
copied; an identity result of the scalar path is copied), and the compound
displays join through mir_fmt_join, which frees both inputs — literals are
left alone by with_str_free's ownership check.

Matrix (base leaks -> fixed, stdout identical in every cell): the issue's
read_file/IoError.to_str 5->0; an `error` type's to_str, display(),
debug_str(), `f"{e}"`, `f"{e:?}"` 11->0 each; plain enum `{v}`/`{v:?}`
5->0; struct `:?` 12->0; formatting then reading the str payload 33->0;
nested enum with Option[str] 38->0; Option[str] 8->0; Option[&str] view
4->0 (the map's value still reads after); Result 14->0; struct holding an
enum 45->0; Option[i32] 12->0.

Fixture: test/debug_alloc/da_enum_display_payload_parts.w (base: leak
count=152; fixed: 0). The drop audit had no display cell:
tools/drop_audit.w gains display_parts_{enum,nested,struct} (LEAK on the
base, PASS fixed).
@ehartford
ehartford force-pushed the fix-1392-error-display-leak branch from 9b2f763 to 1a1168f 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 0dc3e54 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1392-error-display-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.

IoError.to_str() leaks five allocations per call

1 participant