Skip to content

Infer the same type for a method regardless of unrelated methods in its class - #355

Draft
apiology wants to merge 2 commits into
masterfrom
stable-method-pin-order
Draft

apiology wants to merge 2 commits into
masterfrom
stable-method-pin-order

Conversation

@apiology

Copy link
Copy Markdown
Owner

Claude: This PR was written by Claude Code on behalf of @apiology.

Problem

Adding an unrelated method changes what a reopened core method infers:

class String
  # @return [Symbol]
  def upcase; end
  def extra1; end
  def extra2; end
  def extra3; end # with this line x is Symbol; without it, String
end
x = 'a'.upcase
x

Inference, hover and typecheck results shift with code elsewhere in the class, as well as with gem pins. Within this repo, a Float#- overload from bigdecimal makes master report Wrong argument type for Float#-: arg_0 expected BigDecimal, received Float at lib/solargraph/api_map.rb:114.

Solution

ApiMap#inner_get_methods now sorts by name with the store index as a tie-breaker, because Array#sort is unstable and was reordering same-name pins. Same-name pins keep store order: core, then gems, conventions, workspace.

The new sort_by.with_index line has an ignore marker citing apiology/solargraph#354, Type block parameters of map.with_index. That PR clears the Unresolved call to name error it suppresses.

Which pin should win between a workspace reopen and RBS is unchanged, and not decided here. apiology/solargraph#119, Fix swapped @param tags on Hash.new default-block params, apiology/solargraph#49, Integration test: 2026-08-04 and castwide/solargraph#1310, Rebind self in Class.new blocks; nested blocks inherit rebound binders each add a second identical .sort for store.get_methods(''). That sort needs the same change when they land.

ApiMap#inner_get_methods sorted store pins by name with Array#sort,
which is unstable. Two pins with the same name, such as a workspace
reopen of String#upcase and the core RBS one, came out in an order that
depended on how many other methods the class had, so get_method_stack's
first pin and the inferred type of 'a'.upcase flipped between String and
Symbol when an unrelated method was added.

Sort by name with the store index as a tie-breaker, so same-name pins
keep the order the store returns them in (core, then gems, conventions,
workspace and open files).
#354 is closing as a duplicate of
castwide#1415, which also clears this finding.
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