rbs 0.9.1 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +9 -9
  3. data/CHANGELOG.md +31 -0
  4. data/Gemfile +1 -0
  5. data/README.md +1 -1
  6. data/Rakefile +16 -6
  7. data/Steepfile +28 -0
  8. data/bin/steep +4 -0
  9. data/bin/test_runner.rb +7 -5
  10. data/docs/syntax.md +14 -1
  11. data/lib/rbs/ast/comment.rb +7 -1
  12. data/lib/rbs/ast/declarations.rb +15 -9
  13. data/lib/rbs/ast/members.rb +3 -8
  14. data/lib/rbs/buffer.rb +1 -1
  15. data/lib/rbs/cli.rb +62 -1
  16. data/lib/rbs/definition.rb +35 -16
  17. data/lib/rbs/definition_builder.rb +99 -68
  18. data/lib/rbs/environment.rb +24 -11
  19. data/lib/rbs/environment_loader.rb +55 -35
  20. data/lib/rbs/location.rb +1 -5
  21. data/lib/rbs/method_type.rb +5 -5
  22. data/lib/rbs/namespace.rb +14 -3
  23. data/lib/rbs/parser.y +2 -12
  24. data/lib/rbs/prototype/rb.rb +3 -5
  25. data/lib/rbs/prototype/rbi.rb +1 -4
  26. data/lib/rbs/prototype/runtime.rb +0 -4
  27. data/lib/rbs/substitution.rb +4 -3
  28. data/lib/rbs/test/hook.rb +1 -0
  29. data/lib/rbs/test/setup.rb +8 -6
  30. data/lib/rbs/test/setup_helper.rb +15 -0
  31. data/lib/rbs/test/tester.rb +55 -11
  32. data/lib/rbs/test/type_check.rb +43 -14
  33. data/lib/rbs/type_name.rb +18 -1
  34. data/lib/rbs/type_name_resolver.rb +10 -3
  35. data/lib/rbs/types.rb +27 -21
  36. data/lib/rbs/variance_calculator.rb +8 -5
  37. data/lib/rbs/version.rb +1 -1
  38. data/lib/rbs/writer.rb +7 -3
  39. data/sig/annotation.rbs +26 -0
  40. data/sig/buffer.rbs +28 -0
  41. data/sig/builtin_names.rbs +41 -0
  42. data/sig/comment.rbs +26 -0
  43. data/sig/declarations.rbs +202 -0
  44. data/sig/definition.rbs +129 -0
  45. data/sig/definition_builder.rbs +95 -0
  46. data/sig/environment.rbs +94 -0
  47. data/sig/environment_loader.rbs +4 -0
  48. data/sig/location.rbs +52 -0
  49. data/sig/members.rbs +160 -0
  50. data/sig/method_types.rbs +40 -0
  51. data/sig/namespace.rbs +124 -0
  52. data/sig/polyfill.rbs +3 -0
  53. data/sig/rbs.rbs +3 -0
  54. data/sig/substitution.rbs +39 -0
  55. data/sig/type_name_resolver.rbs +24 -0
  56. data/sig/typename.rbs +70 -0
  57. data/sig/types.rbs +361 -0
  58. data/sig/util.rbs +13 -0
  59. data/sig/variance_calculator.rbs +35 -0
  60. data/stdlib/bigdecimal/big_decimal.rbs +887 -0
  61. data/stdlib/bigdecimal/math/big_math.rbs +142 -0
  62. data/stdlib/builtin/array.rbs +2 -1
  63. data/stdlib/builtin/builtin.rbs +0 -3
  64. data/stdlib/builtin/hash.rbs +1 -1
  65. data/stdlib/builtin/math.rbs +26 -26
  66. data/stdlib/builtin/struct.rbs +9 -10
  67. data/stdlib/date/date.rbs +1056 -0
  68. data/stdlib/date/date_time.rbs +582 -0
  69. data/stdlib/forwardable/forwardable.rbs +204 -0
  70. data/stdlib/pty/pty.rbs +5 -29
  71. data/stdlib/set/set.rbs +1 -1
  72. data/stdlib/uri/file.rbs +167 -0
  73. data/stdlib/uri/generic.rbs +875 -0
  74. data/stdlib/zlib/zlib.rbs +392 -0
  75. data/steep/Gemfile +3 -0
  76. data/steep/Gemfile.lock +55 -0
  77. metadata +38 -5
@@ -45,24 +45,41 @@ module RBS
45
45
  def update(type: self.type, member: self.member, defined_in: self.defined_in, implemented_in: self.implemented_in)
46
46
  TypeDef.new(type: type, member: member, defined_in: defined_in, implemented_in: implemented_in)
47
47
  end
48
+
49
+ def overload?
50
+ case mem = member
51
+ when AST::Members::MethodDefinition
52
+ mem.overload?
53
+ else
54
+ false
55
+ end
56
+ end
48
57
  end
49
58
 
50
59
  attr_reader :super_method
51
60
  attr_reader :defs
52
61
  attr_reader :accessibility
62
+ attr_reader :extra_annotations
53
63
 
54
- def initialize(super_method:, defs:, accessibility:)
64
+ def initialize(super_method:, defs:, accessibility:, annotations: [])
55
65
  @super_method = super_method
56
66
  @defs = defs
57
67
  @accessibility = accessibility
68
+ @extra_annotations = annotations
58
69
  end
59
70
 
60
71
  def defined_in
61
- @defined_in ||= defs.last.defined_in
72
+ @defined_in ||= begin
73
+ last_def = defs.last or raise
74
+ last_def.defined_in
75
+ end
62
76
  end
63
77
 
64
78
  def implemented_in
65
- @implemented_in ||= defs.last.implemented_in
79
+ @implemented_in ||= begin
80
+ last_def = defs.last or raise
81
+ last_def.implemented_in
82
+ end
66
83
  end
67
84
 
68
85
  def method_types
@@ -70,16 +87,15 @@ module RBS
70
87
  end
71
88
 
72
89
  def comments
73
- @comments ||= defs.map(&:comment).compact
90
+ @comments ||= _ = defs.map(&:comment).compact
74
91
  end
75
92
 
76
93
  def annotations
77
- @annotations ||= defs.flat_map(&:annotations)
94
+ @annotations ||= @extra_annotations + defs.flat_map(&:annotations)
78
95
  end
79
96
 
80
- # @deprecated
81
- def attributes
82
- []
97
+ def members
98
+ @members ||= defs.map(&:member).uniq
83
99
  end
84
100
 
85
101
  def public?
@@ -116,8 +132,8 @@ module RBS
116
132
  end
117
133
 
118
134
  module Ancestor
119
- Instance = Struct.new(:name, :args, keyword_init: true)
120
- Singleton = Struct.new(:name, keyword_init: true)
135
+ Instance = _ = Struct.new(:name, :args, keyword_init: true)
136
+ Singleton = _ = Struct.new(:name, keyword_init: true)
121
137
  end
122
138
 
123
139
  class InstanceAncestors
@@ -209,7 +225,10 @@ module RBS
209
225
  end
210
226
 
211
227
  def interface?
212
- entry.is_a?(Environment::SingleEntry) && entry.decl.is_a?(AST::Declarations::Interface)
228
+ case en = entry
229
+ when Environment::SingleEntry
230
+ en.decl.is_a?(AST::Declarations::Interface)
231
+ end
213
232
  end
214
233
 
215
234
  def class_type?
@@ -229,16 +248,16 @@ module RBS
229
248
  end
230
249
 
231
250
  def type_params_decl
232
- case entry
251
+ case en = entry
233
252
  when Environment::ClassEntry, Environment::ModuleEntry
234
- entry.type_params
253
+ en.type_params
235
254
  when Environment::SingleEntry
236
- entry.decl.type_params
255
+ en.decl.type_params
237
256
  end
238
257
  end
239
258
 
240
259
  def sub(s)
241
- definition = self.class.new(type_name: type_name, self_type: self_type.sub(s), ancestors: ancestors, entry: entry)
260
+ definition = self.class.new(type_name: type_name, self_type: _ = self_type.sub(s), ancestors: ancestors, entry: entry)
242
261
 
243
262
  definition.methods.merge!(methods.transform_values {|method| method.sub(s) })
244
263
  definition.instance_variables.merge!(instance_variables.transform_values {|v| v.sub(s) })
@@ -258,7 +277,7 @@ module RBS
258
277
  end
259
278
 
260
279
  def each_type(&block)
261
- if block_given?
280
+ if block
262
281
  methods.each_value do |method|
263
282
  if method.defined_in == type_name
264
283
  method.method_types.each do |method_type|
@@ -36,8 +36,11 @@ module RBS
36
36
  end
37
37
 
38
38
  def each_ancestor(&block)
39
- if block_given?
40
- yield super_class if super_class
39
+ if block
40
+ if s = super_class
41
+ yield s
42
+ end
43
+
41
44
  self_types&.each(&block)
42
45
  included_modules&.each(&block)
43
46
  prepended_modules&.each(&block)
@@ -108,7 +111,7 @@ module RBS
108
111
  return if with_super_classes.size <= 1
109
112
 
110
113
  super_types = with_super_classes.map do |d|
111
- super_class = d.decl.super_class
114
+ super_class = d.decl.super_class or raise
112
115
  Types::ClassInstance.new(name: super_class.name, args: super_class.args, location: nil)
113
116
  end
114
117
 
@@ -159,10 +162,10 @@ module RBS
159
162
 
160
163
  entry.self_types.each do |module_self|
161
164
  NoSelfTypeFoundError.check!(module_self, env: env)
162
- ancestors.self_types.push Definition::Ancestor::Instance.new(name: module_self.name, args: module_self.args)
165
+
166
+ self_types = ancestors.self_types or raise
167
+ self_types.push Definition::Ancestor::Instance.new(name: module_self.name, args: module_self.args)
163
168
  end
164
- else
165
- raise "Unexpected entry for: #{type_name}"
166
169
  end
167
170
 
168
171
  mixin_ancestors(entry,
@@ -208,9 +211,6 @@ module RBS
208
211
  type_name: type_name,
209
212
  super_class: Definition::Ancestor::Instance.new(name: BuiltinNames::Module.name, args: [])
210
213
  )
211
-
212
- else
213
- raise "Unexpected entry for: #{type_name}"
214
214
  end
215
215
 
216
216
  mixin_ancestors(entry,
@@ -285,33 +285,38 @@ module RBS
285
285
 
286
286
  case entry
287
287
  when Environment::ClassEntry
288
- if one_ancestors.super_class
289
- super_name = one_ancestors.super_class.name
290
- super_args = one_ancestors.super_class.args
288
+ if super_class = one_ancestors.super_class
289
+ # @type var super_class: Definition::Ancestor::Instance
290
+ super_name = super_class.name
291
+ super_args = super_class.args
291
292
 
292
293
  super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors)
293
294
  ancestors.unshift(*super_ancestors.apply(super_args, location: entry.primary.decl.location))
294
295
  end
295
296
  end
296
297
 
297
- one_ancestors.included_modules.each do |mod|
298
- if mod.name.class?
299
- name = mod.name
300
- args = mod.args
301
- mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
302
- ancestors.unshift(*mod_ancestors.apply(args, location: entry.primary.decl.location))
303
- end
298
+ if included_modules = one_ancestors.included_modules
299
+ included_modules.each do |mod|
300
+ if mod.name.class?
301
+ name = mod.name
302
+ arg_types = mod.args
303
+ mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
304
+ ancestors.unshift(*mod_ancestors.apply(arg_types, location: entry.primary.decl.location))
305
+ end
306
+ end
304
307
  end
305
308
 
306
309
  ancestors.unshift(self_ancestor)
307
310
 
308
- one_ancestors.prepended_modules.each do |mod|
309
- if mod.name.class?
310
- name = mod.name
311
- args = mod.args
312
- mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
313
- ancestors.unshift(*mod_ancestors.apply(args, location: entry.primary.decl.location))
314
- end
311
+ if prepended_modules = one_ancestors.prepended_modules
312
+ prepended_modules.each do |mod|
313
+ if mod.name.class?
314
+ name = mod.name
315
+ arg_types = mod.args
316
+ mod_ancestors = instance_ancestors(name, building_ancestors: building_ancestors)
317
+ ancestors.unshift(*mod_ancestors.apply(arg_types, location: entry.primary.decl.location))
318
+ end
319
+ end
315
320
  end
316
321
 
317
322
  building_ancestors.pop
@@ -338,22 +343,23 @@ module RBS
338
343
 
339
344
  ancestors = []
340
345
 
341
- case one_ancestors.super_class
346
+ case super_class = one_ancestors.super_class
342
347
  when Definition::Ancestor::Instance
343
- super_name = one_ancestors.super_class.name
344
- super_args = one_ancestors.super_class.args
348
+ super_name = super_class.name
349
+ super_args = super_class.args
345
350
 
346
351
  super_ancestors = instance_ancestors(super_name, building_ancestors: building_ancestors)
347
352
  ancestors.unshift(*super_ancestors.apply(super_args, location: entry.primary.decl.location))
348
353
 
349
354
  when Definition::Ancestor::Singleton
350
- super_name = one_ancestors.super_class.name
355
+ super_name = super_class.name
351
356
 
352
357
  super_ancestors = singleton_ancestors(super_name, building_ancestors: [])
353
358
  ancestors.unshift(*super_ancestors.ancestors)
354
359
  end
355
360
 
356
- one_ancestors.extended_modules.each do |mod|
361
+ extended_modules = one_ancestors.extended_modules or raise
362
+ extended_modules.each do |mod|
357
363
  if mod.name.class?
358
364
  name = mod.name
359
365
  args = mod.args
@@ -406,17 +412,17 @@ module RBS
406
412
  location: nil)
407
413
 
408
414
  definition_pairs = ancestors.ancestors.map do |ancestor|
415
+ # @type block: [Definition::Ancestor::t, Definition]
409
416
  case ancestor
410
417
  when Definition::Ancestor::Instance
411
418
  [ancestor, build_one_instance(ancestor.name)]
412
419
  when Definition::Ancestor::Singleton
413
420
  [ancestor, build_one_singleton(ancestor.name)]
414
- else
415
- raise
416
421
  end
417
422
  end
418
423
 
419
- if entry.is_a?(Environment::ModuleEntry)
424
+ case entry
425
+ when Environment::ModuleEntry
420
426
  entry.self_types.each do |module_self|
421
427
  ancestor = Definition::Ancestor::Instance.new(name: module_self.name, args: module_self.args)
422
428
  definition_pairs.push(
@@ -433,9 +439,6 @@ module RBS
433
439
  end
434
440
 
435
441
  merge_definitions(type_name, definition_pairs, entry: entry, self_type: self_type, ancestors: ancestors)
436
-
437
- else
438
- raise
439
442
  end
440
443
  end
441
444
  end
@@ -456,6 +459,7 @@ module RBS
456
459
  )
457
460
 
458
461
  definition_pairs = ancestors.ancestors.map do |ancestor|
462
+ # @type block: [Definition::Ancestor::t, Definition]
459
463
  case ancestor
460
464
  when Definition::Ancestor::Instance
461
465
  [ancestor, build_one_instance(ancestor.name)]
@@ -474,20 +478,18 @@ module RBS
474
478
  ancestor,
475
479
  definition
476
480
  ]
477
- else
478
- raise
479
481
  end
480
482
  end
481
483
 
482
484
  merge_definitions(type_name, definition_pairs, entry: entry, self_type: self_type, ancestors: ancestors)
483
- else
484
- raise
485
485
  end
486
486
  end
487
487
  end
488
488
 
489
489
  def method_definition_members(type_name, entry, kind:)
490
+ # @type var interface_methods: Hash[Symbol, [Definition::Method, AST::Members::t]]
490
491
  interface_methods = {}
492
+ # @type var methods: Hash[Symbol, Array[[AST::Members::MethodDefinition, Definition::accessibility]]]
491
493
  methods = {}
492
494
 
493
495
  entry.decls.each do |d|
@@ -534,11 +536,14 @@ module RBS
534
536
  end
535
537
  end
536
538
 
539
+ # @type var result: Hash[Symbol, member_detail]
537
540
  result = {}
538
541
 
539
542
  interface_methods.each do |name, pair|
540
543
  method_definition, _ = pair
541
- result[name] = [:public, method_definition]
544
+ # @type var detail: member_detail
545
+ detail = [:public, method_definition, nil, []]
546
+ result[name] = detail
542
547
  end
543
548
 
544
549
  methods.each do |method_name, array|
@@ -562,14 +567,14 @@ module RBS
562
567
  )
563
568
  end
564
569
 
565
- result[method_name] += array.map(&:first)
570
+ result[method_name][3].push(*array.map(&:first))
566
571
  else
567
572
  case
568
573
  when array.size == 1 && !array[0][0].overload?
569
574
  member, visibility = array[0]
570
- result[method_name] = [visibility, nil, member]
575
+ result[method_name] = [visibility, nil, member, []]
571
576
 
572
- when array.count {|pair| !pair[0].overload? } == 1
577
+ else
573
578
  visibilities = array.group_by {|pair| pair[1] }
574
579
 
575
580
  if visibilities.size > 1
@@ -582,15 +587,7 @@ module RBS
582
587
  end
583
588
 
584
589
  overloads, primary = array.map(&:first).partition(&:overload?)
585
- result[method_name] = [array[0][1], nil, *primary, *overloads]
586
-
587
- else
588
- raise InvalidOverloadMethodError.new(
589
- type_name: type_name,
590
- method_name: method_name,
591
- kind: :instance,
592
- members: array.map(&:first)
593
- )
590
+ result[method_name] = [array[0][1], nil, primary[0], overloads]
594
591
  end
595
592
  end
596
593
  end
@@ -609,7 +606,13 @@ module RBS
609
606
 
610
607
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
611
608
  method_definition_members(type_name, entry, kind: :instance).each do |method_name, array|
612
- visibility, method_def, *members = array
609
+ visibility, method_def, primary_member, overload_members = array
610
+
611
+ members = if primary_member
612
+ [primary_member, *overload_members]
613
+ else
614
+ overload_members
615
+ end
613
616
 
614
617
  m = if method_def
615
618
  Definition::Method.new(
@@ -778,9 +781,9 @@ module RBS
778
781
 
779
782
  errors = []
780
783
 
781
- if decl.is_a?(AST::Declarations::Class)
782
- if decl.super_class
783
- super_class = decl.super_class
784
+ case decl
785
+ when AST::Declarations::Class
786
+ if super_class = decl.super_class
784
787
  result = calculator.in_inherit(name: super_class.name, args: super_class.args, variables: param_names)
785
788
 
786
789
  validate_params_with type_params, result: result do |param|
@@ -791,6 +794,8 @@ module RBS
791
794
  end
792
795
  end
793
796
 
797
+ # @type var result: VarianceCalculator::Result
798
+
794
799
  decl.members.each do |member|
795
800
  case member
796
801
  when AST::Members::Include
@@ -838,7 +843,13 @@ module RBS
838
843
 
839
844
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
840
845
  method_definition_members(type_name, entry, kind: :singleton).each do |method_name, array|
841
- visibility, method_def, *members = array
846
+ visibility, method_def, primary_member, overload_members = array
847
+
848
+ members = if primary_member
849
+ [primary_member, *overload_members]
850
+ else
851
+ overload_members
852
+ end
842
853
 
843
854
  m = Definition::Method.new(
844
855
  super_method: nil,
@@ -890,7 +901,7 @@ module RBS
890
901
  end
891
902
 
892
903
  unless definition.methods.key?(:new)
893
- instance = build_one_instance(type_name)
904
+ instance = build_instance(type_name)
894
905
  initialize = instance.methods[:initialize]
895
906
 
896
907
  if initialize
@@ -934,7 +945,8 @@ module RBS
934
945
  implemented_in: nil
935
946
  )
936
947
  end,
937
- accessibility: :public
948
+ accessibility: :public,
949
+ annotations: [AST::Annotation.new(location: nil, string: "rbs:test:target")]
938
950
  )
939
951
  end
940
952
  end
@@ -964,7 +976,7 @@ module RBS
964
976
 
965
977
  def merge_definitions(type_name, pairs, entry:, self_type:, ancestors:)
966
978
  Definition.new(type_name: type_name, entry: entry, self_type: self_type, ancestors: ancestors).tap do |definition|
967
- pairs.reverse_each do |(ancestor, current_definition)|
979
+ pairs.reverse_each do |ancestor, current_definition|
968
980
  sub = case ancestor
969
981
  when Definition::Ancestor::Instance
970
982
  Substitution.build(current_definition.type_params, ancestor.args)
@@ -972,8 +984,16 @@ module RBS
972
984
  Substitution.build([], [])
973
985
  end
974
986
 
987
+ # @type var kind: method_kind
988
+ kind = case ancestor
989
+ when Definition::Ancestor::Instance
990
+ :instance
991
+ when Definition::Ancestor::Singleton
992
+ :singleton
993
+ end
994
+
975
995
  current_definition.methods.each do |name, method|
976
- merge_method definition.methods, name, method, sub
996
+ merge_method type_name, definition.methods, name, method, sub, kind: kind
977
997
  end
978
998
 
979
999
  current_definition.instance_variables.each do |name, variable|
@@ -997,18 +1017,25 @@ module RBS
997
1017
  )
998
1018
  end
999
1019
 
1000
- def merge_method(methods, name, method, sub)
1020
+ def merge_method(type_name, methods, name, method, sub, kind:)
1001
1021
  super_method = methods[name]
1002
1022
 
1023
+ defs = if method.defs.all? {|d| d.overload? }
1024
+ raise InvalidOverloadMethodError.new(type_name: type_name, method_name: name, kind: kind, members: method.members) unless super_method
1025
+ method.defs + super_method.defs
1026
+ else
1027
+ method.defs
1028
+ end
1029
+
1003
1030
  methods[name] = Definition::Method.new(
1004
1031
  super_method: super_method,
1005
1032
  accessibility: method.accessibility,
1006
- defs: sub.mapping.empty? ? method.defs : method.defs.map {|defn| defn.update(type: defn.type.sub(sub)) }
1033
+ defs: sub.mapping.empty? ? defs : defs.map {|defn| defn.update(type: defn.type.sub(sub)) }
1007
1034
  )
1008
1035
  end
1009
1036
 
1010
1037
  def try_cache(type_name, cache:)
1011
- cached = cache[type_name]
1038
+ cached = _ = cache[type_name]
1012
1039
 
1013
1040
  case cached
1014
1041
  when Definition
@@ -1020,9 +1047,11 @@ module RBS
1020
1047
  begin
1021
1048
  cache[type_name] = yield
1022
1049
  rescue => ex
1023
- cache[type_name] = nil
1050
+ cache.delete(type_name)
1024
1051
  raise ex
1025
1052
  end
1053
+ else
1054
+ raise
1026
1055
  end
1027
1056
  end
1028
1057
 
@@ -1060,7 +1089,9 @@ module RBS
1060
1089
  mixin = build_interface(member.name)
1061
1090
 
1062
1091
  args = member.args
1063
- type_params = mixin.entry.decl.type_params
1092
+ # @type var interface_entry: Environment::SingleEntry[TypeName, AST::Declarations::Interface]
1093
+ interface_entry = _ = mixin.entry
1094
+ type_params = interface_entry.decl.type_params
1064
1095
 
1065
1096
  InvalidTypeApplicationError.check!(
1066
1097
  type_name: type_name,