rbs 3.2.2 → 3.3.0.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/comments.yml +1 -1
- data/.github/workflows/ruby.yml +7 -2
- data/CHANGELOG.md +85 -0
- data/Gemfile.lock +14 -14
- data/README.md +11 -2
- data/Rakefile +10 -7
- data/Steepfile +7 -7
- data/core/basic_object.rbs +7 -7
- data/core/binding.rbs +3 -3
- data/core/builtin.rbs +171 -5
- data/core/constants.rbs +17 -17
- data/core/dir.rbs +3 -3
- data/core/encoding.rbs +434 -628
- data/core/enumerator.rbs +37 -0
- data/core/exception.rbs +11 -11
- data/core/false_class.rbs +5 -11
- data/core/fiber.rbs +3 -3
- data/core/file_test.rbs +28 -26
- data/core/kernel.rbs +900 -21
- data/core/marshal.rbs +24 -14
- data/core/match_data.rbs +8 -8
- data/core/math.rbs +57 -53
- data/core/method.rbs +3 -1
- data/core/module.rbs +38 -36
- data/core/nil_class.rbs +7 -13
- data/core/object.rbs +3 -966
- data/core/process.rbs +3 -3
- data/core/ractor.rbs +2 -2
- data/core/rb_config.rbs +64 -43
- data/core/regexp.rbs +3 -3
- data/core/signal.rbs +10 -4
- data/core/struct.rbs +1 -1
- data/core/thread.rbs +7 -7
- data/core/thread_group.rbs +9 -9
- data/core/true_class.rbs +5 -11
- data/core/unbound_method.rbs +56 -7
- data/core/warning.rbs +33 -0
- data/docs/collection.md +56 -6
- data/docs/data_and_struct.md +57 -0
- data/docs/stdlib.md +61 -2
- data/docs/syntax.md +123 -2
- data/ext/rbs_extension/lexer.c +624 -569
- data/ext/rbs_extension/lexer.h +1 -0
- data/ext/rbs_extension/lexer.re +1 -0
- data/ext/rbs_extension/lexstate.c +1 -0
- data/ext/rbs_extension/parser.c +6 -0
- data/goodcheck.yml +2 -2
- data/lib/rbs/annotate/formatter.rb +13 -3
- data/lib/rbs/annotate/rdoc_source.rb +10 -1
- data/lib/rbs/cli/colored_io.rb +48 -0
- data/lib/rbs/cli/diff.rb +80 -0
- data/lib/rbs/cli.rb +151 -16
- data/lib/rbs/collection/config/lockfile.rb +0 -25
- data/lib/rbs/collection/config/lockfile_generator.rb +0 -6
- data/lib/rbs/collection/installer.rb +1 -1
- data/lib/rbs/collection/sources/git.rb +6 -4
- data/lib/rbs/collection/sources/local.rb +7 -5
- data/lib/rbs/diff.rb +104 -0
- data/lib/rbs/environment.rb +1 -1
- data/lib/rbs/method_type.rb +23 -0
- data/lib/rbs/prototype/rb.rb +2 -9
- data/lib/rbs/prototype/runtime/helpers.rb +59 -0
- data/lib/rbs/prototype/runtime/value_object_generator.rb +236 -0
- data/lib/rbs/prototype/runtime.rb +234 -150
- data/lib/rbs/sorter.rb +144 -117
- data/lib/rbs/test/guaranteed.rb +31 -0
- data/lib/rbs/test/type_check.rb +4 -4
- data/lib/rbs/test.rb +3 -0
- data/lib/rbs/types.rb +184 -3
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs/writer.rb +4 -4
- data/lib/rbs.rb +1 -0
- data/rbs.gemspec +1 -0
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/cli/colored_io.rbs +15 -0
- data/sig/cli/diff.rbs +21 -0
- data/sig/cli.rbs +2 -0
- data/sig/collection/config/lockfile.rbs +0 -6
- data/sig/diff.rbs +23 -0
- data/sig/errors.rbs +1 -5
- data/sig/method_types.rbs +6 -0
- data/sig/prototype/runtime.rbs +108 -0
- data/sig/rdoc/rbs.rbs +4 -0
- data/sig/shims/bundler.rbs +5 -0
- data/sig/sorter.rbs +23 -5
- data/sig/types.rbs +29 -0
- data/stdlib/benchmark/0/benchmark.rbs +1 -1
- data/stdlib/cgi/0/core.rbs +2 -2
- data/stdlib/did_you_mean/0/did_you_mean.rbs +2 -2
- data/stdlib/digest/0/digest.rbs +1 -1
- data/stdlib/fileutils/0/fileutils.rbs +1 -1
- data/stdlib/forwardable/0/forwardable.rbs +4 -4
- data/stdlib/io-console/0/io-console.rbs +1 -1
- data/stdlib/json/0/json.rbs +37 -0
- data/stdlib/logger/0/logger.rbs +2 -2
- data/stdlib/net-http/0/manifest.yaml +1 -1
- data/stdlib/net-http/0/net-http.rbs +16 -63
- data/stdlib/net-protocol/0/manifest.yaml +2 -0
- data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
- data/stdlib/openssl/0/openssl.rbs +1 -1
- data/stdlib/pp/0/manifest.yaml +2 -0
- data/stdlib/pp/0/pp.rbs +301 -0
- data/stdlib/{yaml → psych}/0/dbm.rbs +3 -3
- data/stdlib/psych/0/manifest.yaml +3 -0
- data/stdlib/psych/0/psych.rbs +391 -0
- data/stdlib/{yaml → psych}/0/store.rbs +2 -2
- data/stdlib/rdoc/0/code_object.rbs +55 -0
- data/stdlib/rdoc/0/comment.rbs +60 -0
- data/stdlib/rdoc/0/context.rbs +153 -0
- data/stdlib/rdoc/0/markup.rbs +119 -0
- data/stdlib/rdoc/0/parser.rbs +56 -0
- data/stdlib/rdoc/0/rdoc.rbs +0 -372
- data/stdlib/rdoc/0/ri.rbs +17 -0
- data/stdlib/rdoc/0/store.rbs +48 -0
- data/stdlib/rdoc/0/top_level.rbs +97 -0
- data/stdlib/socket/0/basic_socket.rbs +1 -1
- data/stdlib/socket/0/socket.rbs +1 -1
- data/stdlib/uri/0/common.rbs +1 -1
- data/stdlib/yaml/0/manifest.yaml +1 -2
- data/stdlib/yaml/0/yaml.rbs +1 -199
- metadata +46 -9
- data/sig/shims/pp.rbs +0 -3
- data/sig/shims.rbs +0 -47
data/docs/stdlib.md
CHANGED
@@ -72,12 +72,71 @@ end
|
|
72
72
|
|
73
73
|
You need include `TypeAssertions` which provide useful methods for you.
|
74
74
|
`testing` method call tells which class is the subject of the class.
|
75
|
-
`
|
76
|
-
And you write the sample programs which calls all of the patterns of overloads.
|
75
|
+
You may need `library` call to test a library if the type definition is provided as a library (under `stdlib` dir).
|
77
76
|
|
78
77
|
Note that the instrumentation is based on refinements and you need to write all method calls in the unit class definitions.
|
79
78
|
If the execution of the program escape from the class definition, the instrumentation is disabled and no check will be done.
|
80
79
|
|
80
|
+
#### 📣 Method type assertions
|
81
|
+
|
82
|
+
`assert_send_type` method call asserts to be valid types and confirms to be able to execute without exceptions.
|
83
|
+
And you write the sample programs which calls all of the patterns of overloads.
|
84
|
+
|
85
|
+
We recommend write method types as _simple_ as possible inside the assertion.
|
86
|
+
It's not very easy to define _simple_, but we try to explain it with a few examples.
|
87
|
+
|
88
|
+
* Instead of `(String | Integer) -> Symbol?`, use `(String) -> Symbol` or `(Integer) -> nil`, because we know the exact argument type we are passing in the test
|
89
|
+
* Instead of `self`, `instance`, or `class`, use concrete types like `String`, `singleton(IO)`, because we know the exact type of the receiver
|
90
|
+
* Sometimes, you need union types if the method is nondeterministic -- `() -> (Integer | String)` for `[1, ""].sample` (But you can rewrite the test code as `[1].sample` instead)
|
91
|
+
* Sometimes, you need union types for heterogeneous collections -- `() { (Integer | String) -> String } -> Array[String | Integer]` for `[1, "2"].each {|i| i.to_s }` (But you can rewrite the test code as `[1, 2].each {|i| i.to_s }`)
|
92
|
+
* Using `void` is allowed if the RBS definition is `void`
|
93
|
+
|
94
|
+
Generally _simple_ means:
|
95
|
+
|
96
|
+
* The type doesn't contain `self`, `instance`, `class`, `top`, `bot`, and `untyped`
|
97
|
+
* The type doesn't contain unions and optionals
|
98
|
+
|
99
|
+
Use them if you cannot write the test without them.
|
100
|
+
|
101
|
+
One clear exception to using _simple_ types is when you use `with_int` or family helpers, that yield values with each case of the given union:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
def test_something
|
105
|
+
with_int(3) do |int|
|
106
|
+
# Yields twice with `Integer` and `ToInt`
|
107
|
+
assert_send_type(
|
108
|
+
"(int) -> Integer",
|
109
|
+
some, :test, int
|
110
|
+
)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
It's clear having type aliases makes sense.
|
116
|
+
|
117
|
+
#### 📣 Constant type assertions
|
118
|
+
|
119
|
+
We also have `assert_const_type` method, to test the type of constant is correct with respect to RBS type definition.
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
class FloatConstantTest < Test::Unit::TestCase
|
123
|
+
include TypeAssertions
|
124
|
+
|
125
|
+
def test_infinity
|
126
|
+
assert_const_type "Float", "Float::INFINITY"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
It confirms:
|
132
|
+
|
133
|
+
1. The type of constant `Float::INFINITY` is `Float`
|
134
|
+
2. The type of constant `Float::INFINITY` is correct with respect to RBS definition
|
135
|
+
|
136
|
+
We don't have any strong recommendation about where the constants test should be written in.
|
137
|
+
The `FloatConstantTest` example defines a test case only for the constant tests.
|
138
|
+
You may write the tests inside `FloatInstanceTest` or `FloatSingletonTest`.
|
139
|
+
|
81
140
|
### Running tests
|
82
141
|
|
83
142
|
You can run the test with:
|
data/docs/syntax.md
CHANGED
@@ -225,6 +225,87 @@ Proc type denotes type of procedures, `Proc` instances.
|
|
225
225
|
|
226
226
|
See the next section for details.
|
227
227
|
|
228
|
+
### Types and contexts
|
229
|
+
|
230
|
+
We have contextual limitations on some types:
|
231
|
+
|
232
|
+
* `void` is only allowed as a return type or a generic parameter
|
233
|
+
* `self` is only allowed in *self-context*
|
234
|
+
* `class` and `instance` is only allowed in *classish-context*
|
235
|
+
|
236
|
+
These contextual limitation is introduced at RBS 3.3.
|
237
|
+
The parser accepts those types even if it doesn't satisfy contextual limitation, but warning is reported with `rbs validate` command.
|
238
|
+
We plan to change the parser to reject those types if it breaks the contextual limitations in next release -- `3.4`.
|
239
|
+
|
240
|
+
#### Limitations on `void` types
|
241
|
+
|
242
|
+
The following `void` types are allowed.
|
243
|
+
|
244
|
+
```rbs
|
245
|
+
type t1 = ^() -> void
|
246
|
+
type t2 = Enumerator[Integer, void]
|
247
|
+
```
|
248
|
+
|
249
|
+
The following `void` types are prohibited.
|
250
|
+
|
251
|
+
```rbs
|
252
|
+
type t1 = ^(void) -> untyped # void as a function parameter is prohibited
|
253
|
+
type t2 = ^() -> void? # void cannot be used inside an optional type
|
254
|
+
type t3 = Enumerator[Integer, void | String] # void cannot be used inside a union type
|
255
|
+
```
|
256
|
+
|
257
|
+
#### Examples of *self-context*
|
258
|
+
|
259
|
+
The following `self` types are allowed.
|
260
|
+
|
261
|
+
```rbs
|
262
|
+
class Foo
|
263
|
+
attr_reader parent: self
|
264
|
+
|
265
|
+
def foo: () -> self
|
266
|
+
end
|
267
|
+
```
|
268
|
+
|
269
|
+
The following `self` types are prohibited.
|
270
|
+
|
271
|
+
```rbs
|
272
|
+
class Foo
|
273
|
+
include Enumerable[self] # Mixin argument is not self-context
|
274
|
+
|
275
|
+
VERSION: self # Constant type is not self-context
|
276
|
+
|
277
|
+
@@foos: Array[self] # Class variable type is not self-context
|
278
|
+
|
279
|
+
type list = nil | [self, list] # Type alias is not self-context
|
280
|
+
end
|
281
|
+
```
|
282
|
+
|
283
|
+
#### Examples of *classish-context*
|
284
|
+
|
285
|
+
The following `class`/`instance` types are allowed.
|
286
|
+
|
287
|
+
```rbs
|
288
|
+
class Foo
|
289
|
+
attr_reader parent: class
|
290
|
+
|
291
|
+
def foo: () -> instance
|
292
|
+
|
293
|
+
@@foos: Array[instances]
|
294
|
+
|
295
|
+
include Enumerable[class]
|
296
|
+
end
|
297
|
+
```
|
298
|
+
|
299
|
+
The following `class`/`instance` types are prohibited.
|
300
|
+
|
301
|
+
```rbs
|
302
|
+
class Foo
|
303
|
+
VERSION: class # Constant type is not classish-context
|
304
|
+
|
305
|
+
type list = nil | [instance, list] # Type alias is not classish-context
|
306
|
+
end
|
307
|
+
```
|
308
|
+
|
228
309
|
## Method Types and Proc Types
|
229
310
|
|
230
311
|
```markdown
|
@@ -319,6 +400,8 @@ _member_ ::= _ivar-member_ # Ivar definition
|
|
319
400
|
| _visibility-member_ # Visibility member
|
320
401
|
|
321
402
|
_ivar-member_ ::= _ivar-name_ `:` _type_
|
403
|
+
| `self` `.` _ivar-name_ `:` _type_
|
404
|
+
| _cvar-name_ `:` _type_
|
322
405
|
|
323
406
|
_method-member_ ::= _visibility_ `def` _method-name_ `:` _method-types_ # Instance method
|
324
407
|
| _visibility_ `def self.` _method-name_ `:` _method-types_ # Singleton method
|
@@ -351,6 +434,7 @@ _alias-member_ ::= `alias` _method-name_ _method-name_
|
|
351
434
|
_visibility-member_ ::= _visibility_
|
352
435
|
|
353
436
|
_ivar-name_ ::= /@\w+/
|
437
|
+
_cvar-name_ ::= /@@\w+/
|
354
438
|
_method-name_ ::= ...
|
355
439
|
| /`[^`]+`/
|
356
440
|
```
|
@@ -361,9 +445,14 @@ An instance variable definition consists of the name of an instance variable and
|
|
361
445
|
|
362
446
|
```rbs
|
363
447
|
@name: String
|
364
|
-
|
448
|
+
self.@value: Hash[Symbol, Key]
|
449
|
+
@@instances: Array[instance]
|
365
450
|
```
|
366
451
|
|
452
|
+
* Instance variables definition is *self-context* and *classish-context*
|
453
|
+
* Class instance variables definition is *self-context* and *classish-context*
|
454
|
+
* Class variables definition is *classish-context*, but NOT *self-context*
|
455
|
+
|
367
456
|
### Method definition
|
368
457
|
|
369
458
|
Method definition has several syntax variations.
|
@@ -411,6 +500,8 @@ public def self.puts: (*untyped) -> void # Defines public singleton method
|
|
411
500
|
public def self?.puts: (*untyped) -> void # 🚨🚨🚨 Error: `?.` has own visibility semantics (== `module_function`) 🚨🚨🚨
|
412
501
|
```
|
413
502
|
|
503
|
+
* Method types are *self-context* and *classish-context*
|
504
|
+
|
414
505
|
### Attribute definition
|
415
506
|
|
416
507
|
Attribute definitions help to define methods and instance variables based on the convention of `attr_reader`, `attr_writer` and `attr_accessor` methods in Ruby.
|
@@ -442,6 +533,8 @@ private attr_accessor id: Integer
|
|
442
533
|
private attr_reader self.name: String
|
443
534
|
```
|
444
535
|
|
536
|
+
* Attribute types are *self-context* and *classish-context*
|
537
|
+
|
445
538
|
### Mixin (include), Mixin (extend), Mixin (prepend)
|
446
539
|
|
447
540
|
You can define mixins between class and modules.
|
@@ -461,6 +554,8 @@ extend _LikeString
|
|
461
554
|
|
462
555
|
This allows importing `def`s from the interface to help developer implementing a set of methods.
|
463
556
|
|
557
|
+
* Mixin arguments are *classish-context*, but not *self-context*
|
558
|
+
|
464
559
|
### Alias
|
465
560
|
|
466
561
|
You can define an alias between methods.
|
@@ -544,6 +639,8 @@ _module-type-parameters_ ::= #
|
|
544
639
|
|
545
640
|
Class declaration can have type parameters and superclass. When you omit superclass, `::Object` is assumed.
|
546
641
|
|
642
|
+
* Super class arguments and generic class upperbounds are not *classish-context* nor *self-context*
|
643
|
+
|
547
644
|
### Module declaration
|
548
645
|
|
549
646
|
Module declaration takes optional _self type_ parameter, which defines a constraint about a class when the module is mixed.
|
@@ -560,6 +657,8 @@ end
|
|
560
657
|
|
561
658
|
The `Enumerable` module above requires `each` method for enumerating objects.
|
562
659
|
|
660
|
+
* Self type arguments and generic class upperbounds are not *classish-context* nor *self-context*
|
661
|
+
|
563
662
|
### Class/module alias declaration
|
564
663
|
|
565
664
|
An alias of a class or module can be defined in RBS.
|
@@ -624,6 +723,8 @@ Type alias can be generic like class, module, and interface.
|
|
624
723
|
type list[out T] = [T, list[T]] | nil
|
625
724
|
```
|
626
725
|
|
726
|
+
* Alias types are not *classish-context* nor *self-context*
|
727
|
+
|
627
728
|
### Constant type declaration
|
628
729
|
|
629
730
|
You can declare a constant.
|
@@ -632,6 +733,8 @@ You can declare a constant.
|
|
632
733
|
Person::DefaultEmailAddress: String
|
633
734
|
```
|
634
735
|
|
736
|
+
* Constant types are not *classish-context* nor *self-context*
|
737
|
+
|
635
738
|
### Global type declaration
|
636
739
|
|
637
740
|
You can declare a global variable.
|
@@ -640,6 +743,8 @@ You can declare a global variable.
|
|
640
743
|
$LOAD_PATH: Array[String]
|
641
744
|
```
|
642
745
|
|
746
|
+
* Constant types are not *classish-context* nor *self-context*
|
747
|
+
|
643
748
|
### Generics
|
644
749
|
|
645
750
|
```markdown
|
@@ -723,7 +828,7 @@ If a type parameter has an upper bound, the type parameter must be instantiated
|
|
723
828
|
|
724
829
|
```rbs
|
725
830
|
type str_printer = PrettyPrint[String] # OK
|
726
|
-
type int_printer = PrettyPrint[Integer]
|
831
|
+
type int_printer = PrettyPrint[Integer] # Type error
|
727
832
|
```
|
728
833
|
|
729
834
|
The upper bound must be one of a class instance type, interface type, or class singleton type.
|
@@ -764,3 +869,19 @@ interface _Foo
|
|
764
869
|
def new: () -> Foo
|
765
870
|
end
|
766
871
|
```
|
872
|
+
|
873
|
+
### Annotations
|
874
|
+
|
875
|
+
Annotations are placed before declarations, members, and method types to mark up a metadata for the declaration, the member, or method types.
|
876
|
+
The meaning of annotations are defined by the toolchain (ex. steep).
|
877
|
+
|
878
|
+
```markdown
|
879
|
+
_annotations_ ::= _annotation_ ...
|
880
|
+
_annotation_ ::= `%a{` _annotation-text_ `}` # Annotation using {}
|
881
|
+
| `%a(` _annotation-text_ `)` # Annotation using ()
|
882
|
+
| `%a[` _annotation-text_ `]` # Annotation using []
|
883
|
+
| `%a|` _annotation-text_ `|` # Annotation using ||
|
884
|
+
| `%a<` _annotation-text_ `>` # Annotation using <>
|
885
|
+
|
886
|
+
_annotation-text_ ::= /[^\x00]*/ # Any characters except NUL (and parenthesis)
|
887
|
+
```
|