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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +12 -11
- data/Gemfile.steep +1 -1
- data/Gemfile.steep.lock +9 -9
- data/README.md +3 -3
- data/Steepfile +23 -0
- data/bin/steep-prof +2 -1
- data/lib/steep/annotation_parser.rb +1 -1
- data/lib/steep/ast/types/class.rb +4 -0
- data/lib/steep/ast/types/factory.rb +86 -602
- data/lib/steep/ast/types/instance.rb +4 -0
- data/lib/steep/ast/types/literal.rb +1 -1
- data/lib/steep/ast/types/proc.rb +22 -8
- data/lib/steep/ast/types/self.rb +4 -0
- data/lib/steep/ast/types/union.rb +1 -1
- data/lib/steep/cli.rb +24 -1
- data/lib/steep/diagnostic/ruby.rb +17 -22
- data/lib/steep/drivers/checkfile.rb +205 -0
- data/lib/steep/drivers/validate.rb +3 -1
- data/lib/steep/equatable.rb +2 -0
- data/lib/steep/interface/block.rb +21 -11
- data/lib/steep/interface/builder.rb +756 -0
- data/lib/steep/interface/function.rb +32 -24
- data/lib/steep/interface/method_type.rb +191 -78
- data/lib/steep/interface/shape.rb +132 -0
- data/lib/steep/interface/substitution.rb +23 -12
- data/lib/steep/interface/type_param.rb +1 -2
- data/lib/steep/path_helper.rb +1 -1
- data/lib/steep/project.rb +5 -7
- data/lib/steep/server/base_worker.rb +2 -2
- data/lib/steep/server/change_buffer.rb +4 -3
- data/lib/steep/server/interaction_worker.rb +1 -1
- data/lib/steep/server/master.rb +69 -9
- data/lib/steep/server/type_check_worker.rb +13 -11
- data/lib/steep/server/worker_process.rb +9 -7
- data/lib/steep/services/completion_provider.rb +15 -3
- data/lib/steep/services/hover_provider/singleton_methods.rb +5 -6
- data/lib/steep/services/signature_service.rb +13 -8
- data/lib/steep/services/type_check_service.rb +2 -0
- data/lib/steep/signature/validator.rb +1 -1
- data/lib/steep/subtyping/check.rb +154 -103
- data/lib/steep/subtyping/relation.rb +3 -3
- data/lib/steep/subtyping/result.rb +20 -2
- data/lib/steep/subtyping/variable_variance.rb +9 -0
- data/lib/steep/type_construction.rb +558 -299
- data/lib/steep/type_inference/block_params.rb +169 -86
- data/lib/steep/type_inference/logic_type_interpreter.rb +9 -14
- data/lib/steep/type_inference/method_params.rb +12 -7
- data/lib/steep/type_inference/send_args.rb +41 -35
- data/lib/steep/type_inference/type_env_builder.rb +1 -1
- data/lib/steep/version.rb +1 -1
- data/lib/steep.rb +71 -2
- data/rbs_collection.steep.lock.yaml +18 -30
- data/rbs_collection.steep.yaml +1 -0
- data/sig/shims/language-server_protocol.rbs +20 -0
- data/sig/shims/tagged_logging.rbs +6 -0
- data/sig/steep/ast/annotation/collection.rbs +6 -6
- data/sig/steep/ast/types/class.rbs +3 -0
- data/sig/steep/ast/types/factory.rbs +38 -32
- data/sig/steep/ast/types/instance.rbs +3 -0
- data/sig/steep/ast/types/intersection.rbs +1 -1
- data/sig/steep/ast/types/literal.rbs +7 -7
- data/sig/steep/ast/types/name.rbs +14 -13
- data/sig/steep/ast/types/proc.rbs +3 -1
- data/sig/steep/ast/types/self.rbs +3 -0
- data/sig/steep/ast/types/var.rbs +1 -1
- data/sig/steep/diagnostic/ruby.rbs +30 -34
- data/sig/steep/drivers/annotations.rbs +17 -0
- data/sig/steep/drivers/check.rbs +33 -0
- data/sig/steep/drivers/checkfile.rbs +26 -0
- data/sig/steep/drivers/diagnostic_printer.rbs +25 -0
- data/sig/steep/drivers/init.rbs +19 -0
- data/sig/steep/drivers/langserver.rbs +35 -0
- data/sig/steep/drivers/print_project.rbs +15 -0
- data/sig/steep/drivers/stats.rbs +37 -0
- data/sig/steep/drivers/utils/driver_helper.rbs +23 -0
- data/sig/steep/drivers/utils/jobs_count.rbs +11 -0
- data/sig/steep/drivers/validate.rbs +15 -0
- data/sig/steep/drivers/vendor.rbs +19 -0
- data/sig/steep/drivers/watch.rbs +27 -0
- data/sig/steep/drivers/worker.rbs +31 -0
- data/sig/steep/equatable.rbs +11 -0
- data/sig/steep/index/rbs_index.rbs +91 -0
- data/sig/steep/index/signature_symbol_provider.rbs +29 -0
- data/sig/steep/index/source_index.rbs +63 -0
- data/sig/steep/interface/block.rbs +3 -1
- data/sig/steep/interface/builder.rbs +152 -0
- data/sig/steep/interface/function.rbs +67 -55
- data/sig/steep/interface/method_type.rbs +60 -12
- data/sig/steep/interface/shape.rbs +61 -0
- data/sig/steep/interface/substitution.rbs +18 -22
- data/sig/steep/interface/type_param.rbs +9 -1
- data/sig/steep/path_helper.rbs +7 -0
- data/sig/steep/project/pattern.rbs +10 -10
- data/sig/steep/project/target.rbs +2 -2
- data/sig/steep/project.rbs +15 -8
- data/sig/steep/server/base_worker.rbs +49 -0
- data/sig/steep/server/change_buffer.rbs +32 -0
- data/sig/steep/server/interaction_worker.rbs +41 -0
- data/sig/steep/server/lsp_formatter.rbs +29 -0
- data/sig/steep/server/master.rbs +260 -0
- data/sig/steep/server/type_check_worker.rbs +135 -0
- data/sig/steep/server/worker_process.rbs +29 -0
- data/sig/steep/services/completion_provider.rbs +5 -5
- data/sig/steep/services/content_change.rbs +14 -12
- data/sig/steep/services/file_loader.rbs +12 -4
- data/sig/steep/services/hover_provider/singleton_methods.rbs +1 -1
- data/sig/steep/services/path_assignment.rbs +7 -7
- data/sig/steep/services/signature_service.rbs +67 -40
- data/sig/steep/services/type_check_service.rbs +53 -39
- data/sig/steep/subtyping/check.rbs +80 -44
- data/sig/steep/subtyping/relation.rbs +24 -22
- data/sig/steep/subtyping/result.rbs +48 -30
- data/sig/steep/subtyping/variable_variance.rbs +2 -0
- data/sig/steep/type_construction.rbs +132 -23
- data/sig/steep/type_inference/block_params.rbs +120 -18
- data/sig/steep/type_inference/context.rbs +45 -20
- data/sig/steep/type_inference/context_array.rbs +37 -0
- data/sig/steep/type_inference/logic_type_interpreter.rbs +3 -1
- data/sig/steep/type_inference/method_params.rbs +13 -9
- data/sig/steep/type_inference/send_args.rbs +229 -0
- data/sig/steep/type_inference/type_env_builder.rbs +1 -1
- data/sig/steep/typing.rbs +4 -4
- data/sig/steep.rbs +4 -2
- data/smoke/block/e.rb +12 -0
- data/smoke/block/e.rbs +4 -0
- data/smoke/block/test_expectations.yml +12 -0
- data/smoke/regression/block_param_split.rb +7 -0
- data/smoke/regression/block_param_split.rbs +3 -0
- data/smoke/regression/empty_yield.rb +5 -0
- data/smoke/regression/empty_yield.rbs +3 -0
- data/smoke/regression/test_expectations.yml +12 -0
- data/smoke/yield/test_expectations.yml +4 -4
- data/steep.gemspec +2 -1
- metadata +61 -9
- data/lib/steep/interface/interface.rb +0 -34
- data/sig/steep/interface/interface.rbs +0 -23
- data/sig/version.rbs +0 -3
@@ -4,11 +4,8 @@ module Steep
|
|
4
4
|
class Factory
|
5
5
|
attr_reader :definition_builder
|
6
6
|
|
7
|
-
attr_reader :type_name_cache
|
8
7
|
attr_reader :type_cache
|
9
8
|
|
10
|
-
attr_reader :type_interface_cache
|
11
|
-
|
12
9
|
def inspect
|
13
10
|
s = "#<%s:%#018x " % [self.class, object_id]
|
14
11
|
s << "@definition_builder=#<%s:%#018x>" % [definition_builder.class, definition_builder.object_id]
|
@@ -18,9 +15,9 @@ module Steep
|
|
18
15
|
def initialize(builder:)
|
19
16
|
@definition_builder = builder
|
20
17
|
|
21
|
-
@type_name_cache = {}
|
22
18
|
@type_cache = {}
|
23
|
-
@
|
19
|
+
@method_type_cache = {}
|
20
|
+
@method_type_cache.compare_by_identity
|
24
21
|
end
|
25
22
|
|
26
23
|
def type_name_resolver
|
@@ -33,6 +30,12 @@ module Steep
|
|
33
30
|
end
|
34
31
|
end
|
35
32
|
|
33
|
+
def type_1_opt(type)
|
34
|
+
if type
|
35
|
+
type_1(type)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
36
39
|
def type(type)
|
37
40
|
ty = type_cache[type] and return ty
|
38
41
|
|
@@ -101,11 +104,16 @@ module Steep
|
|
101
104
|
return_type: type(type.block.type.return_type),
|
102
105
|
location: type.location
|
103
106
|
),
|
104
|
-
optional: !type.block.required
|
107
|
+
optional: !type.block.required,
|
108
|
+
self_type: type_opt(type.block.self_type)
|
105
109
|
)
|
106
110
|
end
|
107
111
|
|
108
|
-
Proc.new(
|
112
|
+
Proc.new(
|
113
|
+
type: func,
|
114
|
+
block: block,
|
115
|
+
self_type: type_opt(type.self_type)
|
116
|
+
)
|
109
117
|
else
|
110
118
|
raise "Unexpected type given: #{type}"
|
111
119
|
end
|
@@ -179,11 +187,13 @@ module Steep
|
|
179
187
|
block = if type.block
|
180
188
|
RBS::Types::Block.new(
|
181
189
|
type: function_1(type.block.type),
|
182
|
-
required: !type.block.optional
|
190
|
+
required: !type.block.optional?,
|
191
|
+
self_type: type_1_opt(type.block.self_type)
|
183
192
|
)
|
184
193
|
end
|
185
194
|
RBS::Types::Proc.new(
|
186
195
|
type: function_1(type.type),
|
196
|
+
self_type: type_1_opt(type.self_type),
|
187
197
|
block: block,
|
188
198
|
location: type.location
|
189
199
|
)
|
@@ -224,7 +234,7 @@ module Steep
|
|
224
234
|
def type_param(type_param)
|
225
235
|
Interface::TypeParam.new(
|
226
236
|
name: type_param.name,
|
227
|
-
upper_bound: type_param.upper_bound
|
237
|
+
upper_bound: type_opt(type_param.upper_bound),
|
228
238
|
variance: type_param.variance,
|
229
239
|
unchecked: type_param.unchecked?
|
230
240
|
)
|
@@ -239,135 +249,80 @@ module Steep
|
|
239
249
|
).unchecked!(type_param.unchecked)
|
240
250
|
end
|
241
251
|
|
242
|
-
def method_type(method_type,
|
243
|
-
|
244
|
-
|
245
|
-
type_params = []
|
246
|
-
conflicting_names = []
|
247
|
-
|
248
|
-
type_params = method_type.type_params.map do |type_param|
|
249
|
-
if fvs.include?(type_param.name)
|
250
|
-
conflicting_names << type_param.name
|
251
|
-
end
|
252
|
-
|
253
|
-
type_param(type_param)
|
254
|
-
end
|
255
|
-
|
256
|
-
type_params, subst = Interface::TypeParam.rename(type_params, conflicting_names)
|
257
|
-
subst.merge!(subst2, overwrite: true) if subst2
|
258
|
-
|
259
|
-
type =
|
252
|
+
def method_type(method_type, method_decls:)
|
253
|
+
mt = @method_type_cache[method_type] ||=
|
260
254
|
Interface::MethodType.new(
|
261
|
-
type_params: type_params,
|
255
|
+
type_params: method_type.type_params.map {|param| type_param(param) },
|
262
256
|
type: Interface::Function.new(
|
263
|
-
params: params(method_type.type)
|
264
|
-
return_type: type(method_type.type.return_type)
|
257
|
+
params: params(method_type.type),
|
258
|
+
return_type: type(method_type.type.return_type),
|
265
259
|
location: method_type.location
|
266
260
|
),
|
267
261
|
block: method_type.block&.yield_self do |block|
|
268
262
|
Interface::Block.new(
|
269
263
|
optional: !block.required,
|
270
264
|
type: Interface::Function.new(
|
271
|
-
params: params(block.type)
|
272
|
-
return_type: type(block.type.return_type)
|
265
|
+
params: params(block.type),
|
266
|
+
return_type: type(block.type.return_type),
|
273
267
|
location: nil
|
274
|
-
)
|
268
|
+
),
|
269
|
+
self_type: type_opt(block.self_type)
|
275
270
|
)
|
276
271
|
end,
|
277
|
-
method_decls:
|
272
|
+
method_decls: Set[]
|
278
273
|
)
|
279
274
|
|
280
|
-
|
281
|
-
yield type
|
282
|
-
else
|
283
|
-
type
|
284
|
-
end
|
275
|
+
mt.with(method_decls: method_decls)
|
285
276
|
end
|
286
277
|
|
287
|
-
def method_type_1(method_type
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
conflicting_names = method_type.type_params.each.with_object([]) do |param, names|
|
293
|
-
names << params.name if fvs.include?(param.name)
|
294
|
-
end
|
295
|
-
|
296
|
-
type_params, subst = Interface::TypeParam.rename(method_type.type_params, conflicting_names)
|
297
|
-
|
298
|
-
type = RBS::MethodType.new(
|
299
|
-
type_params: type_params.map {|param| type_param_1(param) },
|
300
|
-
type: function_1(method_type.type.subst(subst)),
|
278
|
+
def method_type_1(method_type)
|
279
|
+
RBS::MethodType.new(
|
280
|
+
type_params: method_type.type_params.map {|param| type_param_1(param) },
|
281
|
+
type: function_1(method_type.type),
|
301
282
|
block: method_type.block&.yield_self do |block|
|
302
|
-
block_type = block.type.subst(subst)
|
303
|
-
|
304
283
|
RBS::Types::Block.new(
|
305
|
-
type: function_1(
|
306
|
-
required: !block.optional
|
284
|
+
type: function_1(block.type),
|
285
|
+
required: !block.optional,
|
286
|
+
self_type: type_1_opt(block.self_type)
|
307
287
|
)
|
308
288
|
end,
|
309
289
|
location: nil
|
310
290
|
)
|
311
|
-
|
312
|
-
if block_given?
|
313
|
-
yield type
|
314
|
-
else
|
315
|
-
type
|
316
|
-
end
|
317
|
-
end
|
318
|
-
|
319
|
-
class InterfaceCalculationError < StandardError
|
320
|
-
attr_reader :type
|
321
|
-
|
322
|
-
def initialize(type:, message:)
|
323
|
-
@type = type
|
324
|
-
super message
|
325
|
-
end
|
326
291
|
end
|
327
292
|
|
328
293
|
def unfold(type_name, args)
|
329
294
|
type(
|
330
295
|
definition_builder.expand_alias2(
|
331
296
|
type_name,
|
332
|
-
args.empty? ?
|
297
|
+
args.empty? ? [] : args.map {|t| type_1(t) }
|
333
298
|
)
|
334
299
|
)
|
335
300
|
end
|
336
301
|
|
337
302
|
def expand_alias(type)
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
else
|
342
|
-
type
|
343
|
-
end
|
344
|
-
|
345
|
-
if block_given?
|
346
|
-
yield unfolded
|
303
|
+
case type
|
304
|
+
when AST::Types::Name::Alias
|
305
|
+
unfold(type.name, type.args)
|
347
306
|
else
|
348
|
-
|
307
|
+
type
|
349
308
|
end
|
350
309
|
end
|
351
310
|
|
352
|
-
def deep_expand_alias(type, recursive: Set.new
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
end
|
366
|
-
|
367
|
-
if block_given?
|
368
|
-
yield ty
|
311
|
+
def deep_expand_alias(type, recursive: Set.new)
|
312
|
+
case type
|
313
|
+
when AST::Types::Name::Alias
|
314
|
+
unless recursive.member?(type.name)
|
315
|
+
unfolded = expand_alias(type)
|
316
|
+
deep_expand_alias(unfolded, recursive: recursive.union([type.name]))
|
317
|
+
end
|
318
|
+
when AST::Types::Union
|
319
|
+
types = type.types.map {|ty| deep_expand_alias(ty, recursive: recursive) or return }
|
320
|
+
AST::Types::Union.build(types: types, location: type.location)
|
321
|
+
when AST::Types::Intersection
|
322
|
+
types = type.types.map {|ty| deep_expand_alias(ty, recursive: recursive) or return }
|
323
|
+
AST::Types::Intersection.build(types: types, location: type.location)
|
369
324
|
else
|
370
|
-
|
325
|
+
type
|
371
326
|
end
|
372
327
|
end
|
373
328
|
|
@@ -403,515 +358,24 @@ module Steep
|
|
403
358
|
end
|
404
359
|
end
|
405
360
|
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
defined_in = method_def.defined_in
|
410
|
-
member = method_def.member
|
411
|
-
|
412
|
-
if member.is_a?(RBS::AST::Members::MethodDefinition)
|
413
|
-
case method_name
|
414
|
-
when :is_a?, :kind_of?, :instance_of?
|
415
|
-
if defined_in == RBS::BuiltinNames::Object.name && member.instance?
|
416
|
-
return method_type.with(
|
417
|
-
type: method_type.type.with(
|
418
|
-
return_type: AST::Types::Logic::ReceiverIsArg.new(location: method_type.type.return_type.location)
|
419
|
-
)
|
420
|
-
)
|
421
|
-
end
|
422
|
-
|
423
|
-
when :nil?
|
424
|
-
case defined_in
|
425
|
-
when RBS::BuiltinNames::Object.name,
|
426
|
-
NilClassName
|
427
|
-
return method_type.with(
|
428
|
-
type: method_type.type.with(
|
429
|
-
return_type: AST::Types::Logic::ReceiverIsNil.new(location: method_type.type.return_type.location)
|
430
|
-
)
|
431
|
-
)
|
432
|
-
end
|
433
|
-
|
434
|
-
when :!
|
435
|
-
case defined_in
|
436
|
-
when RBS::BuiltinNames::BasicObject.name,
|
437
|
-
RBS::BuiltinNames::TrueClass.name,
|
438
|
-
RBS::BuiltinNames::FalseClass.name,
|
439
|
-
AST::Builtin::NilClass.module_name
|
440
|
-
return method_type.with(
|
441
|
-
type: method_type.type.with(
|
442
|
-
return_type: AST::Types::Logic::Not.new(location: method_type.type.return_type.location)
|
443
|
-
)
|
444
|
-
)
|
445
|
-
end
|
446
|
-
|
447
|
-
when :===
|
448
|
-
case defined_in
|
449
|
-
when RBS::BuiltinNames::Module.name
|
450
|
-
return method_type.with(
|
451
|
-
type: method_type.type.with(
|
452
|
-
return_type: AST::Types::Logic::ArgIsReceiver.new(location: method_type.type.return_type.location)
|
453
|
-
)
|
454
|
-
)
|
455
|
-
when RBS::BuiltinNames::Object.name, RBS::BuiltinNames::String.name, RBS::BuiltinNames::Integer.name, RBS::BuiltinNames::Symbol.name,
|
456
|
-
RBS::BuiltinNames::TrueClass.name, RBS::BuiltinNames::FalseClass.name, TypeName("::NilClass")
|
457
|
-
# Value based type-case works on literal types which is available for String, Integer, Symbol, TrueClass, FalseClass, and NilClass
|
458
|
-
return method_type.with(
|
459
|
-
type: method_type.type.with(
|
460
|
-
return_type: AST::Types::Logic::ArgEqualsReceiver.new(location: method_type.type.return_type.location)
|
461
|
-
)
|
462
|
-
)
|
463
|
-
end
|
464
|
-
end
|
465
|
-
end
|
466
|
-
|
467
|
-
method_type
|
468
|
-
end
|
469
|
-
|
470
|
-
def interface(type, private:, self_type: type)
|
471
|
-
Steep.logger.debug { "Factory#interface: #{type}, private=#{private}, self_type=#{self_type}" }
|
472
|
-
|
473
|
-
cache_key = [type, self_type, private]
|
474
|
-
if type_interface_cache.key?(cache_key)
|
475
|
-
return type_interface_cache[cache_key]
|
476
|
-
end
|
477
|
-
|
478
|
-
case type
|
479
|
-
when Name::Alias
|
480
|
-
interface(expand_alias(type), private: private, self_type: self_type)
|
481
|
-
|
482
|
-
when Self
|
483
|
-
if self_type != type
|
484
|
-
interface self_type, private: private, self_type: Self.new
|
485
|
-
else
|
486
|
-
raise "Unexpected `self` type interface"
|
487
|
-
end
|
488
|
-
|
489
|
-
when Name::Instance
|
490
|
-
Interface::Interface.new(type: self_type, private: private).tap do |interface|
|
491
|
-
definition = definition_builder.build_instance(type.name)
|
492
|
-
|
493
|
-
instance_type = Name::Instance.new(name: type.name,
|
494
|
-
args: type.args.map { Any.new(location: nil) },
|
495
|
-
location: nil)
|
496
|
-
module_type = type.to_module()
|
497
|
-
|
498
|
-
subst = Interface::Substitution.build(
|
499
|
-
definition.type_params,
|
500
|
-
type.args,
|
501
|
-
instance_type: instance_type,
|
502
|
-
module_type: module_type,
|
503
|
-
self_type: self_type
|
504
|
-
)
|
505
|
-
|
506
|
-
definition.methods.each do |name, method|
|
507
|
-
Steep.logger.tagged "method = #{name}" do
|
508
|
-
next if method.private? && !private
|
509
|
-
|
510
|
-
interface.methods[name] = Interface::Interface::Entry.new(
|
511
|
-
method_types: method.defs.map do |type_def|
|
512
|
-
method_name = InstanceMethodName.new(type_name: type_def.implemented_in || type_def.defined_in, method_name: name)
|
513
|
-
decl = TypeInference::MethodCall::MethodDecl.new(method_name: method_name, method_def: type_def)
|
514
|
-
setup_primitives(
|
515
|
-
name,
|
516
|
-
type_def,
|
517
|
-
method_type(type_def.type,
|
518
|
-
method_decls: Set[decl],
|
519
|
-
self_type: self_type,
|
520
|
-
subst2: subst)
|
521
|
-
)
|
522
|
-
end
|
523
|
-
)
|
524
|
-
end
|
525
|
-
end
|
526
|
-
end
|
527
|
-
|
528
|
-
when Name::Interface
|
529
|
-
Interface::Interface.new(type: self_type, private: private).tap do |interface|
|
530
|
-
type_name = type.name
|
531
|
-
definition = definition_builder.build_interface(type_name)
|
532
|
-
|
533
|
-
subst = Interface::Substitution.build(
|
534
|
-
definition.type_params,
|
535
|
-
type.args,
|
536
|
-
self_type: self_type
|
537
|
-
)
|
538
|
-
|
539
|
-
definition.methods.each do |name, method|
|
540
|
-
interface.methods[name] = Interface::Interface::Entry.new(
|
541
|
-
method_types: method.defs.map do |type_def|
|
542
|
-
decls = Set[TypeInference::MethodCall::MethodDecl.new(
|
543
|
-
method_name: InstanceMethodName.new(type_name: type_def.implemented_in || type_def.defined_in, method_name: name),
|
544
|
-
method_def: type_def
|
545
|
-
)]
|
546
|
-
method_type(type_def.type, method_decls: decls, self_type: self_type, subst2: subst)
|
547
|
-
end
|
548
|
-
)
|
549
|
-
end
|
550
|
-
end
|
551
|
-
|
552
|
-
when Name::Singleton
|
553
|
-
Interface::Interface.new(type: self_type, private: private).tap do |interface|
|
554
|
-
definition = definition_builder.build_singleton(type.name)
|
555
|
-
|
556
|
-
instance_type = Name::Instance.new(name: type.name,
|
557
|
-
args: definition.type_params.map {Any.new(location: nil)},
|
558
|
-
location: nil)
|
559
|
-
subst = Interface::Substitution.build(
|
560
|
-
[],
|
561
|
-
instance_type: instance_type,
|
562
|
-
module_type: type,
|
563
|
-
self_type: self_type
|
564
|
-
)
|
565
|
-
|
566
|
-
definition.methods.each do |name, method|
|
567
|
-
next if !private && method.private?
|
568
|
-
|
569
|
-
interface.methods[name] = Interface::Interface::Entry.new(
|
570
|
-
method_types: method.defs.map do |type_def|
|
571
|
-
decl = TypeInference::MethodCall::MethodDecl.new(
|
572
|
-
method_name: SingletonMethodName.new(type_name: type_def.implemented_in || type_def.defined_in,
|
573
|
-
method_name: name),
|
574
|
-
method_def: type_def
|
575
|
-
)
|
576
|
-
setup_primitives(
|
577
|
-
name,
|
578
|
-
type_def,
|
579
|
-
method_type(type_def.type,
|
580
|
-
method_decls: Set[decl],
|
581
|
-
self_type: self_type,
|
582
|
-
subst2: subst)
|
583
|
-
)
|
584
|
-
end
|
585
|
-
)
|
586
|
-
end
|
587
|
-
end
|
588
|
-
|
589
|
-
when Literal
|
590
|
-
interface type.back_type, private: private, self_type: self_type
|
591
|
-
|
592
|
-
when Nil
|
593
|
-
interface Builtin::NilClass.instance_type, private: private, self_type: self_type
|
594
|
-
|
595
|
-
when Boolean
|
596
|
-
interface(AST::Types::Union.build(types: [Builtin::TrueClass.instance_type, Builtin::FalseClass.instance_type]),
|
597
|
-
private: private,
|
598
|
-
self_type: self_type)
|
599
|
-
|
600
|
-
when Union
|
601
|
-
yield_self do
|
602
|
-
interfaces = type.types.map {|ty| interface(ty, private: private, self_type: self_type) }
|
603
|
-
interfaces.inject do |interface1, interface2|
|
604
|
-
Interface::Interface.new(type: self_type, private: private).tap do |interface|
|
605
|
-
common_methods = Set.new(interface1.methods.keys) & Set.new(interface2.methods.keys)
|
606
|
-
common_methods.each do |name|
|
607
|
-
types1 = interface1.methods[name].method_types
|
608
|
-
types2 = interface2.methods[name].method_types
|
609
|
-
|
610
|
-
if types1 == types2
|
611
|
-
interface.methods[name] = interface1.methods[name]
|
612
|
-
else
|
613
|
-
method_types = {}
|
614
|
-
|
615
|
-
types1.each do |type1|
|
616
|
-
types2.each do |type2|
|
617
|
-
type = type1 | type2 or next
|
618
|
-
method_types[type] = true
|
619
|
-
end
|
620
|
-
end
|
621
|
-
|
622
|
-
unless method_types.empty?
|
623
|
-
interface.methods[name] = Interface::Interface::Entry.new(method_types: method_types.keys)
|
624
|
-
end
|
625
|
-
end
|
626
|
-
end
|
627
|
-
end
|
628
|
-
end
|
629
|
-
end
|
630
|
-
|
631
|
-
when Intersection
|
632
|
-
yield_self do
|
633
|
-
interfaces = type.types.map {|ty| interface(ty, private: private, self_type: self_type) }
|
634
|
-
interfaces.inject do |interface1, interface2|
|
635
|
-
Interface::Interface.new(type: self_type, private: private).tap do |interface|
|
636
|
-
interface.methods.merge!(interface1.methods)
|
637
|
-
interface.methods.merge!(interface2.methods)
|
638
|
-
end
|
639
|
-
end
|
640
|
-
end
|
641
|
-
|
642
|
-
when Tuple
|
643
|
-
yield_self do
|
644
|
-
element_type = Union.build(types: type.types, location: nil)
|
645
|
-
array_type = Builtin::Array.instance_type(element_type)
|
646
|
-
interface(array_type, private: private, self_type: self_type).tap do |array_interface|
|
647
|
-
array_interface.methods[:[]] = array_interface.methods[:[]].yield_self do |aref|
|
648
|
-
Interface::Interface::Entry.new(
|
649
|
-
method_types: type.types.map.with_index {|elem_type, index|
|
650
|
-
Interface::MethodType.new(
|
651
|
-
type_params: [],
|
652
|
-
type: Interface::Function.new(
|
653
|
-
params: Interface::Function::Params.build(required: [AST::Types::Literal.new(value: index)]),
|
654
|
-
return_type: elem_type,
|
655
|
-
location: nil
|
656
|
-
),
|
657
|
-
block: nil,
|
658
|
-
method_decls: Set[]
|
659
|
-
)
|
660
|
-
} + aref.method_types
|
661
|
-
)
|
662
|
-
end
|
663
|
-
|
664
|
-
array_interface.methods[:[]=] = array_interface.methods[:[]=].yield_self do |update|
|
665
|
-
Interface::Interface::Entry.new(
|
666
|
-
method_types: type.types.map.with_index {|elem_type, index|
|
667
|
-
Interface::MethodType.new(
|
668
|
-
type_params: [],
|
669
|
-
type: Interface::Function.new(
|
670
|
-
params: Interface::Function::Params.build(required: [AST::Types::Literal.new(value: index), elem_type]),
|
671
|
-
return_type: elem_type,
|
672
|
-
location: nil
|
673
|
-
),
|
674
|
-
block: nil,
|
675
|
-
method_decls: Set[]
|
676
|
-
)
|
677
|
-
} + update.method_types
|
678
|
-
)
|
679
|
-
end
|
680
|
-
|
681
|
-
array_interface.methods[:fetch] = array_interface.methods[:fetch].yield_self do |fetch|
|
682
|
-
Interface::Interface::Entry.new(
|
683
|
-
method_types: type.types.flat_map.with_index {|elem_type, index|
|
684
|
-
[
|
685
|
-
Interface::MethodType.new(
|
686
|
-
type_params: [],
|
687
|
-
type: Interface::Function.new(
|
688
|
-
params: Interface::Function::Params.build(required: [AST::Types::Literal.new(value: index)]),
|
689
|
-
return_type: elem_type,
|
690
|
-
location: nil
|
691
|
-
),
|
692
|
-
block: nil,
|
693
|
-
method_decls: Set[]
|
694
|
-
),
|
695
|
-
Interface::MethodType.new(
|
696
|
-
type_params: [Interface::TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false)],
|
697
|
-
type: Interface::Function.new(
|
698
|
-
params: Interface::Function::Params.build(
|
699
|
-
required: [
|
700
|
-
AST::Types::Literal.new(value: index),
|
701
|
-
AST::Types::Var.new(name: :T)
|
702
|
-
]
|
703
|
-
),
|
704
|
-
return_type: AST::Types::Union.build(types: [elem_type, AST::Types::Var.new(name: :T)]),
|
705
|
-
location: nil
|
706
|
-
),
|
707
|
-
block: nil,
|
708
|
-
method_decls: Set[]
|
709
|
-
),
|
710
|
-
Interface::MethodType.new(
|
711
|
-
type_params: [Interface::TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false)],
|
712
|
-
type: Interface::Function.new(
|
713
|
-
params: Interface::Function::Params.build(required: [AST::Types::Literal.new(value: index)]),
|
714
|
-
return_type: AST::Types::Union.build(types: [elem_type, AST::Types::Var.new(name: :T)]),
|
715
|
-
location: nil
|
716
|
-
),
|
717
|
-
block: Interface::Block.new(
|
718
|
-
type: Interface::Function.new(
|
719
|
-
params: Interface::Function::Params.build(required: [AST::Builtin::Integer.instance_type]),
|
720
|
-
return_type: AST::Types::Var.new(name: :T),
|
721
|
-
location: nil
|
722
|
-
),
|
723
|
-
optional: false
|
724
|
-
),
|
725
|
-
method_decls: Set[]
|
726
|
-
)
|
727
|
-
]
|
728
|
-
} + fetch.method_types
|
729
|
-
)
|
730
|
-
end
|
731
|
-
|
732
|
-
array_interface.methods[:first] = array_interface.methods[:first].yield_self do |first|
|
733
|
-
Interface::Interface::Entry.new(
|
734
|
-
method_types: [
|
735
|
-
Interface::MethodType.new(
|
736
|
-
type_params: [],
|
737
|
-
type: Interface::Function.new(
|
738
|
-
params: Interface::Function::Params.empty,
|
739
|
-
return_type: type.types[0] || AST::Builtin.nil_type,
|
740
|
-
location: nil
|
741
|
-
),
|
742
|
-
block: nil,
|
743
|
-
method_decls: Set[]
|
744
|
-
)
|
745
|
-
]
|
746
|
-
)
|
747
|
-
end
|
748
|
-
|
749
|
-
array_interface.methods[:last] = array_interface.methods[:last].yield_self do |last|
|
750
|
-
Interface::Interface::Entry.new(
|
751
|
-
method_types: [
|
752
|
-
Interface::MethodType.new(
|
753
|
-
type_params: [],
|
754
|
-
type: Interface::Function.new(
|
755
|
-
params: Interface::Function::Params.empty,
|
756
|
-
return_type: type.types.last || AST::Builtin.nil_type,
|
757
|
-
location: nil
|
758
|
-
),
|
759
|
-
block: nil,
|
760
|
-
method_decls: Set[]
|
761
|
-
)
|
762
|
-
]
|
763
|
-
)
|
764
|
-
end
|
765
|
-
end
|
766
|
-
end
|
767
|
-
|
768
|
-
when Record
|
769
|
-
yield_self do
|
770
|
-
all_key_type = type.elements.keys.map {|value| Literal.new(value: value, location: nil) }.yield_self do |types|
|
771
|
-
Union.build(types: types, location: nil)
|
772
|
-
end
|
773
|
-
all_value_type = Union.build(types: type.elements.values, location: nil)
|
774
|
-
hash_type = Builtin::Hash.instance_type(all_key_type, all_value_type)
|
775
|
-
|
776
|
-
interface(hash_type, private: private, self_type: self_type).tap do |hash_interface|
|
777
|
-
hash_interface.methods[:[]] = hash_interface.methods[:[]].yield_self do |ref|
|
778
|
-
Interface::Interface::Entry.new(
|
779
|
-
method_types: type.elements.map {|key_value, value_type|
|
780
|
-
key_type = Literal.new(value: key_value, location: nil)
|
781
|
-
|
782
|
-
Interface::MethodType.new(
|
783
|
-
type_params: [],
|
784
|
-
type: Interface::Function.new(
|
785
|
-
params: Interface::Function::Params.build(
|
786
|
-
required: [key_type],
|
787
|
-
optional: [],
|
788
|
-
rest: nil,
|
789
|
-
required_keywords: {},
|
790
|
-
optional_keywords: {},
|
791
|
-
rest_keywords: nil
|
792
|
-
),
|
793
|
-
return_type: value_type,
|
794
|
-
location: nil
|
795
|
-
),
|
796
|
-
block: nil,
|
797
|
-
method_decls: Set[]
|
798
|
-
)
|
799
|
-
} + ref.method_types
|
800
|
-
)
|
801
|
-
end
|
802
|
-
|
803
|
-
hash_interface.methods[:[]=] = hash_interface.methods[:[]=].yield_self do |update|
|
804
|
-
Interface::Interface::Entry.new(
|
805
|
-
method_types: type.elements.map {|key_value, value_type|
|
806
|
-
key_type = Literal.new(value: key_value, location: nil)
|
807
|
-
Interface::MethodType.new(
|
808
|
-
type_params: [],
|
809
|
-
type: Interface::Function.new(
|
810
|
-
params: Interface::Function::Params.build(
|
811
|
-
required: [key_type, value_type],
|
812
|
-
optional: [],
|
813
|
-
rest: nil,
|
814
|
-
required_keywords: {},
|
815
|
-
optional_keywords: {},
|
816
|
-
rest_keywords: nil
|
817
|
-
),
|
818
|
-
return_type: value_type,
|
819
|
-
location: nil),
|
820
|
-
block: nil,
|
821
|
-
method_decls: Set[]
|
822
|
-
)
|
823
|
-
} + update.method_types
|
824
|
-
)
|
825
|
-
end
|
826
|
-
|
827
|
-
hash_interface.methods[:fetch] = hash_interface.methods[:fetch].yield_self do |update|
|
828
|
-
Interface::Interface::Entry.new(
|
829
|
-
method_types: type.elements.flat_map {|key_value, value_type|
|
830
|
-
key_type = Literal.new(value: key_value, location: nil)
|
831
|
-
|
832
|
-
[
|
833
|
-
Interface::MethodType.new(
|
834
|
-
type_params: [],
|
835
|
-
type: Interface::Function.new(
|
836
|
-
params: Interface::Function::Params.build(required: [key_type]),
|
837
|
-
return_type: value_type,
|
838
|
-
location: nil
|
839
|
-
),
|
840
|
-
block: nil,
|
841
|
-
method_decls: Set[]
|
842
|
-
),
|
843
|
-
Interface::MethodType.new(
|
844
|
-
type_params: [Interface::TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false)],
|
845
|
-
type: Interface::Function.new(
|
846
|
-
params: Interface::Function::Params.build(required: [key_type, AST::Types::Var.new(name: :T)]),
|
847
|
-
return_type: AST::Types::Union.build(types: [value_type, AST::Types::Var.new(name: :T)]),
|
848
|
-
location: nil
|
849
|
-
),
|
850
|
-
block: nil,
|
851
|
-
method_decls: Set[]
|
852
|
-
),
|
853
|
-
Interface::MethodType.new(
|
854
|
-
type_params: [Interface::TypeParam.new(name: :T, upper_bound: nil, variance: :invariant, unchecked: false)],
|
855
|
-
type: Interface::Function.new(
|
856
|
-
params: Interface::Function::Params.build(required: [key_type]),
|
857
|
-
return_type: AST::Types::Union.build(types: [value_type, AST::Types::Var.new(name: :T)]),
|
858
|
-
location: nil
|
859
|
-
),
|
860
|
-
block: Interface::Block.new(
|
861
|
-
type: Interface::Function.new(
|
862
|
-
params: Interface::Function::Params.build(required: [all_key_type]),
|
863
|
-
return_type: AST::Types::Var.new(name: :T),
|
864
|
-
location: nil
|
865
|
-
),
|
866
|
-
optional: false
|
867
|
-
),
|
868
|
-
method_decls: Set[]
|
869
|
-
)
|
870
|
-
]
|
871
|
-
} + update.method_types
|
872
|
-
)
|
873
|
-
end
|
874
|
-
end
|
875
|
-
end
|
876
|
-
|
877
|
-
when Proc
|
878
|
-
interface(Builtin::Proc.instance_type, private: private, self_type: self_type).tap do |interface|
|
879
|
-
method_type = Interface::MethodType.new(
|
880
|
-
type_params: [],
|
881
|
-
type: type.type,
|
882
|
-
block: type.block,
|
883
|
-
method_decls: Set[]
|
884
|
-
)
|
885
|
-
|
886
|
-
interface.methods[:call] = Interface::Interface::Entry.new(method_types: [method_type])
|
887
|
-
|
888
|
-
if type.block_required?
|
889
|
-
interface.methods.delete(:[])
|
890
|
-
else
|
891
|
-
interface.methods[:[]] = Interface::Interface::Entry.new(method_types: [method_type.with(block: nil)])
|
892
|
-
end
|
893
|
-
end
|
894
|
-
|
895
|
-
when Logic::Base
|
896
|
-
interface(AST::Builtin.bool_type, private: private, self_type: self_type)
|
897
|
-
|
361
|
+
def module_name?(type_name)
|
362
|
+
if entry = env.class_decls[type_name]
|
363
|
+
entry.is_a?(RBS::Environment::ModuleEntry)
|
898
364
|
else
|
899
|
-
|
900
|
-
end.tap do |interface|
|
901
|
-
type_interface_cache[cache_key] = interface
|
365
|
+
false
|
902
366
|
end
|
903
367
|
end
|
904
368
|
|
905
|
-
def module_name?(type_name)
|
906
|
-
entry = env.class_decls[type_name] and entry.is_a?(RBS::Environment::ModuleEntry)
|
907
|
-
end
|
908
|
-
|
909
369
|
def class_name?(type_name)
|
910
|
-
entry = env.class_decls[type_name]
|
370
|
+
if entry = env.class_decls[type_name]
|
371
|
+
entry.is_a?(RBS::Environment::ClassEntry)
|
372
|
+
else
|
373
|
+
false
|
374
|
+
end
|
911
375
|
end
|
912
376
|
|
913
377
|
def env
|
914
|
-
|
378
|
+
definition_builder.env
|
915
379
|
end
|
916
380
|
|
917
381
|
def absolute_type(type, context:)
|
@@ -939,6 +403,26 @@ module Steep
|
|
939
403
|
|
940
404
|
AST::Types::Name::Instance.new(location: location, name: type_name, args: args)
|
941
405
|
end
|
406
|
+
|
407
|
+
def try_instance_type(type)
|
408
|
+
case type
|
409
|
+
when AST::Types::Name::Instance
|
410
|
+
instance_type(type.name)
|
411
|
+
when AST::Types::Name::Singleton
|
412
|
+
instance_type(type.name)
|
413
|
+
else
|
414
|
+
nil
|
415
|
+
end
|
416
|
+
end
|
417
|
+
|
418
|
+
def try_singleton_type(type)
|
419
|
+
case type
|
420
|
+
when AST::Types::Name::Instance, AST::Types::Name::Singleton
|
421
|
+
AST::Types::Name::Singleton.new(name:type.name)
|
422
|
+
else
|
423
|
+
nil
|
424
|
+
end
|
425
|
+
end
|
942
426
|
end
|
943
427
|
end
|
944
428
|
end
|