Skip to content

Resolve fixed-arity parameter generics against the receiver - #39

Draft
apiology wants to merge 4 commits into
apiology-1196-literal-inferencefrom
fix/issue-1242-fixed-arity-generic-receiver
Draft

Resolve fixed-arity parameter generics against the receiver#39
apiology wants to merge 4 commits into
apiology-1196-literal-inferencefrom
fix/issue-1242-fixed-arity-generic-receiver

Conversation

@apiology

@apiology apiology commented Aug 2, 2026

Copy link
Copy Markdown
Owner

Summary

Stacked on castwide#1223 (this PR's base branch is apiology-1196-literal-inference, which introduces receiver_type plumbing into TypeChecker).

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. 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 against the receiver.

Change

Adds #resolve_param_type_against_receiver and calls 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. Mirrors the unwrap-and-resolve #restarg_problems_for already does for restargs.

User impact

y = [1]; y << 'two' is now flagged as a type error at strict/strong levels, since Array#<< is a fixed-arity method whose single parameter is typed as the generic E. Previously only restarg methods like Array#push caught this class of mistake.

Tests

Added two specs to spec/type_checker/levels/strict_spec.rb:

  • catches a bad #<< argument against a fixed-arity generic parameter resolved against the receiver
  • does not flag a #<< argument matching the receiver element type (no false positive)

Full bundle exec rspec suite passes (1656 examples, 0 failures). rubocop clean. Self-typecheck (solargraph typecheck --level strong) shows zero new violations vs. base branch.

Closes castwide#1242

apiology and others added 4 commits July 30, 2026 05:47
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.
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
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.

2 participants