@@ -157,7 +157,7 @@ def self.rbs_methods(type, method_name, args_types, kwargs_type, has_block)
157157 args = args_types
158158 if kwargs_type &.any? && keyreqs . empty? && keyopts . empty? && keyrest . nil?
159159 kw_value_type = UnionType [ *kwargs_type . values ]
160- args += [ InstanceType . new ( Hash , K : SYMBOL , V : kw_value_type ) ]
160+ args += [ InstanceType . new ( Hash , [ SYMBOL , kw_value_type ] ) ]
161161 end
162162 if has_splat
163163 score += 1 if args . count ( &:itself ) <= reqs . size + opts . size + trailings . size
@@ -270,20 +270,29 @@ def params
270270 @params ||= expand_params
271271 end
272272
273+ def named_params
274+ return { } if params . empty?
275+ if Types . rbs_builder
276+ type_name = RBS ::TypeName . parse ( @klass . name ) . absolute!
277+ names = Types . rbs_builder . build_instance ( type_name ) &.type_params rescue nil
278+ end
279+ names ? names . zip ( params ) . to_h . compact : { }
280+ end
281+
273282 def expand_params
274- params = @raw_params || { }
283+ params = @raw_params || [ ]
275284 return params unless @instances
276285
277286 if @klass == Array
278287 type = Types . union_type_from_objects_list ( @instances )
279- { Elem : UnionType [ *params [ :Elem ] , *type ] }
288+ [ UnionType [ *params [ 0 ] , *type ] ]
280289 elsif @klass == Hash
281290 key = Types . union_type_from_objects_list ( @instances . map ( &:keys ) )
282291 value = Types . union_type_from_objects_list ( @instances . map ( &:values ) )
283- {
284- K : UnionType [ *params [ :K ] , key ] ,
285- V : UnionType [ *params [ :V ] , value ]
286- }
292+ [
293+ UnionType [ *params [ 0 ] , key ] ,
294+ UnionType [ *params [ 1 ] , value ]
295+ ]
287296 else
288297 params
289298 end
@@ -308,7 +317,12 @@ def inspect
308317 elsif params . empty?
309318 inspect_without_params
310319 else
311- params_string = "[#{ params . map { "#{ _1 } : #{ _2 . inspect } " } . join ( ', ' ) } ]"
320+ named = named_params
321+ if named . empty? && !params . empty?
322+ params_string = "[#{ params . map ( &:inspect ) . join ( ', ' ) } ]"
323+ else
324+ params_string = "[#{ named . map { "#{ _1 } : #{ _2 . inspect } " } . join ( ', ' ) } ]"
325+ end
312326 "#{ inspect_without_params } #{ params_string } "
313327 end
314328 end
@@ -356,18 +370,18 @@ def initialize(*types)
356370 in UnionType
357371 type . types . each ( &collect )
358372 in InstanceType
359- params , instances = ( instance_types [ type . klass ] ||= [ { } , [ ] ] )
373+ params , instances = ( instance_types [ type . klass ] ||= [ [ ] , [ ] ] )
360374 type . instances &.each { instances << _1 }
361- type . raw_params &.each do |k , v |
362- ( params [ k ] ||= [ ] ) << v
375+ type . raw_params &.each_with_index do |v , index |
376+ ( params [ index ] ||= [ ] ) << v
363377 end
364378 in SingletonType
365379 singleton_types << type
366380 end
367381 end
368382 types . each ( &collect )
369383 @types = singleton_types . uniq + instance_types . map do |klass , ( params , instances ) |
370- params = params . transform_values { |v | UnionType [ *v ] }
384+ params = params . map { |v | UnionType [ *v ] }
371385 InstanceType . new ( klass , params , instances )
372386 end
373387 end
@@ -405,7 +419,7 @@ def inspect() = @types.map(&:inspect).sort.join(' | ')
405419
406420 def self . array_of ( *types )
407421 type = types . size >= 2 ? UnionType [ *types ] : types . first || OBJECT
408- InstanceType . new Array , Elem : type
422+ InstanceType . new ( Array , [ type ] )
409423 end
410424
411425 def self . from_rbs_type ( return_type , self_type , extra_vars = { } )
@@ -445,19 +459,19 @@ def self.from_rbs_type(return_type, self_type, extra_vars = {})
445459 PROC
446460 when RBS ::Types ::Tuple
447461 elem = UnionType [ *return_type . types . map { from_rbs_type _1 , self_type , extra_vars } ]
448- InstanceType . new Array , Elem : elem
462+ InstanceType . new ( Array , [ elem ] )
449463 when RBS ::Types ::Record
450- InstanceType . new Hash , K : SYMBOL , V : OBJECT
464+ InstanceType . new ( Hash , [ SYMBOL , OBJECT ] )
451465 when RBS ::Types ::Literal
452466 InstanceType . new return_type . literal . class
453467 when RBS ::Types ::Variable
454468 if extra_vars . key? return_type . name
455469 extra_vars [ return_type . name ]
456470 elsif self_type . is_a? InstanceType
457- self_type . params [ return_type . name ] || OBJECT
471+ self_type . named_params [ return_type . name ] || OBJECT
458472 elsif self_type . is_a? UnionType
459473 types = self_type . types . filter_map do |t |
460- t . params [ return_type . name ] if t . is_a? InstanceType
474+ t . named_params [ return_type . name ] if t . is_a? InstanceType
461475 end
462476 UnionType [ *types ]
463477 else
@@ -483,11 +497,9 @@ def self.from_rbs_type(return_type, self_type, extra_vars = {})
483497 when RBS ::Types ::ClassInstance
484498 klass = return_type . name . to_namespace . path . reduce ( Object ) { _1 . const_get _2 }
485499 if return_type . args
486- args = return_type . args . map { from_rbs_type _1 , self_type , extra_vars }
487- names = rbs_builder . build_singleton ( return_type . name ) . type_params
488- params = names . map . with_index { [ _1 , args [ _2 ] || OBJECT ] } . to_h
500+ params = return_type . args . map { from_rbs_type _1 , self_type , extra_vars }
489501 end
490- InstanceType . new klass , params || { }
502+ InstanceType . new ( klass , params || [ ] )
491503 else
492504 OBJECT
493505 end
@@ -512,11 +524,11 @@ def self._match_free_variable(vars, rbs_type, value, accumulator)
512524 in [ RBS ::Types ::ClassInstance , InstanceType ]
513525 names = rbs_builder . build_singleton ( rbs_type . name ) . type_params
514526 names . zip ( rbs_type . args ) . each do |name , arg |
515- v = value . params [ name ]
527+ v = value . named_params [ name ]
516528 _match_free_variable vars , arg , v , accumulator if v
517529 end
518530 in [ RBS ::Types ::Tuple , InstanceType ] if value . klass == Array
519- v = value . params [ :Elem ]
531+ v = value . params [ 0 ]
520532 rbs_type . types . each do |t |
521533 _match_free_variable vars , t , v , accumulator
522534 end
0 commit comments