Skip to content

Resolve an untyped lambda parameter from the value that flows into it - #450

Open
Rigidity wants to merge 7 commits into
mainfrom
rigidity/lambda-var-lower-bounds
Open

Rigidity wants to merge 7 commits into
mainfrom
rigidity/lambda-var-lower-bounds

Conversation

@Rigidity

@Rigidity Rigidity commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #362

let r: 5 | Bytes = map(fn(x) => x, 5) (with fn map<A, B>(function: fn(a: A) -> B, value: A) -> B) was rejected, though map::<5, 5> compiles, and so do let r: 5 = map(fn(x) => x, 5) and let s: 5 | Bytes = id(5). It now infers A = B = 5. This is a correctness change: only rejected programs change.

Cause

In the final solve, the untyped parameter x gets a var X (Inference::add_var, #335).

  • X's upper candidates: Int, the type the lambda body was compiled with, since A had been widened while the arguments were compiled, and 5 | Bytes, passed on from the expected type through B ≥ X.
  • No lower candidates: A ≤ X is only recorded on A's side, as its upper candidate X.
  • The pick: neither upper is assignable to the other, so pick_inferred_type falls back to the first, Int, and A and B follow it. With an expected 5, 5 is the most specific upper, which is why that case worked.

What changed

add_lambda_var_lower_bounds (infer/bounds.rs) runs in CallSolver::solve, after the expected type's bounds have been added. It gives each added var the generics it's an exact upper candidate of as lower candidates, under the same union members. X and A are then resolved together, from A's value and all of X's upper candidates, as the explicit instantiation is. Inference gets an added() accessor.

A var's type is a bare parameter type, never a union containing the var, so A ≤ X isn't collected under a union member in practice. The members are carried over anyway, so the new candidate holds exactly when the one it came from does.

Behaviour change, measured

infer-fuzz --compare against baselines from main 09000478:

Generator Seeds Variants Regressed Newly inferred Rejected, diagnostic or fingerprint changed
1 0..4000 11638 0 17 3
1 4000..8000 11641 0 10 2
2 0..4000 14785 0 10 3
2 4000..8000 14725 0 4 1
  • No inferred variant changes.
  • One diagnostic change in programs that compile today (found in round 2, declared here): in let result: 5 = map(fn(x) => x, 5); result, needless_let_return no longer offers "Return the value directly". x now resolves from the value's own 5 rather than the expected type's, and the lint decides whether a binding is annotated by comparing type ids. That's what main already does for let result: 5 = id(5); result and let result: 5 = 5; result, so the call now behaves like those. Pinned by returned_binding.
  • The newly inferred variants are mostly the same shape as the issue: an untyped lambda's parameter under an expected literal union such as 5 | nil, or Maybe<7>, which was Int before.
  • No snapshot or example changes, apart from the two new tests.

Tests

tests/types/generic_inference_lambda_parameter_vars.rue, both rejected on main:

  • union_expected_type: the issue's program, with 5 | Bytes.
  • optional_expected_type: with 5 | nil.
  • returned_binding: compiles on main and here. It pins the warning without the suggestion.

Checks

  • cargo nextest run --all-features --workspace: 1450 passed.
  • Clippy with -D warnings, fmt, rue fmt and machete are clean.
  • compare_impl and memo.rs are untouched.

Review

Two rounds. Round 1 had three reviewers.

  • Correctness: nothing breaks. Across 52 probes, no program that compiles on main changed. The ten that main rejects and that now compile each give the explicit instantiation's CLVM.

  • Simplification:

    • judge the var by its matched candidates, as the resolver does;
    • move the call into CallSolver::solve;
    • use an accessor rather than a pub(crate) field.

    Done. The first point became moot when the condition was removed (see below).

  • Tests: one major finding. No test caught a mutant that dropped the condition then limiting the rule (only vars whose upper candidates didn't decide them), and the reviewer's fuzz run of the unconditional rule showed no regressions.

    • The condition had been justified by 51 fuzzer regressions. Those came from a scratch worktree that hadn't been moved onto the rebased commit, so they were main's own changes between 6aea07a5 and 09000478, not the rule's.
    • Measured properly, the unconditional rule regresses nothing and infers 9 more variants, so the condition is gone.
    • Their union-member mutant can't be observed, for the reason above, and the doc comment says so.
  • Round 2, one reviewer, built against a fresh build of 09000478: about 50 probes, with no changed output except new inferences and the needless_let_return suggestion above, which is now declared and pinned.

Note

Medium Risk
This changes inferred types in every call with an untyped lambda parameter, in the final solve, where the lambda's parameter var now also takes the generics that flow into it. The fuzzer shows 0 regressions and no changed inferred variant over 52,789 variants, and only rejected programs newly compile, with the explicit instantiation's output. No snapshots change.

  • Solver (infer/bounds.rs, infer/solve.rs, infer.rs): add_lambda_var_lower_bounds in CallSolver::solve, and the Inference::added accessor.
  • Tests: two programs that main rejects.

- Share the most specific upper bound search with pick_inferred_type
- Judge a var by the candidates of the union members that matched
- Add the lower bounds in CallSolver::solve, after the expected type's, through an accessor for the added vars
The restriction to vars whose upper candidates don't decide them guarded against fuzzer
regressions that came from comparing against an older main. Without it there are none, and nine
more variants infer.

This branch has not been deployed

No deployments
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