steep 1.1.1 → 1.2.0.pre.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +20 -0
  3. data/Gemfile +0 -1
  4. data/Gemfile.lock +12 -11
  5. data/Gemfile.steep +1 -1
  6. data/Gemfile.steep.lock +9 -9
  7. data/README.md +3 -3
  8. data/Steepfile +23 -0
  9. data/bin/steep-prof +2 -1
  10. data/lib/steep/annotation_parser.rb +1 -1
  11. data/lib/steep/ast/types/class.rb +4 -0
  12. data/lib/steep/ast/types/factory.rb +86 -602
  13. data/lib/steep/ast/types/instance.rb +4 -0
  14. data/lib/steep/ast/types/literal.rb +1 -1
  15. data/lib/steep/ast/types/proc.rb +22 -8
  16. data/lib/steep/ast/types/self.rb +4 -0
  17. data/lib/steep/ast/types/union.rb +1 -1
  18. data/lib/steep/cli.rb +24 -1
  19. data/lib/steep/diagnostic/ruby.rb +17 -22
  20. data/lib/steep/drivers/checkfile.rb +205 -0
  21. data/lib/steep/drivers/validate.rb +3 -1
  22. data/lib/steep/equatable.rb +2 -0
  23. data/lib/steep/interface/block.rb +21 -11
  24. data/lib/steep/interface/builder.rb +756 -0
  25. data/lib/steep/interface/function.rb +32 -24
  26. data/lib/steep/interface/method_type.rb +191 -78
  27. data/lib/steep/interface/shape.rb +132 -0
  28. data/lib/steep/interface/substitution.rb +23 -12
  29. data/lib/steep/interface/type_param.rb +1 -2
  30. data/lib/steep/path_helper.rb +1 -1
  31. data/lib/steep/project.rb +5 -7
  32. data/lib/steep/server/base_worker.rb +2 -2
  33. data/lib/steep/server/change_buffer.rb +4 -3
  34. data/lib/steep/server/interaction_worker.rb +1 -1
  35. data/lib/steep/server/master.rb +69 -9
  36. data/lib/steep/server/type_check_worker.rb +13 -11
  37. data/lib/steep/server/worker_process.rb +9 -7
  38. data/lib/steep/services/completion_provider.rb +15 -3
  39. data/lib/steep/services/hover_provider/singleton_methods.rb +5 -6
  40. data/lib/steep/services/signature_service.rb +13 -8
  41. data/lib/steep/services/type_check_service.rb +2 -0
  42. data/lib/steep/signature/validator.rb +1 -1
  43. data/lib/steep/subtyping/check.rb +154 -103
  44. data/lib/steep/subtyping/relation.rb +3 -3
  45. data/lib/steep/subtyping/result.rb +20 -2
  46. data/lib/steep/subtyping/variable_variance.rb +9 -0
  47. data/lib/steep/type_construction.rb +558 -299
  48. data/lib/steep/type_inference/block_params.rb +169 -86
  49. data/lib/steep/type_inference/logic_type_interpreter.rb +9 -14
  50. data/lib/steep/type_inference/method_params.rb +12 -7
  51. data/lib/steep/type_inference/send_args.rb +41 -35
  52. data/lib/steep/type_inference/type_env_builder.rb +1 -1
  53. data/lib/steep/version.rb +1 -1
  54. data/lib/steep.rb +71 -2
  55. data/rbs_collection.steep.lock.yaml +18 -30
  56. data/rbs_collection.steep.yaml +1 -0
  57. data/sig/shims/language-server_protocol.rbs +20 -0
  58. data/sig/shims/tagged_logging.rbs +6 -0
  59. data/sig/steep/ast/annotation/collection.rbs +6 -6
  60. data/sig/steep/ast/types/class.rbs +3 -0
  61. data/sig/steep/ast/types/factory.rbs +38 -32
  62. data/sig/steep/ast/types/instance.rbs +3 -0
  63. data/sig/steep/ast/types/intersection.rbs +1 -1
  64. data/sig/steep/ast/types/literal.rbs +7 -7
  65. data/sig/steep/ast/types/name.rbs +14 -13
  66. data/sig/steep/ast/types/proc.rbs +3 -1
  67. data/sig/steep/ast/types/self.rbs +3 -0
  68. data/sig/steep/ast/types/var.rbs +1 -1
  69. data/sig/steep/diagnostic/ruby.rbs +30 -34
  70. data/sig/steep/drivers/annotations.rbs +17 -0
  71. data/sig/steep/drivers/check.rbs +33 -0
  72. data/sig/steep/drivers/checkfile.rbs +26 -0
  73. data/sig/steep/drivers/diagnostic_printer.rbs +25 -0
  74. data/sig/steep/drivers/init.rbs +19 -0
  75. data/sig/steep/drivers/langserver.rbs +35 -0
  76. data/sig/steep/drivers/print_project.rbs +15 -0
  77. data/sig/steep/drivers/stats.rbs +37 -0
  78. data/sig/steep/drivers/utils/driver_helper.rbs +23 -0
  79. data/sig/steep/drivers/utils/jobs_count.rbs +11 -0
  80. data/sig/steep/drivers/validate.rbs +15 -0
  81. data/sig/steep/drivers/vendor.rbs +19 -0
  82. data/sig/steep/drivers/watch.rbs +27 -0
  83. data/sig/steep/drivers/worker.rbs +31 -0
  84. data/sig/steep/equatable.rbs +11 -0
  85. data/sig/steep/index/rbs_index.rbs +91 -0
  86. data/sig/steep/index/signature_symbol_provider.rbs +29 -0
  87. data/sig/steep/index/source_index.rbs +63 -0
  88. data/sig/steep/interface/block.rbs +3 -1
  89. data/sig/steep/interface/builder.rbs +152 -0
  90. data/sig/steep/interface/function.rbs +67 -55
  91. data/sig/steep/interface/method_type.rbs +60 -12
  92. data/sig/steep/interface/shape.rbs +61 -0
  93. data/sig/steep/interface/substitution.rbs +18 -22
  94. data/sig/steep/interface/type_param.rbs +9 -1
  95. data/sig/steep/path_helper.rbs +7 -0
  96. data/sig/steep/project/pattern.rbs +10 -10
  97. data/sig/steep/project/target.rbs +2 -2
  98. data/sig/steep/project.rbs +15 -8
  99. data/sig/steep/server/base_worker.rbs +49 -0
  100. data/sig/steep/server/change_buffer.rbs +32 -0
  101. data/sig/steep/server/interaction_worker.rbs +41 -0
  102. data/sig/steep/server/lsp_formatter.rbs +29 -0
  103. data/sig/steep/server/master.rbs +260 -0
  104. data/sig/steep/server/type_check_worker.rbs +135 -0
  105. data/sig/steep/server/worker_process.rbs +29 -0
  106. data/sig/steep/services/completion_provider.rbs +5 -5
  107. data/sig/steep/services/content_change.rbs +14 -12
  108. data/sig/steep/services/file_loader.rbs +12 -4
  109. data/sig/steep/services/hover_provider/singleton_methods.rbs +1 -1
  110. data/sig/steep/services/path_assignment.rbs +7 -7
  111. data/sig/steep/services/signature_service.rbs +67 -40
  112. data/sig/steep/services/type_check_service.rbs +53 -39
  113. data/sig/steep/subtyping/check.rbs +80 -44
  114. data/sig/steep/subtyping/relation.rbs +24 -22
  115. data/sig/steep/subtyping/result.rbs +48 -30
  116. data/sig/steep/subtyping/variable_variance.rbs +2 -0
  117. data/sig/steep/type_construction.rbs +132 -23
  118. data/sig/steep/type_inference/block_params.rbs +120 -18
  119. data/sig/steep/type_inference/context.rbs +45 -20
  120. data/sig/steep/type_inference/context_array.rbs +37 -0
  121. data/sig/steep/type_inference/logic_type_interpreter.rbs +3 -1
  122. data/sig/steep/type_inference/method_params.rbs +13 -9
  123. data/sig/steep/type_inference/send_args.rbs +229 -0
  124. data/sig/steep/type_inference/type_env_builder.rbs +1 -1
  125. data/sig/steep/typing.rbs +4 -4
  126. data/sig/steep.rbs +4 -2
  127. data/smoke/block/e.rb +12 -0
  128. data/smoke/block/e.rbs +4 -0
  129. data/smoke/block/test_expectations.yml +12 -0
  130. data/smoke/regression/block_param_split.rb +7 -0
  131. data/smoke/regression/block_param_split.rbs +3 -0
  132. data/smoke/regression/empty_yield.rb +5 -0
  133. data/smoke/regression/empty_yield.rbs +3 -0
  134. data/smoke/regression/test_expectations.yml +12 -0
  135. data/smoke/yield/test_expectations.yml +4 -4
  136. data/steep.gemspec +2 -1
  137. metadata +61 -9
  138. data/lib/steep/interface/interface.rb +0 -34
  139. data/sig/steep/interface/interface.rbs +0 -23
  140. data/sig/version.rbs +0 -3
@@ -1,16 +1,19 @@
1
1
  module Steep
2
2
  module Subtyping
3
3
  class Check
4
- attr_reader :factory
4
+ attr_reader :builder
5
5
  attr_reader :cache
6
- attr_reader :assumptions
7
6
 
8
- def initialize(factory:)
9
- @factory = factory
7
+ def initialize(builder:)
8
+ @builder = builder
10
9
  @cache = Cache.new()
11
10
  @bounds = []
12
11
  end
13
12
 
13
+ def factory
14
+ builder.factory
15
+ end
16
+
14
17
  def with_context(self_type:, instance_type:, class_type:, constraints:)
15
18
  @self_type = self_type
16
19
  @instance_type = instance_type
@@ -61,16 +64,26 @@ module Steep
61
64
  nil
62
65
  end
63
66
 
67
+ def variable_upper_bounds
68
+ @bounds.each_with_object({}) do |bounds, hash|
69
+ hash.merge!(bounds)
70
+ end
71
+ end
72
+
73
+ def assumptions
74
+ @assumptions || raise
75
+ end
76
+
64
77
  def self_type
65
78
  @self_type || raise
66
79
  end
67
80
 
68
81
  def instance_type
69
- @instance_type || raise
82
+ @instance_type
70
83
  end
71
84
 
72
85
  def class_type
73
- @class_type || raise
86
+ @class_type
74
87
  end
75
88
 
76
89
  def constraints
@@ -78,7 +91,7 @@ module Steep
78
91
  end
79
92
 
80
93
  def each_ancestor(ancestors, &block)
81
- if block_given?
94
+ if block
82
95
  if ancestors.super_class
83
96
  yield ancestors.super_class
84
97
  end
@@ -97,7 +110,8 @@ module Steep
97
110
 
98
111
  subst = unless args.empty?
99
112
  args_ = args.map {|type| factory.type_1(type) }
100
- RBS::Substitution.build(ancestors.params, args_)
113
+ params = ancestors.params or raise
114
+ RBS::Substitution.build(params, args_)
101
115
  end
102
116
 
103
117
  each_ancestor(ancestors).map do |ancestor|
@@ -177,8 +191,10 @@ module Steep
177
191
 
178
192
  Steep.logger.tagged "#{relation.sub_type} <: #{relation.super_type}" do
179
193
  bounds = cache_bounds(relation)
194
+ fvs = relation.sub_type.free_variables + relation.super_type.free_variables
180
195
  cached = cache[relation, @self_type, @instance_type, @class_type, bounds]
181
- if cached && constraints.empty?
196
+ if cached && fvs.none? {|var| constraints.unknown?(var) }
197
+ Steep.logger.fatal { { cached: relation.to_s }.inspect }
182
198
  cached
183
199
  else
184
200
  if assumptions.member?(relation)
@@ -384,13 +400,26 @@ module Steep
384
400
 
385
401
  when relation.super_type.is_a?(AST::Types::Name::Interface)
386
402
  Expand(relation) do
387
- check_interface(relation.map {|type| factory.interface(type, private: false) })
403
+ check_interface(
404
+ relation.map {|type|
405
+ builder.shape(
406
+ type,
407
+ public_only: true,
408
+ config: Interface::Builder::Config.new(
409
+ self_type: type,
410
+ instance_type: instance_type,
411
+ class_type: class_type,
412
+ variable_bounds: variable_upper_bounds
413
+ )
414
+ ) or raise
415
+ }
416
+ )
388
417
  end
389
418
 
390
419
  when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
391
420
  if relation.sub_type.name == relation.super_type.name && relation.sub_type.class == relation.super_type.class
392
421
  if arg_type?(relation.sub_type) && arg_type?(relation.super_type)
393
- check_type_arg(relation)
422
+ check_type_arg(_ = relation)
394
423
  else
395
424
  Success(relation)
396
425
  end
@@ -419,17 +448,30 @@ module Steep
419
448
  end
420
449
 
421
450
  when relation.sub_type.is_a?(AST::Types::Proc) && relation.super_type.is_a?(AST::Types::Proc)
422
- name = :__proc__
451
+ yield_self do
452
+ name = :__proc__
423
453
 
424
- All(relation) do |result|
425
- result.add(relation.map {|p| p.type }) do |rel|
426
- check_function(name, rel)
427
- end
454
+ sub_type = relation.sub_type
455
+ super_type = relation.super_type
456
+
457
+ All(relation) do |result|
458
+ result.add(Relation(sub_type.type, super_type.type)) do |rel|
459
+ check_function(name, rel)
460
+ end
428
461
 
429
- result.add(relation.map {|p| p.block }) do |rel|
430
- check_block_given(name, rel) do
431
- Expand(rel.map {|b| b.type }) do |rel|
432
- check_function(name, rel.flip)
462
+ result.add_result check_self_type_binding(relation, sub_type.self_type, super_type.self_type)
463
+
464
+ result.add(Relation(sub_type.block, super_type.block)) do |rel|
465
+ case ret = expand_block_given(name, rel)
466
+ when Relation
467
+ All(ret) do |result|
468
+ result.add_result check_self_type_binding(ret, ret.super_type.self_type, ret.sub_type.self_type)
469
+ result.add(ret.map {|b| b.type }) {|r| check_function(name, r.flip) }
470
+ end
471
+ when Result::Base
472
+ ret
473
+ when true
474
+ nil
433
475
  end
434
476
  end
435
477
  end
@@ -441,6 +483,7 @@ module Steep
441
483
 
442
484
  All(relation) do |result|
443
485
  pairs.each do |t1, t2|
486
+ t2 or raise
444
487
  result.add(Relation.new(sub_type: t1, super_type: t2)) do |rel|
445
488
  check_type(rel)
446
489
  end
@@ -477,7 +520,20 @@ module Steep
477
520
 
478
521
  when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Name::Base)
479
522
  Expand(relation) do
480
- check_interface(relation.map {|type| factory.interface(type, private: false) })
523
+ check_interface(
524
+ relation.map {|type|
525
+ builder.shape(
526
+ type,
527
+ public_only: true,
528
+ config: Interface::Builder::Config.new(
529
+ self_type: type,
530
+ instance_type: instance_type,
531
+ class_type: class_type,
532
+ variable_bounds: variable_upper_bounds
533
+ )
534
+ ) or raise
535
+ }
536
+ )
481
537
  end
482
538
 
483
539
  when relation.sub_type.is_a?(AST::Types::Proc) && AST::Builtin::Proc.instance_type?(relation.super_type)
@@ -510,15 +566,13 @@ module Steep
510
566
  end
511
567
 
512
568
  def definition_for_type(type)
513
- type_name = type.name
514
-
515
569
  case type
516
570
  when AST::Types::Name::Instance
517
- factory.definition_builder.build_instance(type_name)
571
+ factory.definition_builder.build_instance(type.name)
518
572
  when AST::Types::Name::Singleton
519
- factory.definition_builder.build_singleton(type_name)
573
+ factory.definition_builder.build_singleton(type.name)
520
574
  when AST::Types::Name::Interface
521
- factory.definition_builder.build_interface(type_name)
575
+ factory.definition_builder.build_interface(type.name)
522
576
  else
523
577
  raise
524
578
  end
@@ -611,18 +665,6 @@ module Steep
611
665
  end
612
666
  end
613
667
 
614
- def check_type_application(result, type_params, type_args)
615
- raise unless type_params.size == type_args.size
616
-
617
- rels = type_params.zip(type_args).filter_map do |(param, arg)|
618
- if ub = param.upper_bound
619
- Relation.new(sub_type: arg, super_type: ub.subst(sub))
620
- end
621
- end
622
-
623
- result.add(*rels) {|rel| check_type(rel) } and yield
624
- end
625
-
626
668
  def check_generic_method_type(name, relation)
627
669
  relation.method!
628
670
 
@@ -644,7 +686,9 @@ module Steep
644
686
 
645
687
  relation = Relation.new(sub_type: sub_type_, super_type: super_type)
646
688
  All(relation) do |result|
647
- sub_args.zip(sub_type.type_params).each do |(arg, param)|
689
+ sub_args.zip(sub_type.type_params).each do |arg, param|
690
+ param or raise
691
+
648
692
  if ub = param.upper_bound
649
693
  result.add(Relation.new(sub_type: arg, super_type: ub)) do |rel|
650
694
  check_type(rel)
@@ -652,23 +696,10 @@ module Steep
652
696
  end
653
697
  end
654
698
 
655
- match_method_type(name, relation) do |pairs|
656
- rels =
657
- pairs.each.with_object([]) do |(sub, sup), rels|
658
- rels << Relation.new(sub_type: sub, super_type: sup)
659
- rels << Relation.new(sub_type: sup, super_type: sub)
660
- end
661
-
662
- result.add(*rels) do |rel|
663
- check_type(rel)
664
- end
665
-
666
- result.add(Relation.new(sub_type: sub_type_, super_type: super_type)) do |rel|
667
- check_method_type(
668
- name,
669
- Relation.new(sub_type: sub_type_, super_type: super_type)
670
- )
671
- end
699
+ if failure = match_method_type_fails?(name, sub_type_, super_type)
700
+ result.add_result(failure)
701
+ else
702
+ result.add_result(check_method_type(name, Relation(sub_type_, super_type)))
672
703
  end
673
704
 
674
705
  result.add(relation) do |rel|
@@ -683,22 +714,25 @@ module Steep
683
714
 
684
715
  when sub_type.type_params.empty? && !super_type.type_params.empty?
685
716
  # Check if sub_type is an instance of super_type && no constraints on type variables (any).
686
- Expand(relation) do
687
- match_method_type(name, relation) do
688
- sub_args = sub_type.type_params.map {|param| AST::Types::Var.fresh(param.name) }
689
- sub_type_ = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params.map(&:name), sub_args))
717
+ All(relation) do |result|
718
+ super_args = super_type.type_params.map {|param| AST::Types::Var.fresh(param.name) }
719
+ super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params.map(&:name), super_args))
690
720
 
691
- sub_args.each do |arg|
721
+ if failure = match_method_type_fails?(name, sub_type, super_type_)
722
+ result.add_result(failure)
723
+ else
724
+ super_args.each do |arg|
692
725
  constraints.unknown!(arg.name)
693
726
  end
694
727
 
695
- rel_ = Relation.new(sub_type: sub_type_, super_type: super_type)
696
- result = check_method_type(name, rel_)
728
+ result.add(Relation(sub_type, super_type_)) do |rel_|
729
+ ret = check_method_type(name, rel_)
697
730
 
698
- if result.success? && sub_args.map(&:name).none? {|var| constraints.has_constraint?(var) }
699
- result
700
- else
701
- Failure(rel_, Result::Failure::PolyMethodSubtyping.new(name: name))
731
+ if ret.success? && super_args.map(&:name).none? {|var| constraints.has_constraint?(var) }
732
+ ret
733
+ else
734
+ Failure(rel_, Result::Failure::PolyMethodSubtyping.new(name: name))
735
+ end
702
736
  end
703
737
  end
704
738
  end
@@ -723,9 +757,9 @@ module Steep
723
757
  when sub_ub && !sup_ub
724
758
  upper_bounds[arg.name] = sub_ub
725
759
  when !sub_ub && sup_ub
726
- result.add(
727
- Failure(relation, Result::Failure::PolyMethodSubtyping.new(name: name))
728
- )
760
+ result.add(Relation.new(sub_type: AST::Types::Var.new(name: sub_param.name), super_type: sub_ub)) do |rel|
761
+ Failure(rel, Result::Failure::PolyMethodSubtyping.new(name: name))
762
+ end
729
763
  when !sub_ub && !sup_ub
730
764
  # no constraints
731
765
  end
@@ -748,7 +782,7 @@ module Steep
748
782
  end
749
783
 
750
784
  def check_constraints(relation, variables:, variance:)
751
- checker = Check.new(factory: factory)
785
+ checker = Check.new(builder: builder)
752
786
 
753
787
  constraints.solution(
754
788
  checker,
@@ -769,33 +803,46 @@ module Steep
769
803
 
770
804
  sub_type, super_type = relation
771
805
 
806
+ sub_type.type_params.empty? or raise "Expected monomorphic method type: #{sub_type}"
807
+ super_type.type_params.empty? or raise "Expected monomorphic method type: #{super_type}"
808
+
772
809
  All(relation) do |result|
773
810
  result.add(Relation.new(sub_type: sub_type.type, super_type: super_type.type)) do |rel|
774
811
  check_function(name, rel)
775
812
  end
776
813
 
777
814
  result.add(Relation.new(sub_type: sub_type.block, super_type: super_type.block)) do |rel|
778
- check_block_given(name, rel) do
779
- Expand(Relation.new(sub_type: super_type.block.type, super_type: sub_type.block.type)) do |rel|
780
- check_function(name, rel)
815
+ ret = expand_block_given(name, rel)
816
+
817
+ case ret
818
+ when Relation
819
+ All(ret) do |result|
820
+ result.add_result(check_self_type_binding(ret, ret.super_type.self_type, ret.sub_type.self_type))
821
+ result.add(Relation(ret.super_type.type, ret.sub_type.type)) do |rel|
822
+ check_function(name, rel)
823
+ end
781
824
  end
825
+ when Result::Base
826
+ ret
827
+ else
828
+ nil
782
829
  end
783
830
  end
784
831
  end
785
832
  end
786
833
 
787
- def check_block_given(name, relation, &block)
834
+ def expand_block_given(name, relation, &block)
788
835
  relation.block!
789
836
 
790
837
  sub_block, super_block = relation
791
838
 
792
839
  case
793
840
  when !super_block && !sub_block
794
- Success(relation)
841
+ true
795
842
  when super_block && sub_block && super_block.optional? == sub_block.optional?
796
- Expand(relation, &block)
843
+ Relation.new(sub_type: sub_block, super_type: super_block)
797
844
  when sub_block&.optional?
798
- Success(relation)
845
+ true
799
846
  else
800
847
  Failure(relation, Result::Failure::BlockMismatchError.new(name: name))
801
848
  end
@@ -815,6 +862,23 @@ module Steep
815
862
  end
816
863
  end
817
864
 
865
+ def check_self_type_binding(relation, sub_self, super_self)
866
+ case
867
+ when sub_self.nil? && super_self.nil?
868
+ nil
869
+ when sub_self && super_self
870
+ # ^() [self: T] -> void <: ^() [self: S] -> void ==> S <: T
871
+ # () { () [self: S] -> void } -> void <: () { () [self: T] -> void } -> void ==> S <: T
872
+ check_type(Relation(super_self, sub_self))
873
+ when sub_self.is_a?(AST::Types::Top) && super_self.nil?
874
+ # ^() [self: top] -> void <: ^() -> void ==> OK
875
+ # () { () -> void } -> void <: () { () [self: top] -> void } -> void ==> OK
876
+ nil
877
+ else
878
+ Failure(relation, Result::Failure::SelfBindingMismatch.new)
879
+ end
880
+ end
881
+
818
882
  def check_method_params(name, relation)
819
883
  relation.params!
820
884
  pairs = match_params(name, relation)
@@ -823,7 +887,7 @@ module Steep
823
887
  when Array
824
888
  unless pairs.empty?
825
889
  All(relation) do |result|
826
- pairs.each do |(sub_type, super_type)|
890
+ pairs.each do |sub_type, super_type|
827
891
  result.add(Relation.new(sub_type: super_type, super_type: sub_type)) do |rel|
828
892
  check_type(rel)
829
893
  end
@@ -839,38 +903,25 @@ module Steep
839
903
  end
840
904
  end
841
905
 
842
- # ```rbs
843
- # (Symbol, Relation[MethodType]) -> (Array[[Symbol, Symbol]] | Result::t)
844
- # [A] (Symbol, Relation[MethodType]) { (Array[[Symbol, Symbol]]) -> A } -> (A | Result::t)
845
- # ````
846
- def match_method_type(name, relation)
847
- relation.method!
848
-
849
- sub_type, super_type = relation
850
-
851
- pairs = []
906
+ def Relation(sub, sup)
907
+ Relation.new(sub_type: sub, super_type: sup)
908
+ end
852
909
 
853
- match_params(name, relation.map {|m| m.type.params }).tap do |param_pairs|
910
+ def match_method_type_fails?(name, type1, type2)
911
+ match_params(name, Relation(type1.type.params, type2.type.params)).tap do |param_pairs|
854
912
  return param_pairs unless param_pairs.is_a?(Array)
855
-
856
- pairs.push(*param_pairs)
857
- pairs.push [sub_type.type.return_type, super_type.type.return_type]
858
913
  end
859
914
 
860
- check_block_given(name, relation.map {|m| m.block }) do |rel|
861
- match_params(name, rel.map {|m| m.type.params }).tap do |param_pairs|
915
+ case result = expand_block_given(name, Relation(type1.block, type2.block))
916
+ when Result::Base
917
+ return result
918
+ when Relation
919
+ match_params(name, result.map {|m| m.type.params }).tap do |param_pairs|
862
920
  return param_pairs unless param_pairs.is_a?(Array)
863
-
864
- pairs.push(*param_pairs)
865
- pairs.push [sub_type.type.return_type, super_type.type.return_type]
866
921
  end
867
922
  end
868
923
 
869
- if block_given?
870
- yield pairs
871
- else
872
- pairs
873
- end
924
+ nil
874
925
  end
875
926
 
876
927
  def match_params(name, relation)
@@ -32,12 +32,12 @@ module Steep
32
32
  end
33
33
 
34
34
  def interface?
35
- sub_type.is_a?(Interface::Interface) && super_type.is_a?(Interface::Interface)
35
+ sub_type.is_a?(Interface::Shape) && super_type.is_a?(Interface::Shape)
36
36
  end
37
37
 
38
38
  def method?
39
- (sub_type.is_a?(Interface::Interface::Entry) || sub_type.is_a?(Interface::MethodType)) &&
40
- (super_type.is_a?(Interface::Interface::Entry) || super_type.is_a?(Interface::MethodType))
39
+ (sub_type.is_a?(Interface::Shape::Entry) || sub_type.is_a?(Interface::MethodType)) &&
40
+ (super_type.is_a?(Interface::Shape::Entry) || super_type.is_a?(Interface::MethodType))
41
41
  end
42
42
 
43
43
  def function?
@@ -15,6 +15,7 @@ module Steep
15
15
  def then
16
16
  if success?
17
17
  yield self
18
+ self
18
19
  else
19
20
  self
20
21
  end
@@ -23,6 +24,7 @@ module Steep
23
24
  def else
24
25
  if failure?
25
26
  yield self
27
+ self
26
28
  else
27
29
  self
28
30
  end
@@ -83,7 +85,9 @@ module Steep
83
85
  relations.each do |relation|
84
86
  if success?
85
87
  result = yield(relation)
86
- branches << result
88
+ if result
89
+ branches << result
90
+ end
87
91
  else
88
92
  # Already failed.
89
93
  branches << Skip.new(relation)
@@ -94,6 +98,14 @@ module Steep
94
98
  success?
95
99
  end
96
100
 
101
+ def add_result(result)
102
+ if result
103
+ add(result.relation) { result }
104
+ else
105
+ success?
106
+ end
107
+ end
108
+
97
109
  def success?
98
110
  !failure?
99
111
  end
@@ -104,7 +116,7 @@ module Steep
104
116
 
105
117
  def failure_path(path = [])
106
118
  if failure?
107
- r = branches.find(&:failure?)
119
+ r = branches.find(&:failure?) or raise
108
120
  path.unshift(self)
109
121
  r.failure_path(path)
110
122
  end
@@ -251,6 +263,12 @@ module Steep
251
263
  end
252
264
  end
253
265
 
266
+ class SelfBindingMismatch
267
+ def message
268
+ "Self binding is incompatible"
269
+ end
270
+ end
271
+
254
272
  attr_reader :error
255
273
 
256
274
  def initialize(relation, error)
@@ -21,6 +21,15 @@ module Steep
21
21
  covariants.member?(var) && contravariants.member?(var)
22
22
  end
23
23
 
24
+ def self.from_type(type)
25
+ covariants = Set.new
26
+ contravariants = Set.new
27
+
28
+ add_type(type, variance: :covariant, covariants: covariants, contravariants: contravariants)
29
+
30
+ new(covariants: covariants, contravariants: contravariants)
31
+ end
32
+
24
33
  def self.from_method_type(method_type)
25
34
  covariants = Set.new
26
35
  contravariants = Set.new