Skip to content

A default trait method is lowered as any function's body is: it drops its owned parameters and runs its defers - #3361

Merged
ehartford merged 2 commits into
mainfrom
default-method-params
Nov 16, 2026
Merged

ehartford merged 2 commits into
mainfrom
default-method-params

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

The leak. A default trait method an impl inherits never dropped an owned parameter: a List[str] argument leaked (debug allocator: leak count=3) and a Drop type's destructor never ran. Its body had a lowering of its own (in codegen until #3359, copied into the MIR phase by it) that created the parameter locals and nothing else: no drop scheduling, no StorageLive, no defer at return, no D22 fact context, none of the passes an ordinary body gets.

Fix: one owner. lower_fn_with_sig reads what it needs of a declaration into a FnLowerShape (flags, body, parameter records, spans) and calls lower_fn_shape; a default method, which has no declaration node, builds the same shape from the trait's parameter records and the impl's copy of the body. The second lowering is gone.

The bug this exposed, exact line. Codegen.gen_module generated default method bodies in pass 1.3 and registered destructors in pass 1.5 (detect_drop_functions). A default body that drops a Drop type built that type's glue (ensure_structural_drop_fn, cached per module) while drop_fn_values was still empty: __drop_struct_<ty> was entry: ret void (read with WITH_DUMP_LLIR_PRE=1), and every drop of that type in the program called it. Destructors are now detected before any body is generated.

Tests (both fail on the compiler before this, pass after): behav_default_method_is_an_ordinary_body (destructor of an owned parameter, defer), da_default_method_drops_owned_parameters (debug allocator, leak count=0).

Filed, not in this change: #3360 (Sema checks a default body with a smaller copy of the function-body check: an implicit Ok tail there is an ICE).

Battery: GREEN (820 s).

Eric Hartford added 2 commits November 16, 2026 02:42
… its owned parameters and runs its defers; destructors are known before any body is generated
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