rbs 4.0.3 → 4.1.0.pre.2

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 (125) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/c-check.yml +3 -0
  3. data/.github/workflows/dependabot.yml +1 -1
  4. data/.github/workflows/jruby.yml +67 -0
  5. data/.github/workflows/milestone.yml +4 -1
  6. data/.github/workflows/ruby.yml +40 -0
  7. data/.github/workflows/rust.yml +3 -2
  8. data/.github/workflows/truffleruby.yml +54 -0
  9. data/.github/workflows/typecheck.yml +3 -0
  10. data/.github/workflows/wasm.yml +53 -0
  11. data/.github/workflows/windows.yml +3 -0
  12. data/.gitignore +7 -0
  13. data/CHANGELOG.md +0 -8
  14. data/README.md +3 -3
  15. data/Rakefile +118 -3
  16. data/Steepfile +7 -0
  17. data/config.yml +24 -0
  18. data/core/array.rbs +144 -144
  19. data/core/builtin.rbs +7 -6
  20. data/core/class.rbs +5 -3
  21. data/core/enumerable.rbs +109 -109
  22. data/core/enumerator/product.rbs +5 -5
  23. data/core/enumerator.rbs +28 -28
  24. data/core/file.rbs +24 -1018
  25. data/core/file_constants.rbs +463 -0
  26. data/core/file_stat.rbs +534 -0
  27. data/core/hash.rbs +117 -101
  28. data/core/integer.rbs +21 -58
  29. data/core/io.rbs +25 -7
  30. data/core/kernel.rbs +26 -11
  31. data/core/module.rbs +88 -74
  32. data/core/numeric.rbs +3 -0
  33. data/core/object_space/weak_key_map.rbs +7 -7
  34. data/core/range.rbs +23 -23
  35. data/core/rbs/ops.rbs +154 -0
  36. data/core/rbs/unnamed/argf.rbs +3 -3
  37. data/core/ruby_vm.rbs +40 -0
  38. data/core/set.rbs +3 -3
  39. data/core/struct.rbs +16 -16
  40. data/core/thread.rbs +6 -6
  41. data/docs/CONTRIBUTING.md +2 -1
  42. data/docs/inline.md +65 -7
  43. data/docs/rbs_by_example.md +20 -20
  44. data/docs/syntax.md +2 -2
  45. data/docs/wasm_serialization.md +80 -0
  46. data/ext/rbs_extension/ast_translation.c +1298 -956
  47. data/ext/rbs_extension/ast_translation.h +4 -0
  48. data/ext/rbs_extension/class_constants.c +2 -0
  49. data/ext/rbs_extension/class_constants.h +1 -0
  50. data/ext/rbs_extension/extconf.rb +1 -0
  51. data/ext/rbs_extension/main.c +139 -4
  52. data/include/rbs/ast.h +323 -298
  53. data/include/rbs/defines.h +13 -0
  54. data/include/rbs/lexer.h +1 -0
  55. data/include/rbs/serialize.h +39 -0
  56. data/lib/rbs/annotate/rdoc_annotator.rb +27 -31
  57. data/lib/rbs/ast/ruby/annotations.rb +42 -0
  58. data/lib/rbs/ast/ruby/comment_block.rb +6 -4
  59. data/lib/rbs/ast/ruby/declarations.rb +11 -1
  60. data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
  61. data/lib/rbs/ast/ruby/members.rb +40 -1
  62. data/lib/rbs/buffer.rb +48 -11
  63. data/lib/rbs/cli.rb +3 -5
  64. data/lib/rbs/collection/config/lockfile_generator.rb +14 -1
  65. data/lib/rbs/collection/sources/git.rb +6 -0
  66. data/lib/rbs/definition_builder/method_builder.rb +12 -6
  67. data/lib/rbs/environment.rb +10 -3
  68. data/lib/rbs/inline_parser.rb +54 -28
  69. data/lib/rbs/namespace.rb +47 -11
  70. data/lib/rbs/prototype/runtime.rb +2 -0
  71. data/lib/rbs/resolver/type_name_resolver.rb +12 -14
  72. data/lib/rbs/rewriter.rb +70 -0
  73. data/lib/rbs/test/type_check.rb +6 -1
  74. data/lib/rbs/type_name.rb +33 -13
  75. data/lib/rbs/unit_test/type_assertions.rb +9 -0
  76. data/lib/rbs/version.rb +1 -1
  77. data/lib/rbs/wasm/deserializer.rb +213 -0
  78. data/lib/rbs/wasm/location.rb +61 -0
  79. data/lib/rbs/wasm/parser.rb +137 -0
  80. data/lib/rbs/wasm/runtime.rb +217 -0
  81. data/lib/rbs/wasm/serialization_schema.rb +110 -0
  82. data/lib/rbs.rb +14 -2
  83. data/rbs.gemspec +19 -2
  84. data/sig/annotate/rdoc_annotater.rbs +12 -9
  85. data/sig/ast/ruby/annotations.rbs +49 -0
  86. data/sig/ast/ruby/members.rbs +21 -1
  87. data/sig/buffer.rbs +19 -1
  88. data/sig/collection/config/lockfile_generator.rbs +2 -0
  89. data/sig/inline_parser.rbs +2 -0
  90. data/sig/manifest.yaml +0 -1
  91. data/sig/namespace.rbs +20 -0
  92. data/sig/parser.rbs +10 -0
  93. data/sig/resolver/type_name_resolver.rbs +2 -4
  94. data/sig/rewriter.rbs +45 -0
  95. data/sig/typename.rbs +15 -0
  96. data/sig/unit_test/type_assertions.rbs +4 -0
  97. data/sig/wasm/deserializer.rbs +66 -0
  98. data/sig/wasm/serialization_schema.rbs +13 -0
  99. data/src/ast.c +186 -162
  100. data/src/lexer.c +137 -114
  101. data/src/lexer.re +1 -0
  102. data/src/lexstate.c +6 -1
  103. data/src/parser.c +55 -5
  104. data/src/serialize.c +958 -0
  105. data/src/util/rbs_allocator.c +1 -4
  106. data/stdlib/abbrev/0/array.rbs +1 -1
  107. data/stdlib/csv/0/csv.rbs +5 -5
  108. data/stdlib/digest/0/digest.rbs +1 -1
  109. data/stdlib/etc/0/etc.rbs +18 -4
  110. data/stdlib/fileutils/0/fileutils.rbs +21 -21
  111. data/stdlib/json/0/json.rbs +6 -6
  112. data/stdlib/openssl/0/openssl.rbs +7 -7
  113. data/stdlib/resolv/0/resolv.rbs +1 -1
  114. data/stdlib/shellwords/0/shellwords.rbs +1 -1
  115. data/stdlib/stringio/0/stringio.rbs +32 -10
  116. data/stdlib/strscan/0/string_scanner.rbs +74 -55
  117. data/stdlib/tsort/0/cyclic.rbs +1 -1
  118. data/stdlib/tsort/0/interfaces.rbs +8 -8
  119. data/stdlib/tsort/0/tsort.rbs +9 -9
  120. data/stdlib/zlib/0/gzip_reader.rbs +2 -2
  121. data/wasm/README.md +59 -0
  122. data/wasm/rbs_wasm.c +411 -0
  123. metadata +23 -5
  124. data/.vscode/extensions.json +0 -5
  125. data/.vscode/settings.json +0 -19
@@ -107,14 +107,14 @@ end
107
107
  For now, it's safe to ignore them, but they're included for completeness.
108
108
 
109
109
  ```rbs
110
- class Array[Elem]
110
+ class Array[E]
111
111
  def *: (String) -> String
112
- | (Integer) -> Array[Elem]
112
+ | (Integer) -> Array[E]
113
113
  end
114
114
  ```
115
115
 
116
116
  `Array`'s `*` method, when given a `String` returns a `String`. When given an
117
- `Integer`, it returns an `Array` of the same contained type `Elem` (in our example case, `Elem` corresponds to `Integer`).
117
+ `Integer`, it returns an `Array` of the same contained type `E` (in our example case, `E` corresponds to `Integer`).
118
118
 
119
119
  ### Union types
120
120
 
@@ -150,9 +150,9 @@ end
150
150
  ```
151
151
 
152
152
  ```rbs
153
- class Enumerable[Elem]
154
- def first: () -> Elem?
155
- | (Integer) -> Array[Elem]
153
+ class Enumerable[E]
154
+ def first: () -> E?
155
+ | (Integer) -> Array[E]
156
156
  end
157
157
  ```
158
158
 
@@ -160,12 +160,12 @@ end
160
160
 
161
161
  When called with no arguments, the return value will either be an instance of
162
162
  whatever type is contained in the enumerable, or `nil`. We represent that with
163
- the type variable `Elem`, and the `?` suffix nilable marker.
163
+ the type variable `E`, and the `?` suffix nilable marker.
164
164
 
165
165
  When called with an `Integer` positional argument, the return value will be an
166
166
  `Array` of whatever type is contained.
167
167
 
168
- The `?` syntax is a convenient shorthand for a union with nil. An equivalent union type would be `(Elem | nil)`.
168
+ The `?` syntax is a convenient shorthand for a union with nil. An equivalent union type would be `(E | nil)`.
169
169
 
170
170
  ### Keyword Arguments
171
171
 
@@ -222,9 +222,9 @@ end
222
222
  ```
223
223
 
224
224
  ```rbs
225
- class Array[Elem]
226
- def filter: () { (Elem) -> boolish } -> ::Array[Elem]
227
- | () -> ::Enumerator[Elem, ::Array[Elem]]
225
+ class Array[E]
226
+ def filter: () { (E) -> boolish } -> ::Array[E]
227
+ | () -> ::Enumerator[E, ::Array[E]]
228
228
  end
229
229
  ```
230
230
 
@@ -264,13 +264,13 @@ a.collect.with_index {|x, i| x * i}
264
264
  ```
265
265
 
266
266
  ```rbs
267
- class Array[Elem]
268
- def collect: [U] () { (Elem) -> U } -> Array[U]
269
- | () -> Enumerator[Elem, Array[untyped]]
267
+ class Array[E]
268
+ def collect: [U] () { (E) -> U } -> Array[U]
269
+ | () -> Enumerator[E, Array[untyped]]
270
270
  end
271
271
  ```
272
272
 
273
- Type variables can also be introduced in methods. Here, in `Array`'s `#collect` method, we introduce a type variable `U`. The block passed to `#collect` will receive a parameter of type `Elem`, and return a value of type `U`. Then `#collect` will return an `Array` of type `U`.
273
+ Type variables can also be introduced in methods. Here, in `Array`'s `#collect` method, we introduce a type variable `U`. The block passed to `#collect` will receive a parameter of type `E`, and return a value of type `U`. Then `#collect` will return an `Array` of type `U`.
274
274
 
275
275
  In this example, the method receives its signature from the inferred return type of the passed block. When then block is absent, as in when the method returns an `Enumerator`, we can't infer the type, and so the return value of the enumerator can only be described as `Array[untyped]`.
276
276
 
@@ -284,9 +284,9 @@ In this example, the method receives its signature from the inferred return type
284
284
  ```
285
285
 
286
286
  ```rbs
287
- class Enumerable[Elem]
288
- def partition: () { (Elem) -> boolish } -> [Array[Elem], Array[Elem]]
289
- | () -> ::Enumerator[Elem, [Array[Elem], Array[Elem] ]]
287
+ class Enumerable[E]
288
+ def partition: () { (E) -> boolish } -> [Array[E], Array[E]]
289
+ | () -> ::Enumerator[E, [Array[E], Array[E] ]]
290
290
  end
291
291
  ```
292
292
 
@@ -300,9 +300,9 @@ Tuples can be of any size, and they can have mixed types.
300
300
  ```
301
301
 
302
302
  ```rbs
303
- class Enumerable[Elem]
303
+ class Enumerable[E]
304
304
  def to_h: () -> ::Hash[untyped, untyped]
305
- | [T, U] () { (Elem) -> [T, U] } -> ::Hash[T, U]
305
+ | [T, U] () { (E) -> [T, U] } -> ::Hash[T, U]
306
306
  end
307
307
  ```
308
308
 
data/docs/syntax.md CHANGED
@@ -196,8 +196,8 @@ It is an alias of `top` type, and you can use `boolish` if we want to allow any
196
196
  We can see an example at the definition of `Enumerable#find`:
197
197
 
198
198
  ```rbs
199
- module Enumerable[Elem, Return]
200
- def find: () { (Elem) -> boolish } -> Elem?
199
+ module Enumerable[E, R]
200
+ def find: () { (E) -> boolish } -> E?
201
201
  end
202
202
  ```
203
203
 
@@ -0,0 +1,80 @@
1
+ # RBS AST binary serialization
2
+
3
+ This document describes the binary format used to move a parsed RBS AST out of
4
+ the parser and into Ruby objects without going through the Ruby C API. It exists
5
+ so that RBS can run on Ruby implementations that cannot load the C extension
6
+ (notably JRuby): the parser runs inside WebAssembly, serializes the result with
7
+ this format, and the host rebuilds `RBS::AST` objects in pure Ruby.
8
+
9
+ The encoder (`rbs_serialize_node`, `src/serialize.c`) and the schema that drives
10
+ the decoder (`RBS::WASM::SerializationSchema`, `lib/rbs/wasm/serialization_schema.rb`)
11
+ are both generated from `config.yml`, so they always agree. The decoder itself
12
+ is `RBS::WASM::Deserializer`.
13
+
14
+ ## Conventions
15
+
16
+ - All multi-byte integers are **little-endian**.
17
+ - `u8`, `u32` are unsigned; `i32` is signed.
18
+ - `str` is a `u32` byte length followed by that many raw bytes (no terminator).
19
+ - A value is reconstructed to mirror exactly what `ast_translation.c` produces,
20
+ including string encodings: string/integer literal nodes are UTF-8, while
21
+ comments, annotations and symbols use the source buffer's encoding.
22
+
23
+ ## Nodes
24
+
25
+ Every node begins with a `u8` **tag**:
26
+
27
+ - `0` — a NULL node (`nil` on the Ruby side).
28
+ - `1..N` — a node type, in the order they appear in `SerializationSchema::SCHEMA`.
29
+ - `SYMBOL_TAG` (`N + 1`) — an interned symbol, followed by `str` (the symbol's
30
+ bytes). Decoded with `String#to_sym`.
31
+
32
+ A few node types are encoded specially, matching their bespoke handling in
33
+ `ast_translation.c`:
34
+
35
+ | Node | Payload after tag | Decoded as |
36
+ | --- | --- | --- |
37
+ | `RBS::AST::Bool` | `u8` | `true` / `false` |
38
+ | `RBS::AST::Integer` | `str` | `String#to_i` |
39
+ | `RBS::AST::String` | `str` | the string (UTF-8) |
40
+ | `RBS::Types::Record::FieldType` | node, then `u8` | `[type, required]` |
41
+ | `RBS::Signature` | node-list, then node-list | `[directives, declarations]` |
42
+ | `RBS::Namespace` | node-list, then `u8` | `RBS::Namespace[path, absolute]` |
43
+ | `RBS::TypeName` | node, then node | `RBS::TypeName[namespace, name]` |
44
+
45
+ Every other node is encoded generically:
46
+
47
+ 1. If the node exposes a location, its **base location** is written (see below),
48
+ followed by one location range per declared child, in order.
49
+ 2. Each field is written in declaration order, encoded by its type (see below).
50
+
51
+ The decoder constructs `Klass.new(location:, **fields)` (omitting `location:`
52
+ for nodes that do not expose one). For `Class`, `Module`, `Interface`,
53
+ `TypeAlias` and `MethodType`, `RBS::AST::TypeParam.resolve_variables` is applied
54
+ to `type_params` first, exactly as the C translation does.
55
+
56
+ ## Fields
57
+
58
+ | Field type | Encoding |
59
+ | --- | --- |
60
+ | node (`rbs_node`, `rbs_type_name`, `rbs_ast_comment`, `rbs_ast_symbol`, ...) | a node (recursive; NULL allowed) |
61
+ | `rbs_node_list` | `u32` count, then that many nodes |
62
+ | `rbs_hash` | `u32` count, then count × (key node, value node) |
63
+ | `rbs_string` | `str` (source encoding) |
64
+ | `bool` | `u8` |
65
+ | enum | `u8` index into the enum's values (see `SCHEMA`) |
66
+ | `rbs_location_range` | a location range |
67
+ | `rbs_location_range_list` | `u32` count, then that many location ranges |
68
+ | `rbs_attr_ivar_name` | `u8` tag: `0` → `nil`, `1` → `false`, `2` → `str` → symbol |
69
+
70
+ ## Location ranges
71
+
72
+ A location range is a `u8` presence flag:
73
+
74
+ - `0` — null range (`nil`, or a node with no location).
75
+ - `1` — followed by `i32` start and `i32` end **character** positions.
76
+
77
+ The base location and child ranges together let the decoder rebuild an
78
+ `RBS::Location` (with its required/optional children) through the public
79
+ `RBS::Location` API, so the same decoder works whether `RBS::Location` is backed
80
+ by the C extension or a pure-Ruby implementation.