rbs 3.8.1 → 3.9.0.dev.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.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependabot.yml +1 -1
  3. data/.github/workflows/typecheck.yml +2 -0
  4. data/.github/workflows/windows.yml +15 -0
  5. data/.rubocop.yml +13 -1
  6. data/Rakefile +5 -2
  7. data/config.yml +6 -0
  8. data/core/data.rbs +1 -1
  9. data/core/enumerator.rbs +14 -2
  10. data/core/exception.rbs +1 -1
  11. data/core/gc.rbs +1 -1
  12. data/core/io.rbs +7 -3
  13. data/core/kernel.rbs +2 -2
  14. data/core/method.rbs +2 -2
  15. data/core/module.rbs +3 -3
  16. data/core/proc.rbs +2 -2
  17. data/core/rbs/unnamed/argf.rbs +3 -3
  18. data/core/rubygems/version.rbs +2 -2
  19. data/core/string.rbs +1 -1
  20. data/core/unbound_method.rbs +1 -1
  21. data/docs/syntax.md +10 -5
  22. data/ext/rbs_extension/extconf.rb +2 -1
  23. data/ext/rbs_extension/location.c +32 -10
  24. data/ext/rbs_extension/location.h +4 -3
  25. data/ext/rbs_extension/main.c +22 -1
  26. data/ext/rbs_extension/parser.c +144 -136
  27. data/ext/rbs_extension/parserstate.c +40 -9
  28. data/ext/rbs_extension/parserstate.h +6 -4
  29. data/include/rbs/ruby_objs.h +6 -6
  30. data/include/rbs/util/rbs_constant_pool.h +219 -0
  31. data/lib/rbs/ast/declarations.rb +9 -4
  32. data/lib/rbs/ast/directives.rb +10 -0
  33. data/lib/rbs/ast/members.rb +2 -0
  34. data/lib/rbs/ast/type_param.rb +2 -2
  35. data/lib/rbs/cli/validate.rb +1 -0
  36. data/lib/rbs/cli.rb +3 -3
  37. data/lib/rbs/collection/config/lockfile_generator.rb +28 -7
  38. data/lib/rbs/collection/sources/rubygems.rb +1 -1
  39. data/lib/rbs/definition.rb +46 -31
  40. data/lib/rbs/definition_builder/ancestor_builder.rb +2 -0
  41. data/lib/rbs/definition_builder.rb +86 -30
  42. data/lib/rbs/environment.rb +33 -18
  43. data/lib/rbs/errors.rb +23 -0
  44. data/lib/rbs/locator.rb +2 -0
  45. data/lib/rbs/method_type.rb +2 -0
  46. data/lib/rbs/parser_aux.rb +38 -1
  47. data/lib/rbs/subtractor.rb +3 -3
  48. data/lib/rbs/test/hook.rb +2 -2
  49. data/lib/rbs/test/type_check.rb +7 -5
  50. data/lib/rbs/types.rb +44 -5
  51. data/lib/rbs/unit_test/spy.rb +4 -2
  52. data/lib/rbs/unit_test/type_assertions.rb +17 -11
  53. data/lib/rbs/validator.rb +4 -0
  54. data/lib/rbs/version.rb +1 -1
  55. data/lib/rbs/writer.rb +10 -5
  56. data/lib/rbs.rb +1 -0
  57. data/rbs.gemspec +1 -1
  58. data/sig/collection/config/lockfile_generator.rbs +1 -1
  59. data/sig/declarations.rbs +10 -3
  60. data/sig/definition.rbs +67 -14
  61. data/sig/definition_builder.rbs +17 -3
  62. data/sig/directives.rbs +17 -1
  63. data/sig/environment.rbs +2 -0
  64. data/sig/errors.rbs +16 -0
  65. data/sig/parser.rbs +5 -1
  66. data/sig/subtractor.rbs +1 -1
  67. data/sig/test/type_check.rbs +2 -2
  68. data/sig/type_param.rbs +1 -1
  69. data/sig/types.rbs +3 -0
  70. data/sig/unit_test/spy.rbs +2 -0
  71. data/sig/unit_test/type_assertions.rbs +2 -0
  72. data/sig/validator.rbs +4 -0
  73. data/sig/writer.rbs +1 -1
  74. data/src/ruby_objs.c +12 -6
  75. data/src/util/rbs_constant_pool.c +342 -0
  76. data/stdlib/cgi/0/core.rbs +10 -0
  77. data/stdlib/json/0/json.rbs +52 -50
  78. data/stdlib/net-http/0/net-http.rbs +2 -2
  79. data/stdlib/openssl/0/openssl.rbs +73 -73
  80. data/stdlib/resolv/0/resolv.rbs +8 -8
  81. data/stdlib/socket/0/addrinfo.rbs +1 -1
  82. data/stdlib/socket/0/unix_socket.rbs +4 -2
  83. data/stdlib/stringio/0/stringio.rbs +1 -1
  84. data/stdlib/uri/0/common.rbs +17 -0
  85. metadata +4 -7
  86. data/templates/include/rbs/constants.h.erb +0 -20
  87. data/templates/include/rbs/ruby_objs.h.erb +0 -10
  88. data/templates/src/constants.c.erb +0 -36
  89. data/templates/src/ruby_objs.c.erb +0 -27
  90. data/templates/template.rb +0 -122
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a48774543c23555bbcb9c6a13b387db3069fe4c65d534a7915798e85d37fa770
4
- data.tar.gz: 034e53e2be22a3ca9ac15b8f95585c8fe42bf2267268f1eb23cffe2e92098213
3
+ metadata.gz: 311aed14b7b84b6b21cb187b72c1ddd87a427b6ec02805017f83b5fbcbfaac7b
4
+ data.tar.gz: 4fc2e119368352b2fbded9355834757399cde34f3912c24ae82e91db67042762
5
5
  SHA512:
6
- metadata.gz: 31c29db0f7d38c22d3ab9c3ee5df5403bba9b489b38211fdd824b76f948e89efafd4b2e3afe81ec9420b3a76c350daccb5b23dddd57d85199a93531fd28e73b3
7
- data.tar.gz: d8077101a4f4f26073d047ad4c7b04752f7e1849a1b3bdcfc41703e82b6b109d68e8c295ed2a0af57fc5e5744600ad89b77f24b1b93fbee226b42bd5d86493ee
6
+ metadata.gz: 6c9b1a6188ac4585b3ea5b9dd72432f12ea13ee4c8311a15a86f0cee5595acef46901e04105e40a84703ea51e036efb645a53cacaa1517d213420b2251f3f506
7
+ data.tar.gz: 4061149d53166bbe1a6029cebbacc54117eccf5d414c1859b50c5049d2898245a27ccf8f558b9c11d5edaa0b276ba88c111b8c80505a5b911664e77b5351dc2a
@@ -14,7 +14,7 @@ jobs:
14
14
  if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
15
15
  steps:
16
16
  - name: Dependabot metadata
17
- uses: dependabot/fetch-metadata@dbb049abf0d677abbd7f7eee0375145b417fdd34 # v2.2.0
17
+ uses: dependabot/fetch-metadata@d7267f607e9d3fb96fc2fbe83e0af444713e90b7 # v2.3.0
18
18
  id: metadata
19
19
  - name: Checkout repository
20
20
  uses: actions/checkout@v4
@@ -20,6 +20,8 @@ jobs:
20
20
  bundler: none
21
21
  - name: Set working directory as safe
22
22
  run: git config --global --add safe.directory $(pwd)
23
+ - name: Set up permission
24
+ run: chmod -R o-w /opt/hostedtoolcache/Ruby
23
25
  - name: Install dependencies
24
26
  run: |
25
27
  sudo apt-get update
@@ -20,6 +20,21 @@ jobs:
20
20
  uses: ruby/setup-ruby@v1
21
21
  with:
22
22
  ruby-version: ${{ matrix.ruby }}
23
+
24
+ # ucrt and mswin have the dev version Ruby.
25
+ # It introduce checksum mismatches for bundled gems. So remove them before `bundle install`
26
+ - name: Purge gem caches
27
+ run: |
28
+ ruby -e '
29
+ exit if "${{ matrix.ruby }}" != "ucrt" && "${{ matrix.ruby }}" != "mswin"
30
+
31
+ require "open-uri"
32
+ require "json"
33
+
34
+ res = URI.parse("https://stdgems.org/bundled_gems.json").read
35
+ bundled_gems = JSON.parse(res)["gems"].map{_1["gem"]}
36
+ system "gem uninstall #{bundled_gems.join(" ")}", exception: true
37
+ '
23
38
  - name: bundle install
24
39
  run: |
25
40
  bundle config set without profilers libs
data/.rubocop.yml CHANGED
@@ -3,7 +3,7 @@ require:
3
3
  - rubocop-on-rbs
4
4
 
5
5
  AllCops:
6
- TargetRubyVersion: 3.0
6
+ TargetRubyVersion: 3.1
7
7
  DisabledByDefault: true
8
8
  Exclude:
9
9
  - 'vendor/bundle/**/*'
@@ -44,10 +44,22 @@ RBS/Style:
44
44
  Exclude:
45
45
  - 'sig/**/*'
46
46
  - 'test/**/*'
47
+ RBS/Style/BlockReturnBoolish:
48
+ Enabled: true
49
+ RBS/Style/ClassWithSingleton:
50
+ Enabled: true
51
+ RBS/Style/DuplicatedType:
52
+ Enabled: true
47
53
  RBS/Style/EmptyArgument:
48
54
  Enabled: true
49
55
  RBS/Style/InitializeReturnType:
50
56
  Enabled: true
57
+ RBS/Style/InstanceWithInstance:
58
+ Enabled: true
59
+ RBS/Style/OptionalNil:
60
+ Enabled: true
61
+ RBS/Style/RedundantParentheses:
62
+ Enabled: true
51
63
 
52
64
  Lint/DuplicateMethods:
53
65
  Enabled: true
data/Rakefile CHANGED
@@ -112,7 +112,7 @@ end
112
112
 
113
113
  task :stdlib_test => :compile do
114
114
  test_files = FileList["test/stdlib/**/*_test.rb"].reject do |path|
115
- path =~ %r{Ractor} || path =~ %r{Encoding}
115
+ path =~ %r{Ractor} || path =~ %r{Encoding} || path =~ %r{CGI_test}
116
116
  end
117
117
 
118
118
  if ENV["RANDOMIZE_STDLIB_TEST_ORDER"] == "true"
@@ -121,6 +121,7 @@ task :stdlib_test => :compile do
121
121
 
122
122
  sh "#{ruby} -Ilib #{bin}/test_runner.rb #{test_files.join(' ')}"
123
123
  # TODO: Ractor tests need to be run in a separate process
124
+ sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/CGI_test.rb"
124
125
  sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Ractor_test.rb"
125
126
  sh "#{ruby} -Ilib #{bin}/test_runner.rb test/stdlib/Encoding_test.rb"
126
127
  end
@@ -139,7 +140,9 @@ task :typecheck_test => :compile do
139
140
  end
140
141
 
141
142
  task :raap => :compile do
142
- sh %q[ruby test/raap.rb | xargs bundle exec raap -r digest/bubblebabble --library digest --allow-private]
143
+ sh "ruby test/raap/core.rb"
144
+ sh "ruby test/raap/digest.rb"
145
+ sh "ruby test/raap/openssl.rb"
143
146
  end
144
147
 
145
148
  task :rubocop do
data/config.yml CHANGED
@@ -27,18 +27,21 @@ nodes:
27
27
  - name: old_name
28
28
  - name: location
29
29
  - name: comment
30
+ - name: annotations
30
31
  - name: RBS::AST::Declarations::Constant
31
32
  fields:
32
33
  - name: name
33
34
  - name: type
34
35
  - name: location
35
36
  - name: comment
37
+ - name: annotations
36
38
  - name: RBS::AST::Declarations::Global
37
39
  fields:
38
40
  - name: name
39
41
  - name: type
40
42
  - name: location
41
43
  - name: comment
44
+ - name: annotations
42
45
  - name: RBS::AST::Declarations::Interface
43
46
  fields:
44
47
  - name: name
@@ -67,6 +70,7 @@ nodes:
67
70
  - name: old_name
68
71
  - name: location
69
72
  - name: comment
73
+ - name: annotations
70
74
  - name: RBS::AST::Declarations::TypeAlias
71
75
  fields:
72
76
  - name: name
@@ -191,6 +195,7 @@ nodes:
191
195
  - name: variance
192
196
  - name: upper_bound
193
197
  - name: default_type
198
+ - name: unchecked
194
199
  - name: location
195
200
  - name: RBS::MethodType
196
201
  fields:
@@ -213,6 +218,7 @@ nodes:
213
218
  - name: location
214
219
  - name: RBS::Types::Bases::Any
215
220
  fields:
221
+ - name: todo
216
222
  - name: location
217
223
  - name: RBS::Types::Bases::Bool
218
224
  fields:
data/core/data.rbs CHANGED
@@ -411,5 +411,5 @@ class Data
411
411
  # out = origin.with(z: 1) # ArgumentError: unknown keyword: :z
412
412
  # some_point = origin.with(1, 2) # ArgumentError: expected keyword arguments, got positional arguments
413
413
  #
414
- def with: (**untyped) -> instance
414
+ def with: (**untyped) -> self
415
415
  end
data/core/enumerator.rbs CHANGED
@@ -613,7 +613,18 @@ end
613
613
  # This type of objects can be created by Enumerable#chain and Enumerator#+.
614
614
  #
615
615
  class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
616
- include Enumerable[Elem]
616
+ # <!--
617
+ # rdoc-file=enumerator.c
618
+ # - Enumerator::Chain.new(*enums) -> enum
619
+ # -->
620
+ # Generates a new enumerator object that iterates over the elements of given
621
+ # enumerable objects in sequence.
622
+ #
623
+ # e = Enumerator::Chain.new(1..3, [4, 5])
624
+ # e.to_a #=> [1, 2, 3, 4, 5]
625
+ # e.size #=> 5
626
+ #
627
+ def initialize: (*_Each[Elem] enums) -> void
617
628
 
618
629
  # <!--
619
630
  # rdoc-file=enumerator.c
@@ -626,5 +637,6 @@ class Enumerator::Chain[out Elem] < Enumerator[Elem, void]
626
637
  #
627
638
  # If no block is given, returns an enumerator.
628
639
  #
629
- def each: () { (Elem) -> void } -> void
640
+ def each: () { (Elem) -> void } -> self
641
+ | () -> Enumerator[Elem, self]
630
642
  end
data/core/exception.rbs CHANGED
@@ -270,7 +270,7 @@ class Exception
270
270
  # x0..equal?(x1) # => false
271
271
  #
272
272
  def exception: (?self) -> self
273
- | (string | _ToS message) -> instance
273
+ | (string | _ToS message) -> self
274
274
 
275
275
  # <!--
276
276
  # rdoc-file=error.c
data/core/gc.rbs CHANGED
@@ -111,7 +111,7 @@ module GC
111
111
  # `:HAVE_FINALIZE`
112
112
  # :
113
113
  #
114
- def self.raw_data: () -> Array[Hash[Symbol, untyped]]
114
+ def self.raw_data: () -> Array[Hash[Symbol, untyped]]?
115
115
 
116
116
  # <!--
117
117
  # rdoc-file=gc.c
data/core/io.rbs CHANGED
@@ -2278,7 +2278,7 @@ class IO < Object
2278
2278
  # potential security vulnerabilities if called with untrusted input; see
2279
2279
  # [Command Injection](rdoc-ref:command_injection.rdoc).
2280
2280
  #
2281
- def self.binread: (String name, ?Integer length, ?Integer offset) -> String
2281
+ def self.binread: (String name, ?Integer? length, ?Integer offset) -> String
2282
2282
 
2283
2283
  # <!--
2284
2284
  # rdoc-file=io.c
@@ -2343,7 +2343,7 @@ class IO < Object
2343
2343
  # IO.copy_stream('t.txt', 't.tmp', 11, 11) # => 11
2344
2344
  # IO.read('t.tmp') # => "Second line"
2345
2345
  #
2346
- def self.copy_stream: (String | _Reader | _ReaderPartial src, String | _Writer dst, ?Integer copy_length, ?Integer src_offset) -> Integer
2346
+ def self.copy_stream: (String | _Reader | _ReaderPartial src, String | _Writer dst, ?Integer? copy_length, ?Integer src_offset) -> Integer
2347
2347
 
2348
2348
  # <!--
2349
2349
  # rdoc-file=io.c
@@ -2716,7 +2716,7 @@ class IO < Object
2716
2716
  # * [Open Options](rdoc-ref:IO@Open+Options).
2717
2717
  # * [Encoding options](rdoc-ref:encodings.rdoc@Encoding+Options).
2718
2718
  #
2719
- def self.read: (String name, ?Integer length, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> String
2719
+ def self.read: (String name, ?Integer? length, ?Integer offset, ?external_encoding: String | Encoding | nil, ?internal_encoding: String | Encoding | nil, ?encoding: String | Encoding | nil, ?textmode: boolish, ?binmode: boolish, ?autoclose: boolish, ?mode: String) -> String
2720
2720
 
2721
2721
  # <!--
2722
2722
  # rdoc-file=io.c
@@ -3251,6 +3251,10 @@ class IO < Object
3251
3251
  # f.close
3252
3252
  #
3253
3253
  alias to_i fileno
3254
+
3255
+ interface _ForFd[RET]
3256
+ def for_fd: (?) -> RET
3257
+ end
3254
3258
  end
3255
3259
 
3256
3260
  IO::APPEND: Integer
data/core/kernel.rbs CHANGED
@@ -1163,7 +1163,7 @@ module Kernel : BasicObject
1163
1163
  # The style of programming using `$_` as an implicit parameter is gradually
1164
1164
  # losing favor in the Ruby community.
1165
1165
  #
1166
- def self?.gets: (?String arg0, ?Integer arg1) -> String?
1166
+ def self?.gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
1167
1167
 
1168
1168
  # <!--
1169
1169
  # rdoc-file=eval.c
@@ -1486,7 +1486,7 @@ module Kernel : BasicObject
1486
1486
  # Optional keyword argument `chomp` specifies whether line separators are to be
1487
1487
  # omitted.
1488
1488
  #
1489
- def self?.readline: (?String arg0, ?Integer arg1) -> String
1489
+ def self?.readline: (?String arg0, ?Integer arg1, ?chomp: boolish) -> String
1490
1490
 
1491
1491
  # <!--
1492
1492
  # rdoc-file=io.c
data/core/method.rbs CHANGED
@@ -54,7 +54,7 @@ class Method
54
54
  #
55
55
  def hash: () -> Integer
56
56
 
57
- def dup: () -> instance
57
+ def dup: () -> self
58
58
 
59
59
  # <!--
60
60
  # rdoc-file=proc.c
@@ -252,7 +252,7 @@ class Method
252
252
  # m.call # => "bar"
253
253
  # n = m.clone.call # => "bar"
254
254
  #
255
- def clone: () -> instance
255
+ def clone: () -> self
256
256
 
257
257
  # <!--
258
258
  # rdoc-file=proc.c
data/core/module.rbs CHANGED
@@ -731,8 +731,8 @@ class Module < Object
731
731
  # I'm Dino!
732
732
  # #<B:0x401b39e8>
733
733
  #
734
- def define_method: (interned symbol, ^() [self: instance] -> untyped | Method | UnboundMethod method) -> Symbol
735
- | (interned symbol) { () [self: instance] -> untyped } -> Symbol
734
+ def define_method: (interned symbol, ^() [self: self] -> untyped | Method | UnboundMethod method) -> Symbol
735
+ | (interned symbol) { () [self: self] -> untyped } -> Symbol
736
736
 
737
737
  # <!--
738
738
  # rdoc-file=object.c
@@ -1164,7 +1164,7 @@ class Module < Object
1164
1164
  # -->
1165
1165
  # Returns the name of the module *mod*. Returns `nil` for anonymous modules.
1166
1166
  #
1167
- def name: () -> String?
1167
+ def name: %a{implicitly-returns-nil} () -> String
1168
1168
 
1169
1169
  # <!--
1170
1170
  # rdoc-file=eval.c
data/core/proc.rbs CHANGED
@@ -376,8 +376,8 @@ class Proc
376
376
  #
377
377
  def self.new: () { (?) -> untyped } -> instance
378
378
 
379
- def clone: () -> instance
380
- def dup: () -> instance
379
+ def clone: () -> self
380
+ def dup: () -> self
381
381
 
382
382
  # <!-- rdoc-file=proc.c -->
383
383
  # Invokes the block, setting the block's parameters to the values in *params*
@@ -639,7 +639,7 @@ module RBS
639
639
  # See IO.readlines for details about getline_args.
640
640
  #
641
641
  %a{annotate:rdoc:copy:ARGF#gets}
642
- def gets: (?String sep, ?Integer limit) -> String?
642
+ def gets: (?String sep, ?Integer limit, ?chomp: boolish) -> String?
643
643
 
644
644
  # <!--
645
645
  # rdoc-file=io.c
@@ -1024,7 +1024,7 @@ module RBS
1024
1024
  # An EOFError is raised at the end of the file.
1025
1025
  #
1026
1026
  %a{annotate:rdoc:copy:ARGF#readline}
1027
- def readline: (?String sep, ?Integer limit) -> String
1027
+ def readline: (?String sep, ?Integer limit, ?chomp: boolish) -> String
1028
1028
 
1029
1029
  # <!--
1030
1030
  # rdoc-file=io.c
@@ -1044,7 +1044,7 @@ module RBS
1044
1044
  # See `IO.readlines` for a full description of all options.
1045
1045
  #
1046
1046
  %a{annotate:rdoc:copy:ARGF#readlines}
1047
- def readlines: (?String sep, ?Integer limit) -> ::Array[String]
1047
+ def readlines: (?String sep, ?Integer limit, ?chomp: boolish) -> ::Array[String]
1048
1048
 
1049
1049
  # <!--
1050
1050
  # rdoc-file=io.c
@@ -221,7 +221,7 @@ module Gem
221
221
  #
222
222
  # Pre-release (alpha) parts, e.g, 5.3.1.b.2 => 5.4, are ignored.
223
223
  #
224
- def bump: () -> instance
224
+ def bump: () -> self
225
225
 
226
226
  # <!--
227
227
  # rdoc-file=lib/rubygems/version.rb
@@ -274,7 +274,7 @@ module Gem
274
274
  # The release for this version (e.g. 1.2.0.a -> 1.2.0). Non-prerelease versions
275
275
  # return themselves.
276
276
  #
277
- def release: () -> instance
277
+ def release: () -> self
278
278
 
279
279
  # <!--
280
280
  # rdoc-file=lib/rubygems/version.rb
data/core/string.rbs CHANGED
@@ -1936,7 +1936,7 @@ class String
1936
1936
  ?crlf_newline: boolish,
1937
1937
  ?lf_newline: boolish,
1938
1938
  ?fallback: ^(String) -> string? | Method | _EncodeFallbackAref
1939
- ) -> instance
1939
+ ) -> self
1940
1940
 
1941
1941
  interface _EncodeFallbackAref
1942
1942
  def []: (String) -> string?
@@ -100,7 +100,7 @@ class UnboundMethod
100
100
  # m.call # => "bar"
101
101
  # n = m.clone.call # => "bar"
102
102
  #
103
- def clone: () -> instance
103
+ def clone: () -> self
104
104
 
105
105
  # <!--
106
106
  # rdoc-file=proc.c
data/docs/syntax.md CHANGED
@@ -289,7 +289,9 @@ The following `class`/`instance` types are allowed.
289
289
  class Foo
290
290
  attr_reader parent: class
291
291
 
292
- def foo: () -> instance
292
+ def foo: () -> instance # behaves like `self` in this context
293
+
294
+ def self?.bar: () -> instance # behaves like `class` for `def self.bar()` and `self` for `def bar()`
293
295
 
294
296
  @@foos: Array[instance]
295
297
 
@@ -405,9 +407,12 @@ _ivar-member_ ::= _ivar-name_ `:` _type_
405
407
  | `self` `.` _ivar-name_ `:` _type_
406
408
  | _cvar-name_ `:` _type_
407
409
 
408
- _method-member_ ::= _visibility_ `def` _method-name_ `:` _method-types_ # Instance method
409
- | _visibility_ `def self.` _method-name_ `:` _method-types_ # Singleton method
410
- | `def self?.` _method-name_ `:` _method-types_ # Singleton and instance method
410
+ _method-member_ ::= _method-class-member_
411
+ | _method-interface-member_
412
+ _method-class-member_ ::= _visibility_ `def` _method-name_ `:` _method-types_ # Instance method
413
+ | _visibility_ `def self.` _method-name_ `:` _method-types_ # Singleton method
414
+ | `def self?.` _method-name_ `:` _method-types_ # Singleton and instance method
415
+ _method-interface-member_ ::= `def` _method-name_ `:` _method-types_ # Instance method
411
416
 
412
417
  _method-types_ ::= _method-type-parameters_ _method-type_ # Single method type
413
418
  | _method-type-parameters_ _method-type_ `|` _method-types_ # Overloading types
@@ -624,7 +629,7 @@ _module-self-types_ ::= _class-name_ _type-arguments_ `,` _module-self-types_
624
629
 
625
630
  _interface-decl_ ::= `interface` _interface-name_ _module-type-parameters_ _interface-members_ `end`
626
631
 
627
- _interface-members_ ::= _method-member_ # Method
632
+ _interface-members_ ::= _method-interface-member_ # Method
628
633
  | _include-interface-member_ # Mixin (include)
629
634
  | _alias-member_ # Alias
630
635
 
@@ -4,10 +4,11 @@ $INCFLAGS << " -I$(top_srcdir)" if $extmk
4
4
  $INCFLAGS << " -I$(srcdir)/../../include"
5
5
 
6
6
  $VPATH << "$(srcdir)/../../src"
7
+ $VPATH << "$(srcdir)/../../src/util"
7
8
  $VPATH << "$(srcdir)/ext/rbs_extension"
8
9
 
9
10
  root_dir = File.expand_path('../../../', __FILE__)
10
- $srcs = Dir.glob("#{root_dir}/src/*.c") +
11
+ $srcs = Dir.glob("#{root_dir}/src/**/*.c") +
11
12
  Dir.glob("#{root_dir}/ext/rbs_extension/*.c")
12
13
 
13
14
  append_cflags ['-std=gnu99']
@@ -56,14 +56,14 @@ static void check_children_cap(rbs_loc *loc) {
56
56
  }
57
57
  }
58
58
 
59
- void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
59
+ void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
60
60
  rbs_loc_add_optional_child(loc, name, r);
61
61
 
62
62
  unsigned short last_index = loc->children->len - 1;
63
63
  loc->children->required_p |= 1 << last_index;
64
64
  }
65
65
 
66
- void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
66
+ void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r) {
67
67
  check_children_cap(loc);
68
68
 
69
69
  unsigned short i = loc->children->len++;
@@ -168,6 +168,15 @@ static VALUE location_end_pos(VALUE self) {
168
168
  return INT2FIX(loc->rg.end);
169
169
  }
170
170
 
171
+ static rbs_constant_id_t rbs_constant_pool_insert_ruby_symbol(VALUE symbol) {
172
+ VALUE name = rb_sym2str(symbol);
173
+
174
+ // Constants inserted here will never be freed, but that's acceptable because:
175
+ // 1. Most symbols passed into here will be the ones already inserted into the constant pool by `parser.c`.
176
+ // 2. Methods like `add_required_child` and `add_optional_child` will usually only get called with a few different symbols.
177
+ return rbs_constant_pool_insert_constant(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
178
+ }
179
+
171
180
  static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
172
181
  rbs_loc *loc = rbs_check_location(self);
173
182
 
@@ -175,7 +184,7 @@ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VA
175
184
  rg.start = rbs_loc_position(FIX2INT(start));
176
185
  rg.end = rbs_loc_position(FIX2INT(end));
177
186
 
178
- rbs_loc_add_required_child(loc, SYM2ID(name), rg);
187
+ rbs_loc_add_required_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg);
179
188
 
180
189
  return Qnil;
181
190
  }
@@ -187,7 +196,7 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA
187
196
  rg.start = rbs_loc_position(FIX2INT(start));
188
197
  rg.end = rbs_loc_position(FIX2INT(end));
189
198
 
190
- rbs_loc_add_optional_child(loc, SYM2ID(name), rg);
199
+ rbs_loc_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg);
191
200
 
192
201
  return Qnil;
193
202
  }
@@ -195,7 +204,7 @@ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VA
195
204
  static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
196
205
  rbs_loc *loc = rbs_check_location(self);
197
206
 
198
- rbs_loc_add_optional_child(loc, SYM2ID(name), NULL_RANGE);
207
+ rbs_loc_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), NULL_RANGE);
199
208
 
200
209
  return Qnil;
201
210
  }
@@ -218,12 +227,18 @@ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
218
227
  return obj;
219
228
  }
220
229
 
230
+ static rbs_constant_id_t rbs_constant_pool_find_ruby_symbol(VALUE symbol) {
231
+ VALUE name = rb_sym2str(symbol);
232
+
233
+ return rbs_constant_pool_find(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
234
+ }
235
+
221
236
  static VALUE location_aref(VALUE self, VALUE name) {
222
237
  rbs_loc *loc = rbs_check_location(self);
223
238
 
224
- ID id = SYM2ID(name);
239
+ rbs_constant_id_t id = rbs_constant_pool_find_ruby_symbol(name);
225
240
 
226
- if (loc->children != NULL) {
241
+ if (loc->children != NULL && id != RBS_CONSTANT_ID_UNSET) {
227
242
  for (unsigned short i = 0; i < loc->children->len; i++) {
228
243
  if (loc->children->entries[i].name == id) {
229
244
  rbs_loc_range result = loc->children->entries[i].rg;
@@ -241,6 +256,10 @@ static VALUE location_aref(VALUE self, VALUE name) {
241
256
  rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string));
242
257
  }
243
258
 
259
+ static VALUE rbs_constant_to_ruby_symbol(rbs_constant_t *constant) {
260
+ return ID2SYM(rb_intern2((const char *) constant->start, constant->length));
261
+ }
262
+
244
263
  static VALUE location_optional_keys(VALUE self) {
245
264
  VALUE keys = rb_ary_new();
246
265
 
@@ -252,8 +271,9 @@ static VALUE location_optional_keys(VALUE self) {
252
271
 
253
272
  for (unsigned short i = 0; i < children->len; i++) {
254
273
  if (RBS_LOC_OPTIONAL_P(loc, i)) {
255
- rb_ary_push(keys, ID2SYM(children->entries[i].name));
256
-
274
+ rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
275
+ VALUE key_sym = rbs_constant_to_ruby_symbol(key_id);
276
+ rb_ary_push(keys, key_sym);
257
277
  }
258
278
  }
259
279
 
@@ -271,7 +291,9 @@ static VALUE location_required_keys(VALUE self) {
271
291
 
272
292
  for (unsigned short i = 0; i < children->len; i++) {
273
293
  if (RBS_LOC_REQUIRED_P(loc, i)) {
274
- rb_ary_push(keys, ID2SYM(children->entries[i].name));
294
+ rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
295
+ VALUE key_sym = rbs_constant_to_ruby_symbol(key_id);
296
+ rb_ary_push(keys, key_sym);
275
297
  }
276
298
  }
277
299
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include "ruby.h"
5
5
  #include "lexer.h"
6
+ #include "rbs/util/rbs_constant_pool.h"
6
7
 
7
8
  /**
8
9
  * RBS::Location class
@@ -15,7 +16,7 @@ typedef struct {
15
16
  } rbs_loc_range;
16
17
 
17
18
  typedef struct {
18
- ID name;
19
+ rbs_constant_id_t name;
19
20
  rbs_loc_range rg;
20
21
  } rbs_loc_entry;
21
22
 
@@ -58,14 +59,14 @@ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap);
58
59
  *
59
60
  * Allocate memory for children with rbs_loc_alloc_children before calling this function.
60
61
  * */
61
- void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r);
62
+ void rbs_loc_add_required_child(rbs_loc *loc, rbs_constant_id_t name, range r);
62
63
 
63
64
  /**
64
65
  * Add an optional child range with given name.
65
66
  *
66
67
  * Allocate memory for children with rbs_loc_alloc_children before calling this function.
67
68
  * */
68
- void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r);
69
+ void rbs_loc_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, range r);
69
70
 
70
71
  /**
71
72
  * Returns RBS::Location object with start/end positions.
@@ -1,12 +1,33 @@
1
1
  #include "rbs_extension.h"
2
+ #include "rbs/util/rbs_constant_pool.h"
3
+
4
+ #include "ruby/vm.h"
5
+
6
+ static
7
+ void Deinit_rbs_extension(ruby_vm_t *_) {
8
+ rbs_constant_pool_free(RBS_GLOBAL_CONSTANT_POOL);
9
+ }
2
10
 
3
11
  void
4
12
  Init_rbs_extension(void)
5
13
  {
6
14
  #ifdef HAVE_RB_EXT_RACTOR_SAFE
7
15
  rb_ext_ractor_safe(true);
8
- #endif
16
+ #endif
9
17
  rbs__init_constants();
10
18
  rbs__init_location();
11
19
  rbs__init_parser();
20
+
21
+ // Calculated based on the number of unique strings used with the `INTERN` macro in `parser.c`.
22
+ //
23
+ // ```bash
24
+ // grep -o 'INTERN("\([^"]*\)")' ext/rbs_extension/parser.c \
25
+ // | sed 's/INTERN("\(.*\)")/\1/' \
26
+ // | sort -u \
27
+ // | wc -l
28
+ // ```
29
+ const size_t num_uniquely_interned_strings = 26;
30
+ rbs_constant_pool_init(RBS_GLOBAL_CONSTANT_POOL, num_uniquely_interned_strings);
31
+
32
+ ruby_vm_at_exit(Deinit_rbs_extension);
12
33
  }