Repository navigation
Conversation
A local variable holding a union of record types is now narrowed when its literal discriminant key is refined, e.g. by `if event[:type] == "CLICK"`. Previously only the pure call `event[:type]` was narrowed; `event` itself stayed the full union. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
`case event[:type] when "CLICK"` now narrows `event` the same way `if`/`unless` already did. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Narrowing went through `refine_types`, which drops every pure call on the variable as if it were reassigned, so `x[:name]` narrowed by an earlier guard reverted to its declared type. Add `TypeEnv#narrow_types`, which keeps them. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
dak2
force-pushed
the
record-union-narrowing
branch
from
October 8, 2026 16:11
0d7de0f to
b05b092
Compare
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.
Summary
Narrowing
hash[:type]viaif/unless/case-whennow also narrowshashitself, whenhashis a union of record types.Details
Given
hash: a | b, wherea/bare record types with a shared literal discriminant key, checking that key's value narrowshashto the member whose key type matches:if hash[:type] == "A"— narrowshashtoain the truthy branch,bin the falsy branchcase hash[:type] when "A"— narrowshashtoain thatwhenclause, and to the bottom type in an exhaustiveelse?type: "A") are treated as"A" | nilThis lets you access member-specific fields (e.g.
hash[:position]) right after the branch, without an extra cast.Not supported
Narrowing is skipped entirely, not partially applied, when the discriminant key's value type is:
Unionof literalsFor this PR scope, I want to keep the specifications as simple as possible.