Skip to content

Instantiate a lambda's caller generics when its body captures none of their values - #448

Merged
Rigidity merged 1 commit into
rigidity/inference-restructure-13e8from
rigidity/inference-restructure-13e9
Nov 19, 2026
Merged

Rigidity merged 1 commit into
rigidity/inference-restructure-13e8from
rigidity/inference-restructure-13e9

Conversation

@Rigidity

@Rigidity Rigidity commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #362 (step 13e, layer 9 of 9).

13e.2 and 13e.3 check a lambda whose untyped parameter was compiled as an uninferred callee generic as that generic's solution, since its body knows nothing about it. They skipped two cases and pinned them as errors, though explicit instantiation compiled:

  • Shared generics. A call nested in an argument of a call to the same function shares its generics, as in apply_to((fn(_x) => apply_to((fn(y) => y, 1)), "hi")).
  • A restored caller's generic. In a recursive call, the parameter is the caller's generic standing for a renamed one, as in lambda_before_its_value((fn(x) => x, 1), depth - 1) inside lambda_before_its_value<T>.

In both cases the body could mix the parameter with outside values of the generic. Now such a lambda is instantiated when two things hold:

  • It captures none of them. Its body references no parameter or binding from outside it whose declared type has the generics. LambdaBody records the HIR and symbol ids allocated while the lambda compiled, and captures_generics scans them.
  • Its type doesn't name them. With its restored parameters replaced, the lambda's type doesn't name the caller's generic. The body can name it (x as T, let y: T = x, ident::<T>(x)), which makes values typed as T that don't come from x.

The decision is worked out once per lambda, as each argument is compiled.

Alternative solve. In 13c's alternative solve (solve_with_lambda_parameters), these parameters aren't vars. They're inferred as the callee's generics and instantiated with that solve's mappings. Before, substituting the vars saw through their references and left fn(x: T) -> T. So let r: 5 = f((fn(x) => x, 1), fn(y) => y, 5) failed though f::<Int, 5, 5> compiles, even without recursion.

Newly compiled, each as its explicit instantiation does:

  • lambda_before_its_value;
  • nested_call_to_the_same_function and callback_of_an_enclosing_call_left_alone;
  • lambda_with_other_captures, lambda_with_its_own_binding and two_lambdas_before_their_value;
  • lambda_returning_its_value;
  • lambda_before_its_value_next_to_another_lambda, recursive and not.

Still rejected, and pinned:

  • passes_on_to_the_caller, captured_in_a_binding and captured_in_a_nested_lambda;
  • names_the_callers_generic (all three ways to name T);
  • one_of_two_lambdas_captures;
  • nested_call_to_the_same_function and callback_of_an_enclosing_call in _errors, which capture x and callback.

Fuzzer for the whole stack

infer-fuzz --compare against baselines from main 6aea07a5:

Generator Seeds Variants Newly inferred Regressed Unsound Inferred variants changed
1 0..4000 11638 25 0 0 0
1 4000..8000 11641 26 0 0 0
2 0..4000 14785 46 0 0 0
2 4000..8000 14725 46 0 0 0

Every change starts from a rejected variant, and no diagnostic leaks a {generic N}.

Checks on the top branch:

  • cargo nextest run --workspace passed (1446 tests).
  • clippy -D warnings (including wasm), fmt, rue fmt and machete are clean.
  • Each layer passes the snapshots on its own.
  • compare_impl and memo.rs are untouched, so the memo differential wasn't needed.

The stack (#362 step 13e, layers 6–9)

These layers close the gaps that the merged 13e layers (#438–#442) left, so the generics campaign ends with no follow-ups. Each layer is its own PR:
6. An argument's expected type counts when it's what the call's solution has to be: wrap(map(fn(x) => x, 5)) with (5, Int) expected.
7. List items past the end of the expected pairs: first(([2], [3, make()])).
8. Struct fields and inner pairs keep a lambda parameter's generic references.
9. A lambda's shared or caller generics are instantiated when its body captures none of their values: the soundness limits that 13e pinned.

Fuzzer, per layer. infer-fuzz --compare against baselines from main 6aea07a5, on all four configurations (generators 1 and 2, seeds 0..4000 and 4000..8000). Every layer has 0 regressions and 0 unsound variants, changes no variant that main infers, and leaks no {generic N}. Newly inferred variants, cumulative: 37 (6), 40 (7), 40 (8), 143 (9). The top PR has the full table.

Breaking changes. None. No program that compiles on main changes its type, CLVM output or diagnostics. Pinned errors that now compile get the type that explicit instantiation gives, and each is a test.

Review. Three rounds:

  • Round 1 (three reviewers):
    • Three conditions in layer 6 had no test. Two are pinned now, and the third, that no later argument writes the generic, is dropped, since it only rejected sound programs.
    • Layer 7 also fired where the expected type goes on as Any; it's limited to a real list end now.
    • Layer 9's alternative solve lost the instantiation of a lambda before its value, which is fixed.
  • Round 2 (three reviewers): a blocker in layer 9. In a recursive call the lambda body could name the caller's T (x as T), so a value escaped with the wrong type. It's fixed, and pinned in names_the_callers_generic.
  • Round 3 (two reviewers): correctness found nothing. A test gap for the per-lambda cache is filled by one_of_two_lambdas_captures.

Mutant rule. Every mutant ran in a scratch worktree, under timeout 120, after a separate build step.

Note

Medium Risk
This changes which types are inferred for calls with lambda arguments, list literals and field accesses, all in the compiler's call inference. A mistake would reject valid programs or accept unsound ones. The risk is limited by these points:

  • Every new solution is checked against the call before it's used.
  • Three review rounds probed soundness, and their one unsound case is pinned as an error.
  • A per-layer fuzzer comparison shows 0 regressions and 0 unsound variants.

Changes by area:

  • call_inference.rs: the hint rule for an argument's expected type; which lambda parameters are instantiated (generic_parameter_types, names_restored_generics); instantiation in the alternative solve.
  • lambda.rs: LambdaParameter and LambdaBody::captures_generics.
  • list.rs: items past the end of the expected pairs.
  • field.rs: wrapped_at and keeps_reference.
  • compiler.rs: ArgumentFrame::lambda_parameters holds LambdaParameters.
  • rue-hir/database.rs: next_hir_id, hirs_between and symbol_count.
  • tests/types/generic_inference_*: new tests and moved pins.

… their values

A lambda whose untyped parameters were compiled as the callee's generics is checked with them as
what the call inferred, since its body knew nothing about them and so works for any type in their
place. That wasn't done when the generics were also the caller's: in a call nested in an argument
of a call to the same function, which shares its generics, or for a caller's generic that stands
for a renamed one in a recursive call, such as `x: T` in `f((fn(x) => x, 1))` inside `f<T>`. There
the body can also reference values of the generics from outside of it, such as the caller's
parameters, which would be unsound to mix with what the parameter is checked as.

Now those lambdas are checked the same way when their bodies don't reference a parameter or a
binding from outside of them whose type has the generics, which is found from the nodes and
symbols allocated while each lambda was compiled, and, for a caller's generic, when their types
don't name it other than through those parameters: the body can name it, as in `x as T`, which
makes values of it that don't come from the parameters as far as the lambda's type says. Which parameters are checked this way is worked
out once per argument, as it's compiled.

When the call is solved again with the other lambdas' parameters as vars, those parameters are
inferred as the callee's generics too, and checked as what that solution makes them: substituting
the vars into a lambda's type saw through their references, which left a lambda before its value
as `fn(x: T) -> T` there, even without a renamed generic. So the calls that compiled with an
explicit `::<...>` but not inferred compile now, as they do with it.
@Rigidity
Rigidity added this pull request to stack #449 November 19, 2026 08:31
@Rigidity
Rigidity merged commit 9956bdc into main Nov 19, 2026
6 checks passed
@Rigidity
Rigidity deleted the rigidity/inference-restructure-13e9 branch November 19, 2026 08:37
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