Improve Record type inference for hash literals#360
Open
sinsoku wants to merge 2 commits intoruby:masterfrom
Open
Improve Record type inference for hash literals#360sinsoku wants to merge 2 commits intoruby:masterfrom
sinsoku wants to merge 2 commits intoruby:masterfrom
Conversation
Record types have statically defined fields, unlike Hash which allows dynamic keys. This change makes the type inference more precise: - Non-existent field access (e.g., `record[:unknown]`) now returns `nil` instead of `untyped`, since Record fields are fixed at definition time - Symbol variable access (e.g., `record[key]`) now returns `nil | union of all field types` to account for the possibility that the key may not exist in the Record Hash type behavior remains unchanged (returns `untyped` for unknown keys).
This improves RBS output precision for hash literals. For example:
- Before: `def foo: -> Hash[:a | :b, Integer | String]`
- After: `def foo: -> { a: Integer, b: String }`
Record type preserves individual field types instead of merging
all keys and values into a single union type.
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
Type::Recordfor hash literals with all symbol keys instead ofType::Hashnilfor non-existent fields)Motivation
More precise type information
Currently, hash literals like
{ a: 1, b: "str" }are inferred asHash[:a | :b, Integer | String]. This loses the association between keys and values, incorrectly suggesting that:acould return aString.By using Record types, each field's type is preserved individually, making RBS output and IDE type information more practical and accurate.
Correct semantics for Record types
Record types have statically defined fields. Unlike Hash, accessing a non-existent key is not "unknown" but definitively "does not exist". Therefore, returning
nilinstead ofuntypedis the correct behavior.