Fix wrong arity diagnostics for .new and .[] of a Struct class - #491
Merged
Merged
Conversation
Struct.[] is an alias for Struct.new, but TypeProf generated it from the member list independently of initialize. When a struct overrode initialize, `self.[]` did not follow it: the RBS showed a stale signature and valid calls were reported as wrong number of arguments.
Struct.new(:x, :y).new and Struct.new(:x, :y)[] are valid, but TypeProf required every member as an argument and reported these calls as wrong number of arguments. The omitted members are not typed as nil, so the readers keep the types of the given values.
sinsoku
force-pushed
the
fix-struct-aref-initialize
branch
from
September 27, 2026 09:23
2b6f083 to
b65ce97
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.
Motivation / Background
This Pull Request has been created because TypeProf reports valid calls to a struct class as wrong number of arguments.
Struct.[]is an alias forStruct.new, but TypeProf generated it from the member list, so it did not follow an overriddeninitialize. This is the part of #458 that #466 left as a limitation. The generatedinitializealso required every member, although Ruby sets omitted members to nil.Detail
Struct.[]now uses the same builtin asClass#new, so it calls theinitializeof the receiver, including a user-defined one. Since[]no longer has its own method definition, the RBS output buildsself.[]from thatinitialize, e.g.def self.[]: (?Integer, ?Integer) -> Pt. Forclass Sub < Pt; end,Sub[1, 2]now returnsSubinstead ofPt.The generated
initializeandself.[]now show every member as optional, e.g.(?Integer, ?String). Omitted members are not typed as nil, because that would make every reader nilable even when all calls pass every member. So the readers keep the types of the given values, e.g.def x: -> Integer.The IDE features also treat
Pt[...]likePt.new: its hover shows the signature ofinitialize, e.g.Pt#initialize : (?Integer, ?Integer) -> void, andself.[]no longer appears in the completion forPt..Data.defineis out of scope:D[...]is still undefined, andD.new(1, 2)is still reported.Verification
bundle exec rake testpasses.🤖 Generated with Claude Code