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.
- checksums.yaml +4 -4
- data/.github/workflows/c-check.yml +3 -0
- data/.github/workflows/dependabot.yml +1 -1
- data/.github/workflows/jruby.yml +67 -0
- data/.github/workflows/milestone.yml +4 -1
- data/.github/workflows/ruby.yml +40 -0
- data/.github/workflows/rust.yml +3 -2
- data/.github/workflows/truffleruby.yml +54 -0
- data/.github/workflows/typecheck.yml +3 -0
- data/.github/workflows/wasm.yml +53 -0
- data/.github/workflows/windows.yml +3 -0
- data/.gitignore +7 -0
- data/CHANGELOG.md +0 -8
- data/README.md +3 -3
- data/Rakefile +118 -3
- data/Steepfile +7 -0
- data/config.yml +24 -0
- data/core/array.rbs +144 -144
- data/core/builtin.rbs +7 -6
- data/core/class.rbs +5 -3
- data/core/enumerable.rbs +109 -109
- data/core/enumerator/product.rbs +5 -5
- data/core/enumerator.rbs +28 -28
- data/core/file.rbs +24 -1018
- data/core/file_constants.rbs +463 -0
- data/core/file_stat.rbs +534 -0
- data/core/hash.rbs +117 -101
- data/core/integer.rbs +21 -58
- data/core/io.rbs +25 -7
- data/core/kernel.rbs +26 -11
- data/core/module.rbs +88 -74
- data/core/numeric.rbs +3 -0
- data/core/object_space/weak_key_map.rbs +7 -7
- data/core/range.rbs +23 -23
- data/core/rbs/ops.rbs +154 -0
- data/core/rbs/unnamed/argf.rbs +3 -3
- data/core/ruby_vm.rbs +40 -0
- data/core/set.rbs +3 -3
- data/core/struct.rbs +16 -16
- data/core/thread.rbs +6 -6
- data/docs/CONTRIBUTING.md +2 -1
- data/docs/inline.md +65 -7
- data/docs/rbs_by_example.md +20 -20
- data/docs/syntax.md +2 -2
- data/docs/wasm_serialization.md +80 -0
- data/ext/rbs_extension/ast_translation.c +1298 -956
- data/ext/rbs_extension/ast_translation.h +4 -0
- data/ext/rbs_extension/class_constants.c +2 -0
- data/ext/rbs_extension/class_constants.h +1 -0
- data/ext/rbs_extension/extconf.rb +1 -0
- data/ext/rbs_extension/main.c +139 -4
- data/include/rbs/ast.h +323 -298
- data/include/rbs/defines.h +13 -0
- data/include/rbs/lexer.h +1 -0
- data/include/rbs/serialize.h +39 -0
- data/lib/rbs/annotate/rdoc_annotator.rb +27 -31
- data/lib/rbs/ast/ruby/annotations.rb +42 -0
- data/lib/rbs/ast/ruby/comment_block.rb +6 -4
- data/lib/rbs/ast/ruby/declarations.rb +11 -1
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +1 -1
- data/lib/rbs/ast/ruby/members.rb +40 -1
- data/lib/rbs/buffer.rb +48 -11
- data/lib/rbs/cli.rb +3 -5
- data/lib/rbs/collection/config/lockfile_generator.rb +14 -1
- data/lib/rbs/collection/sources/git.rb +6 -0
- data/lib/rbs/definition_builder/method_builder.rb +12 -6
- data/lib/rbs/environment.rb +10 -3
- data/lib/rbs/inline_parser.rb +54 -28
- data/lib/rbs/namespace.rb +47 -11
- data/lib/rbs/prototype/runtime.rb +2 -0
- data/lib/rbs/resolver/type_name_resolver.rb +12 -14
- data/lib/rbs/rewriter.rb +70 -0
- data/lib/rbs/test/type_check.rb +6 -1
- data/lib/rbs/type_name.rb +33 -13
- data/lib/rbs/unit_test/type_assertions.rb +9 -0
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/wasm/deserializer.rb +213 -0
- data/lib/rbs/wasm/location.rb +61 -0
- data/lib/rbs/wasm/parser.rb +137 -0
- data/lib/rbs/wasm/runtime.rb +217 -0
- data/lib/rbs/wasm/serialization_schema.rb +110 -0
- data/lib/rbs.rb +14 -2
- data/rbs.gemspec +19 -2
- data/sig/annotate/rdoc_annotater.rbs +12 -9
- data/sig/ast/ruby/annotations.rbs +49 -0
- data/sig/ast/ruby/members.rbs +21 -1
- data/sig/buffer.rbs +19 -1
- data/sig/collection/config/lockfile_generator.rbs +2 -0
- data/sig/inline_parser.rbs +2 -0
- data/sig/manifest.yaml +0 -1
- data/sig/namespace.rbs +20 -0
- data/sig/parser.rbs +10 -0
- data/sig/resolver/type_name_resolver.rbs +2 -4
- data/sig/rewriter.rbs +45 -0
- data/sig/typename.rbs +15 -0
- data/sig/unit_test/type_assertions.rbs +4 -0
- data/sig/wasm/deserializer.rbs +66 -0
- data/sig/wasm/serialization_schema.rbs +13 -0
- data/src/ast.c +186 -162
- data/src/lexer.c +137 -114
- data/src/lexer.re +1 -0
- data/src/lexstate.c +6 -1
- data/src/parser.c +55 -5
- data/src/serialize.c +958 -0
- data/src/util/rbs_allocator.c +1 -4
- data/stdlib/abbrev/0/array.rbs +1 -1
- data/stdlib/csv/0/csv.rbs +5 -5
- data/stdlib/digest/0/digest.rbs +1 -1
- data/stdlib/etc/0/etc.rbs +18 -4
- data/stdlib/fileutils/0/fileutils.rbs +21 -21
- data/stdlib/json/0/json.rbs +6 -6
- data/stdlib/openssl/0/openssl.rbs +7 -7
- data/stdlib/resolv/0/resolv.rbs +1 -1
- data/stdlib/shellwords/0/shellwords.rbs +1 -1
- data/stdlib/stringio/0/stringio.rbs +32 -10
- data/stdlib/strscan/0/string_scanner.rbs +74 -55
- data/stdlib/tsort/0/cyclic.rbs +1 -1
- data/stdlib/tsort/0/interfaces.rbs +8 -8
- data/stdlib/tsort/0/tsort.rbs +9 -9
- data/stdlib/zlib/0/gzip_reader.rbs +2 -2
- data/wasm/README.md +59 -0
- data/wasm/rbs_wasm.c +411 -0
- metadata +23 -5
- data/.vscode/extensions.json +0 -5
- data/.vscode/settings.json +0 -19
data/docs/rbs_by_example.md
CHANGED
|
@@ -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[
|
|
110
|
+
class Array[E]
|
|
111
111
|
def *: (String) -> String
|
|
112
|
-
| (Integer) -> Array[
|
|
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 `
|
|
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[
|
|
154
|
-
def first: () ->
|
|
155
|
-
| (Integer) -> Array[
|
|
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 `
|
|
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 `(
|
|
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[
|
|
226
|
-
def filter: () { (
|
|
227
|
-
| () -> ::Enumerator[
|
|
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[
|
|
268
|
-
def collect: [U] () { (
|
|
269
|
-
| () -> Enumerator[
|
|
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 `
|
|
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[
|
|
288
|
-
def partition: () { (
|
|
289
|
-
| () -> ::Enumerator[
|
|
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[
|
|
303
|
+
class Enumerable[E]
|
|
304
304
|
def to_h: () -> ::Hash[untyped, untyped]
|
|
305
|
-
| [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[
|
|
200
|
-
def find: () { (
|
|
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.
|