Repository navigation
Conversation
… when its upper bounds don't decide it
- 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
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
let r: 5 | Bytes = map(fn(x) => x, 5)(withfn map<A, B>(function: fn(a: A) -> B, value: A) -> B) was rejected, thoughmap::<5, 5>compiles, and so dolet r: 5 = map(fn(x) => x, 5)andlet s: 5 | Bytes = id(5). It now infersA = B = 5. This is a correctness change: only rejected programs change.Cause
In the final solve, the untyped parameter
xgets a varX(Inference::add_var, #335).X's upper candidates:Int, the type the lambda body was compiled with, sinceAhad been widened while the arguments were compiled, and5 | Bytes, passed on from the expected type throughB ≥ X.A ≤ Xis only recorded onA's side, as its upper candidateX.pick_inferred_typefalls back to the first,Int, andAandBfollow it. With an expected5,5is the most specific upper, which is why that case worked.What changed
add_lambda_var_lower_bounds(infer/bounds.rs) runs inCallSolver::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.XandAare then resolved together, fromA's value and all ofX's upper candidates, as the explicit instantiation is.Inferencegets anadded()accessor.A var's type is a bare parameter type, never a union containing the var, so
A ≤ Xisn'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 --compareagainst baselines from main09000478:let result: 5 = map(fn(x) => x, 5); result,needless_let_returnno longer offers "Return the value directly".xnow resolves from the value's own5rather than the expected type's, and the lint decides whether a binding is annotated by comparing type ids. That's what main already does forlet result: 5 = id(5); resultandlet result: 5 = 5; result, so the call now behaves like those. Pinned byreturned_binding.5 | nil, orMaybe<7>, which wasIntbefore.Tests
tests/types/generic_inference_lambda_parameter_vars.rue, both rejected on main:union_expected_type: the issue's program, with5 | Bytes.optional_expected_type: with5 | nil.returned_binding: compiles on main and here. It pins the warning without the suggestion.Checks
cargo nextest run --all-features --workspace: 1450 passed.-D warnings, fmt,rue fmtand machete are clean.compare_implandmemo.rsare 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:
CallSolver::solve;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.
6aea07a5and09000478, not the rule's.Round 2, one reviewer, built against a fresh build of
09000478: about 50 probes, with no changed output except new inferences and theneedless_let_returnsuggestion 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.
infer/bounds.rs,infer/solve.rs,infer.rs):add_lambda_var_lower_boundsinCallSolver::solve, and theInference::addedaccessor.