Repository navigation
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 intoSep 24, 2026
Conversation
…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
force-pushed
the
fix-1392-error-display-leak
branch
from
September 23, 2026 21:39
9b2f763 to
1a1168f
Compare
ehartford
marked this pull request as ready for review
September 23, 2026 22:32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft: layer 2 of 7 in the ownership/leaks stack (batch battery pending; iterate tier +
:drop-audit/:move-auditon 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
errordeclaration's generated Display, whose bodyis
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-allocof the issue repro: leak count=5 (four 16-byte, one32-byte blocks).
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 freeingunsafe: coerce_typed_val_to_str returned a str value ITSELF
(
if val_ty == str_ty: return val), so a formatted str payload aliased thepayload 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
errortype'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).