Repository navigation
Instantiate a lambda's caller generics when its body captures none of their values - #448
Merged
Rigidity merged 1 commit intoNov 19, 2026
Conversation
… 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.
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.
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:
apply_to((fn(_x) => apply_to((fn(y) => y, 1)), "hi")).lambda_before_its_value((fn(x) => x, 1), depth - 1)insidelambda_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:
LambdaBodyrecords the HIR and symbol ids allocated while the lambda compiled, andcaptures_genericsscans them.x as T,let y: T = x,ident::<T>(x)), which makes values typed asTthat don't come fromx.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 leftfn(x: T) -> T. Solet r: 5 = f((fn(x) => x, 1), fn(y) => y, 5)failed thoughf::<Int, 5, 5>compiles, even without recursion.Newly compiled, each as its explicit instantiation does:
lambda_before_its_value;nested_call_to_the_same_functionandcallback_of_an_enclosing_call_left_alone;lambda_with_other_captures,lambda_with_its_own_bindingandtwo_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_bindingandcaptured_in_a_nested_lambda;names_the_callers_generic(all three ways to nameT);one_of_two_lambdas_captures;nested_call_to_the_same_functionandcallback_of_an_enclosing_callin_errors, which capturexandcallback.Fuzzer for the whole stack
infer-fuzz --compareagainst baselines from main6aea07a5:Every change starts from a rejected variant, and no diagnostic leaks a
{generic N}.Checks on the top branch:
cargo nextest run --workspacepassed (1446 tests).-D warnings(including wasm), fmt, rue fmt and machete are clean.compare_implandmemo.rsare 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 --compareagainst baselines from main6aea07a5, on all four configurations (generators 1 and 2, seeds0..4000and4000..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:
Any; it's limited to a real list end now.T(x as T), so a value escaped with the wrong type. It's fixed, and pinned innames_the_callers_generic.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:
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:LambdaParameterandLambdaBody::captures_generics.list.rs: items past the end of the expected pairs.field.rs:wrapped_atandkeeps_reference.compiler.rs:ArgumentFrame::lambda_parametersholdsLambdaParameters.rue-hir/database.rs:next_hir_id,hirs_betweenandsymbol_count.tests/types/generic_inference_*: new tests and moved pins.