Skip to content

A default trait method an impl inherits is lowered with the module (#3343); code is generated for the functions a program reaches (#3351) - #3359

Merged
ehartford merged 2 commits into
mainfrom
lower-from-roots
Nov 16, 2026
Merged

ehartford merged 2 commits into
mainfrom
lower-from-roots

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

#3343: default trait method bodies are the module's. The body an impl inherits from its trait was built inside codegen (generate_default_trait_method_for_impl made its own MirBuilder), after every check on MIR had run. It is now lowered in the MIR phase (lower_default_trait_method_bodies, over the set Sema checks, in its order) and codegen takes it from the module.

  • A closure in a default method was BUG: anonymous expression lacks retained MIR; it runs.
  • with analyze lists these bodies (select:stage=mir,kind=body), so audit:resolution and the validators read them. The validators, seeing them for the first time, refused the old construction twice (no parameter count; a unit return assigned into an i32 slot); the new lowering records the count and lowers a unit body as an ordinary function does.
  • With codegen units the body belongs to one unit, by the plan, like any function (every unit emitted it and all but unit 0 deleted theirs).

#3351: codegen skips the declared functions nothing reaches. mir_unreached_functions walks from the roots (main, entry/test/bench/hook functions, a function with a calling-convention attribute, the functions codegen calls by name, every trait impl method, and every body that is not a declared function's own) over the functions each body's MIR names, plus the functions a global's initializer names. Codegen leaves an unreached function an external declaration.

  • Every body is still lowered and checked: this decides what is emitted, never what is accepted (the tail-call, await-guard and D66 callback-pair checks run on MIR; lowering only reached functions would have skipped them).
  • A unit with no main keeps everything. An analysis generates everything (its facts are per declaration; deep-debug-tool-tests asks for the matrix of a method main never calls).
  • A reference the walk missed is an undefined symbol at link, never a wrong program. The names codegen calls directly are one list (sema_codegen_named_fns), and codegen refuses a name not on it.

Measured. LLVM time for a 31-line test that imports Sema: 12.6 s -> 4.2 s (dev compiler). internals-tests lane: 337 s -> 260 s. Battery wall 805 s. Battery: GREEN.

Tests: behav_3343_default_method_body_is_the_modules (the closure; a function only the default body names), behav_3343_analysis_sees_default_method_bodies.

Closes #3343.

Eric Hartford added 2 commits November 16, 2026 01:51
…3343); code is generated for the functions a program reaches (#3351)
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.

with analyze audit:resolution says ok over a call whose MIR argument count differs from Sema's resolution (the #3335 program)

1 participant