Skip to content

Count an argument's expected type when it's what the call's solution has to be - #445

Merged
Rigidity merged 2 commits into
mainfrom
rigidity/inference-restructure-13e6
Nov 19, 2026
Merged

Rigidity merged 2 commits into
mainfrom
rigidity/inference-restructure-13e6

Conversation

@Rigidity

@Rigidity Rigidity commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

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

An argument's expected type that references the callee's generics was always a hint. Now it counts when both of these hold:

  • The call's own expected type is final: not a hint and not fallback-only.
  • The callee's generics in that parameter occur only where values flow out of the argument (occurs_contravariantly).

In that case a solution that fits the call's expected type has the argument fit it. So let r: (5, Int) = wrap(map(fn(x) => x, 5)) solves map for 5, as wrap::<5>(map::<5, 5>(...)) does.

  • Contravariant spots stay hints. In a spot like compose's A in fn(a: A) -> B, what's inferred so far can be wider than the solution. The fuzzer found chain::<Int | nil, 5, Int>(compose(fn(_x) => 5), compose(feed(fn(x) => x, five_only)), nil) regressing on an earlier version, and it's pinned as argument_whose_generic_takes_values.
  • Later arguments don't matter. The call's expected type only bounds the generics from above, so wrap_after(map(fn(x) => x, 5), 6) with (5 | 6, Int) expected compiles, as wrap_after::<5 | 6>(map::<5, 5>(...), 6) does.
  • A guard's type is still only a hint. wrap(map(fn(x) => x, 5)) is (5, Int) still checks at runtime (argument_of_a_guarded_call).
  • Still rejected, as on main. wrap_with(1, map(fn(x) => x, 5)) with (5, Int) expected: no instantiation gives it that type. It's pinned.

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.

…has to be

An argument's expected type that references the callee's generics isn't a hint when the call's own
expected type is a type of its own and no argument still to be compiled can add to those generics:
the call's solution has to fit it then, and a narrower solution for the argument only narrows the
call's, which its expected type then still holds. So `wrap(map(fn(x) => x, 5))` with `(5, Int)`
expected compiles, as `wrap::<5>(map::<5, 5>(...))` does.
… it are covariant

Where values of the callee's generics flow into the argument, such as `A` in `fn(a: A) -> B`, what
they're inferred as so far can be wider than the call's solution, which would ask more of the
argument than the call does: `compose(feed(fn(x) => x, five_only))` then gave `feed` the
`fn(value: 5) -> Int` type that `compose` can't take. Where they only flow out of it, a solution
that fits the call's final expected type has the argument fit it, whatever the arguments after it
add, since the type only bounds them from above, so that condition is dropped:
`wrap_after(map(fn(x) => x, 5), 6)` with `(5 | 6, Int)` expected compiles, as
`wrap_after::<5 | 6>(map::<5, 5>(...), 6)` does.
@Rigidity
Rigidity added this pull request to stack #449 November 19, 2026 08:31
@Rigidity Rigidity changed the title rigidity/inference restructure 13e6 Count an argument's expected type when it's what the call's solution has to be Nov 19, 2026
@Rigidity
Rigidity merged commit 9956bdc into main Nov 19, 2026
6 checks passed
@Rigidity
Rigidity deleted the rigidity/inference-restructure-13e6 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