steep 0.28.0 → 0.29.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e15a2388cd89e6b169243007211840853be8c126e035f7aa6434b00258e1087
4
- data.tar.gz: e3bb8c7a1d816cb2667e7dc977f5e3155bcb9444e995a210bd58dc716a6a71b7
3
+ metadata.gz: 5d09c66d40509db4956625f6be82477c4cda1b375d4cc37ebc2b4fdfdfdd971f
4
+ data.tar.gz: bfe074e9fc6e833faa4442f718e2bade7e54a5b6921fd8cf63465972ec39ced5
5
5
  SHA512:
6
- metadata.gz: fb77f06dcef27e222984269a637966fa581cd78b3157c31a71793e8447f7059467140b7bb3f8f3e7f059a37492c9988f428234011ff7b3f7133a627f60a63cf4
7
- data.tar.gz: 9319aebc6f74f89d055098312f460b2ed86ae9cfcd5c17bd21425c69e3ebe8838dffa3f4db9c99bc23bbc4657e4ee9f453bf1a2b3b7e4afe1531c0ba0ccb386c
6
+ metadata.gz: 05d54b1565ccf9189d335163d952d075bd6f7b3a08c26362efb815c3d19cfd2ffa2eb13e124a5dc7a4a5f1343a6fbbe5694ed3d6000a4fe37f3ace4cb2d7c1d5
7
+ data.tar.gz: 2661d5a8a5273e5c4341b27fd78a5f57c296c31663f16d46bf8de39ea1af40b827bfdf48f9266e071a37b1fe395a58ec716149d88656cf983587ecbe89fa24e6
@@ -2,6 +2,12 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.29.0 (2020-09-28)
6
+
7
+ * Implement reasoning on `is_a?`, `nil?`, and `===` methods. ([#218](https://github.com/soutaro/steep/pull/218))
8
+ * Better completion based on interface ([#215](https://github.com/soutaro/steep/pull/215))
9
+ * Fix begin-rescue typing ([#221](https://github.com/soutaro/steep/pull/221))
10
+
5
11
  ## 0.28.0 (2020-09-17)
6
12
 
7
13
  * Fix typing case-when with empty body ([#200](https://github.com/soutaro/steep/pull/200))
@@ -16,8 +16,6 @@ require 'uri'
16
16
 
17
17
  require "rbs"
18
18
 
19
- require "steep/ast/namespace"
20
- require "steep/names"
21
19
  require "steep/ast/location"
22
20
  require "steep/ast/types/helper"
23
21
  require "steep/ast/types/any"
@@ -37,6 +35,7 @@ require "steep/ast/types/boolean"
37
35
  require "steep/ast/types/tuple"
38
36
  require "steep/ast/types/proc"
39
37
  require "steep/ast/types/record"
38
+ require "steep/ast/types/logic"
40
39
  require "steep/ast/type_params"
41
40
  require "steep/ast/annotation"
42
41
  require "steep/ast/annotation/collection"
@@ -62,6 +61,7 @@ require "steep/source"
62
61
  require "steep/annotation_parser"
63
62
  require "steep/typing"
64
63
  require "steep/errors"
64
+ require "steep/module_helper"
65
65
  require "steep/type_construction"
66
66
  require "steep/type_inference/context"
67
67
  require "steep/type_inference/context_array"
@@ -71,6 +71,7 @@ require "steep/type_inference/constant_env"
71
71
  require "steep/type_inference/type_env"
72
72
  require "steep/type_inference/local_variable_type_env"
73
73
  require "steep/type_inference/logic"
74
+ require "steep/type_inference/logic_type_interpreter"
74
75
  require "steep/ast/types"
75
76
 
76
77
  require "steep/server/utils"
@@ -80,9 +80,7 @@ module Steep
80
80
  name = match[:name]
81
81
  type = parse_type(match[:type])
82
82
 
83
- AST::Annotation::ConstType.new(name: Names::Module.parse(name),
84
- type: type,
85
- location: location)
83
+ AST::Annotation::ConstType.new(name: TypeName(name), type: type, location: location)
86
84
  end
87
85
 
88
86
  when keyword_subject_type("ivar", IVAR_NAME)
@@ -152,7 +150,7 @@ module Steep
152
150
 
153
151
  when /@implements\s+(?<name>#{CONST_NAME})#{TYPE_PARAMS}$/
154
152
  Regexp.last_match.yield_self do |match|
155
- type_name = Names::Module.parse(match[:name])
153
+ type_name = TypeName(match[:name])
156
154
  params = match[:params]&.yield_self {|params| params.split(/,/).map {|param| param.strip.to_sym } } || []
157
155
 
158
156
  name = AST::Annotation::Implements::Module.new(name: type_name, args: params)
@@ -6,7 +6,7 @@ module Steep
6
6
  attr_reader :arity
7
7
 
8
8
  def initialize(module_name, arity: 0)
9
- @module_name = Names::Module.parse(module_name)
9
+ @module_name = TypeName(module_name)
10
10
  @arity = arity
11
11
  end
12
12
 
@@ -81,6 +81,14 @@ module Steep
81
81
  def self.optional(type)
82
82
  AST::Types::Union.build(types: [type, nil_type])
83
83
  end
84
+
85
+ def self.true_type
86
+ AST::Types::Literal.new(value: true)
87
+ end
88
+
89
+ def self.false_type
90
+ AST::Types::Literal.new(value: false)
91
+ end
84
92
  end
85
93
  end
86
94
  end
@@ -43,18 +43,18 @@ module Steep
43
43
  when RBS::Types::Variable
44
44
  Var.new(name: type.name, location: nil)
45
45
  when RBS::Types::ClassSingleton
46
- type_name = type_name(type.name)
46
+ type_name = type.name
47
47
  Name::Singleton.new(name: type_name, location: nil)
48
48
  when RBS::Types::ClassInstance
49
- type_name = type_name(type.name)
49
+ type_name = type.name
50
50
  args = type.args.map {|arg| type(arg) }
51
51
  Name::Instance.new(name: type_name, args: args, location: nil)
52
52
  when RBS::Types::Interface
53
- type_name = type_name(type.name)
53
+ type_name = type.name
54
54
  args = type.args.map {|arg| type(arg) }
55
55
  Name::Interface.new(name: type_name, args: args, location: nil)
56
56
  when RBS::Types::Alias
57
- type_name = type_name(type.name)
57
+ type_name = type.name
58
58
  Name::Alias.new(name: type_name, args: [], location: nil)
59
59
  when RBS::Types::Union
60
60
  Union.build(types: type.types.map {|ty| type(ty) }, location: nil)
@@ -103,22 +103,22 @@ module Steep
103
103
  when Var
104
104
  RBS::Types::Variable.new(name: type.name, location: nil)
105
105
  when Name::Singleton
106
- RBS::Types::ClassSingleton.new(name: type_name_1(type.name), location: nil)
106
+ RBS::Types::ClassSingleton.new(name: type.name, location: nil)
107
107
  when Name::Instance
108
108
  RBS::Types::ClassInstance.new(
109
- name: type_name_1(type.name),
109
+ name: type.name,
110
110
  args: type.args.map {|arg| type_1(arg) },
111
111
  location: nil
112
112
  )
113
113
  when Name::Interface
114
114
  RBS::Types::Interface.new(
115
- name: type_name_1(type.name),
115
+ name: type.name,
116
116
  args: type.args.map {|arg| type_1(arg) },
117
117
  location: nil
118
118
  )
119
119
  when Name::Alias
120
120
  type.args.empty? or raise "alias type with args is not supported"
121
- RBS::Types::Alias.new(name: type_name_1(type.name), location: nil)
121
+ RBS::Types::Alias.new(name: type.name, location: nil)
122
122
  when Union
123
123
  RBS::Types::Union.new(
124
124
  types: type.types.map {|ty| type_1(ty) },
@@ -151,32 +151,6 @@ module Steep
151
151
  end
152
152
  end
153
153
 
154
- def type_name(name)
155
- n = type_name_cache[name] and return n
156
-
157
- type_name_cache[name] =
158
- (case
159
- when name.class?
160
- Names::Module.new(name: name.name, namespace: namespace(name.namespace), location: nil)
161
- when name.interface?
162
- Names::Interface.new(name: name.name, namespace: namespace(name.namespace), location: nil)
163
- when name.alias?
164
- Names::Alias.new(name: name.name, namespace: namespace(name.namespace), location: nil)
165
- end)
166
- end
167
-
168
- def type_name_1(name)
169
- RBS::TypeName.new(name: name.name, namespace: namespace_1(name.namespace))
170
- end
171
-
172
- def namespace(namespace)
173
- Namespace.parse(namespace.to_s)
174
- end
175
-
176
- def namespace_1(namespace)
177
- RBS::Namespace.parse(namespace.to_s)
178
- end
179
-
180
154
  def function_1(params, return_type)
181
155
  RBS::Types::Function.new(
182
156
  required_positionals: params.required.map {|type| RBS::Types::Function::Param.new(name: nil, type: type_1(type)) },
@@ -201,7 +175,7 @@ module Steep
201
175
  )
202
176
  end
203
177
 
204
- def method_type(method_type, self_type:, subst2: nil)
178
+ def method_type(method_type, self_type:, subst2: nil, method_def: nil)
205
179
  fvs = self_type.free_variables()
206
180
 
207
181
  type_params = []
@@ -225,14 +199,15 @@ module Steep
225
199
  type_params: type_params,
226
200
  return_type: type(method_type.type.return_type).subst(subst),
227
201
  params: params(method_type.type).subst(subst),
228
- location: nil,
229
202
  block: method_type.block&.yield_self do |block|
230
203
  Interface::Block.new(
231
204
  optional: !block.required,
232
205
  type: Proc.new(params: params(block.type).subst(subst),
233
206
  return_type: type(block.type.return_type).subst(subst), location: nil)
234
207
  )
235
- end
208
+ end,
209
+ method_def: method_def,
210
+ location: method_def&.member&.location
236
211
  )
237
212
 
238
213
  if block_given?
@@ -292,7 +267,7 @@ module Steep
292
267
  end
293
268
 
294
269
  def unfold(type_name)
295
- type_name_1(type_name).yield_self do |type_name|
270
+ type_name.yield_self do |type_name|
296
271
  type(definition_builder.expand_alias(type_name))
297
272
  end
298
273
  end
@@ -312,6 +287,105 @@ module Steep
312
287
  end
313
288
  end
314
289
 
290
+ def deep_expand_alias(type, recursive: Set.new, &block)
291
+ raise "Recursive type definition: #{type}" if recursive.member?(type)
292
+
293
+ ty = case type
294
+ when AST::Types::Name::Alias
295
+ deep_expand_alias(expand_alias(type), recursive: recursive.union([type]))
296
+ when AST::Types::Union
297
+ AST::Types::Union.build(
298
+ types: type.types.map {|ty| deep_expand_alias(ty, recursive: recursive, &block) },
299
+ location: type.location
300
+ )
301
+ else
302
+ type
303
+ end
304
+
305
+ if block_given?
306
+ yield ty
307
+ else
308
+ ty
309
+ end
310
+ end
311
+
312
+ def flatten_union(type, acc = [])
313
+ case type
314
+ when AST::Types::Union
315
+ type.types.each {|ty| flatten_union(ty, acc) }
316
+ else
317
+ acc << type
318
+ end
319
+
320
+ acc
321
+ end
322
+
323
+ def unwrap_optional(type)
324
+ case type
325
+ when AST::Types::Union
326
+ falsy_types, truthy_types = type.types.partition do |type|
327
+ (type.is_a?(AST::Types::Literal) && type.value == false) ||
328
+ type.is_a?(AST::Types::Nil)
329
+ end
330
+
331
+ [
332
+ AST::Types::Union.build(types: truthy_types),
333
+ AST::Types::Union.build(types: falsy_types)
334
+ ]
335
+ when AST::Types::Name::Alias
336
+ unwrap_optional(expand_alias(type))
337
+ else
338
+ [type, nil]
339
+ end
340
+ end
341
+
342
+ def setup_primitives(method_name, method_type)
343
+ if method_def = method_type.method_def
344
+ defined_in = method_def.defined_in
345
+ member = method_def.member
346
+
347
+ case
348
+ when defined_in == RBS::BuiltinNames::Object.name && member.instance?
349
+ case method_name
350
+ when :is_a?, :kind_of?, :instance_of?
351
+ return method_type.with(
352
+ return_type: AST::Types::Logic::ReceiverIsArg.new(location: method_type.return_type.location)
353
+ )
354
+ when :nil?
355
+ return method_type.with(
356
+ return_type: AST::Types::Logic::ReceiverIsNil.new(location: method_type.return_type.location)
357
+ )
358
+ end
359
+
360
+ when defined_in == AST::Builtin::NilClass.module_name && member.instance?
361
+ case method_name
362
+ when :nil?
363
+ return method_type.with(
364
+ return_type: AST::Types::Logic::ReceiverIsNil.new(location: method_type.return_type.location)
365
+ )
366
+ end
367
+
368
+ when defined_in == RBS::BuiltinNames::BasicObject.name && member.instance?
369
+ case method_name
370
+ when :!
371
+ return method_type.with(
372
+ return_type: AST::Types::Logic::Not.new(location: method_type.return_type.location)
373
+ )
374
+ end
375
+
376
+ when defined_in == RBS::BuiltinNames::Module.name && member.instance?
377
+ case method_name
378
+ when :===
379
+ return method_type.with(
380
+ return_type: AST::Types::Logic::ArgIsReceiver.new(location: method_type.return_type.location)
381
+ )
382
+ end
383
+ end
384
+ end
385
+
386
+ method_type
387
+ end
388
+
315
389
  def interface(type, private:, self_type: type)
316
390
  Steep.logger.debug { "Factory#interface: #{type}, private=#{private}, self_type=#{self_type}" }
317
391
  type = expand_alias(type)
@@ -325,7 +399,7 @@ module Steep
325
399
  end
326
400
  when Name::Instance
327
401
  Interface::Interface.new(type: self_type, private: private).tap do |interface|
328
- definition = definition_builder.build_instance(type_name_1(type.name))
402
+ definition = definition_builder.build_instance(type.name)
329
403
 
330
404
  instance_type = Name::Instance.new(name: type.name,
331
405
  args: type.args.map { Any.new(location: nil) },
@@ -345,8 +419,14 @@ module Steep
345
419
  next if method.private? && !private
346
420
 
347
421
  interface.methods[name] = Interface::Interface::Entry.new(
348
- method_types: method.method_types.map do |type|
349
- method_type(type, self_type: self_type, subst2: subst)
422
+ method_types: method.defs.map do |type_def|
423
+ setup_primitives(
424
+ name,
425
+ method_type(type_def.type,
426
+ method_def: type_def,
427
+ self_type: self_type,
428
+ subst2: subst)
429
+ )
350
430
  end
351
431
  )
352
432
  end
@@ -355,7 +435,7 @@ module Steep
355
435
 
356
436
  when Name::Interface
357
437
  Interface::Interface.new(type: self_type, private: private).tap do |interface|
358
- type_name = type_name_1(type.name)
438
+ type_name = type.name
359
439
  definition = definition_builder.build_interface(type_name)
360
440
 
361
441
  subst = Interface::Substitution.build(
@@ -366,8 +446,8 @@ module Steep
366
446
 
367
447
  definition.methods.each do |name, method|
368
448
  interface.methods[name] = Interface::Interface::Entry.new(
369
- method_types: method.method_types.map do |type|
370
- method_type(type, self_type: self_type, subst2: subst)
449
+ method_types: method.defs.map do |type_def|
450
+ method_type(type_def.type, method_def: type_def, self_type: self_type, subst2: subst)
371
451
  end
372
452
  )
373
453
  end
@@ -375,7 +455,7 @@ module Steep
375
455
 
376
456
  when Name::Singleton
377
457
  Interface::Interface.new(type: self_type, private: private).tap do |interface|
378
- definition = definition_builder.build_singleton(type_name_1(type.name))
458
+ definition = definition_builder.build_singleton(type.name)
379
459
 
380
460
  instance_type = Name::Instance.new(name: type.name,
381
461
  args: definition.type_params.map {Any.new(location: nil)},
@@ -391,8 +471,14 @@ module Steep
391
471
  next if !private && method.private?
392
472
 
393
473
  interface.methods[name] = Interface::Interface::Entry.new(
394
- method_types: method.method_types.map do |type|
395
- method_type(type, self_type: self_type, subst2: subst)
474
+ method_types: method.defs.map do |type_def|
475
+ setup_primitives(
476
+ name,
477
+ method_type(type_def.type,
478
+ method_def: type_def,
479
+ self_type: self_type,
480
+ subst2: subst)
481
+ )
396
482
  end
397
483
  )
398
484
  end
@@ -469,6 +555,7 @@ module Steep
469
555
  rest_keywords: nil),
470
556
  block: nil,
471
557
  return_type: elem_type,
558
+ method_def: nil,
472
559
  location: nil
473
560
  )
474
561
  } + aref.method_types
@@ -488,6 +575,7 @@ module Steep
488
575
  rest_keywords: nil),
489
576
  block: nil,
490
577
  return_type: elem_type,
578
+ method_def: nil,
491
579
  location: nil
492
580
  )
493
581
  } + update.method_types
@@ -502,6 +590,7 @@ module Steep
502
590
  params: Interface::Params.empty,
503
591
  block: nil,
504
592
  return_type: type.types[0] || AST::Builtin.nil_type,
593
+ method_def: nil,
505
594
  location: nil
506
595
  )
507
596
  ]
@@ -516,6 +605,7 @@ module Steep
516
605
  params: Interface::Params.empty,
517
606
  block: nil,
518
607
  return_type: type.types.last || AST::Builtin.nil_type,
608
+ method_def: nil,
519
609
  location: nil
520
610
  )
521
611
  ]
@@ -547,6 +637,7 @@ module Steep
547
637
  rest_keywords: nil),
548
638
  block: nil,
549
639
  return_type: value_type,
640
+ method_def: nil,
550
641
  location: nil
551
642
  )
552
643
  } + ref.method_types
@@ -567,6 +658,7 @@ module Steep
567
658
  rest_keywords: nil),
568
659
  block: nil,
569
660
  return_type: value_type,
661
+ method_def: nil,
570
662
  location: nil
571
663
  )
572
664
  } + update.method_types
@@ -582,6 +674,7 @@ module Steep
582
674
  params: type.params,
583
675
  return_type: type.return_type,
584
676
  block: nil,
677
+ method_def: nil,
585
678
  location: nil
586
679
  )
587
680
 
@@ -589,19 +682,20 @@ module Steep
589
682
  interface.methods[:call] = Interface::Interface::Entry.new(method_types: [method_type])
590
683
  end
591
684
 
685
+ when Logic::Base
686
+ interface(AST::Builtin.bool_type, private: private, self_type: self_type)
687
+
592
688
  else
593
689
  raise "Unexpected type for interface: #{type}"
594
690
  end
595
691
  end
596
692
 
597
693
  def module_name?(type_name)
598
- name = type_name_1(type_name)
599
- entry = env.class_decls[name] and entry.is_a?(RBS::Environment::ModuleEntry)
694
+ entry = env.class_decls[type_name] and entry.is_a?(RBS::Environment::ModuleEntry)
600
695
  end
601
696
 
602
697
  def class_name?(type_name)
603
- name = type_name_1(type_name)
604
- entry = env.class_decls[name] and entry.is_a?(RBS::Environment::ClassEntry)
698
+ entry = env.class_decls[type_name] and entry.is_a?(RBS::Environment::ClassEntry)
605
699
  end
606
700
 
607
701
  def env
@@ -616,7 +710,22 @@ module Steep
616
710
  end
617
711
 
618
712
  def absolute_type_name(type_name, namespace:)
619
- type_name_resolver.resolve(type_name, context: namespace_1(namespace).ascend)
713
+ type_name_resolver.resolve(type_name, context: namespace.ascend)
714
+ end
715
+
716
+ def instance_type(type_name, args: nil, location: nil)
717
+ raise unless type_name.class?
718
+
719
+ definition = definition_builder.build_singleton(type_name)
720
+ def_args = definition.type_params.map { Any.new(location: nil) }
721
+
722
+ if args
723
+ raise if def_args.size != args.size
724
+ else
725
+ args = def_args
726
+ end
727
+
728
+ AST::Types::Name::Instance.new(location: location, name: type_name, args: args)
620
729
  end
621
730
  end
622
731
  end