Skip to content

Sema: a generic function that calls itself binds to its instance in flight (#2533) - #2537

Merged
ehartford merged 1 commit into
d103-optionfrom
generic-recursion
Oct 21, 2026
Merged

ehartford merged 1 commit into
d103-optionfrom
generic-recursion

Conversation

@ehartford

@ehartford ehartford commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

A generic function that calls itself (#2533)

Eric's generic quicksort (fn quicksort[T](xs: List[T]) -> List[T]) exited the compiler with 139 and no diagnostic. Any generic free function that calls itself did: check_generic_call filled the specialization cache only after the body check returned, so the recursive call inside the body instantiated the same key again without end. lldb showed the cycle check_fn_body_concrete → check_fn_body_with_sig_at → … → check_generic_call → check_fn_body_concrete, 735 body checks before the fault at the stack top. The explicit-return form survived only because a cutoff ended it after hundreds of checks.

A specialization key in flight is recorded before its body check. A call that meets its own instance takes the declared return type for the instance and binds to the signature the body check added before it began; with no annotation it reads Unit, exactly as a non-generic recursive call does under D43 today (#2535 is the extension to inferred recursion).

Fixture behav_generic_fn_calls_itself.w (crashes the installed compiler, passes here). The quicksort as Eric wrote it runs with this layer.

Also filed from the quicksort: #2534 (a pipeline over a List or slice does not iterate it; a slice has no iter()), #2535 (D43 for a recursive body).

Closes #2533.

…light (#2533)

check_generic_call filled generic_specialization_cache only after the body
check returned, so the recursive call inside the body found no entry and
instantiated the same key again without end: the if-tail form overflowed
the stack (rc 139, no diagnostic), the return form survived a cutoff after
hundreds of body checks. lldb cycle: check_fn_body_concrete ->
check_fn_body_with_sig_at -> ... -> check_generic_call ->
check_fn_body_concrete, 735 body checks before EXC_BAD_ACCESS at the
stack top.

A specialization key in flight is recorded before its body check. A call
that meets its own instance takes the declared return type for the
instance and binds to the signature the body check added before it began;
with no annotation it reads Unit, as a non-generic recursive call does
under D43 today (#2535 is the extension).
@ehartford ehartford mentioned this pull request Oct 21, 2026
@ehartford
ehartford added this pull request to stack #2539 October 21, 2026 17:20
@ehartford ehartford changed the title generic recursion Sema: a generic function that calls itself binds to its instance in flight (#2533) Oct 21, 2026
@ehartford
ehartford marked this pull request as ready for review October 21, 2026 20:53
@ehartford
ehartford merged commit be23beb into main Oct 21, 2026
@ehartford
ehartford deleted the generic-recursion branch October 21, 2026 20:53
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.

A generic function that calls itself overflows the compiler's stack (rc 139, no diagnostic)

1 participant