rbs 0.11.0 → 0.12.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.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Rakefile +9 -4
  4. data/Steepfile +28 -0
  5. data/bin/steep +4 -0
  6. data/bin/test_runner.rb +10 -5
  7. data/lib/rbs/ast/comment.rb +7 -1
  8. data/lib/rbs/ast/declarations.rb +15 -9
  9. data/lib/rbs/buffer.rb +1 -1
  10. data/lib/rbs/definition.rb +22 -13
  11. data/lib/rbs/definition_builder.rb +79 -55
  12. data/lib/rbs/environment.rb +24 -10
  13. data/lib/rbs/location.rb +1 -5
  14. data/lib/rbs/method_type.rb +5 -5
  15. data/lib/rbs/namespace.rb +14 -3
  16. data/lib/rbs/parser.y +0 -8
  17. data/lib/rbs/prototype/rb.rb +3 -4
  18. data/lib/rbs/prototype/rbi.rb +1 -2
  19. data/lib/rbs/substitution.rb +4 -3
  20. data/lib/rbs/type_name.rb +18 -1
  21. data/lib/rbs/type_name_resolver.rb +10 -3
  22. data/lib/rbs/types.rb +27 -21
  23. data/lib/rbs/variance_calculator.rb +8 -5
  24. data/lib/rbs/version.rb +1 -1
  25. data/sig/annotation.rbs +26 -0
  26. data/sig/buffer.rbs +28 -0
  27. data/sig/builtin_names.rbs +41 -0
  28. data/sig/comment.rbs +26 -0
  29. data/sig/declarations.rbs +202 -0
  30. data/sig/definition.rbs +129 -0
  31. data/sig/definition_builder.rbs +95 -0
  32. data/sig/environment.rbs +94 -0
  33. data/sig/environment_loader.rbs +4 -0
  34. data/sig/location.rbs +52 -0
  35. data/sig/members.rbs +160 -0
  36. data/sig/method_types.rbs +40 -0
  37. data/sig/namespace.rbs +124 -0
  38. data/sig/polyfill.rbs +3 -0
  39. data/sig/rbs.rbs +3 -0
  40. data/sig/substitution.rbs +39 -0
  41. data/sig/type_name_resolver.rbs +24 -0
  42. data/sig/typename.rbs +70 -0
  43. data/sig/types.rbs +361 -0
  44. data/sig/util.rbs +13 -0
  45. data/sig/variance_calculator.rbs +35 -0
  46. data/stdlib/bigdecimal/big_decimal.rbs +887 -0
  47. data/stdlib/bigdecimal/math/big_math.rbs +142 -0
  48. data/stdlib/builtin/builtin.rbs +0 -3
  49. data/stdlib/builtin/math.rbs +26 -26
  50. data/stdlib/builtin/struct.rbs +9 -10
  51. data/stdlib/forwardable/forwardable.rbs +204 -0
  52. data/stdlib/set/set.rbs +1 -1
  53. data/stdlib/uri/file.rbs +167 -0
  54. data/stdlib/uri/generic.rbs +875 -0
  55. data/steep/Gemfile +3 -0
  56. data/steep/Gemfile.lock +55 -0
  57. metadata +36 -6
@@ -13,14 +13,15 @@ module RBS
13
13
  def context
14
14
  @context ||= begin
15
15
  (outer + [decl]).each.with_object([Namespace.root]) do |decl, array|
16
- array.unshift(array.first + decl.name.to_namespace)
16
+ first = array.first or raise
17
+ array.unshift(first + decl.name.to_namespace)
17
18
  end
18
19
  end
19
20
  end
20
21
  end
21
22
 
22
23
  class MultiEntry
23
- D = Struct.new(:decl, :outer, keyword_init: true) do
24
+ D = _ = Struct.new(:decl, :outer, keyword_init: true) do
24
25
  include ContextUtil
25
26
  end
26
27
 
@@ -40,6 +41,8 @@ module RBS
40
41
  def validate_type_params
41
42
  unless decls.empty?
42
43
  hd_decl, *tl_decls = decls
44
+ raise unless hd_decl
45
+
43
46
  hd_params = hd_decl.decl.type_params
44
47
  hd_names = hd_params.params.map(&:name)
45
48
 
@@ -56,6 +59,10 @@ module RBS
56
59
  def type_params
57
60
  primary.decl.type_params
58
61
  end
62
+
63
+ def primary
64
+ raise "Not implemented"
65
+ end
59
66
  end
60
67
 
61
68
  class ModuleEntry < MultiEntry
@@ -68,7 +75,7 @@ module RBS
68
75
  def primary
69
76
  @primary ||= begin
70
77
  validate_type_params
71
- decls.first
78
+ decls.first or raise("decls cannot be empty")
72
79
  end
73
80
  end
74
81
  end
@@ -77,7 +84,7 @@ module RBS
77
84
  def primary
78
85
  @primary ||= begin
79
86
  validate_type_params
80
- decls.find {|d| d.decl.super_class } || decls.first
87
+ decls.find {|d| d.decl.super_class } || decls.first or raise("decls cannot be empty")
81
88
  end
82
89
  end
83
90
  end
@@ -154,15 +161,17 @@ module RBS
154
161
 
155
162
  case
156
163
  when decl.is_a?(AST::Declarations::Module) && existing_entry.is_a?(ModuleEntry)
157
- # OK
164
+ # @type var existing_entry: ModuleEntry
165
+ # @type var decl: AST::Declarations::Module
166
+ existing_entry.insert(decl: decl, outer: outer)
158
167
  when decl.is_a?(AST::Declarations::Class) && existing_entry.is_a?(ClassEntry)
159
- # OK
168
+ # @type var existing_entry: ClassEntry
169
+ # @type var decl: AST::Declarations::Class
170
+ existing_entry.insert(decl: decl, outer: outer)
160
171
  else
161
172
  raise DuplicatedDeclarationError.new(name, decl, existing_entry.primary.decl)
162
173
  end
163
174
 
164
- existing_entry.insert(decl: decl, outer: outer)
165
-
166
175
  prefix = outer + [decl]
167
176
  ns = name.to_namespace
168
177
  decl.each_decl do |d|
@@ -211,6 +220,7 @@ module RBS
211
220
 
212
221
  def resolve_declaration(resolver, decl, outer:, prefix:)
213
222
  if decl.is_a?(AST::Declarations::Global)
223
+ # @type var decl: AST::Declarations::Global
214
224
  return AST::Declarations::Global.new(
215
225
  name: decl.name,
216
226
  type: absolute_type(resolver, decl.type, context: [Namespace.root]),
@@ -220,7 +230,8 @@ module RBS
220
230
  end
221
231
 
222
232
  context = (outer + [decl]).each.with_object([Namespace.root]) do |decl, array|
223
- array.unshift(array.first + decl.name.to_namespace)
233
+ head = array.first or raise
234
+ array.unshift(head + decl.name.to_namespace)
224
235
  end
225
236
 
226
237
  case decl
@@ -310,6 +321,9 @@ module RBS
310
321
  location: decl.location,
311
322
  comment: decl.comment
312
323
  )
324
+
325
+ else
326
+ raise
313
327
  end
314
328
  end
315
329
 
@@ -409,7 +423,7 @@ module RBS
409
423
  end
410
424
 
411
425
  def absolute_type(resolver, type, context:)
412
- type.map_type_name do |name|
426
+ type.map_type_name do |name, _, _|
413
427
  absolute_type_name(resolver, name, context: context)
414
428
  end
415
429
  end
@@ -43,7 +43,7 @@ module RBS
43
43
  end
44
44
 
45
45
  def source
46
- @source ||= buffer.content[start_pos...end_pos]
46
+ @source ||= buffer.content[start_pos...end_pos] or raise
47
47
  end
48
48
 
49
49
  def to_s
@@ -73,10 +73,6 @@ module RBS
73
73
  end
74
74
  end
75
75
 
76
- def self.concat(*locations)
77
- locations.inject {|l1, l2| l1 + l2 }
78
- end
79
-
80
76
  def concat(*others)
81
77
  others.each { |other| self << other }
82
78
  self
@@ -93,7 +93,7 @@ module RBS
93
93
  end
94
94
 
95
95
  def each_type(&block)
96
- if block_given?
96
+ if block
97
97
  type.each_type(&block)
98
98
  self.block&.yield_self do |b|
99
99
  b.type.each_type(&block)
@@ -105,10 +105,10 @@ module RBS
105
105
 
106
106
  def to_s
107
107
  s = case
108
- when block && block.required
109
- "(#{type.param_to_s}) { (#{block.type.param_to_s}) -> #{block.type.return_to_s} } -> #{type.return_to_s}"
110
- when block
111
- "(#{type.param_to_s}) ?{ (#{block.type.param_to_s}) -> #{block.type.return_to_s} } -> #{type.return_to_s}"
108
+ when (b = block) && b.required
109
+ "(#{type.param_to_s}) { (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
110
+ when b = block
111
+ "(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
112
112
  else
113
113
  "(#{type.param_to_s}) -> #{type.return_to_s}"
114
114
  end
@@ -63,7 +63,9 @@ module RBS
63
63
  end
64
64
 
65
65
  def split
66
- [parent, path.last]
66
+ last = path.last or return
67
+ parent = self.parent
68
+ [parent, last]
67
69
  end
68
70
 
69
71
  def to_s
@@ -77,6 +79,10 @@ module RBS
77
79
 
78
80
  def to_type_name
79
81
  parent, name = split
82
+
83
+ raise unless name
84
+ raise unless parent
85
+
80
86
  TypeName.new(name: name, namespace: parent)
81
87
  end
82
88
 
@@ -88,14 +94,13 @@ module RBS
88
94
  end
89
95
  end
90
96
 
91
-
92
97
  def ascend
93
98
  if block_given?
94
99
  current = self
95
100
 
96
101
  until current.empty?
97
102
  yield current
98
- current = current.parent
103
+ current = _ = current.parent
99
104
  end
100
105
 
101
106
  yield current
@@ -107,3 +112,9 @@ module RBS
107
112
  end
108
113
  end
109
114
  end
115
+
116
+ module Kernel
117
+ def Namespace(name)
118
+ RBS::Namespace.parse(name)
119
+ end
120
+ end
@@ -435,14 +435,6 @@ rule
435
435
  method_member:
436
436
  annotations attributes overload kDEF method_kind def_name method_types {
437
437
  location = val[3].location + val[6].last.location
438
- types = val[6].map do |type|
439
- case type
440
- when LocatedValue
441
- type.value
442
- else
443
- type
444
- end
445
- end
446
438
 
447
439
  last_type = val[6].last
448
440
  if last_type.is_a?(LocatedValue) && last_type.value == :dot3
@@ -36,7 +36,7 @@ module RBS
36
36
  tokens.each.with_object({}) do |token, hash|
37
37
  if token[1] == :on_comment
38
38
  line = token[0][0]
39
- body = token[2][2..]
39
+ body = token[2][2..-1]
40
40
 
41
41
  body = "\n" if body.empty?
42
42
 
@@ -353,7 +353,6 @@ module RBS
353
353
  Types::Bases::Nil.new(location: nil)
354
354
  when :LIT
355
355
  lit = node.children[0]
356
- name = lit.class.name
357
356
  case lit
358
357
  when Symbol
359
358
  if lit.match?(/\A[ -~]+\z/)
@@ -364,7 +363,7 @@ module RBS
364
363
  when Integer
365
364
  Types::Literal.new(literal: lit, location: nil)
366
365
  else
367
- type_name = TypeName.new(name: name, namespace: Namespace.root)
366
+ type_name = TypeName.new(name: lit.class.name.to_sym, namespace: Namespace.root)
368
367
  Types::ClassInstance.new(name: type_name, args: [], location: nil)
369
368
  end
370
369
  when :ZLIST, :ZARRAY
@@ -420,7 +419,7 @@ module RBS
420
419
 
421
420
  types = types.map do |t|
422
421
  if t.is_a?(Types::Literal)
423
- type_name = TypeName.new(name: t.literal.class.name, namespace: Namespace.root)
422
+ type_name = TypeName.new(name: t.literal.class.name.to_sym, namespace: Namespace.root)
424
423
  Types::ClassInstance.new(name: type_name, args: [], location: nil)
425
424
  else
426
425
  t
@@ -16,7 +16,7 @@ module RBS
16
16
  tokens.each.with_object({}) do |token, hash|
17
17
  if token[1] == :on_comment
18
18
  line = token[0][0]
19
- body = token[2][2..]
19
+ body = token[2][2..-1]
20
20
 
21
21
  body = "\n" if body.empty?
22
22
 
@@ -500,7 +500,6 @@ module RBS
500
500
  else
501
501
  type_node.type == :CALL && proc_type?(type_node.children[0])
502
502
  end
503
-
504
503
  end
505
504
 
506
505
  def call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true })
@@ -31,10 +31,11 @@ module RBS
31
31
  def apply(ty)
32
32
  case ty
33
33
  when Types::Variable
34
+ # @type var ty: Types::Variable
34
35
  mapping[ty.name] || ty
35
36
  when Types::Bases::Instance
36
- if instance_type
37
- instance_type
37
+ if t = instance_type
38
+ t
38
39
  else
39
40
  ty
40
41
  end
@@ -44,7 +45,7 @@ module RBS
44
45
  end
45
46
 
46
47
  def without(*vars)
47
- self.class.new.tap do |subst|
48
+ Substitution.new.tap do |subst|
48
49
  subst.mapping.merge!(mapping)
49
50
  vars.each do |var|
50
51
  subst.mapping.delete(var)
@@ -1,3 +1,4 @@
1
+
1
2
  module RBS
2
3
  class TypeName
3
4
  attr_reader :namespace
@@ -14,13 +15,15 @@ module RBS
14
15
  :alias
15
16
  when "_"
16
17
  :interface
18
+ else
19
+ raise
17
20
  end
18
21
  end
19
22
 
20
23
  def ==(other)
21
24
  other.is_a?(self.class) && other.namespace == namespace && other.name == name
22
25
  end
23
-
26
+
24
27
  alias eql? ==
25
28
 
26
29
  def hash
@@ -68,3 +71,17 @@ module RBS
68
71
  end
69
72
  end
70
73
  end
74
+
75
+ module Kernel
76
+ def TypeName(string)
77
+ absolute = string.start_with?("::")
78
+
79
+ *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
80
+ raise unless name
81
+
82
+ RBS::TypeName.new(
83
+ name: name,
84
+ namespace: RBS::Namespace.new(path: path, absolute: absolute)
85
+ )
86
+ end
87
+ end
@@ -1,6 +1,6 @@
1
1
  module RBS
2
2
  class TypeNameResolver
3
- Query = Struct.new(:type_name, :context, keyword_init: true)
3
+ Query = _ = Struct.new(:type_name, :context, keyword_init: true)
4
4
 
5
5
  attr_reader :all_names
6
6
  attr_reader :cache
@@ -36,15 +36,22 @@ module RBS
36
36
  query = Query.new(type_name: type_name, context: context)
37
37
  try_cache(query) do
38
38
  path_head, *path_tail = type_name.to_namespace.path
39
+ raise unless path_head
40
+
39
41
  name_head = TypeName.new(name: path_head, namespace: Namespace.empty)
40
42
 
41
- absolute_head = context.each.find do |namespace|
43
+ absolute_head = context.find do |namespace|
44
+ # @type break: TypeName
42
45
  full_name = name_head.with_prefix(namespace)
43
46
  has_name?(full_name) and break full_name
44
47
  end
45
48
 
46
- if absolute_head
49
+ case absolute_head
50
+ when TypeName
47
51
  has_name?(Namespace.new(path: absolute_head.to_namespace.path.push(*path_tail), absolute: true).to_type_name)
52
+ when Namespace
53
+ # This cannot happen because the `context.find` doesn't return a Namespace.
54
+ raise
48
55
  end
49
56
  end
50
57
  end
@@ -1,3 +1,4 @@
1
+
1
2
  module RBS
2
3
  module Types
3
4
  module NoFreeVariables
@@ -77,7 +78,7 @@ module RBS
77
78
  when Types::Bases::Class
78
79
  'class'
79
80
  else
80
- raise "Unexpected base type: #{type.inspect}"
81
+ raise "Unexpected base type: #{inspect}"
81
82
  end
82
83
  end
83
84
  end
@@ -138,8 +139,6 @@ module RBS
138
139
  new(name: v, location: nil)
139
140
  when Array
140
141
  v.map {|x| new(name: x, location: nil) }
141
- else
142
- raise
143
142
  end
144
143
  end
145
144
 
@@ -227,7 +226,7 @@ module RBS
227
226
  end
228
227
 
229
228
  def each_type(&block)
230
- if block_given?
229
+ if block
231
230
  args.each(&block)
232
231
  else
233
232
  enum_for :each_type
@@ -380,7 +379,7 @@ module RBS
380
379
  end
381
380
 
382
381
  def each_type(&block)
383
- if block_given?
382
+ if block
384
383
  types.each(&block)
385
384
  else
386
385
  enum_for :each_type
@@ -445,7 +444,7 @@ module RBS
445
444
  end
446
445
 
447
446
  def each_type(&block)
448
- if block_given?
447
+ if block
449
448
  fields.each_value(&block)
450
449
  else
451
450
  enum_for :each_type
@@ -492,11 +491,15 @@ module RBS
492
491
  end
493
492
 
494
493
  def to_s(level = 0)
495
- if type.is_a?(RBS::Types::Literal) && type.literal.is_a?(Symbol)
496
- "#{type.to_s(1)} ?"
497
- else
498
- "#{type.to_s(1)}?"
494
+ case t = type
495
+ when RBS::Types::Literal
496
+ case t.literal
497
+ when Symbol
498
+ return "#{type.to_s(1)} ?"
499
+ end
499
500
  end
501
+
502
+ "#{type.to_s(1)}?"
500
503
  end
501
504
 
502
505
  def each_type
@@ -560,7 +563,7 @@ module RBS
560
563
  end
561
564
 
562
565
  def each_type(&block)
563
- if block_given?
566
+ if block
564
567
  types.each(&block)
565
568
  else
566
569
  enum_for :each_type
@@ -568,7 +571,7 @@ module RBS
568
571
  end
569
572
 
570
573
  def map_type(&block)
571
- if block_given?
574
+ if block
572
575
  Union.new(types: types.map(&block), location: location)
573
576
  else
574
577
  enum_for :map_type
@@ -629,7 +632,7 @@ module RBS
629
632
  end
630
633
 
631
634
  def each_type(&block)
632
- if block_given?
635
+ if block
633
636
  types.each(&block)
634
637
  else
635
638
  enum_for :each_type
@@ -637,7 +640,7 @@ module RBS
637
640
  end
638
641
 
639
642
  def map_type(&block)
640
- if block_given?
643
+ if block
641
644
  Intersection.new(types: types.map(&block), location: location)
642
645
  else
643
646
  enum_for :map_type
@@ -672,8 +675,8 @@ module RBS
672
675
  self.class.hash ^ type.hash ^ name.hash
673
676
  end
674
677
 
675
- def map_type
676
- if block_given?
678
+ def map_type(&block)
679
+ if block
677
680
  Param.new(name: name, type: yield(type))
678
681
  else
679
682
  enum_for :map_type
@@ -772,7 +775,7 @@ module RBS
772
775
  end
773
776
 
774
777
  def map_type(&block)
775
- if block_given?
778
+ if block
776
779
  Function.new(
777
780
  required_positionals: required_positionals.map {|param| param.map_type(&block) },
778
781
  optional_positionals: optional_positionals.map {|param| param.map_type(&block) },
@@ -810,7 +813,7 @@ module RBS
810
813
  end
811
814
 
812
815
  def each_param(&block)
813
- if block_given?
816
+ if block
814
817
  required_positionals.each(&block)
815
818
  optional_positionals.each(&block)
816
819
  rest_positionals&.yield_self(&block)
@@ -891,7 +894,9 @@ module RBS
891
894
  end
892
895
 
893
896
  def param_to_s
897
+ # @type var params: Array[String]
894
898
  params = []
899
+
895
900
  params.push(*required_positionals.map(&:to_s))
896
901
  params.push(*optional_positionals.map {|p| "?#{p}"})
897
902
  params.push("*#{rest_positionals}") if rest_positionals
@@ -927,8 +932,9 @@ module RBS
927
932
  def drop_tail
928
933
  case
929
934
  when !trailing_positionals.empty?
935
+ last = trailing_positionals.last or raise
930
936
  [
931
- trailing_positionals.last,
937
+ last,
932
938
  update(trailing_positionals: trailing_positionals.take(trailing_positionals.size - 1))
933
939
  ]
934
940
  else
@@ -960,7 +966,7 @@ module RBS
960
966
  self.class.hash ^ type.hash
961
967
  end
962
968
 
963
- def free_variables(set)
969
+ def free_variables(set = Set[])
964
970
  type.free_variables(set)
965
971
  end
966
972
 
@@ -977,7 +983,7 @@ module RBS
977
983
  end
978
984
 
979
985
  def each_type(&block)
980
- if block_given?
986
+ if block
981
987
  type.each_type(&block)
982
988
  else
983
989
  enum_for :each_type