Fix generalization of call-argument lambdas causing type errors#9243
Open
Fix generalization of call-argument lambdas causing type errors#9243
Conversation
Lambdas passed as call arguments (e.g. `list.fold_rev(dest, |item, acc| ...)`) were independently generalized at an inner rank, setting their type vars to rank 0. When those vars were then unified with outer scope vars via Rank.min in merge, the outer vars were pulled to rank 0, preventing the enclosing function from generalizing. This caused spurious TYPE MISMATCH errors when a polymorphic function with nested lambdas was tested from within its own module (via expect), while working correctly from external modules. Skip generalization for standalone lambdas that are direct call arguments, since they are consumed immediately and don't need independent generalization. Their type vars remain at the enclosing scope's rank and generalize together with the enclosing function. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The existing test only covered cross-module usage (importing the function). Add an expect inside the module itself and a CLI test that runs `roc test` on it, covering the case that was previously broken. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
See https://roc.zulipchat.com/#narrow/channel/231634-beginners/topic/Unexpected.20type.20mismatch.20with.20parameterised.20function/near/577730388
Lambdas passed as call arguments (e.g.
list.fold_rev(dest, |item, acc| ...))were independently generalized at an inner rank, setting their type vars to
rank 0. When those vars were then unified with outer scope vars via Rank.min
in merge, the outer vars were pulled to rank 0, preventing the enclosing
function from generalizing. This caused spurious TYPE MISMATCH errors when
a polymorphic function with nested lambdas was tested from within its own
module (via expect), while working correctly from external modules.
Skip generalization for standalone lambdas that are direct call arguments,
since they are consumed immediately and don't need independent generalization.
Their type vars remain at the enclosing scope's rank and generalize together
with the enclosing function.
Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com