rbs 3.8.1 → 3.9.0.pre.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 (93) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependabot.yml +1 -1
  3. data/.github/workflows/ruby.yml +26 -0
  4. data/.github/workflows/typecheck.yml +2 -0
  5. data/.github/workflows/windows.yml +15 -0
  6. data/.rubocop.yml +13 -1
  7. data/CHANGELOG.md +49 -0
  8. data/Rakefile +16 -3
  9. data/config.yml +6 -0
  10. data/core/data.rbs +1 -1
  11. data/core/enumerator.rbs +14 -2
  12. data/core/exception.rbs +1 -1
  13. data/core/gc.rbs +1 -1
  14. data/core/io.rbs +7 -3
  15. data/core/kernel.rbs +2 -2
  16. data/core/method.rbs +2 -2
  17. data/core/module.rbs +3 -3
  18. data/core/proc.rbs +2 -2
  19. data/core/rbs/unnamed/argf.rbs +3 -3
  20. data/core/rubygems/version.rbs +2 -2
  21. data/core/string.rbs +1 -1
  22. data/core/unbound_method.rbs +1 -1
  23. data/docs/syntax.md +10 -5
  24. data/ext/rbs_extension/extconf.rb +2 -1
  25. data/ext/rbs_extension/location.c +32 -10
  26. data/ext/rbs_extension/location.h +4 -3
  27. data/ext/rbs_extension/main.c +22 -1
  28. data/ext/rbs_extension/parser.c +164 -143
  29. data/ext/rbs_extension/parserstate.c +54 -9
  30. data/ext/rbs_extension/parserstate.h +6 -4
  31. data/include/rbs/ruby_objs.h +6 -6
  32. data/include/rbs/util/rbs_constant_pool.h +219 -0
  33. data/lib/rbs/ast/declarations.rb +9 -4
  34. data/lib/rbs/ast/directives.rb +10 -0
  35. data/lib/rbs/ast/members.rb +2 -0
  36. data/lib/rbs/ast/type_param.rb +2 -2
  37. data/lib/rbs/cli/validate.rb +1 -0
  38. data/lib/rbs/cli.rb +3 -3
  39. data/lib/rbs/collection/config/lockfile_generator.rb +28 -7
  40. data/lib/rbs/collection/sources/rubygems.rb +1 -1
  41. data/lib/rbs/definition.rb +46 -31
  42. data/lib/rbs/definition_builder/ancestor_builder.rb +2 -0
  43. data/lib/rbs/definition_builder.rb +86 -30
  44. data/lib/rbs/environment.rb +33 -18
  45. data/lib/rbs/errors.rb +24 -0
  46. data/lib/rbs/locator.rb +2 -0
  47. data/lib/rbs/method_type.rb +2 -0
  48. data/lib/rbs/parser_aux.rb +38 -1
  49. data/lib/rbs/subtractor.rb +3 -3
  50. data/lib/rbs/test/hook.rb +2 -2
  51. data/lib/rbs/test/type_check.rb +7 -5
  52. data/lib/rbs/types.rb +44 -5
  53. data/lib/rbs/unit_test/spy.rb +4 -2
  54. data/lib/rbs/unit_test/type_assertions.rb +17 -11
  55. data/lib/rbs/validator.rb +4 -0
  56. data/lib/rbs/version.rb +1 -1
  57. data/lib/rbs/writer.rb +10 -5
  58. data/lib/rbs.rb +1 -0
  59. data/rbs.gemspec +1 -1
  60. data/sig/collection/config/lockfile_generator.rbs +1 -1
  61. data/sig/declarations.rbs +10 -3
  62. data/sig/definition.rbs +67 -14
  63. data/sig/definition_builder.rbs +17 -3
  64. data/sig/directives.rbs +17 -1
  65. data/sig/environment.rbs +2 -0
  66. data/sig/errors.rbs +19 -0
  67. data/sig/parser.rbs +5 -1
  68. data/sig/subtractor.rbs +1 -1
  69. data/sig/test/type_check.rbs +2 -2
  70. data/sig/type_param.rbs +1 -1
  71. data/sig/types.rbs +3 -0
  72. data/sig/unit_test/spy.rbs +2 -0
  73. data/sig/unit_test/type_assertions.rbs +2 -0
  74. data/sig/validator.rbs +4 -0
  75. data/sig/writer.rbs +1 -1
  76. data/src/ruby_objs.c +12 -6
  77. data/src/util/rbs_constant_pool.c +342 -0
  78. data/stdlib/cgi/0/core.rbs +10 -0
  79. data/stdlib/ipaddr/0/ipaddr.rbs +8 -0
  80. data/stdlib/json/0/json.rbs +52 -50
  81. data/stdlib/net-http/0/net-http.rbs +2 -2
  82. data/stdlib/openssl/0/openssl.rbs +73 -73
  83. data/stdlib/resolv/0/resolv.rbs +8 -8
  84. data/stdlib/socket/0/addrinfo.rbs +1 -1
  85. data/stdlib/socket/0/unix_socket.rbs +4 -2
  86. data/stdlib/stringio/0/stringio.rbs +1 -1
  87. data/stdlib/uri/0/common.rbs +17 -0
  88. metadata +4 -7
  89. data/templates/include/rbs/constants.h.erb +0 -20
  90. data/templates/include/rbs/ruby_objs.h.erb +0 -10
  91. data/templates/src/constants.c.erb +0 -36
  92. data/templates/src/ruby_objs.c.erb +0 -27
  93. data/templates/template.rb +0 -122
@@ -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
  }