steep 0.13.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +28 -0
  4. data/Rakefile +0 -13
  5. data/bin/setup +0 -2
  6. data/bin/smoke_runner.rb +0 -1
  7. data/exe/steep +0 -1
  8. data/lib/steep.rb +33 -1
  9. data/lib/steep/annotation_parser.rb +4 -4
  10. data/lib/steep/ast/buffer.rb +11 -7
  11. data/lib/steep/ast/builtin.rb +8 -0
  12. data/lib/steep/ast/types/factory.rb +124 -89
  13. data/lib/steep/cli.rb +16 -1
  14. data/lib/steep/drivers/annotations.rb +1 -1
  15. data/lib/steep/drivers/check.rb +20 -4
  16. data/lib/steep/drivers/init.rb +5 -5
  17. data/lib/steep/drivers/langserver.rb +13 -287
  18. data/lib/steep/drivers/utils/driver_helper.rb +1 -1
  19. data/lib/steep/drivers/vendor.rb +2 -2
  20. data/lib/steep/drivers/watch.rb +97 -85
  21. data/lib/steep/drivers/worker.rb +51 -0
  22. data/lib/steep/project.rb +9 -5
  23. data/lib/steep/project/completion_provider.rb +298 -0
  24. data/lib/steep/project/dsl.rb +14 -0
  25. data/lib/steep/project/file.rb +54 -47
  26. data/lib/steep/project/hover_content.rb +17 -8
  27. data/lib/steep/project/options.rb +25 -3
  28. data/lib/steep/project/target.rb +40 -24
  29. data/lib/steep/server/base_worker.rb +56 -0
  30. data/lib/steep/server/code_worker.rb +151 -0
  31. data/lib/steep/server/interaction_worker.rb +281 -0
  32. data/lib/steep/server/master.rb +196 -0
  33. data/lib/steep/server/signature_worker.rb +148 -0
  34. data/lib/steep/server/utils.rb +36 -0
  35. data/lib/steep/server/worker_process.rb +62 -0
  36. data/lib/steep/signature/errors.rb +1 -1
  37. data/lib/steep/signature/validator.rb +13 -13
  38. data/lib/steep/source.rb +1 -1
  39. data/lib/steep/type_construction.rb +1004 -727
  40. data/lib/steep/type_inference/constant_env.rb +3 -11
  41. data/lib/steep/type_inference/context.rb +8 -3
  42. data/lib/steep/type_inference/context_array.rb +111 -0
  43. data/lib/steep/type_inference/local_variable_type_env.rb +226 -0
  44. data/lib/steep/type_inference/logic.rb +130 -0
  45. data/lib/steep/type_inference/type_env.rb +5 -69
  46. data/lib/steep/typing.rb +91 -23
  47. data/lib/steep/version.rb +1 -1
  48. data/smoke/alias/Steepfile +1 -0
  49. data/smoke/alias/a.rb +1 -1
  50. data/smoke/and/Steepfile +1 -0
  51. data/smoke/array/Steepfile +1 -0
  52. data/smoke/array/b.rb +0 -2
  53. data/smoke/block/Steepfile +1 -0
  54. data/smoke/case/Steepfile +1 -0
  55. data/smoke/class/Steepfile +1 -0
  56. data/smoke/const/Steepfile +1 -0
  57. data/smoke/dstr/Steepfile +1 -0
  58. data/smoke/ensure/Steepfile +1 -0
  59. data/smoke/enumerator/Steepfile +1 -0
  60. data/smoke/extension/Steepfile +1 -0
  61. data/smoke/extension/c.rb +1 -0
  62. data/smoke/hash/Steepfile +1 -0
  63. data/smoke/hello/Steepfile +1 -0
  64. data/smoke/if/Steepfile +1 -0
  65. data/smoke/if/a.rb +1 -1
  66. data/smoke/implements/Steepfile +1 -0
  67. data/smoke/initialize/Steepfile +1 -0
  68. data/smoke/integer/Steepfile +1 -0
  69. data/smoke/interface/Steepfile +1 -0
  70. data/smoke/kwbegin/Steepfile +1 -0
  71. data/smoke/lambda/Steepfile +1 -0
  72. data/smoke/literal/Steepfile +1 -0
  73. data/smoke/map/Steepfile +1 -0
  74. data/smoke/method/Steepfile +1 -0
  75. data/smoke/module/Steepfile +1 -0
  76. data/smoke/regexp/Steepfile +1 -0
  77. data/smoke/regexp/b.rb +4 -4
  78. data/smoke/regression/Steepfile +1 -0
  79. data/smoke/rescue/Steepfile +1 -0
  80. data/smoke/rescue/a.rb +1 -1
  81. data/smoke/self/Steepfile +1 -0
  82. data/smoke/skip/Steepfile +1 -0
  83. data/smoke/stdout/Steepfile +1 -0
  84. data/smoke/super/Steepfile +1 -0
  85. data/smoke/type_case/Steepfile +1 -0
  86. data/smoke/yield/Steepfile +1 -0
  87. data/steep.gemspec +8 -8
  88. metadata +38 -138
  89. data/exe/rbs +0 -3
  90. data/exe/ruby-signature +0 -3
  91. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  92. data/vendor/ruby-signature/.gitignore +0 -12
  93. data/vendor/ruby-signature/.rubocop.yml +0 -15
  94. data/vendor/ruby-signature/BSDL +0 -22
  95. data/vendor/ruby-signature/COPYING +0 -56
  96. data/vendor/ruby-signature/Gemfile +0 -6
  97. data/vendor/ruby-signature/README.md +0 -93
  98. data/vendor/ruby-signature/Rakefile +0 -66
  99. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -156
  100. data/vendor/ruby-signature/bin/console +0 -14
  101. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  102. data/vendor/ruby-signature/bin/setup +0 -10
  103. data/vendor/ruby-signature/bin/sort +0 -88
  104. data/vendor/ruby-signature/bin/test_runner.rb +0 -17
  105. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  106. data/vendor/ruby-signature/docs/sigs.md +0 -148
  107. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  108. data/vendor/ruby-signature/docs/syntax.md +0 -528
  109. data/vendor/ruby-signature/exe/rbs +0 -3
  110. data/vendor/ruby-signature/exe/ruby-signature +0 -7
  111. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -64
  112. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +0 -29
  113. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +0 -29
  114. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +0 -391
  115. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +0 -364
  116. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +0 -52
  117. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +0 -54
  118. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +0 -534
  119. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +0 -28
  120. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +0 -152
  121. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +0 -172
  122. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +0 -921
  123. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +0 -283
  124. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +0 -138
  125. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +0 -126
  126. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +0 -189
  127. data/vendor/ruby-signature/lib/ruby/signature/location.rb +0 -104
  128. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +0 -125
  129. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +0 -93
  130. data/vendor/ruby-signature/lib/ruby/signature/parser.y +0 -1343
  131. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +0 -441
  132. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +0 -579
  133. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +0 -383
  134. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +0 -48
  135. data/vendor/ruby-signature/lib/ruby/signature/test.rb +0 -28
  136. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +0 -63
  137. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +0 -290
  138. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +0 -58
  139. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +0 -324
  140. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +0 -185
  141. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +0 -256
  142. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +0 -72
  143. data/vendor/ruby-signature/lib/ruby/signature/types.rb +0 -932
  144. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +0 -140
  145. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +0 -49
  146. data/vendor/ruby-signature/lib/ruby/signature/version.rb +0 -5
  147. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +0 -271
  148. data/vendor/ruby-signature/ruby-signature.gemspec +0 -45
  149. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -3
  150. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -15
  151. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  152. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  153. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  154. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -35
  155. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  156. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  157. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  158. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  159. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  160. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  161. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -419
  162. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -606
  163. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  164. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  165. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  166. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  167. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -193
  168. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  169. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  170. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  171. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -476
  172. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  173. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  174. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -121
  175. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  176. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -710
  177. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  178. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -574
  179. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -135
  180. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -141
  181. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -66
  182. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -182
  183. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -248
  184. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  185. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  186. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  187. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -426
  188. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -354
  189. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -93
  190. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  191. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  192. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -10
  193. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -131
  194. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  195. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  196. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -770
  197. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -13
  198. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  199. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -230
  200. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1112
  201. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  202. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -739
  203. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -91
  204. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  205. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -159
  206. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  207. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -18
  208. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -44
  209. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -21
  210. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  211. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  212. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  213. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -77
  214. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,185 +0,0 @@
1
- module Ruby
2
- module Signature
3
- module Test
4
- module TypeAssertions
5
- module ClassMethods
6
- attr_reader :target
7
-
8
- def library(*libs)
9
- @libs = libs
10
- @env = nil
11
- @target = nil
12
- end
13
-
14
- def env
15
- @env ||= begin
16
- loader = Ruby::Signature::EnvironmentLoader.new
17
- (@libs || []).each do |lib|
18
- loader.add library: lib
19
- end
20
-
21
- Ruby::Signature::Environment.new.tap do |env|
22
- loader.load(env: env)
23
- end
24
- end
25
- end
26
-
27
- def builder
28
- @builder ||= DefinitionBuilder.new(env: env)
29
- end
30
-
31
- def testing(type_or_string)
32
- type = case type_or_string
33
- when String
34
- Ruby::Signature::Parser.parse_type(type_or_string, variables: [])
35
- else
36
- type_or_string
37
- end
38
-
39
- definition = case type
40
- when Types::ClassInstance
41
- builder.build_instance(type.name)
42
- when Types::ClassSingleton
43
- builder.build_singleton(type.name)
44
- else
45
- raise "Test target should be class instance or class singleton: #{type}"
46
- end
47
-
48
- @target = [type, definition]
49
- end
50
- end
51
-
52
- def self.included(base)
53
- base.extend ClassMethods
54
- end
55
-
56
- def env
57
- self.class.env
58
- end
59
-
60
- def builder
61
- self.class.builder
62
- end
63
-
64
- def targets
65
- @targets ||= []
66
- end
67
-
68
- def target
69
- targets.last || self.class.target
70
- end
71
-
72
- def testing(type_or_string)
73
- type = case type_or_string
74
- when String
75
- Ruby::Signature::Parser.parse_type(type_or_string, variables: [])
76
- else
77
- type_or_string
78
- end
79
-
80
- definition = case type
81
- when Types::ClassInstance
82
- builder.build_instance(type.name)
83
- when Types::ClassSingleton
84
- builder.build_singleton(type.name)
85
- else
86
- raise "Test target should be class instance or class singleton: #{type}"
87
- end
88
-
89
- targets.push [type, definition]
90
-
91
- if block_given?
92
- begin
93
- yield
94
- ensure
95
- targets.pop
96
- end
97
- else
98
- [type, definition]
99
- end
100
- end
101
-
102
- ruby2_keywords def assert_send_type(method_type, receiver, method, *args, &block)
103
- trace = []
104
- spy = Spy.wrap(receiver, method)
105
- spy.callback = -> (result) { trace << result }
106
-
107
- exception = nil
108
-
109
- begin
110
- spy.wrapped_object.__send__(method, *args, &block)
111
- rescue => exn
112
- exception = exn
113
- end
114
-
115
- mt = case method_type
116
- when String
117
- Ruby::Signature::Parser.parse_method_type(method_type, variables: [])
118
- when Ruby::Signature::MethodType
119
- method_type
120
- end
121
-
122
- typecheck = TypeCheck.new(self_class: receiver.class, builder: builder)
123
- errors = typecheck.method_call(method, mt, trace.last, errors: [])
124
-
125
- assert_empty errors.map {|x| Ruby::Signature::Test::Errors.to_string(x) }, "Call trace does not match with given method type: #{trace.last.inspect}"
126
-
127
- type, definition = target
128
- method_types = case
129
- when definition.instance_type?
130
- subst = Substitution.build(definition.declaration.type_params.each.map(&:name),
131
- type.args)
132
- definition.methods[method].method_types.map do |method_type|
133
- method_type.sub(subst)
134
- end
135
- when definition.class_type?
136
- definition.methods[method].method_types
137
- end
138
-
139
- all_errors = method_types.map {|t| typecheck.method_call(method, t, trace.last, errors: []) }
140
- assert all_errors.any? {|es| es.empty? }, "Call trace does not match one of method definitions:\n #{trace.last.inspect}\n #{method_types.join(" | ")}"
141
-
142
- if exception
143
- raise exception
144
- end
145
- end
146
-
147
- ruby2_keywords def refute_send_type(method_type, receiver, method, *args, &block)
148
- trace = []
149
- spy = Spy.wrap(receiver, method)
150
- spy.callback = -> (result) { trace << result }
151
-
152
- exception = nil
153
- begin
154
- spy.wrapped_object.__send__(method, *args, &block)
155
- rescue Exception => exn
156
- exception = exn
157
- end
158
-
159
- mt = case method_type
160
- when String
161
- Ruby::Signature::Parser.parse_method_type(method_type, variables: [])
162
- when Ruby::Signature::MethodType
163
- method_type
164
- end
165
-
166
- mt = mt.update(block: if mt.block
167
- MethodType::Block.new(
168
- type: mt.block.type.with_return_type(Types::Bases::Any.new(location: nil)),
169
- required: mt.block.required
170
- )
171
- end,
172
- type: mt.type.with_return_type(Types::Bases::Any.new(location: nil)))
173
-
174
- typecheck = TypeCheck.new(self_class: receiver.class, builder: builder)
175
- errors = typecheck.method_call(method, mt, trace.last, errors: [])
176
-
177
- assert_operator exception, :is_a?, ::Exception
178
- assert_empty errors.map {|x| Ruby::Signature::Test::Errors.to_string(x) }
179
-
180
- exception
181
- end
182
- end
183
- end
184
- end
185
- end
@@ -1,256 +0,0 @@
1
- module Ruby
2
- module Signature
3
- module Test
4
- class TypeCheck
5
- attr_reader :self_class
6
- attr_reader :builder
7
-
8
- def initialize(self_class:, builder:)
9
- @self_class = self_class
10
- @builder = builder
11
- end
12
-
13
- def method_call(method_name, method_type, call, errors:)
14
- args(method_name, method_type, method_type.type, call.method_call, errors, type_error: Errors::ArgumentTypeError, argument_error: Errors::ArgumentError)
15
- self.return(method_name, method_type, method_type.type, call.method_call, errors, return_error: Errors::ReturnTypeError)
16
-
17
- if method_type.block
18
- case
19
- when !call.block_calls.empty?
20
- call.block_calls.each do |block_call|
21
- args(method_name, method_type, method_type.block.type, block_call, errors, type_error: Errors::BlockArgumentTypeError, argument_error: Errors::BlockArgumentError)
22
- self.return(method_name, method_type, method_type.block.type, block_call, errors, return_error: Errors::BlockReturnTypeError)
23
- end
24
- when !call.block_given
25
- # Block is not given
26
- if method_type.block.required
27
- errors << Errors::MissingBlockError.new(klass: self_class, method_name: method_name, method_type: method_type)
28
- end
29
- else
30
- # Block is given, but not yielded
31
- end
32
- else
33
- if call.block_given
34
- errors << Errors::UnexpectedBlockError.new(klass: self_class, method_name: method_name, method_type: method_type)
35
- end
36
- end
37
-
38
- errors
39
- end
40
-
41
- def args(method_name, method_type, fun, call, errors, type_error:, argument_error:)
42
- test = zip_args(call.arguments, fun) do |val, param|
43
- unless self.value(val, param.type)
44
- errors << type_error.new(klass: self_class,
45
- method_name: method_name,
46
- method_type: method_type,
47
- param: param,
48
- value: val)
49
- end
50
- end
51
-
52
- unless test
53
- errors << argument_error.new(klass: self_class,
54
- method_name: method_name,
55
- method_type: method_type)
56
- end
57
- end
58
-
59
- def return(method_name, method_type, fun, call, errors, return_error:)
60
- unless call.exception
61
- unless value(call.return_value, fun.return_type)
62
- errors << return_error.new(klass: self_class,
63
- method_name: method_name,
64
- method_type: method_type,
65
- type: fun.return_type,
66
- value: call.return_value)
67
- end
68
- end
69
- end
70
-
71
- def zip_keyword_args(hash, fun)
72
- fun.required_keywords.each do |name, param|
73
- if hash.key?(name)
74
- yield(hash[name], param)
75
- else
76
- return false
77
- end
78
- end
79
-
80
- fun.optional_keywords.each do |name, param|
81
- if hash.key?(name)
82
- yield(hash[name], param)
83
- end
84
- end
85
-
86
- hash.each do |name, value|
87
- next if fun.required_keywords.key?(name)
88
- next if fun.optional_keywords.key?(name)
89
-
90
- if fun.rest_keywords
91
- yield value, fun.rest_keywords
92
- else
93
- return false
94
- end
95
- end
96
-
97
- true
98
- end
99
-
100
- def keyword?(value)
101
- value.is_a?(Hash) && value.keys.all? {|key| key.is_a?(Symbol) }
102
- end
103
-
104
- def zip_args(args, fun, &block)
105
- case
106
- when args.empty?
107
- if fun.required_positionals.empty? && fun.trailing_positionals.empty? && fun.required_keywords.empty?
108
- true
109
- else
110
- false
111
- end
112
- when !fun.required_positionals.empty?
113
- yield_self do
114
- param, fun_ = fun.drop_head
115
- yield(args.first, param)
116
- zip_args(args.drop(1), fun_, &block)
117
- end
118
- when fun.has_keyword?
119
- yield_self do
120
- hash = args.last
121
- if keyword?(hash)
122
- zip_keyword_args(hash, fun, &block) &&
123
- zip_args(args.take(args.size - 1),
124
- fun.update(required_keywords: {}, optional_keywords: {}, rest_keywords: nil),
125
- &block)
126
- else
127
- fun.required_keywords.empty? &&
128
- zip_args(args,
129
- fun.update(required_keywords: {}, optional_keywords: {}, rest_keywords: nil),
130
- &block)
131
- end
132
- end
133
- when !fun.trailing_positionals.empty?
134
- yield_self do
135
- param, fun_ = fun.drop_tail
136
- yield(args.last, param)
137
- zip_args(args.take(args.size - 1), fun_, &block)
138
- end
139
- when !fun.optional_positionals.empty?
140
- yield_self do
141
- param, fun_ = fun.drop_head
142
- yield(args.first, param)
143
- zip_args(args.drop(1), fun_, &block)
144
- end
145
- when fun.rest_positionals
146
- yield_self do
147
- yield(args.first, fun.rest_positionals)
148
- zip_args(args.drop(1), fun, &block)
149
- end
150
- else
151
- false
152
- end
153
- end
154
-
155
- def value(val, type)
156
- case type
157
- when Types::Bases::Any
158
- true
159
- when Types::Bases::Bool
160
- true
161
- when Types::Bases::Top
162
- true
163
- when Types::Bases::Bottom
164
- false
165
- when Types::Bases::Void
166
- true
167
- when Types::Bases::Self
168
- Test.call(val, IS_AP, self_class)
169
- when Types::Bases::Nil
170
- Test.call(val, IS_AP, ::NilClass)
171
- when Types::Bases::Class
172
- Test.call(val, IS_AP, Class)
173
- when Types::Bases::Instance
174
- Test.call(val, IS_AP, self_class)
175
- when Types::ClassInstance
176
- klass = Object.const_get(type.name.to_s)
177
- case
178
- when klass == ::Array
179
- Test.call(val, IS_AP, klass) && val.all? {|v| value(v, type.args[0]) }
180
- when klass == ::Hash
181
- Test.call(val, IS_AP, klass) && val.all? {|k, v| value(k, type.args[0]) && value(v, type.args[1]) }
182
- when klass == ::Range
183
- Test.call(val, IS_AP, klass) && value(val.begin, type.args[0]) && value(val.end, type.args[0])
184
- when klass == ::Enumerator
185
- if Test.call(val, IS_AP, klass)
186
- case val.size
187
- when Float::INFINITY
188
- values = []
189
- ret = self
190
- val.lazy.take(10).each do |*args|
191
- values << args
192
- nil
193
- end
194
- else
195
- values = []
196
- ret = val.each do |*args|
197
- values << args
198
- nil
199
- end
200
- end
201
-
202
- values.all? do |v|
203
- if v.size == 1
204
- # Only one block argument.
205
- value(v[0], type.args[0]) || value(v, type.args[0])
206
- else
207
- value(v, type.args[0])
208
- end
209
- end &&
210
- if ret.equal?(self)
211
- type.args[1].is_a?(Types::Bases::Bottom)
212
- else
213
- value(ret, type.args[1])
214
- end
215
- end
216
- else
217
- Test.call(val, IS_AP, klass)
218
- end
219
- when Types::ClassSingleton
220
- klass = Object.const_get(type.name.to_s)
221
- val == klass
222
- when Types::Interface
223
- methods = Set.new(Test.call(val, METHODS))
224
- decl = builder.env.find_class(type.name)
225
- if (definition = builder.build_interface(type.name, decl))
226
- definition.methods.each_key.all? do |method_name|
227
- methods.member?(method_name)
228
- end
229
- end
230
- when Types::Variable
231
- true
232
- when Types::Literal
233
- val == type.literal
234
- when Types::Union
235
- type.types.any? {|type| value(val, type) }
236
- when Types::Intersection
237
- type.types.all? {|type| value(val, type) }
238
- when Types::Optional
239
- Test.call(val, IS_AP, ::NilClass) || value(val, type.type)
240
- when Types::Alias
241
- value(val, builder.expand_alias(type.name))
242
- when Types::Tuple
243
- Test.call(val, IS_AP, ::Array) &&
244
- type.types.map.with_index {|ty, index| value(val[index], ty) }.all?
245
- when Types::Record
246
- Test::call(val, IS_AP, ::Hash)
247
- when Types::Proc
248
- Test::call(val, IS_AP, ::Proc)
249
- else
250
- false
251
- end
252
- end
253
- end
254
- end
255
- end
256
- end
@@ -1,72 +0,0 @@
1
- module Ruby
2
- module Signature
3
- class TypeName
4
- attr_reader :namespace
5
- attr_reader :name
6
- attr_reader :kind
7
-
8
- def initialize(namespace:, name:)
9
- @namespace = namespace
10
- @name = name
11
- @kind = case name.to_s[0,1]
12
- when /[A-Z]/
13
- :class
14
- when /[a-z]/
15
- :alias
16
- when "_"
17
- :interface
18
- end
19
- end
20
-
21
- def ==(other)
22
- other.is_a?(self.class) && other.namespace == namespace && other.name == name
23
- end
24
-
25
- alias eql? ==
26
-
27
- def hash
28
- self.class.hash ^ namespace.hash ^ name.hash
29
- end
30
-
31
- def to_s
32
- "#{namespace.to_s}#{name}"
33
- end
34
-
35
- def to_json(*a)
36
- to_s.to_json(*a)
37
- end
38
-
39
- def to_namespace
40
- namespace.append(self.name)
41
- end
42
-
43
- def class?
44
- kind == :class
45
- end
46
-
47
- def alias?
48
- kind == :alias
49
- end
50
-
51
- def absolute!
52
- self.class.new(namespace: namespace.absolute!, name: name)
53
- end
54
-
55
- def absolute?
56
- namespace.absolute?
57
- end
58
-
59
- def relative!
60
- self.class.new(namespace: namespace.relative!, name: name)
61
- end
62
-
63
- def interface?
64
- kind == :interface
65
- end
66
-
67
- def with_prefix(namespace)
68
- self.class.new(namespace: namespace + self.namespace, name: name)
69
- end
70
- end
71
- end
72
- end