rbs 3.4.4 → 3.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +12 -4
  3. data/.github/workflows/dependabot.yml +26 -0
  4. data/.github/workflows/ruby.yml +14 -1
  5. data/CHANGELOG.md +87 -1
  6. data/README.md +5 -3
  7. data/Rakefile +20 -3
  8. data/Steepfile +1 -1
  9. data/core/enumerator.rbs +1 -1
  10. data/core/float.rbs +2 -1
  11. data/core/gc.rbs +272 -150
  12. data/core/integer.rbs +6 -4
  13. data/core/io/wait.rbs +4 -4
  14. data/core/io.rbs +10 -3
  15. data/core/kernel.rbs +8 -7
  16. data/core/module.rbs +17 -4
  17. data/core/proc.rbs +1 -1
  18. data/core/range.rbs +2 -2
  19. data/core/rational.rbs +2 -1
  20. data/core/regexp.rbs +101 -90
  21. data/core/ruby_vm.rbs +107 -103
  22. data/core/rubygems/rubygems.rbs +1 -1
  23. data/core/set.rbs +13 -7
  24. data/core/string.rbs +3 -3
  25. data/core/symbol.rbs +2 -1
  26. data/core/thread.rbs +1 -1
  27. data/core/time.rbs +24 -4
  28. data/docs/architecture.md +110 -0
  29. data/docs/gem.md +0 -1
  30. data/docs/syntax.md +5 -1
  31. data/ext/rbs_extension/constants.c +2 -0
  32. data/ext/rbs_extension/constants.h +1 -0
  33. data/ext/rbs_extension/lexer.c +1338 -1341
  34. data/ext/rbs_extension/lexer.h +2 -0
  35. data/ext/rbs_extension/lexer.re +2 -3
  36. data/ext/rbs_extension/lexstate.c +5 -1
  37. data/ext/rbs_extension/location.c +80 -70
  38. data/ext/rbs_extension/location.h +25 -5
  39. data/ext/rbs_extension/parser.c +149 -43
  40. data/ext/rbs_extension/parserstate.c +12 -1
  41. data/ext/rbs_extension/parserstate.h +9 -0
  42. data/ext/rbs_extension/ruby_objs.c +13 -3
  43. data/ext/rbs_extension/ruby_objs.h +1 -0
  44. data/lib/rbs/cli/validate.rb +2 -2
  45. data/lib/rbs/cli.rb +4 -6
  46. data/lib/rbs/collection/config.rb +1 -1
  47. data/lib/rbs/collection/sources/git.rb +1 -6
  48. data/lib/rbs/definition_builder/method_builder.rb +1 -1
  49. data/lib/rbs/definition_builder.rb +8 -8
  50. data/lib/rbs/diff.rb +1 -1
  51. data/lib/rbs/environment_loader.rb +2 -1
  52. data/lib/rbs/errors.rb +0 -14
  53. data/lib/rbs/location_aux.rb +6 -1
  54. data/lib/rbs/parser/lex_result.rb +15 -0
  55. data/lib/rbs/parser/token.rb +23 -0
  56. data/lib/rbs/parser_aux.rb +12 -5
  57. data/lib/rbs/prototype/helpers.rb +22 -12
  58. data/lib/rbs/prototype/rb.rb +38 -4
  59. data/lib/rbs/prototype/rbi.rb +30 -20
  60. data/lib/rbs/test/errors.rb +19 -14
  61. data/lib/rbs/test/tester.rb +1 -1
  62. data/lib/rbs/test/type_check.rb +95 -16
  63. data/lib/rbs/types.rb +112 -13
  64. data/lib/rbs/unit_test/spy.rb +1 -1
  65. data/lib/rbs/version.rb +1 -1
  66. data/rbs.gemspec +7 -2
  67. data/sig/environment_loader.rbs +1 -1
  68. data/sig/errors.rbs +1 -1
  69. data/sig/manifest.yaml +0 -1
  70. data/sig/method_types.rbs +3 -3
  71. data/sig/parser.rbs +28 -0
  72. data/sig/prototype/helpers.rbs +4 -0
  73. data/sig/prototype/rbi.rbs +2 -0
  74. data/sig/types.rbs +54 -4
  75. data/sig/variance_calculator.rbs +2 -2
  76. data/stdlib/csv/0/csv.rbs +4 -1
  77. data/stdlib/fileutils/0/fileutils.rbs +1 -1
  78. data/stdlib/net-http/0/net-http.rbs +29 -27
  79. data/stdlib/socket/0/socket.rbs +2 -2
  80. data/stdlib/timeout/0/timeout.rbs +6 -0
  81. data/stdlib/uri/0/generic.rbs +2 -2
  82. data/stdlib/uri/0/http.rbs +2 -2
  83. data/stdlib/uri/0/mailto.rbs +84 -0
  84. metadata +7 -9
  85. data/Gemfile +0 -30
  86. data/Gemfile.lock +0 -117
  87. data/lib/rbs/parser_compat/lexer_error.rb +0 -6
  88. data/lib/rbs/parser_compat/located_value.rb +0 -7
  89. data/lib/rbs/parser_compat/semantics_error.rb +0 -6
  90. data/lib/rbs/parser_compat/syntax_error.rb +0 -6
@@ -78,6 +78,8 @@ enum TokenType {
78
78
  tCOMMENT, /* Comment */
79
79
  tLINECOMMENT, /* Comment of all line */
80
80
 
81
+ tTRIVIA, /* Trivia tokens -- space and new line */
82
+
81
83
  tDQSTRING, /* Double quoted string */
82
84
  tSQSTRING, /* Single quoted string */
83
85
  tINTEGER, /* Integer */
@@ -3,7 +3,6 @@
3
3
  token rbsparser_next_token(lexstate *state) {
4
4
  lexstate backup;
5
5
 
6
- start:
7
6
  backup = *state;
8
7
 
9
8
  /*!re2c
@@ -139,9 +138,9 @@ start:
139
138
 
140
139
  "$" global_ident { return next_token(state, tGIDENT); }
141
140
 
142
- skip = [ \t\n\r]+;
141
+ skip = ([ \t]+|[\r\n]);
143
142
 
144
- skip { state->start = state->current; goto start; }
143
+ skip { return next_token(state, tTRIVIA); }
145
144
  "\x00" { return next_token(state, pEOF); }
146
145
  * { return next_token(state, ErrorToken); }
147
146
  */
@@ -77,6 +77,8 @@ static const char *RBS_TOKENTYPE_NAMES[] = {
77
77
  "tCOMMENT",
78
78
  "tLINECOMMENT",
79
79
 
80
+ "tTRIVIA",
81
+
80
82
  "tDQSTRING", /* Double quoted string */
81
83
  "tSQSTRING", /* Single quoted string */
82
84
  "tINTEGER", /* Integer */
@@ -120,7 +122,9 @@ token next_token(lexstate *state, enum TokenType type) {
120
122
  t.range.start = state->start;
121
123
  t.range.end = state->current;
122
124
  state->start = state->current;
123
- state->first_token_of_line = false;
125
+ if (type != tTRIVIA) {
126
+ state->first_token_of_line = false;
127
+ }
124
128
 
125
129
  return t;
126
130
  }
@@ -1,83 +1,77 @@
1
1
  #include "rbs_extension.h"
2
2
 
3
+ #define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
4
+ #define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
5
+ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
6
+
3
7
  VALUE RBS_Location;
4
8
 
5
- rbs_loc_list *rbs_loc_list_add(rbs_loc_list *list, const ID name, const range r) {
6
- rbs_loc_list *new = malloc(sizeof(rbs_loc_list));
7
- new->next = list;
8
- new->name = name;
9
- new->rg = r;
10
- return new;
9
+ position rbs_loc_position(int char_pos) {
10
+ position pos = { 0, char_pos, -1, -1 };
11
+ return pos;
11
12
  }
12
13
 
13
- rbs_loc_list *rbs_loc_list_dup(rbs_loc_list *list) {
14
- if (list) {
15
- return rbs_loc_list_add(rbs_loc_list_dup(list->next), list->name, list->rg);
16
- } else {
17
- return NULL;
18
- }
14
+ position rbs_loc_position3(int char_pos, int line, int column) {
15
+ position pos = { 0, char_pos, line, column };
16
+ return pos;
19
17
  }
20
18
 
21
- void rbs_loc_list_free(rbs_loc_list *list) {
22
- while (list) {
23
- rbs_loc_list *next = list->next;
24
- free(list);
25
- list = next;
19
+ static void check_children_max(unsigned short n) {
20
+ size_t max = sizeof(rbs_loc_entry_bitmap) * 8;
21
+ if (n > max) {
22
+ rb_raise(rb_eRuntimeError, "Too many children added to location: %d", n);
26
23
  }
27
24
  }
28
25
 
29
- bool rbs_loc_list_find(const rbs_loc_list *list, ID name, range *rg) {
30
- while (list) {
31
- if (list->name == name) {
32
- *rg = list->rg;
33
- return true;
34
- }
26
+ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) {
27
+ check_children_max(cap);
35
28
 
36
- list = list->next;
37
- }
29
+ size_t s = RBS_LOC_CHILDREN_SIZE(cap);
30
+ loc->children = malloc(s);
38
31
 
39
- return false;
32
+ loc->children->len = 0;
33
+ loc->children->required_p = 0;
34
+ loc->children->cap = cap;
40
35
  }
41
36
 
42
- size_t rbs_loc_list_size(const rbs_loc_list *list) {
43
- size_t size = 0;
44
-
45
- while (list) {
46
- size += 1;
47
- list = list->next;
37
+ static void check_children_cap(rbs_loc *loc) {
38
+ if (loc->children == NULL) {
39
+ rbs_loc_alloc_children(loc, 1);
40
+ } else {
41
+ if (loc->children->len == loc->children->cap) {
42
+ check_children_max(loc->children->cap + 1);
43
+ size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap);
44
+ loc->children = realloc(loc->children, s);
45
+ }
48
46
  }
49
-
50
- return size;
51
47
  }
52
48
 
53
- position rbs_loc_position(int char_pos) {
54
- position pos = { 0, char_pos, -1, -1 };
55
- return pos;
56
- }
49
+ void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
50
+ check_children_cap(loc);
57
51
 
58
- position rbs_loc_position3(int char_pos, int line, int column) {
59
- position pos = { 0, char_pos, line, column };
60
- return pos;
61
- }
52
+ unsigned short i = loc->children->len++;
53
+ loc->children->entries[i].name = name;
54
+ loc->children->entries[i].rg = r;
62
55
 
63
- void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
64
- loc->requireds = rbs_loc_list_add(loc->requireds, name, r);
56
+ loc->children->required_p |= 1 << i;
65
57
  }
66
58
 
67
59
  void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
68
- loc->optionals = rbs_loc_list_add(loc->optionals, name, r);
60
+ check_children_cap(loc);
61
+
62
+ unsigned short i = loc->children->len++;
63
+ loc->children->entries[i].name = name;
64
+ loc->children->entries[i].rg = r;
69
65
  }
70
66
 
71
67
  void rbs_loc_init(rbs_loc *loc, VALUE buffer, range rg) {
72
68
  loc->buffer = buffer;
73
69
  loc->rg = rg;
74
- loc->optionals = NULL;
75
- loc->requireds = NULL;
70
+ loc->children = NULL;
76
71
  }
77
72
 
78
73
  void rbs_loc_free(rbs_loc *loc) {
79
- rbs_loc_list_free(loc->optionals);
80
- rbs_loc_list_free(loc->requireds);
74
+ free(loc->children);
81
75
  ruby_xfree(loc);
82
76
  }
83
77
 
@@ -89,7 +83,11 @@ static void rbs_loc_mark(void *ptr)
89
83
 
90
84
  static size_t rbs_loc_memsize(const void *ptr) {
91
85
  const rbs_loc *loc = ptr;
92
- return sizeof(*loc) + (rbs_loc_list_size(loc->optionals) + rbs_loc_list_size(loc->requireds)) * sizeof(rbs_loc_list);
86
+ if (loc->children == NULL) {
87
+ return sizeof(rbs_loc);
88
+ } else {
89
+ return sizeof(rbs_loc) + RBS_LOC_CHILDREN_SIZE(loc->children->cap);
90
+ }
93
91
  }
94
92
 
95
93
  static rb_data_type_t location_type = {
@@ -130,8 +128,10 @@ static VALUE location_initialize_copy(VALUE self, VALUE other) {
130
128
 
131
129
  self_loc->buffer = other_loc->buffer;
132
130
  self_loc->rg = other_loc->rg;
133
- self_loc->requireds = rbs_loc_list_dup(other_loc->requireds);
134
- self_loc->optionals = rbs_loc_list_dup(other_loc->optionals);
131
+ if (other_loc->children != NULL) {
132
+ rbs_loc_alloc_children(self_loc, other_loc->children->cap);
133
+ memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
134
+ }
135
135
 
136
136
  return Qnil;
137
137
  }
@@ -221,18 +221,19 @@ VALUE rbs_new_location(VALUE buffer, range rg) {
221
221
  static VALUE location_aref(VALUE self, VALUE name) {
222
222
  rbs_loc *loc = rbs_check_location(self);
223
223
 
224
- range result;
225
224
  ID id = SYM2ID(name);
226
225
 
227
- if (rbs_loc_list_find(loc->requireds, id, &result)) {
228
- return rbs_new_location(loc->buffer, result);
229
- }
230
-
231
- if (rbs_loc_list_find(loc->optionals, id, &result)) {
232
- if (null_range_p(result)) {
233
- return Qnil;
234
- } else {
235
- return rbs_new_location(loc->buffer, result);
226
+ if (loc->children != NULL) {
227
+ for (unsigned short i = 0; i < loc->children->len; i++) {
228
+ if (loc->children->entries[i].name == id) {
229
+ range result = loc->children->entries[i].rg;
230
+
231
+ if (RBS_LOC_OPTIONAL_P(loc, i) && null_range_p(result)) {
232
+ return Qnil;
233
+ } else {
234
+ return rbs_new_location(loc->buffer, result);
235
+ }
236
+ }
236
237
  }
237
238
  }
238
239
 
@@ -244,11 +245,16 @@ static VALUE location_optional_keys(VALUE self) {
244
245
  VALUE keys = rb_ary_new();
245
246
 
246
247
  rbs_loc *loc = rbs_check_location(self);
247
- rbs_loc_list *list = loc->optionals;
248
+ rbs_loc_children *children = loc->children;
249
+ if (children == NULL) {
250
+ return keys;
251
+ }
248
252
 
249
- while (list) {
250
- rb_ary_push(keys, ID2SYM(list->name));
251
- list = list->next;
253
+ for (unsigned short i = 0; i < children->len; i++) {
254
+ if (RBS_LOC_OPTIONAL_P(loc, i)) {
255
+ rb_ary_push(keys, ID2SYM(children->entries[i].name));
256
+
257
+ }
252
258
  }
253
259
 
254
260
  return keys;
@@ -258,11 +264,15 @@ static VALUE location_required_keys(VALUE self) {
258
264
  VALUE keys = rb_ary_new();
259
265
 
260
266
  rbs_loc *loc = rbs_check_location(self);
261
- rbs_loc_list *list = loc->requireds;
267
+ rbs_loc_children *children = loc->children;
268
+ if (children == NULL) {
269
+ return keys;
270
+ }
262
271
 
263
- while (list) {
264
- rb_ary_push(keys, ID2SYM(list->name));
265
- list = list->next;
272
+ for (unsigned short i = 0; i < children->len; i++) {
273
+ if (RBS_LOC_REQUIRED_P(loc, i)) {
274
+ rb_ary_push(keys, ID2SYM(children->entries[i].name));
275
+ }
266
276
  }
267
277
 
268
278
  return keys;
@@ -9,17 +9,26 @@
9
9
  * */
10
10
  extern VALUE RBS_Location;
11
11
 
12
- typedef struct rbs_loc_list {
12
+ typedef struct {
13
13
  ID name;
14
14
  range rg;
15
- struct rbs_loc_list *next;
16
- } rbs_loc_list;
15
+ } rbs_loc_entry;
16
+
17
+ typedef unsigned int rbs_loc_entry_bitmap;
18
+
19
+ // The flexible array always allocates, but it's okay.
20
+ // This struct is not allocated when the `rbs_loc` doesn't have children.
21
+ typedef struct {
22
+ unsigned short len;
23
+ unsigned short cap;
24
+ rbs_loc_entry_bitmap required_p;
25
+ rbs_loc_entry entries[1];
26
+ } rbs_loc_children;
17
27
 
18
28
  typedef struct {
19
29
  VALUE buffer;
20
30
  range rg;
21
- rbs_loc_list *requireds;
22
- rbs_loc_list *optionals;
31
+ rbs_loc_children *children; // NULL when no children is allocated
23
32
  } rbs_loc;
24
33
 
25
34
  /**
@@ -32,13 +41,24 @@ VALUE rbs_new_location(VALUE buffer, range rg);
32
41
  * */
33
42
  rbs_loc *rbs_check_location(VALUE location);
34
43
 
44
+ /**
45
+ * Allocate memory for child locations.
46
+ *
47
+ * Do not call twice for the same location.
48
+ * */
49
+ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap);
50
+
35
51
  /**
36
52
  * Add a required child range with given name.
53
+ *
54
+ * Allocate memory for children with rbs_loc_alloc_children before calling this function.
37
55
  * */
38
56
  void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r);
39
57
 
40
58
  /**
41
59
  * Add an optional child range with given name.
60
+ *
61
+ * Allocate memory for children with rbs_loc_alloc_children before calling this function.
42
62
  * */
43
63
  void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r);
44
64