Skip to content

Match trailing keyword arguments to keyword/kwrest parameters, not by position - #1292

Draft
apiology wants to merge 14 commits into
castwide:masterfrom
apiology:fix-kwarg-signature-matching
Draft

apiology wants to merge 14 commits into
castwide:masterfrom
apiology:fix-kwarg-signature-matching

Conversation

@apiology

Copy link
Copy Markdown
Contributor

Summary

Call#inferred_pins matched call-site arguments to a signature's parameters purely by array index. A trailing keyword-arguments hash (e.g. use_ssl: true) is just another element in that array, so it was checked against whatever positional parameter happened to sit at that index instead of the method's keyword or kwrest parameter. The type mismatch rejected the whole overload, so a generic block-form overload lost its block-param typing whenever the call also passed a keyword argument.

Found while following up on #1290, which reported this against Net::HTTP.start, an RBS-declared generic block-form overload:

require 'net/http'

Net::HTTP.start('example.com', use_ssl: true) do |http|
  http.request(Net::HTTP::Get.new('/'))
end
$ bundle exec solargraph typecheck --level strong repro.rb
repro.rb:4: Unresolved call to request

Dropping use_ssl: true resolves http correctly, matching the original report.

Confirmed this is independent of #1290/#1289: it reproduces on master before that PR's commit, and with a plain YARD **kwrest method that has no RBS involvement at all — the gap is in call-site argument matching (Call#inferred_pins, lib/solargraph/source/chain/call.rb), not in RBS translation.

Fix

Split the trailing keyword-arguments hash out of the positional argument list and match it against the signature's keyword?/kwrestarg? parameters instead of by position.

Test plan

  • Added spec/source/chain/call_spec.rb cases using an inline RBS signature (Box.start) shaped like Net::HTTP.start (multiple optional positional params before a generic block-form overload with **kwrest), covering: kwarg matches the kwrest param, no-kwarg baseline still works.
  • bundle exec rspec — 1626 examples, 0 failures
  • bundle exec rubocop on changed files — no new offenses
  • bundle exec solargraph typecheck --level strong on changed files — 0 problems

… position

Call#inferred_pins matched call-site arguments to a signature's
parameters purely by array index. A trailing keyword-arguments hash
(e.g. use_ssl: true) is just another element in that array, so it was
checked against whatever positional parameter happened to sit at that
index instead of the method's keyword or kwrest parameter. The type
mismatch rejected the whole overload, so a generic block-form overload
lost its block-param typing whenever the call also passed a keyword
argument.

Confirmed independent of PR castwide#1289: reproducible on master before that
fix, and with a plain YARD **kwrest method (no RBS involved).

Split the keyword-arguments hash out of the positional list and match
it against the signature's keyword?/kwrestarg? parameters instead.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vue2FLfnJ3M7pM1oGdYiZy
keyword_argument_matches? inferred a single type for the entire
trailing keyword-arguments hash and asked whether ANY of an
overload's keyword/kwrest parameters accepted that type. A parameter
whose type didn't resolve (e.g. an RBS `untyped` keyword) trivially
"matched" via Parameter#compatible_arg?'s undefined-type bypass,
regardless of which keys the call actually passed. This let an
earlier overload's unrelated keyword steal the match from a later,
correct overload whenever it had such a parameter, e.g.
RBS::EnvironmentLoader#add(path:) getting matched against
add(library:, ?resolve_dependencies: boolish) because
resolve_dependencies has no resolvable type.

Fix it by walking the hash literal's actual keys and checking each
one against the identically-named parameter (or the kwrest
parameter, if present), and requiring all non-optional keyword
parameters to be present.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DyGKiHX7YHkwAoX4fHe3nU
apiology added a commit to apiology/solargraph that referenced this pull request Aug 13, 2026
…st parameters, not by position

A trailing keyword-arguments hash doesn't line up positionally with the
method's declared parameters, so Chain::Call now splits it off and matches
it separately against the overload's keyword/kwrest parameters instead of
checking it positionally by index.

Follow-up commit fixes a regression this introduced: matching by inferring
a single type for the whole trailing hash let a parameter with an
unresolvable type (e.g. an RBS `untyped` keyword) trivially "match" via
Parameter#compatible_arg?'s undefined-type bypass, regardless of which
keys the call actually passed - stealing the match from a later, correct
overload (e.g. RBS::EnvironmentLoader#add(path:) matching against
add(library:, ?resolve_dependencies: boolish) instead). Now walks the
hash literal's actual keys and checks each one against the
identically-named parameter (or kwrest, if present), requiring all
non-optional keyword parameters to be present.

Removes 2 @sg-ignore comments the fix made unneeded.

One residual, narrower issue remains and is marked `pending` in
spec/type_checker/levels/strong_spec.rb's "understands Open3 methods"
test with a citation to castwide#1292's PR comment: a
pre-existing bypass in positional_arguments_match? (param.restarg?
accepts any argument type against a *rest param) lets an earlier,
positionally-wrong overload "match" and win via first-match-wins
before a later, correct overload is tried. This predates castwide#1292 (masked
previously by less precise position-index matching) and doesn't surface
anywhere in solargraph's own codebase - confirmed via a clean full-project
strong typecheck - only in this one synthetic Open3.capture2e regression
test.
Comment thread spec/source/chain/call_spec.rb Resolved
…iter

The RBS-backed contexts in call_spec.rb reached the pins they needed
with source_map.send(:convention_pins=, conversions.pins). That writer
is private on SourceMap, so the setup depended on an implementation
detail rather than a supported entry point.

Serve the same pins from a Convention instead. RbsPinConvention
returns them from #local, and each example registers and unregisters
it, so the pins arrive through Convention.for_local in
SourceMap#initialize - the path a real gem's pins already travel.

ApiMap#index is the other public option and is what
activesupport_concern_spec.rb uses, but it does not fit here:
ApiMap#catalog rebuilds the store from the core map, doc map,
conventions environ, and the bench's iced and live pins, so pins
supplied through #index are dropped once a source is cataloged. These
contexts need both the pins and a mapped source.

The two contexts shared their tmpdir, loader, and infer_at helper, so
that setup moves into a shared context.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CXmnT5gSB1PheL9UbiGEVA
apiology added a commit to apiology/solargraph that referenced this pull request Aug 18, 2026
One conflict in spec/source/chain/call_spec.rb: this branch added a
standalone example just above the block the PR converts from a context into
a shared_context served through the public Convention API. Kept the example
and took the PR shared_context opener; both include_context call sites come
from the PR side.
@apiology
apiology marked this pull request as ready for review August 26, 2026 14:55
apiology and others added 5 commits August 26, 2026 11:17
…-matching

# Conflicts:
#	lib/solargraph/source/chain/call.rb
Solargraph types every target of a multiple assignment from a
tuple-returning call as the tuple's first element, so the
positional/keyword split produced a strong-typecheck error. Returning
the trailing keyword Chain alone and deriving the positionals from it
avoids the tuple.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
apiology added a commit to apiology/solargraph that referenced this pull request Oct 27, 2026
Hand-parsing rbs_collection.yaml needed a @type cast over untyped
YAML. RBS::Collection::Config parses the file into typed sources with
the relative-path handling rbs collection itself uses. Restore
Dir.glob(base:) and mark its findings with castwide#1292, which fixes them,
and mark the grep(Local) finding with #358.
keyword_argument_matches? fed a call like `bar(*, **, &)` to
convert_hash, whose s(:hash, s(:forwarded_kwrestarg)) child is not a
pair, so inference crashed (NoMethodError on master, and
NoMatchingPatternError once convert_hash uses pattern matching).
Forwarded keywords are whatever the enclosing method received, so
they cannot rule an overload out.

The "accepts anonymous *, ** and & forwarding to a call" example in
strong_spec.rb, added on master by castwide#1400, covers this.
@apiology
apiology marked this pull request as draft October 28, 2026 16:13
Reuse the NodeMethods helper from master instead of comparing against
a node constant, so call sites agree on what counts as forwarded.
The value_chain.infer finding (an each_pair value from a local @type
hash) is fixed on castwide#1223's branch. The compatible_arg? finding is not
about each_pair: it is `param` staying nilable after an
`if param.nil? ... next end` guard, which castwide#1259 fixes.
castwide#1461 restores tuple recognition, which types the values that
Hash#each_pair yields from a local @type hash; castwide#1223 is closed.
@apiology
apiology marked this pull request as ready for review October 30, 2026 00:33
apiology added a commit to apiology/solargraph that referenced this pull request Nov 7, 2026
The example only failed where a cached gem brought in the stdlib open3
pins, so it passed in the plain rspec job and failed in the Plugin
jobs. Load the stdlib open3 pins explicitly so it fails everywhere, and
mark it pending on castwide#1292, which fixes the kwargs
matching in Chain::Call#match_overload_type.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LTpguJfsWUBaw2hbPSiX3T
@apiology
apiology marked this pull request as draft November 25, 2026 20:37
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.

1 participant