Fix #2134: Instantiate generics in expected function types before sub… #2140
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.
When calling
addIndex(filter)where:addIndexhas type<A, B, C>(op: ((value: A) -> B, array: {A}) -> {C}) -> ...filterhas type<A, K>(predicate: (value: A) -> boolean, tbl: {[K]: A}) -> {A}The type checker incorrectly reports:
This happens because the generics
A,B,CfromaddIndexappear in the expected type's signature, and when checking iffiltermatches, the solver treats them as the same generics asfilter's ownA, causing a name collision.Solution
The fix instantiates generics from the calling function with fresh types before performing subtyping when the expected argument type is a function type containing those generics. This ensures that generics from the outer scope don't conflict with generics from the actual function type being checked.
Specifically, in
ConstraintSolver::tryDispatchforFunctionCallConstraint, when checking argument types withpushTypeInto, we now:freshTypeandfreshTypePack)This prevents generic name collisions and allows correct type inference.