@@ -52,6 +52,25 @@ def self.load_rbs_builder
5252 nil
5353 end
5454
55+ def self . array_elem_type_param
56+ ( @array_elem_type_param ||= _class_type_params ( Array ) . first ) || :E
57+ end
58+
59+ def self . hash_type_params
60+ ( @hash_type_params ||= _class_type_params ( Hash ) ) || [ :K , :V ]
61+ end
62+
63+ def self . hash_key_type_param = hash_type_params &.first
64+
65+ def self . hash_value_type_param = hash_type_params &.last
66+
67+ def self . _class_type_params ( klass )
68+ return unless rbs_builder
69+
70+ type_name = rbs_absolute_type_name ( class_name_of ( klass ) )
71+ rbs_builder . build_instance ( type_name ) . type_params
72+ end
73+
5574 def self . class_name_of ( klass )
5675 while true
5776 name = Methods ::MODULE_NAME_METHOD . bind_call klass
@@ -156,8 +175,7 @@ def self.rbs_methods(type, method_name, args_types, kwargs_type, has_block)
156175 keyrest = method_type . type . rest_keywords
157176 args = args_types
158177 if kwargs_type &.any? && keyreqs . empty? && keyopts . empty? && keyrest . nil?
159- kw_value_type = UnionType [ *kwargs_type . values ]
160- args += [ InstanceType . new ( Hash , K : SYMBOL , V : kw_value_type ) ]
178+ args += [ InstanceType . hash_with_params ( Types ::SYMBOL , UnionType [ *kwargs_type . values ] ) ]
161179 end
162180 if has_splat
163181 score += 1 if args . count ( &:itself ) <= reqs . size + opts . size + trailings . size
@@ -276,13 +294,13 @@ def expand_params
276294
277295 if @klass == Array
278296 type = Types . union_type_from_objects_list ( @instances )
279- { Elem : UnionType [ *params [ :Elem ] , *type ] }
297+ { Types . array_elem_type_param => UnionType [ *params [ Types . array_elem_type_param ] , *type ] }
280298 elsif @klass == Hash
281299 key = Types . union_type_from_objects_list ( @instances . map ( &:keys ) )
282300 value = Types . union_type_from_objects_list ( @instances . map ( &:values ) )
283301 {
284- K : UnionType [ *params [ :K ] , key ] ,
285- V : UnionType [ *params [ :V ] , value ]
302+ Types . hash_key_type_param => UnionType [ *params [ Types . hash_key_type_param ] , key ] ,
303+ Types . hash_value_type_param => UnionType [ *params [ Types . hash_value_type_param ] , value ]
286304 }
287305 else
288306 params
@@ -324,6 +342,14 @@ def inspect_without_params
324342 klass . to_s
325343 end
326344 end
345+
346+ def self . array_with_params ( elem_type )
347+ new ( Array , { Types . array_elem_type_param => elem_type } )
348+ end
349+
350+ def self . hash_with_params ( key_type , value_type )
351+ new ( Hash , { Types . hash_key_type_param => key_type , Types . hash_value_type_param => value_type } )
352+ end
327353 end
328354
329355 NIL = InstanceType . new NilClass
@@ -405,7 +431,7 @@ def inspect() = @types.map(&:inspect).sort.join(' | ')
405431
406432 def self . array_of ( *types )
407433 type = types . size >= 2 ? UnionType [ *types ] : types . first || OBJECT
408- InstanceType . new Array , Elem : type
434+ InstanceType . array_with_params ( type )
409435 end
410436
411437 def self . from_rbs_type ( return_type , self_type , extra_vars = { } )
@@ -445,9 +471,9 @@ def self.from_rbs_type(return_type, self_type, extra_vars = {})
445471 PROC
446472 when RBS ::Types ::Tuple
447473 elem = UnionType [ *return_type . types . map { from_rbs_type _1 , self_type , extra_vars } ]
448- InstanceType . new Array , Elem : elem
474+ InstanceType . array_with_params ( elem )
449475 when RBS ::Types ::Record
450- InstanceType . new Hash , K : SYMBOL , V : OBJECT
476+ InstanceType . hash_with_params ( Types :: SYMBOL , Types :: OBJECT )
451477 when RBS ::Types ::Literal
452478 InstanceType . new return_type . literal . class
453479 when RBS ::Types ::Variable
@@ -516,7 +542,7 @@ def self._match_free_variable(vars, rbs_type, value, accumulator)
516542 _match_free_variable vars , arg , v , accumulator if v
517543 end
518544 in [ RBS ::Types ::Tuple , InstanceType ] if value . klass == Array
519- v = value . params [ :Elem ]
545+ v = value . params [ array_elem_type_param ]
520546 rbs_type . types . each do |t |
521547 _match_free_variable vars , t , v , accumulator
522548 end
0 commit comments