rbs 3.5.2 → 3.6.0.dev.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/dependabot.yml +5 -1
  3. data/.github/workflows/ruby.yml +2 -18
  4. data/.github/workflows/windows.yml +26 -0
  5. data/CHANGELOG.md +0 -12
  6. data/core/array.rbs +10 -10
  7. data/core/basic_object.rbs +3 -3
  8. data/core/enumerable.rbs +6 -0
  9. data/core/enumerator.rbs +7 -0
  10. data/core/fiber.rbs +1 -1
  11. data/core/global_variables.rbs +2 -2
  12. data/core/kernel.rbs +67 -38
  13. data/core/method.rbs +98 -7
  14. data/core/module.rbs +2 -2
  15. data/core/proc.rbs +184 -23
  16. data/core/ractor.rbs +1 -1
  17. data/core/range.rbs +30 -0
  18. data/core/refinement.rbs +16 -26
  19. data/core/symbol.rbs +34 -26
  20. data/core/thread.rbs +2 -2
  21. data/core/trace_point.rbs +12 -12
  22. data/core/unbound_method.rbs +1 -1
  23. data/docs/syntax.md +21 -9
  24. data/ext/rbs_extension/parser.c +135 -55
  25. data/ext/rbs_extension/parserstate.c +2 -7
  26. data/ext/rbs_extension/parserstate.h +6 -5
  27. data/ext/rbs_extension/ruby_objs.c +2 -1
  28. data/ext/rbs_extension/ruby_objs.h +1 -1
  29. data/lib/rbs/ast/declarations.rb +36 -0
  30. data/lib/rbs/ast/type_param.rb +71 -15
  31. data/lib/rbs/ast/visitor.rb +137 -0
  32. data/lib/rbs/cli/validate.rb +41 -7
  33. data/lib/rbs/cli.rb +3 -3
  34. data/lib/rbs/definition.rb +2 -1
  35. data/lib/rbs/definition_builder/ancestor_builder.rb +30 -4
  36. data/lib/rbs/definition_builder.rb +21 -6
  37. data/lib/rbs/environment_loader.rb +1 -1
  38. data/lib/rbs/errors.rb +7 -2
  39. data/lib/rbs/file_finder.rb +9 -12
  40. data/lib/rbs/locator.rb +8 -5
  41. data/lib/rbs/prototype/rbi.rb +2 -1
  42. data/lib/rbs/prototype/runtime.rb +3 -2
  43. data/lib/rbs/sorter.rb +9 -6
  44. data/lib/rbs/test/type_check.rb +6 -0
  45. data/lib/rbs/types.rb +11 -0
  46. data/lib/rbs/validator.rb +2 -2
  47. data/lib/rbs/vendorer.rb +3 -3
  48. data/lib/rbs/version.rb +1 -1
  49. data/lib/rbs.rb +1 -0
  50. data/sig/declarations.rbs +6 -0
  51. data/sig/definition.rbs +1 -1
  52. data/sig/definition_builder.rbs +3 -1
  53. data/sig/errors.rbs +3 -2
  54. data/sig/file_finder.rbs +24 -2
  55. data/sig/method_types.rbs +1 -1
  56. data/sig/sorter.rbs +1 -1
  57. data/sig/type_param.rbs +41 -9
  58. data/sig/types.rbs +12 -0
  59. data/sig/visitor.rbs +47 -0
  60. data/stdlib/csv/0/csv.rbs +27 -0
  61. data/stdlib/net-http/0/net-http.rbs +1 -1
  62. data/stdlib/zlib/0/gzip_reader.rbs +5 -1
  63. metadata +6 -3
@@ -52,6 +52,14 @@ typedef struct {
52
52
  VALUE rest_keywords;
53
53
  } method_params;
54
54
 
55
+ static VALUE EMPTY_ARRAY;
56
+
57
+ static void inline melt_array(VALUE *array) {
58
+ if (*array == EMPTY_ARRAY) {
59
+ *array = rb_ary_new();
60
+ }
61
+ }
62
+
55
63
  static bool rbs_is_untyped_params(method_params *params) {
56
64
  return NIL_P(params->required_positionals);
57
65
  }
@@ -137,7 +145,7 @@ void parser_advance_no_gap(parserstate *state) {
137
145
  */
138
146
  VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
139
147
  VALUE absolute = Qfalse;
140
- VALUE path = rb_ary_new();
148
+ VALUE path = EMPTY_ARRAY;
141
149
  VALUE namespace;
142
150
 
143
151
  if (rg) {
@@ -155,6 +163,7 @@ VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
155
163
  && state->current_token.range.end.byte_pos == state->next_token.range.start.byte_pos
156
164
  && state->next_token.range.end.byte_pos == state->next_token2.range.start.byte_pos
157
165
  ) {
166
+ melt_array(&path);
158
167
  rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->current_token)));
159
168
 
160
169
  parser_advance(state);
@@ -211,9 +220,10 @@ VALUE parse_type_name(parserstate *state, TypeNameKind kind, range *rg) {
211
220
  type_list ::= {} type `,` ... <`,`> eol
212
221
  | {} type `,` ... `,` <type> eol
213
222
  */
214
- static VALUE parse_type_list(parserstate *state, enum TokenType eol, VALUE types) {
223
+ static void parse_type_list(parserstate *state, enum TokenType eol, VALUE *types) {
215
224
  while (true) {
216
- rb_ary_push(types, parse_type(state));
225
+ melt_array(types);
226
+ rb_ary_push(*types, parse_type(state));
217
227
 
218
228
  if (state->next_token.type == pCOMMA) {
219
229
  parser_advance(state);
@@ -233,8 +243,6 @@ static VALUE parse_type_list(parserstate *state, enum TokenType eol, VALUE types
233
243
  }
234
244
  }
235
245
  }
236
-
237
- return types;
238
246
  }
239
247
 
240
248
  static bool is_keyword_token(enum TokenType type) {
@@ -329,11 +337,22 @@ static VALUE parse_keyword_key(parserstate *state) {
329
337
  /*
330
338
  keyword ::= {} keyword `:` <function_param>
331
339
  */
332
- static void parse_keyword(parserstate *state, VALUE keywords) {
340
+ static void parse_keyword(parserstate *state, VALUE keywords, VALUE memo) {
333
341
  VALUE key;
334
342
  VALUE param;
335
343
 
336
344
  key = parse_keyword_key(state);
345
+
346
+ if (!NIL_P(rb_hash_aref(memo, key))) {
347
+ raise_syntax_error(
348
+ state,
349
+ state->current_token,
350
+ "duplicated keyword argument"
351
+ );
352
+ } else {
353
+ rb_hash_aset(memo, key, Qtrue);
354
+ }
355
+
337
356
  parser_advance_assert(state, pCOLON);
338
357
  param = parse_function_param(state);
339
358
 
@@ -403,6 +422,8 @@ static void parse_params(parserstate *state, method_params *params) {
403
422
  return;
404
423
  }
405
424
 
425
+ VALUE memo = rb_hash_new();
426
+
406
427
  while (true) {
407
428
  VALUE param;
408
429
 
@@ -422,6 +443,7 @@ static void parse_params(parserstate *state, method_params *params) {
422
443
  }
423
444
 
424
445
  param = parse_function_param(state);
446
+ melt_array(&params->required_positionals);
425
447
  rb_ary_push(params->required_positionals, param);
426
448
 
427
449
  break;
@@ -441,12 +463,13 @@ PARSE_OPTIONAL_PARAMS:
441
463
  parser_advance(state);
442
464
 
443
465
  if (is_keyword(state)) {
444
- parse_keyword(state, params->optional_keywords);
466
+ parse_keyword(state, params->optional_keywords, memo);
445
467
  parser_advance_if(state, pCOMMA);
446
468
  goto PARSE_KEYWORDS;
447
469
  }
448
470
 
449
471
  param = parse_function_param(state);
472
+ melt_array(&params->optional_positionals);
450
473
  rb_ary_push(params->optional_positionals, param);
451
474
 
452
475
  break;
@@ -490,6 +513,7 @@ PARSE_TRAILING_PARAMS:
490
513
  }
491
514
 
492
515
  param = parse_function_param(state);
516
+ melt_array(&params->trailing_positionals);
493
517
  rb_ary_push(params->trailing_positionals, param);
494
518
 
495
519
  break;
@@ -506,7 +530,7 @@ PARSE_KEYWORDS:
506
530
  case pQUESTION:
507
531
  parser_advance(state);
508
532
  if (is_keyword(state)) {
509
- parse_keyword(state, params->optional_keywords);
533
+ parse_keyword(state, params->optional_keywords, memo);
510
534
  } else {
511
535
  raise_syntax_error(
512
536
  state,
@@ -529,7 +553,7 @@ PARSE_KEYWORDS:
529
553
  case tBANGIDENT:
530
554
  KEYWORD_CASES
531
555
  if (is_keyword(state)) {
532
- parse_keyword(state, params->required_keywords);
556
+ parse_keyword(state, params->required_keywords, memo);
533
557
  } else {
534
558
  raise_syntax_error(
535
559
  state,
@@ -580,10 +604,10 @@ static VALUE parse_optional(parserstate *state) {
580
604
  }
581
605
 
582
606
  static void initialize_method_params(method_params *params){
583
- params->required_positionals = rb_ary_new();
584
- params->optional_positionals = rb_ary_new();
607
+ params->required_positionals = EMPTY_ARRAY;
608
+ params->optional_positionals = EMPTY_ARRAY;
585
609
  params->rest_positionals = Qnil;
586
- params->trailing_positionals = rb_ary_new();
610
+ params->trailing_positionals = EMPTY_ARRAY;
587
611
  params->required_keywords = rb_hash_new();
588
612
  params->optional_keywords = rb_hash_new();
589
613
  params->rest_keywords = Qnil;
@@ -713,6 +737,16 @@ static VALUE parse_proc_type(parserstate *state) {
713
737
  return rbs_proc(function, block, loc, proc_self);
714
738
  }
715
739
 
740
+ static void check_key_duplication(parserstate *state, VALUE fields, VALUE key) {
741
+ if (!NIL_P(rb_hash_aref(fields, key))) {
742
+ raise_syntax_error(
743
+ state,
744
+ state->current_token,
745
+ "duplicated record key"
746
+ );
747
+ }
748
+ }
749
+
716
750
  /**
717
751
  * ... `{` ... `}` ...
718
752
  * > >
@@ -744,6 +778,7 @@ VALUE parse_record_attributes(parserstate *state) {
744
778
  if (is_keyword(state)) {
745
779
  // { foo: type } syntax
746
780
  key = parse_keyword_key(state);
781
+ check_key_duplication(state, fields, key);
747
782
  parser_advance_assert(state, pCOLON);
748
783
  } else {
749
784
  // { key => type } syntax
@@ -765,6 +800,7 @@ VALUE parse_record_attributes(parserstate *state) {
765
800
  "unexpected record key token"
766
801
  );
767
802
  }
803
+ check_key_duplication(state, fields, key);
768
804
  parser_advance_assert(state, pFATARROW);
769
805
  }
770
806
  type = parse_type(state);
@@ -838,7 +874,7 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
838
874
  }
839
875
 
840
876
  VALUE typename = parse_type_name(state, expected_kind, &name_range);
841
- VALUE types = rb_ary_new();
877
+ VALUE types = EMPTY_ARRAY;
842
878
 
843
879
  TypeNameKind kind;
844
880
  if (state->current_token.type == tUIDENT) {
@@ -854,7 +890,7 @@ static VALUE parse_instance_type(parserstate *state, bool parse_alias) {
854
890
  if (state->next_token.type == pLBRACKET) {
855
891
  parser_advance(state);
856
892
  args_range.start = state->current_token.range.start;
857
- parse_type_list(state, pRBRACKET, types);
893
+ parse_type_list(state, pRBRACKET, &types);
858
894
  parser_advance_assert(state, pRBRACKET);
859
895
  args_range.end = state->current_token.range.end;
860
896
  } else {
@@ -993,9 +1029,9 @@ static VALUE parse_simple(parserstate *state) {
993
1029
  case pLBRACKET: {
994
1030
  range rg;
995
1031
  rg.start = state->current_token.range.start;
996
- VALUE types = rb_ary_new();
1032
+ VALUE types = EMPTY_ARRAY;
997
1033
  if (state->next_token.type != pRBRACKET) {
998
- parse_type_list(state, pRBRACKET, types);
1034
+ parse_type_list(state, pRBRACKET, &types);
999
1035
  }
1000
1036
  parser_advance_assert(state, pRBRACKET);
1001
1037
  rg.end = state->current_token.range.end;
@@ -1003,7 +1039,7 @@ static VALUE parse_simple(parserstate *state) {
1003
1039
  return rbs_tuple(types, rbs_new_location(state->buffer, rg));
1004
1040
  }
1005
1041
  case pAREF_OPR: {
1006
- return rbs_tuple(rb_ary_new(), rbs_new_location(state->buffer, state->current_token.range));
1042
+ return rbs_tuple(EMPTY_ARRAY, rbs_new_location(state->buffer, state->current_token.range));
1007
1043
  }
1008
1044
  case pLBRACE: {
1009
1045
  position start = state->current_token.range.start;
@@ -1083,12 +1119,14 @@ VALUE parse_type(parserstate *state) {
1083
1119
  type_params ::= {} `[` type_param `,` ... <`]`>
1084
1120
  | {<>}
1085
1121
 
1086
- type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT (module_type_params == true)
1122
+ type_param ::= kUNCHECKED? (kIN|kOUT|) tUIDENT upper_bound? default_type? (module_type_params == true)
1087
1123
 
1088
- type_param ::= tUIDENT (module_type_params == false)
1124
+ type_param ::= tUIDENT upper_bound? default_type? (module_type_params == false)
1089
1125
  */
1090
1126
  VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params) {
1091
- VALUE params = rb_ary_new();
1127
+ VALUE params = EMPTY_ARRAY;
1128
+
1129
+ bool required_param_allowed = true;
1092
1130
 
1093
1131
  if (state->next_token.type == pLBRACKET) {
1094
1132
  parser_advance(state);
@@ -1100,12 +1138,14 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1100
1138
  bool unchecked = false;
1101
1139
  VALUE variance = ID2SYM(rb_intern("invariant"));
1102
1140
  VALUE upper_bound = Qnil;
1141
+ VALUE default_type = Qnil;
1103
1142
 
1104
1143
  range param_range = NULL_RANGE;
1105
1144
  range name_range;
1106
1145
  range variance_range = NULL_RANGE;
1107
1146
  range unchecked_range = NULL_RANGE;
1108
1147
  range upper_bound_range = NULL_RANGE;
1148
+ range default_type_range = NULL_RANGE;
1109
1149
 
1110
1150
  param_range.start = state->next_token.range.start;
1111
1151
 
@@ -1143,13 +1183,28 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1143
1183
 
1144
1184
  if (state->next_token.type == pLT) {
1145
1185
  parser_advance(state);
1186
+ upper_bound_range.start = state->current_token.range.start;
1187
+ upper_bound = parse_type(state);
1188
+ upper_bound_range.end = state->current_token.range.end;
1189
+ }
1146
1190
 
1147
- if (state->next_token.type == kSINGLETON) {
1191
+ if (module_type_params) {
1192
+ if (state->next_token.type == pEQ) {
1148
1193
  parser_advance(state);
1149
- upper_bound = parse_singleton_type(state);
1194
+
1195
+ default_type_range.start = state->current_token.range.start;
1196
+ default_type = parse_type(state);
1197
+ default_type_range.start = state->current_token.range.end;
1198
+
1199
+ required_param_allowed = false;
1150
1200
  } else {
1151
- parser_advance(state);
1152
- upper_bound = parse_instance_type(state, false);
1201
+ if (!required_param_allowed) {
1202
+ raise_syntax_error(
1203
+ state,
1204
+ state->current_token,
1205
+ "required type parameter is not allowed after optional type parameter"
1206
+ );
1207
+ }
1153
1208
  }
1154
1209
  }
1155
1210
 
@@ -1162,8 +1217,10 @@ VALUE parse_type_params(parserstate *state, range *rg, bool module_type_params)
1162
1217
  rbs_loc_add_optional_child(loc, rb_intern("variance"), variance_range);
1163
1218
  rbs_loc_add_optional_child(loc, rb_intern("unchecked"), unchecked_range);
1164
1219
  rbs_loc_add_optional_child(loc, rb_intern("upper_bound"), upper_bound_range);
1220
+ rbs_loc_add_optional_child(loc, rb_intern("default"), default_type_range);
1165
1221
 
1166
- VALUE param = rbs_ast_type_param(name, variance, unchecked, upper_bound, location);
1222
+ VALUE param = rbs_ast_type_param(name, variance, unchecked, upper_bound, default_type, location);
1223
+ melt_array(&params);
1167
1224
  rb_ary_push(params, param);
1168
1225
 
1169
1226
  if (state->next_token.type == pCOMMA) {
@@ -1403,7 +1460,7 @@ VALUE parse_annotation(parserstate *state) {
1403
1460
  annotations ::= {} annotation ... <annotation>
1404
1461
  | {<>}
1405
1462
  */
1406
- void parse_annotations(parserstate *state, VALUE annotations, position *annot_pos) {
1463
+ void parse_annotations(parserstate *state, VALUE *annotations, position *annot_pos) {
1407
1464
  *annot_pos = NullPosition;
1408
1465
 
1409
1466
  while (true) {
@@ -1414,7 +1471,8 @@ void parse_annotations(parserstate *state, VALUE annotations, position *annot_po
1414
1471
  *annot_pos = state->current_token.range.start;
1415
1472
  }
1416
1473
 
1417
- rb_ary_push(annotations, parse_annotation(state));
1474
+ melt_array(annotations);
1475
+ rb_ary_push(*annotations, parse_annotation(state));
1418
1476
  } else {
1419
1477
  break;
1420
1478
  }
@@ -1598,11 +1656,11 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1598
1656
 
1599
1657
  bool loop = true;
1600
1658
  while (loop) {
1601
- VALUE annotations = rb_ary_new();
1659
+ VALUE annotations = EMPTY_ARRAY;
1602
1660
  position overload_annot_pos = NullPosition;
1603
1661
 
1604
1662
  if (state->next_token.type == tANNOTATION) {
1605
- parse_annotations(state, annotations, &overload_annot_pos);
1663
+ parse_annotations(state, &annotations, &overload_annot_pos);
1606
1664
  }
1607
1665
 
1608
1666
  switch (state->next_token.type) {
@@ -1693,7 +1751,7 @@ VALUE parse_member_def(parserstate *state, bool instance_only, bool accept_overl
1693
1751
  *
1694
1752
  * @param kind
1695
1753
  * */
1696
- void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VALUE args, range *name_range, range *args_range) {
1754
+ void class_instance_name(parserstate *state, TypeNameKind kind, VALUE *name, VALUE *args, range *name_range, range *args_range) {
1697
1755
  parser_advance(state);
1698
1756
 
1699
1757
  *name = parse_type_name(state, kind, name_range);
@@ -1760,11 +1818,11 @@ VALUE parse_mixin_member(parserstate *state, bool from_interface, position comme
1760
1818
  parser_push_typevar_table(state, reset_typevar_scope);
1761
1819
 
1762
1820
  VALUE name;
1763
- VALUE args = rb_ary_new();
1821
+ VALUE args = EMPTY_ARRAY;
1764
1822
  class_instance_name(
1765
1823
  state,
1766
1824
  from_interface ? INTERFACE_NAME : (INTERFACE_NAME | CLASS_NAME),
1767
- &name, args, &name_range, &args_range
1825
+ &name, &args, &name_range, &args_range
1768
1826
  );
1769
1827
 
1770
1828
  parser_pop_typevar_table(state);
@@ -2116,13 +2174,13 @@ VALUE parse_attribute_member(parserstate *state, position comment_pos, VALUE ann
2116
2174
  | alias_member (instance only)
2117
2175
  */
2118
2176
  VALUE parse_interface_members(parserstate *state) {
2119
- VALUE members = rb_ary_new();
2177
+ VALUE members = EMPTY_ARRAY;
2120
2178
 
2121
2179
  while (state->next_token.type != kEND) {
2122
- VALUE annotations = rb_ary_new();
2180
+ VALUE annotations = EMPTY_ARRAY;
2123
2181
  position annot_pos = NullPosition;
2124
2182
 
2125
- parse_annotations(state, annotations, &annot_pos);
2183
+ parse_annotations(state, &annotations, &annot_pos);
2126
2184
 
2127
2185
  parser_advance(state);
2128
2186
 
@@ -2150,6 +2208,7 @@ VALUE parse_interface_members(parserstate *state) {
2150
2208
  );
2151
2209
  }
2152
2210
 
2211
+ melt_array(&members);
2153
2212
  rb_ary_push(members, member);
2154
2213
  }
2155
2214
 
@@ -2206,7 +2265,7 @@ VALUE parse_interface_decl(parserstate *state, position comment_pos, VALUE annot
2206
2265
  module_self_type ::= <module_name>
2207
2266
  | module_name `[` type_list <`]`>
2208
2267
  */
2209
- void parse_module_self_types(parserstate *state, VALUE array) {
2268
+ void parse_module_self_types(parserstate *state, VALUE *array) {
2210
2269
  while (true) {
2211
2270
  range self_range;
2212
2271
  range name_range;
@@ -2219,11 +2278,11 @@ void parse_module_self_types(parserstate *state, VALUE array) {
2219
2278
  VALUE module_name = parse_type_name(state, CLASS_NAME | INTERFACE_NAME, &name_range);
2220
2279
  self_range.end = name_range.end;
2221
2280
 
2222
- VALUE args = rb_ary_new();
2281
+ VALUE args = EMPTY_ARRAY;
2223
2282
  if (state->next_token.type == pLBRACKET) {
2224
2283
  parser_advance(state);
2225
2284
  args_range.start = state->current_token.range.start;
2226
- parse_type_list(state, pRBRACKET, args);
2285
+ parse_type_list(state, pRBRACKET, &args);
2227
2286
  parser_advance(state);
2228
2287
  self_range.end = args_range.end = state->current_token.range.end;
2229
2288
  }
@@ -2235,7 +2294,8 @@ void parse_module_self_types(parserstate *state, VALUE array) {
2235
2294
  rbs_loc_add_optional_child(loc, rb_intern("args"), args_range);
2236
2295
 
2237
2296
  VALUE self_type = rbs_ast_decl_module_self(module_name, args, location);
2238
- rb_ary_push(array, self_type);
2297
+ melt_array(array);
2298
+ rb_ary_push(*array, self_type);
2239
2299
 
2240
2300
  if (state->next_token.type == pCOMMA) {
2241
2301
  parser_advance(state);
@@ -2259,14 +2319,14 @@ VALUE parse_nested_decl(parserstate *state, const char *nested_in, position anno
2259
2319
  | `private`
2260
2320
  */
2261
2321
  VALUE parse_module_members(parserstate *state) {
2262
- VALUE members = rb_ary_new();
2322
+ VALUE members = EMPTY_ARRAY;
2263
2323
 
2264
2324
  while (state->next_token.type != kEND) {
2265
2325
  VALUE member;
2266
- VALUE annotations = rb_ary_new();
2326
+ VALUE annotations = EMPTY_ARRAY;
2267
2327
  position annot_pos = NullPosition;
2268
2328
 
2269
- parse_annotations(state, annotations, &annot_pos);
2329
+ parse_annotations(state, &annotations, &annot_pos);
2270
2330
 
2271
2331
  parser_advance(state);
2272
2332
 
@@ -2324,6 +2384,7 @@ VALUE parse_module_members(parserstate *state) {
2324
2384
  break;
2325
2385
  }
2326
2386
 
2387
+ melt_array(&members);
2327
2388
  rb_ary_push(members, member);
2328
2389
  }
2329
2390
 
@@ -2346,13 +2407,13 @@ VALUE parse_module_decl0(parserstate *state, range keyword_range, VALUE module_n
2346
2407
  decl_range.start = keyword_range.start;
2347
2408
 
2348
2409
  VALUE type_params = parse_type_params(state, &type_params_range, true);
2349
- VALUE self_types = rb_ary_new();
2410
+ VALUE self_types = EMPTY_ARRAY;
2350
2411
 
2351
2412
  if (state->next_token.type == pCOLON) {
2352
2413
  parser_advance(state);
2353
2414
  colon_range = state->current_token.range;
2354
2415
  self_types_range.start = state->next_token.range.start;
2355
- parse_module_self_types(state, self_types);
2416
+ parse_module_self_types(state, &self_types);
2356
2417
  self_types_range.end = state->current_token.range.end;
2357
2418
  } else {
2358
2419
  colon_range = NULL_RANGE;
@@ -2447,8 +2508,8 @@ VALUE parse_class_decl_super(parserstate *state, range *lt_range) {
2447
2508
  *lt_range = state->current_token.range;
2448
2509
  super_range.start = state->next_token.range.start;
2449
2510
 
2450
- args = rb_ary_new();
2451
- class_instance_name(state, CLASS_NAME, &name, args, &name_range, &args_range);
2511
+ args = EMPTY_ARRAY;
2512
+ class_instance_name(state, CLASS_NAME, &name, &args, &name_range, &args_range);
2452
2513
 
2453
2514
  super_range.end = state->current_token.range.end;
2454
2515
 
@@ -2601,10 +2662,10 @@ VALUE parse_nested_decl(parserstate *state, const char *nested_in, position anno
2601
2662
  }
2602
2663
 
2603
2664
  VALUE parse_decl(parserstate *state) {
2604
- VALUE annotations = rb_ary_new();
2665
+ VALUE annotations = EMPTY_ARRAY;
2605
2666
  position annot_pos = NullPosition;
2606
2667
 
2607
- parse_annotations(state, annotations, &annot_pos);
2668
+ parse_annotations(state, &annotations, &annot_pos);
2608
2669
 
2609
2670
  parser_advance(state);
2610
2671
  switch (state->current_token.type) {
@@ -2645,10 +2706,11 @@ VALUE parse_namespace(parserstate *state, range *rg) {
2645
2706
  parser_advance(state);
2646
2707
  }
2647
2708
 
2648
- VALUE path = rb_ary_new();
2709
+ VALUE path = EMPTY_ARRAY;
2649
2710
 
2650
2711
  while (true) {
2651
2712
  if (state->next_token.type == tUIDENT && state->next_token2.type == pCOLON2) {
2713
+ melt_array(&path);
2652
2714
  rb_ary_push(path, ID2SYM(INTERN_TOKEN(state, state->next_token)));
2653
2715
  if (null_position_p(rg->start)) {
2654
2716
  rg->start = state->next_token.range.start;
@@ -2788,14 +2850,16 @@ VALUE parse_use_directive(parserstate *state) {
2788
2850
  }
2789
2851
 
2790
2852
  VALUE parse_signature(parserstate *state) {
2791
- VALUE dirs = rb_ary_new();
2792
- VALUE decls = rb_ary_new();
2853
+ VALUE dirs = EMPTY_ARRAY;
2854
+ VALUE decls = EMPTY_ARRAY;
2793
2855
 
2794
2856
  while (state->next_token.type == kUSE) {
2857
+ melt_array(&dirs);
2795
2858
  rb_ary_push(dirs, parse_use_directive(state));
2796
2859
  }
2797
2860
 
2798
2861
  while (state->next_token.type != pEOF) {
2862
+ melt_array(&decls);
2799
2863
  rb_ary_push(decls, parse_decl(state));
2800
2864
  }
2801
2865
 
@@ -2836,7 +2900,10 @@ parse_type_try(VALUE a) {
2836
2900
  static VALUE
2837
2901
  rbsparser_parse_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2838
2902
  {
2839
- parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2903
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2904
+ StringValue(string);
2905
+ lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2906
+ parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2840
2907
  struct parse_type_arg arg = {
2841
2908
  parser,
2842
2909
  require_eof
@@ -2864,7 +2931,10 @@ parse_method_type_try(VALUE a) {
2864
2931
  static VALUE
2865
2932
  rbsparser_parse_method_type(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos, VALUE variables, VALUE require_eof)
2866
2933
  {
2867
- parserstate *parser = alloc_parser(buffer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2934
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2935
+ StringValue(string);
2936
+ lexstate *lexer = alloc_lexer(string, FIX2INT(start_pos), FIX2INT(end_pos));
2937
+ parserstate *parser = alloc_parser(buffer, lexer, FIX2INT(start_pos), FIX2INT(end_pos), variables);
2868
2938
  struct parse_type_arg arg = {
2869
2939
  parser,
2870
2940
  require_eof
@@ -2881,13 +2951,18 @@ parse_signature_try(VALUE a) {
2881
2951
  static VALUE
2882
2952
  rbsparser_parse_signature(VALUE self, VALUE buffer, VALUE end_pos)
2883
2953
  {
2884
- parserstate *parser = alloc_parser(buffer, 0, FIX2INT(end_pos), Qnil);
2954
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2955
+ StringValue(string);
2956
+ lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
2957
+ parserstate *parser = alloc_parser(buffer, lexer, 0, FIX2INT(end_pos), Qnil);
2885
2958
  return rb_ensure(parse_signature_try, (VALUE)parser, ensure_free_parser, (VALUE)parser);
2886
2959
  }
2887
2960
 
2888
2961
  static VALUE
2889
2962
  rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
2890
- lexstate *lexer = alloc_lexer(buffer, 0, FIX2INT(end_pos));
2963
+ VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
2964
+ StringValue(string);
2965
+ lexstate *lexer = alloc_lexer(string, 0, FIX2INT(end_pos));
2891
2966
  VALUE results = rb_ary_new();
2892
2967
 
2893
2968
  token token = NullToken;
@@ -2906,6 +2981,11 @@ rbsparser_lex(VALUE self, VALUE buffer, VALUE end_pos) {
2906
2981
 
2907
2982
  void rbs__init_parser(void) {
2908
2983
  RBS_Parser = rb_define_class_under(RBS, "Parser", rb_cObject);
2984
+ rb_gc_register_mark_object(RBS_Parser);
2985
+ VALUE empty_array = rb_obj_freeze(rb_ary_new());
2986
+ rb_gc_register_mark_object(empty_array);
2987
+ EMPTY_ARRAY = empty_array;
2988
+
2909
2989
  rb_define_singleton_method(RBS_Parser, "_parse_type", rbsparser_parse_type, 5);
2910
2990
  rb_define_singleton_method(RBS_Parser, "_parse_method_type", rbsparser_parse_method_type, 5);
2911
2991
  rb_define_singleton_method(RBS_Parser, "_parse_signature", rbsparser_parse_signature, 2);
@@ -274,11 +274,7 @@ VALUE comment_to_ruby(comment *com, VALUE buffer) {
274
274
  );
275
275
  }
276
276
 
277
- lexstate *alloc_lexer(VALUE buffer, int start_pos, int end_pos) {
278
- VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
279
-
280
- StringValue(string);
281
-
277
+ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) {
282
278
  if (start_pos < 0 || end_pos < 0) {
283
279
  rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
284
280
  }
@@ -295,8 +291,7 @@ lexstate *alloc_lexer(VALUE buffer, int start_pos, int end_pos) {
295
291
  return lexer;
296
292
  }
297
293
 
298
- parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables) {
299
- lexstate *lexer = alloc_lexer(buffer, start_pos, end_pos);
294
+ parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_pos, VALUE variables) {
300
295
  parserstate *parser = calloc(1, sizeof(parserstate));
301
296
  parser->lexstate = lexer;
302
297
  parser->buffer = buffer;
@@ -97,20 +97,21 @@ bool parser_typevar_member(parserstate *state, ID id);
97
97
  * Allocate new lexstate object.
98
98
  *
99
99
  * ```
100
- * alloc_lexer(buffer, 0, 31) // New lexstate with buffer
100
+ * VALUE string = rb_funcall(buffer, rb_intern("content"), 0);
101
+ * alloc_lexer(string, 0, 31) // New lexstate with buffer content
101
102
  * ```
102
103
  * */
103
- lexstate *alloc_lexer(VALUE buffer, int start_pos, int end_pos);
104
+ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos);
104
105
 
105
106
  /**
106
107
  * Allocate new parserstate object.
107
108
  *
108
109
  * ```
109
- * alloc_parser(buffer, 0, 1, variables) // New parserstate with variables
110
- * alloc_parser(buffer, 3, 5, Qnil) // New parserstate without variables
110
+ * alloc_parser(buffer, lexer, 0, 1, variables) // New parserstate with variables
111
+ * alloc_parser(buffer, lexer, 3, 5, Qnil) // New parserstate without variables
111
112
  * ```
112
113
  * */
113
- parserstate *alloc_parser(VALUE buffer, int start_pos, int end_pos, VALUE variables);
114
+ parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_pos, VALUE variables);
114
115
  void free_parser(parserstate *parser);
115
116
  /**
116
117
  * Advance one token.
@@ -307,11 +307,12 @@ VALUE rbs_ast_annotation(VALUE string, VALUE location) {
307
307
  );
308
308
  }
309
309
 
310
- VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE location) {
310
+ VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE default_type, VALUE location) {
311
311
  VALUE args = rb_hash_new();
312
312
  rb_hash_aset(args, ID2SYM(rb_intern("name")), name);
313
313
  rb_hash_aset(args, ID2SYM(rb_intern("variance")), variance);
314
314
  rb_hash_aset(args, ID2SYM(rb_intern("upper_bound")), upper_bound);
315
+ rb_hash_aset(args, ID2SYM(rb_intern("default_type")), default_type);
315
316
  rb_hash_aset(args, ID2SYM(rb_intern("location")), location);
316
317
 
317
318
  VALUE type_param = CLASS_NEW_INSTANCE(RBS_AST_TypeParam, 1, &args);
@@ -6,7 +6,7 @@
6
6
  VALUE rbs_alias(VALUE typename, VALUE args, VALUE location);
7
7
  VALUE rbs_ast_annotation(VALUE string, VALUE location);
8
8
  VALUE rbs_ast_comment(VALUE string, VALUE location);
9
- VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE location);
9
+ VALUE rbs_ast_type_param(VALUE name, VALUE variance, bool unchecked, VALUE upper_bound, VALUE default_type, VALUE location);
10
10
  VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment);
11
11
  VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location);
12
12
  VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment);
@@ -104,6 +104,18 @@ module RBS
104
104
  @comment = comment
105
105
  end
106
106
 
107
+ def update(name: self.name, type_params: self.type_params, super_class: self.super_class, members: self.members, annotations: self.annotations, location: self.location, comment: self.comment)
108
+ self.class.new(
109
+ name: name,
110
+ type_params: type_params,
111
+ super_class: super_class,
112
+ members: members,
113
+ annotations: annotations,
114
+ location: location,
115
+ comment: comment
116
+ )
117
+ end
118
+
107
119
  def ==(other)
108
120
  other.is_a?(Class) &&
109
121
  other.name == name &&
@@ -192,6 +204,19 @@ module RBS
192
204
  @comment = comment
193
205
  end
194
206
 
207
+ def update(name: self.name, type_params: self.type_params, members: self.members, self_types: self.self_types, annotations: self.annotations, location: self.location, comment: self.comment)
208
+ self.class.new(
209
+ name: name,
210
+ type_params: type_params,
211
+ members: members,
212
+ self_types: self_types,
213
+ annotations: annotations,
214
+ location: location,
215
+ comment: comment
216
+ )
217
+ end
218
+
219
+
195
220
  def ==(other)
196
221
  other.is_a?(Module) &&
197
222
  other.name == name &&
@@ -239,6 +264,17 @@ module RBS
239
264
  @comment = comment
240
265
  end
241
266
 
267
+ def update(name: self.name, type_params: self.type_params, members: self.members, annotations: self.annotations, location: self.location, comment: self.comment)
268
+ self.class.new(
269
+ name: name,
270
+ type_params: type_params,
271
+ members: members,
272
+ annotations: annotations,
273
+ location: location,
274
+ comment: comment
275
+ )
276
+ end
277
+
242
278
  def ==(other)
243
279
  other.is_a?(Interface) &&
244
280
  other.name == name &&