Resolve fixed-arity parameter generics against the receiver - #39
Draft
apiology wants to merge 4 commits into
Draft
Resolve fixed-arity parameter generics against the receiver#39apiology wants to merge 4 commits into
apiology wants to merge 4 commits into
Conversation
0.68.0's PreCommit::Solargraph hook parses `solargraph typecheck` output with a regex expecting "file:line - message", but current solargraph outputs "file:line: message", so the hook always reported the confusing "Solargraph failed to run" instead of real results, even on a clean checkout. 0.71.0 fixes the regex to accept both separators, and also correctly demotes typecheck findings on lines a commit didn't touch to warnings instead of failures.
…xed-arity-generic-receiver
TypeChecker#signature_argument_problems_for already resolves a restarg's declared generic type (e.g. Elem) against the receiver's actual generic parameters (e.g. Integer for an Array<Integer> receiver) via #restarg_problems_for - but fixed-arity positional and keyword parameters still got their type straight from #signature_param_details, which calls param.typify(api_map) with no receiver context, so a generic type there was never resolved. Add #resolve_param_type_against_receiver and call it for both the :arg branch of #signature_argument_problems_for and the keyword-arg branch of #kwarg_problems_for, threading receiver_type through to the latter. This is what now lets `y = [1]; y << 'two'` get flagged, since Array#<< is a fixed-arity method whose single parameter is typed as the generic E. Closes castwide#1242
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.
Summary
Stacked on castwide#1223 (this PR's base branch is
apiology-1196-literal-inference, which introducesreceiver_typeplumbing intoTypeChecker).TypeChecker#signature_argument_problems_foralready resolves a restarg's declared generic type (e.g.Elem) against the receiver's actual generic parameters (e.g.Integerfor anArray<Integer>receiver) via#restarg_problems_for. Fixed-arity positional and keyword parameters still got their type straight from#signature_param_details, which callsparam.typify(api_map)with no receiver context, so a generic type there was never resolved against the receiver.Change
Adds
#resolve_param_type_against_receiverand calls it for both the:argbranch of#signature_argument_problems_forand the keyword-arg branch of#kwarg_problems_for, threadingreceiver_typethrough to the latter. Mirrors the unwrap-and-resolve#restarg_problems_foralready does for restargs.User impact
y = [1]; y << 'two'is now flagged as a type error atstrict/stronglevels, sinceArray#<<is a fixed-arity method whose single parameter is typed as the genericE. Previously only restarg methods likeArray#pushcaught this class of mistake.Tests
Added two specs to
spec/type_checker/levels/strict_spec.rb:#<<argument against a fixed-arity generic parameter resolved against the receiver#<<argument matching the receiver element type (no false positive)Full
bundle exec rspecsuite passes (1656 examples, 0 failures).rubocopclean. Self-typecheck (solargraph typecheck --level strong) shows zero new violations vs. base branch.Closes castwide#1242