steep-activesupport-4 1.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.gitmodules +0 -0
- data/CHANGELOG.md +1032 -0
- data/LICENSE +21 -0
- data/README.md +260 -0
- data/Rakefile +227 -0
- data/Steepfile +68 -0
- data/bin/console +14 -0
- data/bin/generate-diagnostics-docs.rb +112 -0
- data/bin/mem_graph.rb +67 -0
- data/bin/mem_prof.rb +102 -0
- data/bin/output_rebaseline.rb +34 -0
- data/bin/output_test.rb +60 -0
- data/bin/rbs +20 -0
- data/bin/rbs-inline +19 -0
- data/bin/setup +9 -0
- data/bin/stackprof_test.rb +19 -0
- data/bin/steep +19 -0
- data/bin/steep-check.rb +251 -0
- data/bin/steep-prof +16 -0
- data/doc/narrowing.md +195 -0
- data/doc/shape.md +194 -0
- data/exe/steep +18 -0
- data/guides/README.md +5 -0
- data/guides/src/gem-rbs-collection/gem-rbs-collection.md +126 -0
- data/guides/src/getting-started/getting-started.md +163 -0
- data/guides/src/nil-optional/nil-optional.md +195 -0
- data/lib/steep/annotation_parser.rb +199 -0
- data/lib/steep/ast/annotation/collection.rb +172 -0
- data/lib/steep/ast/annotation.rb +137 -0
- data/lib/steep/ast/builtin.rb +104 -0
- data/lib/steep/ast/ignore.rb +148 -0
- data/lib/steep/ast/node/type_application.rb +88 -0
- data/lib/steep/ast/node/type_assertion.rb +81 -0
- data/lib/steep/ast/types/any.rb +35 -0
- data/lib/steep/ast/types/boolean.rb +45 -0
- data/lib/steep/ast/types/bot.rb +35 -0
- data/lib/steep/ast/types/class.rb +43 -0
- data/lib/steep/ast/types/factory.rb +557 -0
- data/lib/steep/ast/types/helper.rb +40 -0
- data/lib/steep/ast/types/instance.rb +42 -0
- data/lib/steep/ast/types/intersection.rb +93 -0
- data/lib/steep/ast/types/literal.rb +59 -0
- data/lib/steep/ast/types/logic.rb +84 -0
- data/lib/steep/ast/types/name.rb +128 -0
- data/lib/steep/ast/types/nil.rb +41 -0
- data/lib/steep/ast/types/proc.rb +117 -0
- data/lib/steep/ast/types/record.rb +79 -0
- data/lib/steep/ast/types/self.rb +43 -0
- data/lib/steep/ast/types/shared_instance.rb +11 -0
- data/lib/steep/ast/types/top.rb +35 -0
- data/lib/steep/ast/types/tuple.rb +60 -0
- data/lib/steep/ast/types/union.rb +97 -0
- data/lib/steep/ast/types/var.rb +65 -0
- data/lib/steep/ast/types/void.rb +35 -0
- data/lib/steep/cli.rb +401 -0
- data/lib/steep/diagnostic/deprecated/else_on_exhaustive_case.rb +20 -0
- data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
- data/lib/steep/diagnostic/helper.rb +18 -0
- data/lib/steep/diagnostic/lsp_formatter.rb +78 -0
- data/lib/steep/diagnostic/result_printer2.rb +48 -0
- data/lib/steep/diagnostic/ruby.rb +1221 -0
- data/lib/steep/diagnostic/signature.rb +570 -0
- data/lib/steep/drivers/annotations.rb +52 -0
- data/lib/steep/drivers/check.rb +339 -0
- data/lib/steep/drivers/checkfile.rb +210 -0
- data/lib/steep/drivers/diagnostic_printer.rb +105 -0
- data/lib/steep/drivers/init.rb +66 -0
- data/lib/steep/drivers/langserver.rb +56 -0
- data/lib/steep/drivers/print_project.rb +113 -0
- data/lib/steep/drivers/stats.rb +203 -0
- data/lib/steep/drivers/utils/driver_helper.rb +143 -0
- data/lib/steep/drivers/utils/jobs_option.rb +26 -0
- data/lib/steep/drivers/vendor.rb +27 -0
- data/lib/steep/drivers/watch.rb +194 -0
- data/lib/steep/drivers/worker.rb +58 -0
- data/lib/steep/equatable.rb +23 -0
- data/lib/steep/expectations.rb +228 -0
- data/lib/steep/index/rbs_index.rb +350 -0
- data/lib/steep/index/signature_symbol_provider.rb +185 -0
- data/lib/steep/index/source_index.rb +167 -0
- data/lib/steep/interface/block.rb +103 -0
- data/lib/steep/interface/builder.rb +843 -0
- data/lib/steep/interface/function.rb +1090 -0
- data/lib/steep/interface/method_type.rb +330 -0
- data/lib/steep/interface/shape.rb +239 -0
- data/lib/steep/interface/substitution.rb +159 -0
- data/lib/steep/interface/type_param.rb +115 -0
- data/lib/steep/located_value.rb +20 -0
- data/lib/steep/method_name.rb +42 -0
- data/lib/steep/module_helper.rb +24 -0
- data/lib/steep/node_helper.rb +273 -0
- data/lib/steep/path_helper.rb +30 -0
- data/lib/steep/project/dsl.rb +268 -0
- data/lib/steep/project/group.rb +31 -0
- data/lib/steep/project/options.rb +63 -0
- data/lib/steep/project/pattern.rb +59 -0
- data/lib/steep/project/target.rb +92 -0
- data/lib/steep/project.rb +78 -0
- data/lib/steep/rake_task.rb +132 -0
- data/lib/steep/range_extension.rb +29 -0
- data/lib/steep/server/base_worker.rb +97 -0
- data/lib/steep/server/change_buffer.rb +73 -0
- data/lib/steep/server/custom_methods.rb +77 -0
- data/lib/steep/server/delay_queue.rb +45 -0
- data/lib/steep/server/interaction_worker.rb +492 -0
- data/lib/steep/server/lsp_formatter.rb +455 -0
- data/lib/steep/server/master.rb +912 -0
- data/lib/steep/server/target_group_files.rb +205 -0
- data/lib/steep/server/type_check_controller.rb +366 -0
- data/lib/steep/server/type_check_worker.rb +303 -0
- data/lib/steep/server/work_done_progress.rb +64 -0
- data/lib/steep/server/worker_process.rb +176 -0
- data/lib/steep/services/completion_provider.rb +802 -0
- data/lib/steep/services/content_change.rb +61 -0
- data/lib/steep/services/file_loader.rb +74 -0
- data/lib/steep/services/goto_service.rb +441 -0
- data/lib/steep/services/hover_provider/rbs.rb +88 -0
- data/lib/steep/services/hover_provider/ruby.rb +221 -0
- data/lib/steep/services/hover_provider/singleton_methods.rb +20 -0
- data/lib/steep/services/path_assignment.rb +46 -0
- data/lib/steep/services/signature_help_provider.rb +202 -0
- data/lib/steep/services/signature_service.rb +428 -0
- data/lib/steep/services/stats_calculator.rb +68 -0
- data/lib/steep/services/type_check_service.rb +394 -0
- data/lib/steep/services/type_name_completion.rb +236 -0
- data/lib/steep/signature/validator.rb +651 -0
- data/lib/steep/source/ignore_ranges.rb +69 -0
- data/lib/steep/source.rb +691 -0
- data/lib/steep/subtyping/cache.rb +30 -0
- data/lib/steep/subtyping/check.rb +1113 -0
- data/lib/steep/subtyping/constraints.rb +341 -0
- data/lib/steep/subtyping/relation.rb +101 -0
- data/lib/steep/subtyping/result.rb +324 -0
- data/lib/steep/subtyping/variable_variance.rb +89 -0
- data/lib/steep/test.rb +9 -0
- data/lib/steep/thread_waiter.rb +43 -0
- data/lib/steep/type_construction.rb +5183 -0
- data/lib/steep/type_inference/block_params.rb +416 -0
- data/lib/steep/type_inference/case_when.rb +303 -0
- data/lib/steep/type_inference/constant_env.rb +56 -0
- data/lib/steep/type_inference/context.rb +195 -0
- data/lib/steep/type_inference/logic_type_interpreter.rb +613 -0
- data/lib/steep/type_inference/method_call.rb +193 -0
- data/lib/steep/type_inference/method_params.rb +531 -0
- data/lib/steep/type_inference/multiple_assignment.rb +194 -0
- data/lib/steep/type_inference/send_args.rb +712 -0
- data/lib/steep/type_inference/type_env.rb +341 -0
- data/lib/steep/type_inference/type_env_builder.rb +138 -0
- data/lib/steep/typing.rb +321 -0
- data/lib/steep/version.rb +3 -0
- data/lib/steep.rb +369 -0
- data/manual/annotations.md +181 -0
- data/manual/ignore.md +20 -0
- data/manual/ruby-diagnostics.md +1879 -0
- data/sample/Steepfile +22 -0
- data/sample/lib/conference.rb +49 -0
- data/sample/lib/length.rb +35 -0
- data/sample/sig/conference.rbs +42 -0
- data/sample/sig/generics.rbs +15 -0
- data/sample/sig/length.rbs +34 -0
- data/steep-activesupport-4.gemspec +55 -0
- metadata +437 -0
@@ -0,0 +1,1113 @@
|
|
1
|
+
module Steep
|
2
|
+
module Subtyping
|
3
|
+
class Check
|
4
|
+
ABORT_LIMIT = ENV.fetch("STEEP_SUBTYPING_ABORT_LIMIT", 50).to_i
|
5
|
+
|
6
|
+
attr_reader :builder
|
7
|
+
attr_reader :cache
|
8
|
+
|
9
|
+
def initialize(builder:)
|
10
|
+
@builder = builder
|
11
|
+
@cache = Cache.new()
|
12
|
+
@bounds = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def factory
|
16
|
+
builder.factory
|
17
|
+
end
|
18
|
+
|
19
|
+
def with_context(self_type:, instance_type:, class_type:, constraints:)
|
20
|
+
@self_type = self_type
|
21
|
+
@instance_type = instance_type
|
22
|
+
@class_type = class_type
|
23
|
+
@constraints = constraints
|
24
|
+
@assumptions = Set[]
|
25
|
+
|
26
|
+
yield
|
27
|
+
ensure
|
28
|
+
@self_type = nil
|
29
|
+
@instance_type = nil
|
30
|
+
@class_type = nil
|
31
|
+
@constraints = nil
|
32
|
+
@assumptions = nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def push_assumption(relation)
|
36
|
+
assumptions << relation
|
37
|
+
yield
|
38
|
+
ensure
|
39
|
+
assumptions.delete(relation)
|
40
|
+
end
|
41
|
+
|
42
|
+
def push_variable_bounds(params)
|
43
|
+
case params
|
44
|
+
when Array
|
45
|
+
b = params.each.with_object({}) do |param, hash| #$ Hash[Symbol, AST::Types::t?]
|
46
|
+
hash[param.name] = param.upper_bound
|
47
|
+
end
|
48
|
+
when Hash
|
49
|
+
b = params
|
50
|
+
end
|
51
|
+
|
52
|
+
@bounds.push(b)
|
53
|
+
yield
|
54
|
+
|
55
|
+
ensure
|
56
|
+
@bounds.pop
|
57
|
+
end
|
58
|
+
|
59
|
+
def variable_upper_bound(name)
|
60
|
+
@bounds.reverse_each do |hash|
|
61
|
+
if hash.key?(name)
|
62
|
+
return hash.fetch(name)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
nil
|
67
|
+
end
|
68
|
+
|
69
|
+
def variable_upper_bounds
|
70
|
+
@bounds.each_with_object({}) do |bounds, hash| #$ Hash[Symbol, AST::Types::t?]
|
71
|
+
hash.merge!(bounds)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def assumptions
|
76
|
+
@assumptions || raise
|
77
|
+
end
|
78
|
+
|
79
|
+
def self_type
|
80
|
+
@self_type || raise
|
81
|
+
end
|
82
|
+
|
83
|
+
def instance_type
|
84
|
+
@instance_type || AST::Types::Instance.instance
|
85
|
+
end
|
86
|
+
|
87
|
+
def class_type
|
88
|
+
@class_type || AST::Types::Class.instance
|
89
|
+
end
|
90
|
+
|
91
|
+
def constraints
|
92
|
+
@constraints || raise
|
93
|
+
end
|
94
|
+
|
95
|
+
def each_ancestor(ancestors, &block)
|
96
|
+
if block
|
97
|
+
if ancestors.super_class
|
98
|
+
yield ancestors.super_class
|
99
|
+
end
|
100
|
+
ancestors.each_included_module(&block)
|
101
|
+
ancestors.each_included_interface(&block)
|
102
|
+
ancestors.each_prepended_module(&block)
|
103
|
+
ancestors.each_extended_module(&block)
|
104
|
+
ancestors.each_extended_interface(&block)
|
105
|
+
else
|
106
|
+
enum_for :each_ancestor, ancestors
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def instance_super_types(type_name, args:)
|
111
|
+
ancestors = factory.definition_builder.ancestor_builder.one_instance_ancestors(type_name)
|
112
|
+
|
113
|
+
subst = unless args.empty?
|
114
|
+
args_ = args.map {|type| factory.type_1(type) }
|
115
|
+
params = ancestors.params or raise
|
116
|
+
RBS::Substitution.build(params, args_)
|
117
|
+
end
|
118
|
+
|
119
|
+
each_ancestor(ancestors).map do |ancestor|
|
120
|
+
name = ancestor.name
|
121
|
+
|
122
|
+
case ancestor
|
123
|
+
when RBS::Definition::Ancestor::Instance
|
124
|
+
args = ancestor.args.map do |type|
|
125
|
+
type = type.sub(subst) if subst
|
126
|
+
factory.type(type)
|
127
|
+
end
|
128
|
+
|
129
|
+
if ancestor.name.class?
|
130
|
+
AST::Types::Name::Instance.new(
|
131
|
+
name: name,
|
132
|
+
args: args
|
133
|
+
)
|
134
|
+
else
|
135
|
+
AST::Types::Name::Interface.new(
|
136
|
+
name: name,
|
137
|
+
args: args
|
138
|
+
)
|
139
|
+
end
|
140
|
+
when RBS::Definition::Ancestor::Singleton
|
141
|
+
AST::Types::Name::Singleton.new(
|
142
|
+
name: name
|
143
|
+
)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def singleton_super_types(type_name)
|
149
|
+
ancestors = factory.definition_builder.ancestor_builder.one_singleton_ancestors(type_name)
|
150
|
+
|
151
|
+
ancestors.each_ancestor.map do |ancestor|
|
152
|
+
name = ancestor.name
|
153
|
+
|
154
|
+
case ancestor
|
155
|
+
when RBS::Definition::Ancestor::Instance
|
156
|
+
args = ancestor.args.map do |type|
|
157
|
+
factory.type(type)
|
158
|
+
end
|
159
|
+
|
160
|
+
if ancestor.name.class?
|
161
|
+
AST::Types::Name::Instance.new(
|
162
|
+
name: name,
|
163
|
+
args: args
|
164
|
+
)
|
165
|
+
else
|
166
|
+
AST::Types::Name::Interface.new(
|
167
|
+
name: name,
|
168
|
+
args: args
|
169
|
+
)
|
170
|
+
end
|
171
|
+
when RBS::Definition::Ancestor::Singleton
|
172
|
+
AST::Types::Name::Singleton.new(
|
173
|
+
name: name
|
174
|
+
)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def check(relation, constraints:, self_type:, instance_type:, class_type:)
|
180
|
+
with_context(self_type: self_type, instance_type: instance_type, class_type: class_type, constraints: constraints) do
|
181
|
+
check_type(relation)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
def check_type(relation)
|
186
|
+
if assumptions.size > ABORT_LIMIT
|
187
|
+
return Failure(relation, Result::Failure::LoopAbort.new)
|
188
|
+
end
|
189
|
+
|
190
|
+
relation.type!
|
191
|
+
|
192
|
+
Steep.logger.tagged "#{relation.sub_type} <: #{relation.super_type}" do
|
193
|
+
bounds = cache_bounds(relation)
|
194
|
+
fvs = relation.sub_type.free_variables + relation.super_type.free_variables
|
195
|
+
cached = cache[relation, @self_type, @instance_type, @class_type, bounds]
|
196
|
+
if cached && fvs.none? {|var| var.is_a?(Symbol) && constraints.unknown?(var) }
|
197
|
+
cached
|
198
|
+
else
|
199
|
+
if assumptions.member?(relation)
|
200
|
+
success(relation)
|
201
|
+
else
|
202
|
+
push_assumption(relation) do
|
203
|
+
check_type0(relation).tap do |result|
|
204
|
+
Steep.logger.debug "result=#{result.class}"
|
205
|
+
cache[relation, @self_type, @instance_type, @class_type, bounds] = result
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def cache_bounds(relation)
|
214
|
+
vars = relation.sub_type.free_variables + relation.super_type.free_variables
|
215
|
+
vars.each.with_object({}) do |var, hash| #$ Hash[Symbol, AST::Types::t]
|
216
|
+
next unless var.is_a?(Symbol)
|
217
|
+
if upper_bound = variable_upper_bound(var)
|
218
|
+
hash[var] = upper_bound
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def alias?(type)
|
224
|
+
type.is_a?(AST::Types::Name::Alias)
|
225
|
+
end
|
226
|
+
|
227
|
+
def cacheable?(relation)
|
228
|
+
relation.sub_type.free_variables.empty? && relation.super_type.free_variables.empty?
|
229
|
+
end
|
230
|
+
|
231
|
+
def true_type?(type)
|
232
|
+
case type
|
233
|
+
when AST::Types::Literal
|
234
|
+
type.value == true
|
235
|
+
else
|
236
|
+
AST::Builtin::TrueClass.instance_type?(type) ? true : false
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def false_type?(type)
|
241
|
+
case type
|
242
|
+
when AST::Types::Literal
|
243
|
+
type.value == false
|
244
|
+
else
|
245
|
+
AST::Builtin::FalseClass.instance_type?(type) ? true : false
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
include Result::Helper
|
250
|
+
|
251
|
+
def check_type0(relation)
|
252
|
+
case
|
253
|
+
when same_type?(relation)
|
254
|
+
success(relation)
|
255
|
+
|
256
|
+
when relation.sub_type.is_a?(AST::Types::Any) || relation.super_type.is_a?(AST::Types::Any)
|
257
|
+
success(relation)
|
258
|
+
|
259
|
+
when relation.super_type.is_a?(AST::Types::Void)
|
260
|
+
success(relation)
|
261
|
+
|
262
|
+
when relation.super_type.is_a?(AST::Types::Top)
|
263
|
+
success(relation)
|
264
|
+
|
265
|
+
when relation.sub_type.is_a?(AST::Types::Bot)
|
266
|
+
success(relation)
|
267
|
+
|
268
|
+
when relation.super_type.is_a?(AST::Types::Boolean)
|
269
|
+
Expand(relation) do
|
270
|
+
check_type(
|
271
|
+
Relation.new(
|
272
|
+
sub_type: relation.sub_type,
|
273
|
+
super_type: AST::Types::Union.build(types: [AST::Builtin.true_type, AST::Builtin.false_type])
|
274
|
+
)
|
275
|
+
)
|
276
|
+
end
|
277
|
+
|
278
|
+
when relation.sub_type.is_a?(AST::Types::Boolean) || relation.sub_type.is_a?(AST::Types::Logic::Base)
|
279
|
+
Expand(relation) do
|
280
|
+
check_type(
|
281
|
+
Relation.new(
|
282
|
+
sub_type: AST::Types::Union.build(types: [AST::Builtin.true_type, AST::Builtin.false_type]),
|
283
|
+
super_type: relation.super_type
|
284
|
+
)
|
285
|
+
)
|
286
|
+
end
|
287
|
+
|
288
|
+
when relation.sub_type.is_a?(AST::Types::Self) && !self_type.is_a?(AST::Types::Self)
|
289
|
+
Expand(relation) do
|
290
|
+
check_type(Relation.new(sub_type: self_type, super_type: relation.super_type))
|
291
|
+
end
|
292
|
+
|
293
|
+
when relation.sub_type.is_a?(AST::Types::Instance) && !instance_type.is_a?(AST::Types::Instance)
|
294
|
+
Expand(relation) do
|
295
|
+
check_type(Relation.new(sub_type: instance_type, super_type: relation.super_type))
|
296
|
+
end
|
297
|
+
|
298
|
+
when relation.super_type.is_a?(AST::Types::Instance) && !instance_type.is_a?(AST::Types::Instance)
|
299
|
+
All(relation) do |result|
|
300
|
+
rel = Relation.new(sub_type: relation.sub_type, super_type: instance_type)
|
301
|
+
result.add(rel, rel.flip) do |r|
|
302
|
+
check_type(r)
|
303
|
+
end
|
304
|
+
end.tap do
|
305
|
+
Steep.logger.error { "`T <: instance` doesn't hold generally, but testing it with `#{relation} && #{relation.flip}` for compatibility"}
|
306
|
+
end
|
307
|
+
|
308
|
+
when relation.sub_type.is_a?(AST::Types::Class) && !instance_type.is_a?(AST::Types::Class)
|
309
|
+
Expand(relation) do
|
310
|
+
check_type(Relation.new(sub_type: class_type, super_type: relation.super_type))
|
311
|
+
end
|
312
|
+
|
313
|
+
when relation.super_type.is_a?(AST::Types::Class) && !instance_type.is_a?(AST::Types::Class)
|
314
|
+
All(relation) do |result|
|
315
|
+
rel = Relation.new(sub_type: relation.sub_type, super_type: class_type)
|
316
|
+
result.add(rel, rel.flip) do |r|
|
317
|
+
check_type(r)
|
318
|
+
end
|
319
|
+
end.tap do
|
320
|
+
Steep.logger.error { "`T <: class` doesn't hold generally, but testing with `#{relation} && |- #{relation.flip}` for compatibility"}
|
321
|
+
end
|
322
|
+
|
323
|
+
when alias?(relation.sub_type)
|
324
|
+
Expand(relation) do
|
325
|
+
check_type(Relation.new(sub_type: expand_alias(relation.sub_type), super_type: relation.super_type))
|
326
|
+
end
|
327
|
+
|
328
|
+
when alias?(relation.super_type)
|
329
|
+
Expand(relation) do
|
330
|
+
check_type(Relation.new(super_type: expand_alias(relation.super_type), sub_type: relation.sub_type))
|
331
|
+
end
|
332
|
+
|
333
|
+
when relation.super_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.super_type.name)
|
334
|
+
if ub = variable_upper_bound(relation.super_type.name)
|
335
|
+
Expand(relation) do
|
336
|
+
check_type(Relation.new(sub_type: relation.sub_type, super_type: ub))
|
337
|
+
end.tap do |result|
|
338
|
+
if result.success?
|
339
|
+
constraints.add(relation.super_type.name, sub_type: relation.sub_type)
|
340
|
+
end
|
341
|
+
end
|
342
|
+
else
|
343
|
+
constraints.add(relation.super_type.name, sub_type: relation.sub_type)
|
344
|
+
Success(relation)
|
345
|
+
end
|
346
|
+
|
347
|
+
when relation.sub_type.is_a?(AST::Types::Var) && constraints.unknown?(relation.sub_type.name)
|
348
|
+
constraints.add(relation.sub_type.name, super_type: relation.super_type)
|
349
|
+
Success(relation)
|
350
|
+
|
351
|
+
when relation.sub_type.is_a?(AST::Types::Var) && ub = variable_upper_bound(relation.sub_type.name)
|
352
|
+
Expand(relation) do
|
353
|
+
check_type(Relation.new(sub_type: ub, super_type: relation.super_type))
|
354
|
+
end
|
355
|
+
|
356
|
+
when relation.sub_type.is_a?(AST::Types::Intersection) && relation.super_type.is_a?(AST::Types::Union)
|
357
|
+
Any(relation) do |base_result|
|
358
|
+
# Expand the super_type first
|
359
|
+
base_result.add(relation) do
|
360
|
+
Any(relation) do |result|
|
361
|
+
relation.super_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : -Float::INFINITY }.each do |super_type|
|
362
|
+
rel = Relation.new(sub_type: relation.sub_type, super_type: super_type)
|
363
|
+
result.add(rel) do
|
364
|
+
check_type(rel)
|
365
|
+
end
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
370
|
+
# Expand the sub_type if it fails
|
371
|
+
base_result.add(relation) do
|
372
|
+
Any(relation) do |result|
|
373
|
+
relation.sub_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : -Float::INFINITY }.each do |sub_type|
|
374
|
+
rel = Relation.new(sub_type: sub_type, super_type: relation.super_type)
|
375
|
+
result.add(rel) do
|
376
|
+
check_type(rel)
|
377
|
+
end
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
when relation.super_type.is_a?(AST::Types::Intersection)
|
384
|
+
All(relation) do |result|
|
385
|
+
relation.super_type.types.each do |super_type|
|
386
|
+
result.add(Relation.new(sub_type: relation.sub_type, super_type: super_type)) do |rel|
|
387
|
+
check_type(rel)
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
when relation.sub_type.is_a?(AST::Types::Union)
|
393
|
+
All(relation) do |result|
|
394
|
+
relation.sub_type.types.each do |sub_type|
|
395
|
+
rel = Relation.new(sub_type: sub_type, super_type: relation.super_type)
|
396
|
+
result.add(rel) do
|
397
|
+
check_type(rel)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
|
402
|
+
when relation.sub_type.is_a?(AST::Types::Intersection)
|
403
|
+
Any(relation) do |result|
|
404
|
+
relation.sub_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : -Float::INFINITY }.each do |sub_type|
|
405
|
+
rel = Relation.new(sub_type: sub_type, super_type: relation.super_type)
|
406
|
+
result.add(rel) do
|
407
|
+
check_type(rel)
|
408
|
+
end
|
409
|
+
end
|
410
|
+
end
|
411
|
+
|
412
|
+
when relation.super_type.is_a?(AST::Types::Union)
|
413
|
+
Any(relation) do |result|
|
414
|
+
relation.super_type.types.sort_by {|ty| (path = hole_path(ty)) ? -path.size : -Float::INFINITY }.each do |super_type|
|
415
|
+
rel = Relation.new(sub_type: relation.sub_type, super_type: super_type)
|
416
|
+
result.add(rel) do
|
417
|
+
check_type(rel)
|
418
|
+
end
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
when relation.super_type.is_a?(AST::Types::Name::Interface)
|
423
|
+
Expand(relation) do
|
424
|
+
check_interface(
|
425
|
+
relation.map {|type|
|
426
|
+
builder.shape(
|
427
|
+
type,
|
428
|
+
Interface::Builder::Config.new(self_type: type, variable_bounds: variable_upper_bounds)
|
429
|
+
)&.public_shape() or return Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
|
430
|
+
}
|
431
|
+
)
|
432
|
+
end
|
433
|
+
|
434
|
+
when relation.sub_type.is_a?(AST::Types::Name::Base) && relation.super_type.is_a?(AST::Types::Name::Base)
|
435
|
+
if relation.sub_type.name == relation.super_type.name && relation.sub_type.class == relation.super_type.class
|
436
|
+
if arg_type?(relation.sub_type) && arg_type?(relation.super_type)
|
437
|
+
check_type_arg(_ = relation)
|
438
|
+
else
|
439
|
+
Success(relation)
|
440
|
+
end
|
441
|
+
else
|
442
|
+
possible_sub_types =
|
443
|
+
case relation.sub_type
|
444
|
+
when AST::Types::Name::Instance
|
445
|
+
instance_super_types(relation.sub_type.name, args: relation.sub_type.args)
|
446
|
+
when AST::Types::Name::Singleton
|
447
|
+
singleton_super_types(relation.sub_type.name)
|
448
|
+
else
|
449
|
+
[] #: Array[super_type]
|
450
|
+
end
|
451
|
+
|
452
|
+
unless possible_sub_types.empty?
|
453
|
+
Any(relation) do |result|
|
454
|
+
possible_sub_types.each do |sub_type|
|
455
|
+
result.add(Relation.new(sub_type: sub_type, super_type: relation.super_type)) do |rel|
|
456
|
+
check_type(rel)
|
457
|
+
end
|
458
|
+
end
|
459
|
+
end
|
460
|
+
else
|
461
|
+
Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
|
462
|
+
end
|
463
|
+
end
|
464
|
+
|
465
|
+
when relation.sub_type.is_a?(AST::Types::Proc) && relation.super_type.is_a?(AST::Types::Proc)
|
466
|
+
yield_self do
|
467
|
+
name = :__proc__
|
468
|
+
|
469
|
+
sub_type = relation.sub_type
|
470
|
+
super_type = relation.super_type
|
471
|
+
|
472
|
+
All(relation) do |result|
|
473
|
+
result.add(Relation(sub_type.type, super_type.type)) do |rel|
|
474
|
+
check_function(name, rel)
|
475
|
+
end
|
476
|
+
|
477
|
+
result.add_result check_self_type_binding(relation, sub_type.self_type, super_type.self_type)
|
478
|
+
|
479
|
+
result.add(Relation(sub_type.block, super_type.block)) do |rel|
|
480
|
+
case ret = expand_block_given(name, rel)
|
481
|
+
when Relation
|
482
|
+
All(ret) do |result|
|
483
|
+
result.add_result check_self_type_binding(ret, ret.super_type.self_type, ret.sub_type.self_type)
|
484
|
+
result.add(ret.map {|b| b.type }) {|r| check_function(name, r.flip) }
|
485
|
+
end
|
486
|
+
when Result::Base
|
487
|
+
ret
|
488
|
+
when true
|
489
|
+
nil
|
490
|
+
end
|
491
|
+
end
|
492
|
+
end
|
493
|
+
end
|
494
|
+
|
495
|
+
when relation.sub_type.is_a?(AST::Types::Tuple) && relation.super_type.is_a?(AST::Types::Tuple)
|
496
|
+
if relation.sub_type.types.size == relation.super_type.types.size
|
497
|
+
pairs = relation.sub_type.types.take(relation.super_type.types.size).zip(relation.super_type.types)
|
498
|
+
|
499
|
+
All(relation) do |result|
|
500
|
+
pairs.each do |t1, t2|
|
501
|
+
t2 or raise
|
502
|
+
result.add(Relation.new(sub_type: t1, super_type: t2)) do |rel|
|
503
|
+
check_type(rel)
|
504
|
+
end
|
505
|
+
end
|
506
|
+
end
|
507
|
+
else
|
508
|
+
Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
|
509
|
+
end
|
510
|
+
|
511
|
+
when relation.sub_type.is_a?(AST::Types::Tuple) && (super_type = AST::Builtin::Array.instance_type?(relation.super_type))
|
512
|
+
Expand(relation) do
|
513
|
+
tuple_element_type =
|
514
|
+
AST::Types::Union.build(
|
515
|
+
types: relation.sub_type.types
|
516
|
+
)
|
517
|
+
|
518
|
+
check_type(Relation.new(sub_type: tuple_element_type, super_type: super_type.args.fetch(0)))
|
519
|
+
end
|
520
|
+
|
521
|
+
when relation.sub_type.is_a?(AST::Types::Tuple)
|
522
|
+
Any(relation) do |result|
|
523
|
+
# Check by converting the tuple to array
|
524
|
+
tuple_element_type = AST::Types::Union.build(types: relation.sub_type.types)
|
525
|
+
array_type = AST::Builtin::Array.instance_type(tuple_element_type)
|
526
|
+
result.add(Relation.new(sub_type: array_type, super_type: relation.super_type)) do
|
527
|
+
check_type(_1)
|
528
|
+
end
|
529
|
+
|
530
|
+
# Check by shapes
|
531
|
+
shape_relation = relation.map {|type|
|
532
|
+
# @type break: nil
|
533
|
+
builder.shape(
|
534
|
+
type, Interface::Builder::Config.new(self_type: type, variable_bounds: variable_upper_bounds)
|
535
|
+
)&.public_shape or break
|
536
|
+
}
|
537
|
+
if shape_relation
|
538
|
+
result.add(shape_relation) { check_interface(_1) }
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
when relation.sub_type.is_a?(AST::Types::Record) && relation.super_type.is_a?(AST::Types::Record)
|
543
|
+
All(relation) do |result|
|
544
|
+
unchecked_keys = Set.new(relation.sub_type.elements.each_key)
|
545
|
+
|
546
|
+
relation.super_type.elements.each_key do |key|
|
547
|
+
super_element_type = relation.super_type.elements.fetch(key) #: AST::Types::t
|
548
|
+
sub_element_type = relation.sub_type.elements.fetch(key, nil) #: AST::Types::t?
|
549
|
+
|
550
|
+
if relation.super_type.required?(key)
|
551
|
+
rel = Relation.new(sub_type: sub_element_type || AST::Builtin.nil_type, super_type: super_element_type)
|
552
|
+
result.add(rel) { check_type(rel) }
|
553
|
+
else
|
554
|
+
# If the key is optional, it's okay to not have the key
|
555
|
+
if sub_element_type
|
556
|
+
rel = Relation.new(sub_type: sub_element_type, super_type: super_element_type)
|
557
|
+
result.add(rel) { check_type(rel) }
|
558
|
+
end
|
559
|
+
end
|
560
|
+
|
561
|
+
unchecked_keys.delete(key)
|
562
|
+
end
|
563
|
+
|
564
|
+
unless unchecked_keys.empty?
|
565
|
+
return Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
when relation.sub_type.is_a?(AST::Types::Record)
|
570
|
+
Any(relation) do |result|
|
571
|
+
# Check by converting the record to hash
|
572
|
+
key_type = AST::Types::Union.build(types: relation.sub_type.elements.each_key.map {|key| AST::Types::Literal.new(value: key) })
|
573
|
+
value_type = AST::Types::Union.build(types: relation.sub_type.elements.each_value.map {|key| key })
|
574
|
+
hash_type = AST::Builtin::Hash.instance_type(key_type, value_type)
|
575
|
+
result.add(Relation.new(sub_type: hash_type, super_type: relation.super_type)) { check_type(_1) }
|
576
|
+
|
577
|
+
# Check by the shapes
|
578
|
+
shape_relation = relation.map do |type|
|
579
|
+
# @type break: nil
|
580
|
+
builder.shape(
|
581
|
+
type,
|
582
|
+
Interface::Builder::Config.new(self_type: type, variable_bounds: variable_upper_bounds)
|
583
|
+
)&.public_shape or break
|
584
|
+
end
|
585
|
+
if shape_relation
|
586
|
+
result.add(shape_relation) { check_interface(_1) }
|
587
|
+
end
|
588
|
+
end
|
589
|
+
|
590
|
+
when relation.sub_type.is_a?(AST::Types::Proc) && AST::Builtin::Proc.instance_type?(relation.super_type)
|
591
|
+
Success(relation)
|
592
|
+
|
593
|
+
when relation.super_type.is_a?(AST::Types::Literal)
|
594
|
+
case
|
595
|
+
when relation.super_type.value == true && AST::Builtin::TrueClass.instance_type?(relation.sub_type)
|
596
|
+
Success(relation)
|
597
|
+
when relation.super_type.value == false && AST::Builtin::FalseClass.instance_type?(relation.sub_type)
|
598
|
+
Success(relation)
|
599
|
+
else
|
600
|
+
Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
|
601
|
+
end
|
602
|
+
|
603
|
+
when relation.super_type.is_a?(AST::Types::Nil) && AST::Builtin::NilClass.instance_type?(relation.sub_type)
|
604
|
+
# NilClass <: nil
|
605
|
+
Success(relation)
|
606
|
+
|
607
|
+
when relation.sub_type.is_a?(AST::Types::Literal)
|
608
|
+
Expand(relation) do
|
609
|
+
check_type(Relation.new(sub_type: relation.sub_type.back_type, super_type: relation.super_type))
|
610
|
+
end
|
611
|
+
|
612
|
+
else
|
613
|
+
Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
|
614
|
+
end
|
615
|
+
end
|
616
|
+
|
617
|
+
def definition_for_type(type)
|
618
|
+
case type
|
619
|
+
when AST::Types::Name::Instance
|
620
|
+
factory.definition_builder.build_instance(type.name)
|
621
|
+
when AST::Types::Name::Singleton
|
622
|
+
factory.definition_builder.build_singleton(type.name)
|
623
|
+
when AST::Types::Name::Interface
|
624
|
+
factory.definition_builder.build_interface(type.name)
|
625
|
+
else
|
626
|
+
raise
|
627
|
+
end
|
628
|
+
end
|
629
|
+
|
630
|
+
def arg_type?(type)
|
631
|
+
case type
|
632
|
+
when AST::Types::Name::Instance, AST::Types::Name::Interface
|
633
|
+
type.args.size > 0
|
634
|
+
else
|
635
|
+
false
|
636
|
+
end
|
637
|
+
end
|
638
|
+
|
639
|
+
def check_type_arg(relation)
|
640
|
+
sub_args = relation.sub_type.args
|
641
|
+
sup_args = relation.super_type.args
|
642
|
+
|
643
|
+
sup_def = definition_for_type(relation.super_type)
|
644
|
+
sup_params = sup_def.type_params_decl
|
645
|
+
|
646
|
+
All(relation) do |result|
|
647
|
+
sub_args.zip(sup_args, sup_params.each).each do |sub_arg, sup_arg, sup_param|
|
648
|
+
case sup_param.variance
|
649
|
+
when :covariant
|
650
|
+
result.add(Relation.new(sub_type: sub_arg, super_type: sup_arg)) do |rel|
|
651
|
+
check_type(rel)
|
652
|
+
end
|
653
|
+
when :contravariant
|
654
|
+
result.add(Relation.new(sub_type: sup_arg, super_type: sub_arg)) do |rel|
|
655
|
+
check_type(rel)
|
656
|
+
end
|
657
|
+
when :invariant
|
658
|
+
rel = Relation.new(sub_type: sub_arg, super_type: sup_arg)
|
659
|
+
result.add(rel, rel.flip) do |rel|
|
660
|
+
check_type(rel)
|
661
|
+
end
|
662
|
+
end
|
663
|
+
end
|
664
|
+
end
|
665
|
+
end
|
666
|
+
|
667
|
+
def same_type?(relation)
|
668
|
+
if assumptions.include?(relation) && assumptions.include?(relation.flip)
|
669
|
+
return true
|
670
|
+
end
|
671
|
+
|
672
|
+
builder.factory.normalize_type(relation.sub_type) == builder.factory.normalize_type(relation.super_type)
|
673
|
+
end
|
674
|
+
|
675
|
+
def check_interface(relation)
|
676
|
+
relation.interface!
|
677
|
+
|
678
|
+
sub_interface, super_interface = relation
|
679
|
+
|
680
|
+
method_pairs = super_interface.methods.each_with_object({}) do |(method_name, sup_method), hash| #$ Hash[Symbol, Relation[Interface::Shape::Entry]]
|
681
|
+
if sub_method = sub_interface.methods[method_name]
|
682
|
+
hash[method_name] = Relation.new(sub_type: sub_method, super_type: sup_method)
|
683
|
+
else
|
684
|
+
return Failure(relation) { Result::Failure::MethodMissingError.new(name: method_name) }
|
685
|
+
end
|
686
|
+
end
|
687
|
+
|
688
|
+
All(relation) do |result|
|
689
|
+
method_pairs.each do |method_name, method_relation|
|
690
|
+
result.add(method_relation) do
|
691
|
+
check_method(method_name, method_relation)
|
692
|
+
end
|
693
|
+
end
|
694
|
+
end
|
695
|
+
end
|
696
|
+
|
697
|
+
def check_method(name, relation)
|
698
|
+
relation.method!
|
699
|
+
|
700
|
+
sub_method, super_method = relation
|
701
|
+
|
702
|
+
All(relation) do |all|
|
703
|
+
super_method.method_types.each do |super_type|
|
704
|
+
all.add(Relation.new(sub_type: sub_method, super_type: super_type)) do |rel|
|
705
|
+
Any(rel) do |any|
|
706
|
+
sub_method.method_types.each do |sub_type|
|
707
|
+
any.add(Relation.new(sub_type: sub_type, super_type: super_type)) do |rel|
|
708
|
+
check_generic_method_type(name, rel)
|
709
|
+
end
|
710
|
+
end
|
711
|
+
end
|
712
|
+
end
|
713
|
+
end
|
714
|
+
end
|
715
|
+
end
|
716
|
+
|
717
|
+
def check_generic_method_type(name, relation)
|
718
|
+
relation.method!
|
719
|
+
|
720
|
+
sub_type, super_type = relation
|
721
|
+
|
722
|
+
case
|
723
|
+
when sub_type.type_params.empty? && super_type.type_params.empty?
|
724
|
+
check_method_type(name, relation)
|
725
|
+
|
726
|
+
when !sub_type.type_params.empty? && super_type.type_params.empty?
|
727
|
+
# Check if super_type is an instance of sub_type.
|
728
|
+
Expand(relation) do
|
729
|
+
sub_args = sub_type.type_params.map {|param| AST::Types::Var.fresh(param.name) }
|
730
|
+
sub_type_ = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params.map(&:name), sub_args))
|
731
|
+
|
732
|
+
sub_args.each do |s|
|
733
|
+
constraints.unknown!(s.name)
|
734
|
+
end
|
735
|
+
|
736
|
+
relation = Relation.new(sub_type: sub_type_, super_type: super_type)
|
737
|
+
All(relation) do |result|
|
738
|
+
sub_args.zip(sub_type.type_params).each do |arg, param|
|
739
|
+
param or raise
|
740
|
+
|
741
|
+
if ub = param.upper_bound
|
742
|
+
result.add(Relation.new(sub_type: arg, super_type: ub)) do |rel|
|
743
|
+
check_type(rel)
|
744
|
+
end
|
745
|
+
end
|
746
|
+
end
|
747
|
+
|
748
|
+
if failure = match_method_type_fails?(name, sub_type_, super_type)
|
749
|
+
result.add_result(failure)
|
750
|
+
else
|
751
|
+
result.add_result(check_method_type(name, Relation(sub_type_, super_type)))
|
752
|
+
end
|
753
|
+
|
754
|
+
result.add(relation) do |rel|
|
755
|
+
check_constraints(
|
756
|
+
relation,
|
757
|
+
variables: sub_args.map(&:name),
|
758
|
+
variance: VariableVariance.from_method_type(sub_type_)
|
759
|
+
)
|
760
|
+
end
|
761
|
+
end
|
762
|
+
end
|
763
|
+
|
764
|
+
when sub_type.type_params.empty? && !super_type.type_params.empty?
|
765
|
+
# Check if sub_type is an instance of super_type && no constraints on type variables (any).
|
766
|
+
All(relation) do |result|
|
767
|
+
super_args = super_type.type_params.map {|param| AST::Types::Var.fresh(param.name) }
|
768
|
+
super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params.map(&:name), super_args))
|
769
|
+
|
770
|
+
if failure = match_method_type_fails?(name, sub_type, super_type_)
|
771
|
+
result.add_result(failure)
|
772
|
+
else
|
773
|
+
super_args.each do |arg|
|
774
|
+
constraints.unknown!(arg.name)
|
775
|
+
end
|
776
|
+
|
777
|
+
result.add(Relation(sub_type, super_type_)) do |rel_|
|
778
|
+
ret = check_method_type(name, rel_)
|
779
|
+
|
780
|
+
if ret.success? && super_args.map(&:name).none? {|var| constraints.has_constraint?(var) }
|
781
|
+
ret
|
782
|
+
else
|
783
|
+
Failure(rel_, Result::Failure::PolyMethodSubtyping.new(name: name))
|
784
|
+
end
|
785
|
+
end
|
786
|
+
end
|
787
|
+
end
|
788
|
+
|
789
|
+
when super_type.type_params.size == sub_type.type_params.size
|
790
|
+
# If they have the same arity, run the normal F-sub subtyping checking.
|
791
|
+
All(relation) do |result|
|
792
|
+
args = sub_type.type_params.map {|type_param| AST::Types::Var.fresh(type_param.name) }
|
793
|
+
args.each {|arg| constraints.unknown!(arg.name) }
|
794
|
+
|
795
|
+
upper_bounds = {} #: Hash[Symbol, AST::Types::t]
|
796
|
+
relations = [] #: Array[Relation[AST::Types::t]]
|
797
|
+
|
798
|
+
args.zip(sub_type.type_params, super_type.type_params).each do |arg, sub_param, sup_param|
|
799
|
+
# @type var arg: AST::Types::Var
|
800
|
+
# @type var sub_param: Interface::TypeParam?
|
801
|
+
# @type var super_param: Interface::TypeParam?
|
802
|
+
|
803
|
+
sub_param or raise
|
804
|
+
sup_param or raise
|
805
|
+
|
806
|
+
sub_ub = sub_param.upper_bound
|
807
|
+
sup_ub = sup_param.upper_bound
|
808
|
+
|
809
|
+
case
|
810
|
+
when sub_ub && sup_ub
|
811
|
+
upper_bounds[arg.name] = sub_ub
|
812
|
+
relations << Relation.new(sub_type: sub_ub, super_type: sup_ub)
|
813
|
+
when sub_ub && !sup_ub
|
814
|
+
upper_bounds[arg.name] = sub_ub
|
815
|
+
when !sub_ub && sup_ub
|
816
|
+
result.add(Relation.new(sub_type: AST::Types::Var.new(name: sub_param.name), super_type: sub_ub)) do |rel|
|
817
|
+
Failure(rel, Result::Failure::PolyMethodSubtyping.new(name: name))
|
818
|
+
end
|
819
|
+
when !sub_ub && !sup_ub
|
820
|
+
# no constraints
|
821
|
+
end
|
822
|
+
end
|
823
|
+
|
824
|
+
push_variable_bounds(upper_bounds) do
|
825
|
+
sub_type_ = sub_type.instantiate(Interface::Substitution.build(sub_type.type_params.map(&:name), args))
|
826
|
+
super_type_ = super_type.instantiate(Interface::Substitution.build(super_type.type_params.map(&:name), args))
|
827
|
+
|
828
|
+
result.add(*relations) {|rel| check_type(rel) }
|
829
|
+
result.add(Relation.new(sub_type: sub_type_, super_type: super_type_)) do |rel|
|
830
|
+
check_method_type(name, rel)
|
831
|
+
end
|
832
|
+
end
|
833
|
+
end
|
834
|
+
|
835
|
+
else
|
836
|
+
Failure(relation, Result::Failure::PolyMethodSubtyping.new(name: name))
|
837
|
+
end
|
838
|
+
end
|
839
|
+
|
840
|
+
def check_constraints(relation, variables:, variance:)
|
841
|
+
checker = Check.new(builder: builder)
|
842
|
+
|
843
|
+
constraints.solution(
|
844
|
+
checker,
|
845
|
+
variance: variance,
|
846
|
+
variables: variables,
|
847
|
+
self_type: self_type,
|
848
|
+
instance_type: instance_type,
|
849
|
+
class_type: class_type
|
850
|
+
)
|
851
|
+
|
852
|
+
Success(relation)
|
853
|
+
rescue Constraints::UnsatisfiableConstraint => error
|
854
|
+
Failure(relation, Result::Failure::UnsatisfiedConstraints.new(error))
|
855
|
+
end
|
856
|
+
|
857
|
+
def check_method_type(name, relation)
|
858
|
+
Steep.logger.tagged "#{name} : #{relation.sub_type} <: #{relation.super_type}" do
|
859
|
+
relation.method!
|
860
|
+
|
861
|
+
sub_type, super_type = relation
|
862
|
+
|
863
|
+
sub_type.type_params.empty? or raise "Expected monomorphic method type: #{sub_type}"
|
864
|
+
super_type.type_params.empty? or raise "Expected monomorphic method type: #{super_type}"
|
865
|
+
|
866
|
+
All(relation) do |result|
|
867
|
+
type_relation = Relation.new(sub_type: sub_type.type, super_type: super_type.type)
|
868
|
+
|
869
|
+
ret = expand_block_given(name, Relation.new(sub_type: sub_type.block, super_type: super_type.block))
|
870
|
+
|
871
|
+
case ret
|
872
|
+
when true
|
873
|
+
result.add(type_relation) { check_function(name, type_relation) }
|
874
|
+
when Relation
|
875
|
+
result.add(type_relation) { check_function(name, type_relation) }
|
876
|
+
result.add(ret) do
|
877
|
+
All(ret) do |result|
|
878
|
+
result.add_result(check_self_type_binding(ret, ret.super_type.self_type, ret.sub_type.self_type))
|
879
|
+
result.add(Relation(ret.super_type.type, ret.sub_type.type)) do |block_relation|
|
880
|
+
check_function(name, block_relation)
|
881
|
+
end
|
882
|
+
end
|
883
|
+
end
|
884
|
+
when Result::Failure
|
885
|
+
result.add(ret.relation) { ret }
|
886
|
+
end.tap do |ret|
|
887
|
+
Steep.logger.debug "result=#{ret.class}"
|
888
|
+
end
|
889
|
+
end
|
890
|
+
end
|
891
|
+
end
|
892
|
+
|
893
|
+
def expand_block_given(name, relation, &block)
|
894
|
+
relation.block!
|
895
|
+
|
896
|
+
sub_block, super_block = relation
|
897
|
+
|
898
|
+
case
|
899
|
+
when !super_block && !sub_block
|
900
|
+
true
|
901
|
+
when super_block && sub_block && super_block.optional? == sub_block.optional?
|
902
|
+
Relation.new(sub_type: sub_block, super_type: super_block)
|
903
|
+
when sub_block&.optional?
|
904
|
+
true
|
905
|
+
else
|
906
|
+
Failure(relation, Result::Failure::BlockMismatchError.new(name: name))
|
907
|
+
end
|
908
|
+
end
|
909
|
+
|
910
|
+
def check_function(name, relation)
|
911
|
+
relation.function!
|
912
|
+
|
913
|
+
All(relation) do |result|
|
914
|
+
if relation.sub_type.params && relation.super_type.params
|
915
|
+
result.add(relation.map {|fun| _ = fun.params }) do |rel|
|
916
|
+
check_method_params(name, rel)
|
917
|
+
end
|
918
|
+
end
|
919
|
+
|
920
|
+
result.add(relation.map {|fun| fun.return_type }) do |rel|
|
921
|
+
check_type(rel)
|
922
|
+
end
|
923
|
+
end
|
924
|
+
end
|
925
|
+
|
926
|
+
def check_self_type_binding(relation, sub_self, super_self)
|
927
|
+
case
|
928
|
+
when sub_self.nil? && super_self.nil?
|
929
|
+
nil
|
930
|
+
when sub_self && super_self
|
931
|
+
# ^() [self: T] -> void <: ^() [self: S] -> void ==> S <: T
|
932
|
+
# () { () [self: S] -> void } -> void <: () { () [self: T] -> void } -> void ==> S <: T
|
933
|
+
check_type(Relation(super_self, sub_self))
|
934
|
+
when sub_self.is_a?(AST::Types::Top) && super_self.nil?
|
935
|
+
# ^() [self: top] -> void <: ^() -> void ==> OK
|
936
|
+
# () { () -> void } -> void <: () { () [self: top] -> void } -> void ==> OK
|
937
|
+
nil
|
938
|
+
else
|
939
|
+
Failure(relation, Result::Failure::SelfBindingMismatch.new)
|
940
|
+
end
|
941
|
+
end
|
942
|
+
|
943
|
+
def check_method_params(name, relation)
|
944
|
+
relation.params!
|
945
|
+
pairs = match_params(name, relation)
|
946
|
+
|
947
|
+
case pairs
|
948
|
+
when Array
|
949
|
+
unless pairs.empty?
|
950
|
+
All(relation) do |result|
|
951
|
+
pairs.each do |sub_type, super_type|
|
952
|
+
result.add(Relation.new(sub_type: super_type, super_type: sub_type)) do |rel|
|
953
|
+
check_type(rel)
|
954
|
+
end
|
955
|
+
end
|
956
|
+
|
957
|
+
result
|
958
|
+
end
|
959
|
+
else
|
960
|
+
Success(relation)
|
961
|
+
end
|
962
|
+
else
|
963
|
+
pairs
|
964
|
+
end
|
965
|
+
end
|
966
|
+
|
967
|
+
def Relation(sub, sup)
|
968
|
+
Relation.new(sub_type: sub, super_type: sup)
|
969
|
+
end
|
970
|
+
|
971
|
+
def match_method_type_fails?(name, type1, type2)
|
972
|
+
if type1.type.params && type2.type.params
|
973
|
+
match_params(name, Relation(type1.type.params, type2.type.params)).tap do |param_pairs|
|
974
|
+
return param_pairs unless param_pairs.is_a?(Array)
|
975
|
+
end
|
976
|
+
end
|
977
|
+
|
978
|
+
case result = expand_block_given(name, Relation(type1.block, type2.block))
|
979
|
+
when Result::Base
|
980
|
+
return result
|
981
|
+
when Relation
|
982
|
+
type1.block or raise
|
983
|
+
type2.block or raise
|
984
|
+
|
985
|
+
if type1.block.type.params && type2.block.type.params
|
986
|
+
match_params(name, result.map {|m| _ = m.type.params }).tap do |param_pairs|
|
987
|
+
return param_pairs unless param_pairs.is_a?(Array)
|
988
|
+
end
|
989
|
+
end
|
990
|
+
end
|
991
|
+
|
992
|
+
nil
|
993
|
+
end
|
994
|
+
|
995
|
+
def match_params(name, relation)
|
996
|
+
relation.params!
|
997
|
+
|
998
|
+
sub_params, super_params = relation
|
999
|
+
|
1000
|
+
pairs = [] #: Array[[AST::Types::t, AST::Types::t]]
|
1001
|
+
|
1002
|
+
sub_flat = sub_params.flat_unnamed_params
|
1003
|
+
sup_flat = super_params.flat_unnamed_params
|
1004
|
+
|
1005
|
+
failure = Failure(relation, Result::Failure::ParameterMismatchError.new(name: name))
|
1006
|
+
|
1007
|
+
case
|
1008
|
+
when super_params.rest
|
1009
|
+
return failure unless sub_params.rest
|
1010
|
+
|
1011
|
+
while sub_flat.size > 0
|
1012
|
+
sub_type = sub_flat.shift or raise
|
1013
|
+
sup_type = sup_flat.shift
|
1014
|
+
|
1015
|
+
if sup_type
|
1016
|
+
pairs << [sub_type.last, sup_type.last]
|
1017
|
+
else
|
1018
|
+
pairs << [sub_type.last, super_params.rest]
|
1019
|
+
end
|
1020
|
+
end
|
1021
|
+
|
1022
|
+
if sub_params.rest
|
1023
|
+
pairs << [sub_params.rest, super_params.rest]
|
1024
|
+
end
|
1025
|
+
|
1026
|
+
when sub_params.rest
|
1027
|
+
while sub_flat.size > 0
|
1028
|
+
sub_type = sub_flat.shift or raise
|
1029
|
+
sup_type = sup_flat.shift
|
1030
|
+
|
1031
|
+
if sup_type
|
1032
|
+
pairs << [sub_type.last, sup_type.last]
|
1033
|
+
else
|
1034
|
+
break
|
1035
|
+
end
|
1036
|
+
end
|
1037
|
+
|
1038
|
+
if sub_params.rest && !sup_flat.empty?
|
1039
|
+
sup_flat.each do |sup_type|
|
1040
|
+
pairs << [sub_params.rest, sup_type.last]
|
1041
|
+
end
|
1042
|
+
end
|
1043
|
+
when sub_params.required.size + sub_params.optional.size >= super_params.required.size + super_params.optional.size
|
1044
|
+
while sub_flat.size > 0
|
1045
|
+
sub_type = sub_flat.shift or raise
|
1046
|
+
sup_type = sup_flat.shift
|
1047
|
+
|
1048
|
+
if sup_type
|
1049
|
+
pairs << [sub_type.last, sup_type.last]
|
1050
|
+
else
|
1051
|
+
if sub_type.first == :required
|
1052
|
+
return failure
|
1053
|
+
else
|
1054
|
+
break
|
1055
|
+
end
|
1056
|
+
end
|
1057
|
+
end
|
1058
|
+
else
|
1059
|
+
return failure
|
1060
|
+
end
|
1061
|
+
|
1062
|
+
sub_flat_kws = sub_params.flat_keywords
|
1063
|
+
sup_flat_kws = super_params.flat_keywords
|
1064
|
+
|
1065
|
+
sup_flat_kws.each do |name, _|
|
1066
|
+
if sub_flat_kws.key?(name)
|
1067
|
+
pairs << [sub_flat_kws.fetch(name), sup_flat_kws.fetch(name)]
|
1068
|
+
else
|
1069
|
+
if sub_params.rest_keywords
|
1070
|
+
pairs << [sub_params.rest_keywords, sup_flat_kws.fetch(name)]
|
1071
|
+
else
|
1072
|
+
return failure
|
1073
|
+
end
|
1074
|
+
end
|
1075
|
+
end
|
1076
|
+
|
1077
|
+
sub_params.required_keywords.each do |name, _|
|
1078
|
+
unless super_params.required_keywords.key?(name)
|
1079
|
+
return failure
|
1080
|
+
end
|
1081
|
+
end
|
1082
|
+
|
1083
|
+
if sub_params.rest_keywords && super_params.rest_keywords
|
1084
|
+
pairs << [sub_params.rest_keywords, super_params.rest_keywords]
|
1085
|
+
end
|
1086
|
+
|
1087
|
+
pairs
|
1088
|
+
end
|
1089
|
+
|
1090
|
+
def expand_alias(type, &block)
|
1091
|
+
factory.expand_alias(type, &block)
|
1092
|
+
end
|
1093
|
+
|
1094
|
+
# Returns the shortest type paths for one of the _unknown_ type variables.
|
1095
|
+
# Returns nil if there is no path.
|
1096
|
+
def hole_path(type, path = [])
|
1097
|
+
case type
|
1098
|
+
when AST::Types::Var
|
1099
|
+
if constraints.unknown?(type.name)
|
1100
|
+
[type]
|
1101
|
+
else
|
1102
|
+
nil
|
1103
|
+
end
|
1104
|
+
else
|
1105
|
+
paths = type.each_child.map do |ty|
|
1106
|
+
hole_path(ty, path)&.unshift(ty)
|
1107
|
+
end
|
1108
|
+
paths.compact.min_by(&:size)
|
1109
|
+
end
|
1110
|
+
end
|
1111
|
+
end
|
1112
|
+
end
|
1113
|
+
end
|