Narrow is_a? checks joined by || to their union type - #64
Open
apiology wants to merge 3 commits into
Open
Conversation
FlowSensitiveTyping#process_or never computed any true-branch narrowing for an ||-joined condition, so a check like e.is_a?(Array) || e.is_a?(String) left e at its full declared type inside the if body instead of narrowing to Array | String. Add process_or_isa_union, which narrows only when both sides of the || are plain is_a? calls on the same variable, using the same process_facts/find_var/ComplexType.parse primitives process_isa already uses. Any other shape (different variables, or a side that isn't a plain is_a? call) is left unnarrowed rather than guessed at.
apiology
added a commit
that referenced
this pull request
Aug 22, 2026
Cut the process_or_isa_union docstring and the process_or comment explaining the || narrowing exception down to the 1-3 line budget, keeping only the non-obvious why.
apiology
marked this pull request as ready for review
September 7, 2026 00:30
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.
This PR was written by Claude Code on behalf of @apiology.
Problem:
solargraph typecheck --level strongreports a call as accepting the full, unnarrowed union of a variable's declared type, even inside anifbranch that has already ruled out some of those types via||-joinedis_a?checks.This produces a false
Wrong argument typeproblem for any code relying on this common idiom to narrow a variable before use.Solution: When both sides of an
||are plainis_a?checks on the same variable, narrow that variable to the union of the two checked types inside theifbranch; any other shape - different variables, or a side that isn't a plainis_a?call - is left unnarrowed, same as before.Known limitation: a chain of three or more
is_a?checks (a.is_a?(X) || a.is_a?(Y) || a.is_a?(Z)) still isn't narrowed, since the AST nests as(X || Y) || Zand the outer left side is an:ornode rather than a:send. This degrades to the pre-existing no-narrowing behavior rather than narrowing incorrectly.