rbs 3.6.1 → 3.9.5

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 (268) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -2
  3. data/.github/workflows/dependabot.yml +1 -1
  4. data/.github/workflows/ruby.yml +34 -10
  5. data/.github/workflows/windows.yml +20 -3
  6. data/.gitignore +1 -0
  7. data/.rubocop.yml +26 -1
  8. data/CHANGELOG.md +241 -0
  9. data/Rakefile +54 -4
  10. data/config.yml +317 -0
  11. data/core/array.rbs +1756 -1591
  12. data/core/basic_object.rbs +38 -35
  13. data/core/comparable.rbs +1 -1
  14. data/core/complex.rbs +166 -94
  15. data/core/data.rbs +2 -2
  16. data/core/dir.rbs +2 -18
  17. data/core/encoding.rbs +12 -32
  18. data/core/enumerable.rbs +270 -266
  19. data/core/enumerator.rbs +14 -4
  20. data/core/env.rbs +1 -1
  21. data/core/errno.rbs +33 -16
  22. data/core/errors.rbs +6 -2
  23. data/core/exception.rbs +342 -167
  24. data/core/fiber.rbs +3 -2
  25. data/core/file.rbs +26 -75
  26. data/core/float.rbs +125 -72
  27. data/core/gc.rbs +158 -42
  28. data/core/hash.rbs +122 -143
  29. data/core/integer.rbs +79 -50
  30. data/core/io/buffer.rbs +49 -43
  31. data/core/io.rbs +108 -151
  32. data/core/kernel.rbs +341 -209
  33. data/core/match_data.rbs +76 -2
  34. data/core/math.rbs +0 -36
  35. data/core/method.rbs +2 -2
  36. data/core/module.rbs +32 -27
  37. data/core/nil_class.rbs +2 -2
  38. data/core/numeric.rbs +101 -104
  39. data/core/object.rbs +1 -5
  40. data/core/object_space/weak_key_map.rbs +3 -4
  41. data/core/object_space.rbs +3 -3
  42. data/core/proc.rbs +82 -14
  43. data/core/process.rbs +110 -58
  44. data/core/ractor.rbs +57 -4
  45. data/core/range.rbs +114 -87
  46. data/core/rational.rbs +0 -2
  47. data/core/rbs/unnamed/argf.rbs +237 -36
  48. data/core/rbs/unnamed/env_class.rbs +35 -53
  49. data/core/rbs/unnamed/random.rbs +1 -2
  50. data/core/regexp.rbs +10 -56
  51. data/core/ruby_vm.rbs +88 -9
  52. data/core/rubygems/config_file.rbs +3 -0
  53. data/core/rubygems/errors.rbs +3 -6
  54. data/core/rubygems/platform.rbs +0 -9
  55. data/core/rubygems/rubygems.rbs +3 -6
  56. data/core/rubygems/version.rbs +8 -8
  57. data/core/set.rbs +4 -16
  58. data/core/string.rbs +271 -264
  59. data/core/struct.rbs +6 -18
  60. data/core/symbol.rbs +14 -21
  61. data/core/thread.rbs +32 -35
  62. data/core/time.rbs +131 -50
  63. data/core/trace_point.rbs +124 -113
  64. data/core/true_class.rbs +0 -1
  65. data/core/unbound_method.rbs +1 -1
  66. data/core/warning.rbs +9 -2
  67. data/docs/architecture.md +1 -1
  68. data/docs/syntax.md +17 -10
  69. data/ext/rbs_extension/extconf.rb +11 -0
  70. data/ext/rbs_extension/location.c +61 -29
  71. data/ext/rbs_extension/location.h +4 -3
  72. data/ext/rbs_extension/main.c +23 -1
  73. data/ext/rbs_extension/parser.c +506 -517
  74. data/ext/rbs_extension/parserstate.c +109 -30
  75. data/ext/rbs_extension/parserstate.h +6 -4
  76. data/ext/rbs_extension/rbs_extension.h +1 -10
  77. data/{ext/rbs_extension → include/rbs}/constants.h +21 -19
  78. data/include/rbs/ruby_objs.h +72 -0
  79. data/include/rbs/util/rbs_constant_pool.h +219 -0
  80. data/include/rbs.h +7 -0
  81. data/lib/rbs/annotate/annotations.rb +3 -3
  82. data/lib/rbs/annotate/rdoc_source.rb +2 -2
  83. data/lib/rbs/ast/declarations.rb +9 -4
  84. data/lib/rbs/ast/directives.rb +10 -0
  85. data/lib/rbs/ast/members.rb +2 -0
  86. data/lib/rbs/ast/type_param.rb +2 -12
  87. data/lib/rbs/cli/diff.rb +3 -3
  88. data/lib/rbs/cli/validate.rb +2 -1
  89. data/lib/rbs/cli.rb +16 -16
  90. data/lib/rbs/collection/config/lockfile_generator.rb +58 -8
  91. data/lib/rbs/collection/config.rb +5 -3
  92. data/lib/rbs/collection/sources/rubygems.rb +1 -1
  93. data/lib/rbs/collection.rb +1 -0
  94. data/lib/rbs/definition.rb +51 -34
  95. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -3
  96. data/lib/rbs/definition_builder.rb +83 -24
  97. data/lib/rbs/environment.rb +33 -18
  98. data/lib/rbs/environment_loader.rb +6 -1
  99. data/lib/rbs/errors.rb +24 -0
  100. data/lib/rbs/locator.rb +2 -0
  101. data/lib/rbs/method_type.rb +2 -0
  102. data/lib/rbs/namespace.rb +1 -0
  103. data/lib/rbs/parser_aux.rb +40 -3
  104. data/lib/rbs/prototype/rb.rb +20 -12
  105. data/lib/rbs/prototype/rbi.rb +11 -6
  106. data/lib/rbs/prototype/runtime/value_object_generator.rb +7 -5
  107. data/lib/rbs/prototype/runtime.rb +7 -5
  108. data/lib/rbs/subtractor.rb +3 -3
  109. data/lib/rbs/test/hook.rb +47 -42
  110. data/lib/rbs/test/type_check.rb +7 -5
  111. data/lib/rbs/type_name.rb +14 -9
  112. data/lib/rbs/types.rb +63 -14
  113. data/lib/rbs/unit_test/spy.rb +4 -2
  114. data/lib/rbs/unit_test/type_assertions.rb +19 -13
  115. data/lib/rbs/unit_test/with_aliases.rb +3 -1
  116. data/lib/rbs/validator.rb +7 -1
  117. data/lib/rbs/version.rb +1 -1
  118. data/lib/rbs/writer.rb +10 -5
  119. data/lib/rbs.rb +1 -0
  120. data/lib/rdoc_plugin/parser.rb +2 -2
  121. data/rbs.gemspec +6 -2
  122. data/sig/ancestor_graph.rbs +5 -5
  123. data/sig/annotate/rdoc_source.rbs +2 -0
  124. data/sig/cli.rbs +2 -0
  125. data/sig/collection/config/lockfile_generator.rbs +9 -1
  126. data/sig/declarations.rbs +10 -3
  127. data/sig/definition.rbs +80 -12
  128. data/sig/definition_builder.rbs +18 -4
  129. data/sig/directives.rbs +17 -1
  130. data/sig/environment.rbs +3 -1
  131. data/sig/errors.rbs +19 -0
  132. data/sig/namespace.rbs +2 -3
  133. data/sig/parser.rbs +5 -1
  134. data/sig/prototype/rb.rbs +1 -1
  135. data/sig/resolver/constant_resolver.rbs +2 -2
  136. data/sig/resolver/context.rbs +1 -1
  137. data/sig/subtractor.rbs +1 -1
  138. data/sig/test/type_check.rbs +2 -2
  139. data/sig/type_alias_dependency.rbs +2 -2
  140. data/sig/type_alias_regularity.rbs +6 -6
  141. data/sig/type_param.rbs +4 -4
  142. data/sig/typename.rbs +8 -5
  143. data/sig/types.rbs +1 -1
  144. data/sig/unit_test/spy.rbs +2 -0
  145. data/sig/unit_test/type_assertions.rbs +2 -0
  146. data/sig/use_map.rbs +1 -1
  147. data/sig/validator.rbs +6 -2
  148. data/sig/vendorer.rbs +1 -1
  149. data/sig/writer.rbs +1 -1
  150. data/{ext/rbs_extension → src}/constants.c +35 -36
  151. data/src/ruby_objs.c +799 -0
  152. data/src/util/rbs_constant_pool.c +342 -0
  153. data/stdlib/base64/0/base64.rbs +0 -9
  154. data/stdlib/benchmark/0/benchmark.rbs +11 -2
  155. data/stdlib/bigdecimal/0/big_decimal.rbs +26 -182
  156. data/stdlib/cgi/0/core.rbs +60 -3
  157. data/stdlib/cgi/0/manifest.yaml +1 -0
  158. data/stdlib/coverage/0/coverage.rbs +0 -3
  159. data/stdlib/csv/0/csv.rbs +18 -58
  160. data/stdlib/csv/0/manifest.yaml +1 -0
  161. data/stdlib/date/0/date.rbs +27 -42
  162. data/stdlib/did_you_mean/0/did_you_mean.rbs +1 -6
  163. data/stdlib/digest/0/digest.rbs +25 -2
  164. data/stdlib/erb/0/erb.rbs +0 -1
  165. data/stdlib/etc/0/etc.rbs +51 -34
  166. data/stdlib/fileutils/0/fileutils.rbs +3 -44
  167. data/stdlib/io-console/0/io-console.rbs +69 -15
  168. data/stdlib/ipaddr/0/ipaddr.rbs +16 -4
  169. data/stdlib/json/0/json.rbs +107 -120
  170. data/stdlib/logger/0/log_device.rbs +1 -1
  171. data/stdlib/logger/0/logger.rbs +3 -18
  172. data/stdlib/minitest/0/kernel.rbs +2 -2
  173. data/stdlib/minitest/0/minitest/abstract_reporter.rbs +4 -1
  174. data/stdlib/minitest/0/minitest/assertion.rbs +1 -0
  175. data/stdlib/minitest/0/minitest/assertions.rbs +58 -13
  176. data/stdlib/minitest/0/minitest/backtrace_filter.rbs +7 -0
  177. data/stdlib/minitest/0/minitest/bench_spec.rbs +8 -8
  178. data/stdlib/minitest/0/minitest/benchmark.rbs +17 -16
  179. data/stdlib/minitest/0/minitest/compress.rbs +13 -0
  180. data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
  181. data/stdlib/minitest/0/minitest/mock.rbs +9 -5
  182. data/stdlib/minitest/0/minitest/parallel/executor.rbs +4 -0
  183. data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +0 -1
  184. data/stdlib/minitest/0/minitest/pride_io.rbs +8 -0
  185. data/stdlib/minitest/0/minitest/pride_lol.rbs +2 -0
  186. data/stdlib/minitest/0/minitest/progress_reporter.rbs +1 -1
  187. data/stdlib/minitest/0/minitest/reportable.rbs +2 -0
  188. data/stdlib/minitest/0/minitest/runnable.rbs +33 -1
  189. data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +1 -1
  190. data/stdlib/minitest/0/minitest/spec/dsl.rbs +10 -6
  191. data/stdlib/minitest/0/minitest/spec.rbs +1 -1
  192. data/stdlib/minitest/0/minitest/statistics_reporter.rbs +5 -0
  193. data/stdlib/minitest/0/minitest/summary_reporter.rbs +0 -7
  194. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +1 -1
  195. data/stdlib/minitest/0/minitest/test.rbs +7 -14
  196. data/stdlib/minitest/0/minitest/unexpected_error.rbs +2 -0
  197. data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
  198. data/stdlib/minitest/0/minitest/unit.rbs +1 -2
  199. data/stdlib/minitest/0/minitest.rbs +41 -892
  200. data/stdlib/monitor/0/monitor.rbs +13 -4
  201. data/stdlib/net-http/0/net-http.rbs +42 -109
  202. data/stdlib/nkf/0/nkf.rbs +30 -0
  203. data/stdlib/objspace/0/objspace.rbs +1 -2
  204. data/stdlib/observable/0/observable.rbs +1 -1
  205. data/stdlib/open-uri/0/manifest.yaml +1 -0
  206. data/stdlib/open-uri/0/open-uri.rbs +52 -0
  207. data/stdlib/open3/0/open3.rbs +0 -8
  208. data/stdlib/openssl/0/manifest.yaml +1 -0
  209. data/stdlib/openssl/0/openssl.rbs +235 -143
  210. data/stdlib/optparse/0/optparse.rbs +58 -18
  211. data/stdlib/pathname/0/pathname.rbs +2 -8
  212. data/stdlib/pp/0/pp.rbs +3 -1
  213. data/stdlib/prettyprint/0/prettyprint.rbs +0 -4
  214. data/stdlib/pstore/0/pstore.rbs +0 -6
  215. data/stdlib/psych/0/core_ext.rbs +12 -0
  216. data/stdlib/psych/0/psych.rbs +15 -4
  217. data/stdlib/pty/0/pty.rbs +46 -4
  218. data/stdlib/rdoc/0/code_object.rbs +0 -4
  219. data/stdlib/rdoc/0/markup.rbs +10 -12
  220. data/stdlib/rdoc/0/rdoc.rbs +13 -8
  221. data/stdlib/resolv/0/resolv.rbs +21 -12
  222. data/stdlib/ripper/0/ripper.rbs +0 -2
  223. data/stdlib/securerandom/0/securerandom.rbs +7 -2
  224. data/stdlib/shellwords/0/shellwords.rbs +11 -12
  225. data/stdlib/singleton/0/singleton.rbs +0 -1
  226. data/stdlib/socket/0/addrinfo.rbs +1 -2
  227. data/stdlib/socket/0/basic_socket.rbs +0 -5
  228. data/stdlib/socket/0/socket.rbs +32 -27
  229. data/stdlib/socket/0/tcp_server.rbs +0 -3
  230. data/stdlib/socket/0/tcp_socket.rbs +36 -3
  231. data/stdlib/socket/0/udp_socket.rbs +0 -1
  232. data/stdlib/socket/0/unix_server.rbs +0 -3
  233. data/stdlib/socket/0/unix_socket.rbs +4 -2
  234. data/{core/string_io.rbs → stdlib/stringio/0/stringio.rbs} +1 -1
  235. data/stdlib/strscan/0/string_scanner.rbs +1265 -422
  236. data/stdlib/tempfile/0/tempfile.rbs +135 -28
  237. data/stdlib/time/0/time.rbs +48 -35
  238. data/stdlib/timeout/0/timeout.rbs +11 -8
  239. data/stdlib/tmpdir/0/tmpdir.rbs +10 -3
  240. data/stdlib/tsort/0/tsort.rbs +0 -4
  241. data/stdlib/uri/0/common.rbs +28 -30
  242. data/stdlib/uri/0/ftp.rbs +1 -1
  243. data/stdlib/uri/0/generic.rbs +22 -18
  244. data/stdlib/uri/0/http.rbs +2 -2
  245. data/stdlib/uri/0/rfc2396_parser.rbs +3 -0
  246. data/stdlib/zlib/0/buf_error.rbs +1 -70
  247. data/stdlib/zlib/0/data_error.rbs +1 -70
  248. data/stdlib/zlib/0/deflate.rbs +8 -72
  249. data/stdlib/zlib/0/error.rbs +1 -70
  250. data/stdlib/zlib/0/gzip_file/crc_error.rbs +2 -105
  251. data/stdlib/zlib/0/gzip_file/error.rbs +2 -105
  252. data/stdlib/zlib/0/gzip_file/length_error.rbs +2 -105
  253. data/stdlib/zlib/0/gzip_file/no_footer.rbs +2 -105
  254. data/stdlib/zlib/0/gzip_file.rbs +1 -71
  255. data/stdlib/zlib/0/gzip_reader.rbs +3 -74
  256. data/stdlib/zlib/0/gzip_writer.rbs +1 -70
  257. data/stdlib/zlib/0/inflate.rbs +4 -71
  258. data/stdlib/zlib/0/mem_error.rbs +1 -70
  259. data/stdlib/zlib/0/need_dict.rbs +1 -70
  260. data/stdlib/zlib/0/stream_end.rbs +1 -70
  261. data/stdlib/zlib/0/stream_error.rbs +1 -70
  262. data/stdlib/zlib/0/version_error.rbs +1 -70
  263. data/stdlib/zlib/0/zlib.rbs +0 -2
  264. data/stdlib/zlib/0/zstream.rbs +4 -72
  265. metadata +17 -13
  266. data/ext/rbs_extension/ruby_objs.c +0 -602
  267. data/ext/rbs_extension/ruby_objs.h +0 -51
  268. data/stdlib/minitest/0/manifest.yaml +0 -2
@@ -48,7 +48,8 @@ module RBS
48
48
 
49
49
  def mixin_decls(type_name)
50
50
  type_name_absolute = type_name.absolute!
51
- (@mixin_decls_cache ||= {}).fetch(type_name_absolute) do
51
+ @mixin_decls_cache ||= {} #: Hash[TypeName, Array[AST::Members::Mixin]]
52
+ @mixin_decls_cache.fetch(type_name_absolute) do
52
53
  @mixin_decls_cache[type_name_absolute] = @builder.env.class_decls[type_name_absolute].decls.flat_map do |d|
53
54
  d.decl.members.select { |m| m.kind_of?(AST::Members::Mixin) }
54
55
  end
@@ -249,6 +250,8 @@ module RBS
249
250
  method = builder.build_singleton(module_name.absolute!).methods[singleton]
250
251
  method_name = singleton
251
252
  kind = :singleton
253
+ else
254
+ raise
252
255
  end
253
256
 
254
257
  if method
@@ -448,9 +451,9 @@ module RBS
448
451
  location: nil
449
452
  )
450
453
  when ARGF
451
- Types::ClassInstance.new(name: TypeName("::RBS::Unnamed::ARGFClass"), args: [], location: nil)
454
+ Types::ClassInstance.new(name: TypeName.parse("::RBS::Unnamed::ARGFClass"), args: [], location: nil)
452
455
  when ENV
453
- Types::ClassInstance.new(name: TypeName("::RBS::Unnamed::ENVClass"), args: [], location: nil)
456
+ Types::ClassInstance.new(name: TypeName.parse("::RBS::Unnamed::ENVClass"), args: [], location: nil)
454
457
  else
455
458
  value_type_name = to_type_name(const_name!(Reflection.object_class(value)), full_name: true).absolute!
456
459
  args = type_args(value_type_name)
@@ -461,6 +464,7 @@ module RBS
461
464
  name: to_type_name(name.to_s),
462
465
  type: type,
463
466
  location: nil,
467
+ annotations: [],
464
468
  comment: nil
465
469
  )
466
470
  end
@@ -648,8 +652,6 @@ module RBS
648
652
  end
649
653
 
650
654
  def block_from_ast_of(method)
651
- return nil if RUBY_VERSION < '3.1'
652
-
653
655
  begin
654
656
  ast = RubyVM::AbstractSyntaxTree.of(method)
655
657
  rescue ArgumentError
@@ -40,7 +40,7 @@ module RBS
40
40
  name = absolute_typename(decl.new_name, context: context)
41
41
  decl unless @subtrahend.module_alias?(name) || @subtrahend.module_decl?(name)
42
42
  else
43
- raise "unknwon decl: #{(_ = decl).class}"
43
+ raise "unknown decl: #{(_ = decl).class}"
44
44
  end
45
45
  end.compact
46
46
  end
@@ -51,7 +51,7 @@ module RBS
51
51
  context = _ = [context, decl.name]
52
52
  children = call(decl.each_decl.to_a, context: context) +
53
53
  decl.each_member.reject { |m| member_exist?(owner, m, context: context) }
54
- children = filter_redundunt_access_modifiers(children)
54
+ children = filter_redundant_access_modifiers(children)
55
55
  return nil if children.empty?
56
56
 
57
57
  update_decl(decl, members: children)
@@ -145,7 +145,7 @@ module RBS
145
145
  end
146
146
  end
147
147
 
148
- private def filter_redundunt_access_modifiers(decls)
148
+ private def filter_redundant_access_modifiers(decls)
149
149
  decls = decls.dup
150
150
  decls.pop while access_modifier?(decls.last)
151
151
  decls = decls.map.with_index do |decl, i|
data/lib/rbs/test/hook.rb CHANGED
@@ -27,8 +27,8 @@ module RBS
27
27
  :! => "not",
28
28
  :<< => "lshift",
29
29
  :>> => "rshift",
30
- :~ => "tilda",
31
- :=~ => "eqtilda",
30
+ :~ => "tilde",
31
+ :=~ => "eqtilde",
32
32
  :% => "percent",
33
33
  :+@ => "unary_plus",
34
34
  :-@ => "unary_minus"
@@ -96,50 +96,55 @@ def #{with_name}(#{param_source.join(", ")}, &#{block_param})
96
96
  block_calls = []
97
97
 
98
98
  if block_given?
99
- receiver = self
100
99
  block_receives_block = #{block_param}.parameters.last&.yield_self {|type, _| type == :block }
101
100
 
102
- wrapped_block = proc do |*block_args, &block2|
103
- return_from_block = false
104
-
105
- begin
106
- block_result = if receiver.equal?(self)
107
- if block_receives_block
108
- #{block_param}.call(*block_args, &block2)
109
- else
110
- yield(*block_args)
111
- end
112
- else
113
- instance_exec(*block_args, &#{block_param})
114
- end
115
-
116
- return_from_block = true
117
- ensure
118
- exn = $!
119
-
120
- case
121
- when return_from_block
122
- # Returned from yield
123
- block_calls << ::RBS::Test::ArgumentsReturn.return(
124
- arguments: block_args,
125
- value: block_result
126
- )
127
- when exn
128
- # Exception
129
- block_calls << ::RBS::Test::ArgumentsReturn.exception(
130
- arguments: block_args,
131
- exception: exn
132
- )
133
- else
134
- # break?
135
- block_calls << ::RBS::Test::ArgumentsReturn.break(
136
- arguments: block_args
137
- )
101
+ wrapped_block = Object.new.instance_exec do
102
+ new_object = self
103
+
104
+ proc do |*block_args, &block2|
105
+ return_from_block = false
106
+
107
+ begin
108
+ block_result = if self.equal?(new_object)
109
+ if block_receives_block
110
+ #{block_param}.call(*block_args, &block2)
111
+ else
112
+ yield(*block_args)
113
+ end
114
+ else
115
+ # Detected that `self` inside `proc` is not equal to the `self` outside block (which is saved to `new_object`).
116
+ # This means that the block is called with `instance_exec` or `instance_eval`.
117
+ self.instance_exec(*block_args, &#{block_param})
118
+ end
119
+
120
+ return_from_block = true
121
+ ensure
122
+ exn = $!
123
+
124
+ case
125
+ when return_from_block
126
+ # Returned from yield
127
+ block_calls << ::RBS::Test::ArgumentsReturn.return(
128
+ arguments: block_args,
129
+ value: block_result
130
+ )
131
+ when exn
132
+ # Exception
133
+ block_calls << ::RBS::Test::ArgumentsReturn.exception(
134
+ arguments: block_args,
135
+ exception: exn
136
+ )
137
+ else
138
+ # break?
139
+ block_calls << ::RBS::Test::ArgumentsReturn.break(
140
+ arguments: block_args
141
+ )
142
+ end
138
143
  end
139
- end
140
144
 
141
- block_result
142
- end.ruby2_keywords
145
+ block_result
146
+ end.ruby2_keywords
147
+ end
143
148
 
144
149
  result = __send__(:"#{without_name}", *args, &wrapped_block)
145
150
  else
@@ -22,8 +22,8 @@ module RBS
22
22
  end
23
23
 
24
24
  def overloaded_call(method, method_name, call, errors:)
25
- es = method.method_types.map do |method_type|
26
- es = method_call(method_name, method_type, call, errors: [])
25
+ es = method.defs.map do |type_def|
26
+ es = method_call(method_name, type_def.type, call, errors: [], annotations: type_def.annotations)
27
27
 
28
28
  if es.empty?
29
29
  return errors
@@ -58,11 +58,11 @@ module RBS
58
58
  errors
59
59
  end
60
60
 
61
- def method_call(method_name, method_type, call, errors:)
61
+ def method_call(method_name, method_type, call, errors:, annotations: [])
62
62
  return errors if method_type.type.is_a?(Types::UntypedFunction)
63
63
 
64
64
  args(method_name, method_type, method_type.type, call.method_call, errors, type_error: Errors::ArgumentTypeError, argument_error: Errors::ArgumentError)
65
- self.return(method_name, method_type, method_type.type, call.method_call, errors, return_error: Errors::ReturnTypeError)
65
+ self.return(method_name, method_type, method_type.type, call.method_call, errors, return_error: Errors::ReturnTypeError, annotations:)
66
66
 
67
67
  if method_type.block
68
68
  case
@@ -106,8 +106,10 @@ module RBS
106
106
  end
107
107
  end
108
108
 
109
- def return(method_name, method_type, fun, call, errors, return_error:)
109
+ def return(method_name, method_type, fun, call, errors, return_error:, annotations: [])
110
110
  if call.return?
111
+ return if Test.call(call.return_value, IS_AP, NilClass) && annotations.find { |a| a.string == "implicitly-returns-nil" }
112
+
111
113
  unless value(call.return_value, fun.return_type)
112
114
  errors << return_error.new(klass: self_class,
113
115
  method_name: method_name,
data/lib/rbs/type_name.rb CHANGED
@@ -86,19 +86,24 @@ module RBS
86
86
  )
87
87
  end
88
88
  end
89
+
90
+ def self.parse(string)
91
+ absolute = string.start_with?("::")
92
+
93
+ *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
94
+ raise unless name
95
+
96
+ TypeName.new(
97
+ name: name,
98
+ namespace: RBS::Namespace.new(path: path, absolute: absolute)
99
+ )
100
+ end
89
101
  end
90
102
  end
91
103
 
92
104
  module Kernel
93
105
  def TypeName(string)
94
- absolute = string.start_with?("::")
95
-
96
- *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
97
- raise unless name
98
-
99
- RBS::TypeName.new(
100
- name: name,
101
- namespace: RBS::Namespace.new(path: path, absolute: absolute)
102
- )
106
+ warn "Kernel#TypeName() is deprecated. Use RBS::TypeName.parse instead.", category: :deprecated
107
+ RBS::TypeName.parse(string)
103
108
  end
104
109
  end
data/lib/rbs/types.rb CHANGED
@@ -15,7 +15,7 @@ module RBS
15
15
  end
16
16
 
17
17
  module NoTypeName
18
- def map_type_name
18
+ def map_type_name(&)
19
19
  self
20
20
  end
21
21
  end
@@ -107,13 +107,15 @@ module RBS
107
107
  class Bool < Base; end
108
108
  class Void < Base; end
109
109
  class Any < Base
110
- def to_s(level=0)
111
- @string || "untyped"
110
+ def initialize(location:, todo: false)
111
+ super(location: location)
112
+ if todo
113
+ @string = "__todo__"
114
+ end
112
115
  end
113
116
 
114
- def todo!
115
- @string = '__todo__'
116
- self
117
+ def to_s(level=0)
118
+ @string || "untyped"
117
119
  end
118
120
  end
119
121
  class Nil < Base; end
@@ -229,7 +231,7 @@ module RBS
229
231
 
230
232
  include EmptyEachType
231
233
 
232
- def map_type_name
234
+ def map_type_name(&)
233
235
  ClassSingleton.new(
234
236
  name: yield(name, location, self),
235
237
  location: location
@@ -323,6 +325,8 @@ module RBS
323
325
  end
324
326
 
325
327
  def sub(s)
328
+ return self if s.empty?
329
+
326
330
  self.class.new(name: name,
327
331
  args: args.map {|ty| ty.sub(s) },
328
332
  location: location)
@@ -365,6 +369,8 @@ module RBS
365
369
  end
366
370
 
367
371
  def sub(s)
372
+ return self if s.empty?
373
+
368
374
  self.class.new(name: name,
369
375
  args: args.map {|ty| ty.sub(s) },
370
376
  location: location)
@@ -407,6 +413,8 @@ module RBS
407
413
  end
408
414
 
409
415
  def sub(s)
416
+ return self if s.empty?
417
+
410
418
  Alias.new(name: name, args: args.map {|ty| ty.sub(s) }, location: location)
411
419
  end
412
420
 
@@ -463,6 +471,8 @@ module RBS
463
471
  end
464
472
 
465
473
  def sub(s)
474
+ return self if s.empty?
475
+
466
476
  self.class.new(types: types.map {|ty| ty.sub(s) },
467
477
  location: location)
468
478
  end
@@ -521,15 +531,22 @@ module RBS
521
531
  def initialize(all_fields: nil, fields: nil, location:)
522
532
  case
523
533
  when fields && all_fields.nil?
524
- @all_fields = fields.map { |k, v| [k, [v, true]] }.to_h
534
+ @all_fields = fields.transform_values { |v| [v, true] }
525
535
  @fields = fields
526
536
  @optional_fields = {}
527
537
  when all_fields && fields.nil?
528
538
  @all_fields = all_fields
529
- @fields = all_fields.filter_map { |k, (v, required)| [k, v] if required }.to_h
530
- @optional_fields = all_fields.filter_map { |k, (v, required)| [k, v] unless required }.to_h
539
+ @fields = {}
540
+ @optional_fields = {}
541
+ all_fields.each do |(k, (v, required))|
542
+ if required
543
+ @fields[k] = v
544
+ else
545
+ @optional_fields[k] = v
546
+ end
547
+ end
531
548
  else
532
- raise ArgumentError, "only one of `:fields` or `:all_fields` is requireds"
549
+ raise ArgumentError, "only one of `:fields` or `:all_fields` is required"
533
550
  end
534
551
 
535
552
  @location = location
@@ -561,6 +578,8 @@ module RBS
561
578
  end
562
579
 
563
580
  def sub(s)
581
+ return self if s.empty?
582
+
564
583
  self.class.new(
565
584
  all_fields: all_fields.transform_values {|ty, required| [ty.sub(s), required] },
566
585
  location: location
@@ -651,6 +670,8 @@ module RBS
651
670
  end
652
671
 
653
672
  def sub(s)
673
+ return self if s.empty?
674
+
654
675
  self.class.new(type: type.sub(s), location: location)
655
676
  end
656
677
 
@@ -739,15 +760,26 @@ module RBS
739
760
  end
740
761
 
741
762
  def sub(s)
763
+ return self if s.empty?
764
+
742
765
  self.class.new(types: types.map {|ty| ty.sub(s) },
743
766
  location: location)
744
767
  end
745
768
 
746
769
  def to_s(level = 0)
770
+ strs = types.map do |ty|
771
+ case ty
772
+ when Intersection
773
+ ty.to_s([1, level].max)
774
+ else
775
+ ty.to_s
776
+ end
777
+ end
778
+
747
779
  if level > 0
748
- "(#{types.join(" | ")})"
780
+ "(#{strs.join(" | ")})"
749
781
  else
750
- types.join(" | ")
782
+ strs.join(" | ")
751
783
  end
752
784
  end
753
785
 
@@ -819,6 +851,8 @@ module RBS
819
851
  end
820
852
 
821
853
  def sub(s)
854
+ return self if s.empty?
855
+
822
856
  self.class.new(types: types.map {|ty| ty.sub(s) },
823
857
  location: location)
824
858
  end
@@ -1071,6 +1105,8 @@ module RBS
1071
1105
  end
1072
1106
 
1073
1107
  def sub(s)
1108
+ return self if s.empty?
1109
+
1074
1110
  map_type {|ty| ty.sub(s) }
1075
1111
  end
1076
1112
 
@@ -1221,6 +1257,12 @@ module RBS
1221
1257
  end
1222
1258
  end
1223
1259
 
1260
+ def map_type_name(&block)
1261
+ UntypedFunction.new(
1262
+ return_type: return_type.map_type_name(&block)
1263
+ )
1264
+ end
1265
+
1224
1266
  def each_type(&block)
1225
1267
  if block
1226
1268
  yield return_type
@@ -1244,6 +1286,8 @@ module RBS
1244
1286
  end
1245
1287
 
1246
1288
  def sub(subst)
1289
+ return self if subst.empty?
1290
+
1247
1291
  map_type { _1.sub(subst) }
1248
1292
  end
1249
1293
 
@@ -1318,6 +1362,8 @@ module RBS
1318
1362
  end
1319
1363
 
1320
1364
  def sub(s)
1365
+ return self if s.empty?
1366
+
1321
1367
  self.class.new(
1322
1368
  type: type.sub(s),
1323
1369
  required: required,
@@ -1387,6 +1433,8 @@ module RBS
1387
1433
  end
1388
1434
 
1389
1435
  def sub(s)
1436
+ return self if s.empty?
1437
+
1390
1438
  self.class.new(
1391
1439
  type: type.sub(s),
1392
1440
  block: block&.sub(s),
@@ -1414,6 +1462,7 @@ module RBS
1414
1462
  def each_type(&block)
1415
1463
  if block
1416
1464
  type.each_type(&block)
1465
+ yield self_type if self_type
1417
1466
  self.block&.type&.each_type(&block)
1418
1467
  if self_type = self.block&.self_type
1419
1468
  yield self_type
@@ -1454,7 +1503,7 @@ module RBS
1454
1503
  end
1455
1504
 
1456
1505
  def with_nonreturn_void?
1457
- if type.with_nonreturn_void?
1506
+ if type.with_nonreturn_void? || self_type&.with_nonreturn_void?
1458
1507
  true
1459
1508
  else
1460
1509
  if block = block()
@@ -20,6 +20,8 @@ module RBS
20
20
  attr_reader :object
21
21
  attr_reader :method_name
22
22
 
23
+ NO_RETURN = Object.new
24
+
23
25
  def initialize(object:, method_name:)
24
26
  @callback = -> (_) { }
25
27
  @object = object
@@ -39,7 +41,7 @@ module RBS
39
41
  define_method(
40
42
  spy.method_name,
41
43
  _ = -> (*args, &block) do
42
- return_value = nil
44
+ return_value = NO_RETURN
43
45
  exception = nil
44
46
  block_calls = [] #: Array[Test::ArgumentsReturn]
45
47
 
@@ -105,7 +107,7 @@ module RBS
105
107
  arguments: args,
106
108
  exception: exception
107
109
  )
108
- when return_value
110
+ when ::RBS::UnitTest::Spy::WrapSpy::NO_RETURN != return_value
109
111
  Test::ArgumentsReturn.return(
110
112
  arguments: args,
111
113
  value: return_value
@@ -15,7 +15,7 @@ module RBS
15
15
  @target = nil
16
16
  end
17
17
 
18
- @@env_cache = {}
18
+ @@env_cache = {} #: Hash[Array[String], RBS::Environment]
19
19
 
20
20
  def env
21
21
  @env = @@env_cache[@libs] ||=
@@ -163,7 +163,7 @@ module RBS
163
163
  end
164
164
  end
165
165
 
166
- last_trace = trace.last or raise
166
+ last_trace = trace.last or raise "empty trace"
167
167
 
168
168
  yield(mt, last_trace, result, exception)
169
169
  end
@@ -182,9 +182,9 @@ module RBS
182
182
 
183
183
  assert_empty errors.map {|x| RBS::Test::Errors.to_string(x) }, "Call trace does not match with given method type: #{trace.inspect}"
184
184
 
185
- method_types = method_types(method)
186
- all_errors = method_types.map {|t| typecheck.method_call(method, t, trace, errors: []) }
187
- assert all_errors.any? {|es| es.empty? }, "Call trace does not match one of method definitions:\n #{trace.inspect}\n #{method_types.join(" | ")}"
185
+ method_defs = method_defs(method)
186
+ all_errors = method_defs.map {|t| typecheck.method_call(method, t.type, trace, errors: [], annotations: t.each_annotation.to_a) }
187
+ assert all_errors.any? {|es| es.empty? }, "Call trace does not match one of method definitions:\n #{trace.inspect}\n #{method_defs.map(&:type).join(" | ")}"
188
188
 
189
189
  raise exception if exception
190
190
 
@@ -219,30 +219,36 @@ module RBS
219
219
  assert_operator exception, :is_a?, ::Exception
220
220
  assert_empty errors.map {|x| RBS::Test::Errors.to_string(x) }
221
221
 
222
- method_types = method_types(method)
223
- all_errors = method_types.map {|t| typecheck.method_call(method, t, trace, errors: []) }
224
- assert all_errors.all? {|es| es.size > 0 }, "Call trace unexpectedly matches one of method definitions:\n #{trace.inspect}\n #{method_types.join(" | ")}"
222
+ method_defs = method_defs(method)
223
+ all_errors = method_defs.map {|t| typecheck.method_call(method, t.type, trace, errors: [], annotations: t.each_annotation.to_a) }
224
+ assert all_errors.all? {|es| es.size > 0 }, "Call trace unexpectedly matches one of method definitions:\n #{trace.inspect}\n #{method_defs.map(&:type).join(" | ")}"
225
225
 
226
226
  result
227
227
  end
228
228
  end
229
229
 
230
- def method_types(method)
230
+ def method_defs(method)
231
231
  type, definition = target
232
232
 
233
233
  case type
234
234
  when Types::ClassInstance
235
235
  subst = RBS::Substitution.build(definition.type_params, type.args)
236
- definition.methods[method].method_types.map do |method_type|
237
- method_type.sub(subst)
236
+ definition.methods[method].defs.map do |type_def|
237
+ type_def.update(
238
+ type: type_def.type.sub(subst)
239
+ )
238
240
  end
239
241
  when Types::ClassSingleton
240
- definition.methods[method].method_types
242
+ definition.methods[method].defs
241
243
  else
242
244
  raise
243
245
  end
244
246
  end
245
247
 
248
+ def method_types(method)
249
+ method_defs(method).map(&:type)
250
+ end
251
+
246
252
  def allows_error(*errors)
247
253
  yield
248
254
  rescue *errors => exn
@@ -271,7 +277,7 @@ module RBS
271
277
 
272
278
  assert typecheck.value(constant, value_type), "`#{constant_name}` (#{constant.inspect}) must be compatible with given type `#{value_type}`"
273
279
 
274
- type_name = TypeName(constant_name).absolute!
280
+ type_name = TypeName.parse(constant_name).absolute!
275
281
  definition = env.constant_entry(type_name)
276
282
  assert definition, "Cannot find RBS type definition of `#{constant_name}`"
277
283
 
@@ -127,7 +127,9 @@ module RBS
127
127
 
128
128
  # `Range` requires `begin <=> end` to return non-nil, but doesn't actually
129
129
  # end up using the return value of it. This is to add that in when needed.
130
- def lower.<=>(rhs) = :not_nil unless defined? lower.<=>
130
+ unless defined? lower.<=>
131
+ def lower.<=>(rhs) = :not_nil # steep:ignore MethodDefinitionInUndeclaredModule
132
+ end
131
133
 
132
134
  # If `lower <=> rhs` is defined but nil, then that means we're going to be constructing
133
135
  # an illegal range (eg `3..ToInt.new(4)`). So, we need to skip yielding an invalid range
data/lib/rbs/validator.rb CHANGED
@@ -6,13 +6,15 @@ module RBS
6
6
  attr_reader :resolver
7
7
  attr_reader :definition_builder
8
8
 
9
- def initialize(env:, resolver:)
9
+ def initialize(env:, resolver: nil)
10
10
  @env = env
11
11
  @resolver = resolver
12
12
  @definition_builder = DefinitionBuilder.new(env: env)
13
13
  end
14
14
 
15
15
  def absolute_type(type, context:, &block)
16
+ return type unless resolver
17
+
16
18
  type.map_type_name do |type_name, _, type|
17
19
  resolver.resolve(type_name, context: context) || (block ? yield(type) : type_name)
18
20
  end
@@ -149,6 +151,10 @@ module RBS
149
151
  end
150
152
  end
151
153
 
154
+ def validate_variable(var)
155
+ validate_type(var.type, context: nil)
156
+ end
157
+
152
158
  def validate_class_alias(entry:)
153
159
  case env.normalize_module_name?(entry.decl.new_name)
154
160
  when nil
data/lib/rbs/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RBS
4
- VERSION = "3.6.1"
4
+ VERSION = "3.9.5"
5
5
  end
data/lib/rbs/writer.rb CHANGED
@@ -77,14 +77,19 @@ module RBS
77
77
  end
78
78
 
79
79
  def write(contents)
80
- dirs = contents.select {|c| c.is_a?(AST::Directives::Base) } #: Array[AST::Directives::t]
80
+ resolves = contents.select { _1.is_a?(AST::Directives::ResolveTypeNames) } #: Array[AST::Directives::ResolveTypeNames]
81
+ uses = contents.select {|c| c.is_a?(AST::Directives::Use) } #: Array[AST::Directives::Use]
81
82
  decls = contents.select {|c| c.is_a?(AST::Declarations::Base) } #: Array[AST::Declarations::t]
82
83
 
83
- dirs.each do |dir|
84
- write_directive(dir)
84
+ if first_resolves = resolves.first
85
+ puts "# resolve-type-names: #{first_resolves.value}"
86
+ puts
85
87
  end
86
88
 
87
- puts unless dirs.empty?
89
+ uses.each do |dir|
90
+ write_use_directive(dir)
91
+ end
92
+ puts unless uses.empty?
88
93
 
89
94
  [nil, *decls].each_cons(2) do |prev, decl|
90
95
  raise unless decl
@@ -94,7 +99,7 @@ module RBS
94
99
  end
95
100
  end
96
101
 
97
- def write_directive(dir)
102
+ def write_use_directive(dir)
98
103
  clauses = dir.clauses.map do |clause|
99
104
  case clause
100
105
  when AST::Directives::Use::SingleClause
data/lib/rbs.rb CHANGED
@@ -9,6 +9,7 @@ require "pp"
9
9
  require "ripper"
10
10
  require "logger"
11
11
  require "tsort"
12
+ require "strscan"
12
13
 
13
14
  require "rbs/errors"
14
15
  require "rbs/buffer"