Skip to content

Resolve instance variable type when assigned only in an ancestor class's method - #1345

Open
apiology wants to merge 3 commits into
castwide:masterfrom
apiology:fix-issue-1261
Open

apiology wants to merge 3 commits into
castwide:masterfrom
apiology:fix-issue-1261

Conversation

@apiology

@apiology apiology commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Claude: written on behalf of @apiology.

Fixes #1261

Problem: An instance variable assigned only in an ancestor class's method gets no inferred type in the descendant, so every read of it fails at --level strong.

class WidgetBase
  def run
    @name = 'set-in-run'
  end
end

class Widget < WidgetBase
  # @return [void]
  def test_no_block
    run
    puts @name.upcase   # Unresolved call to @name
  end
end

This hits any class hierarchy where a base class sets up state its subclasses read, including when the two live in different files.

Solution: Pin::InstanceVariable overrides visible_in_closure? to accept a candidate when the lexical walk bottoms out at a non-matching NamespaceApiMap#get_instance_variable_pins has already restricted candidates to the reading namespace and its ancestors — and overrides visible_at? so the same-file requirement applies only when presence (flow-sensitive narrowing) is set.

🤖 Generated with Claude Code

Pin::BaseVariable treats every variable as lexically scoped, giving
up once the reading closure's namespace walk finds no match. Ancestor
ivar-assignment candidates from get_instance_variable_pins were
discarded, since a descendant class is never lexically nested inside
the ancestor class that defines it. InstanceVariable now accepts a
pin once the walk bottoms out with no match, and only enforces
same-file placement when presence (flow-sensitive narrowing) is
actually set.

Fixes castwide#1261
Compress the two method docstrings this PR added down to the
non-obvious why, drop a duplicate inline comment restating the same
point, and shorten the top-level-visibility guard comment. No code
changes.
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.

Instance variable type not inferred when set only in an ancestor class's method

1 participant