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.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +1 -1
  3. data/.github/workflows/ruby.yml +7 -2
  4. data/CHANGELOG.md +85 -0
  5. data/Gemfile.lock +14 -14
  6. data/README.md +11 -2
  7. data/Rakefile +10 -7
  8. data/Steepfile +7 -7
  9. data/core/basic_object.rbs +7 -7
  10. data/core/binding.rbs +3 -3
  11. data/core/builtin.rbs +171 -5
  12. data/core/constants.rbs +17 -17
  13. data/core/dir.rbs +3 -3
  14. data/core/encoding.rbs +434 -628
  15. data/core/enumerator.rbs +37 -0
  16. data/core/exception.rbs +11 -11
  17. data/core/false_class.rbs +5 -11
  18. data/core/fiber.rbs +3 -3
  19. data/core/file_test.rbs +28 -26
  20. data/core/kernel.rbs +900 -21
  21. data/core/marshal.rbs +24 -14
  22. data/core/match_data.rbs +8 -8
  23. data/core/math.rbs +57 -53
  24. data/core/method.rbs +3 -1
  25. data/core/module.rbs +38 -36
  26. data/core/nil_class.rbs +7 -13
  27. data/core/object.rbs +3 -966
  28. data/core/process.rbs +3 -3
  29. data/core/ractor.rbs +2 -2
  30. data/core/rb_config.rbs +64 -43
  31. data/core/regexp.rbs +3 -3
  32. data/core/signal.rbs +10 -4
  33. data/core/struct.rbs +1 -1
  34. data/core/thread.rbs +7 -7
  35. data/core/thread_group.rbs +9 -9
  36. data/core/true_class.rbs +5 -11
  37. data/core/unbound_method.rbs +56 -7
  38. data/core/warning.rbs +33 -0
  39. data/docs/collection.md +56 -6
  40. data/docs/data_and_struct.md +57 -0
  41. data/docs/stdlib.md +61 -2
  42. data/docs/syntax.md +123 -2
  43. data/ext/rbs_extension/lexer.c +624 -569
  44. data/ext/rbs_extension/lexer.h +1 -0
  45. data/ext/rbs_extension/lexer.re +1 -0
  46. data/ext/rbs_extension/lexstate.c +1 -0
  47. data/ext/rbs_extension/parser.c +6 -0
  48. data/goodcheck.yml +2 -2
  49. data/lib/rbs/annotate/formatter.rb +13 -3
  50. data/lib/rbs/annotate/rdoc_source.rb +10 -1
  51. data/lib/rbs/cli/colored_io.rb +48 -0
  52. data/lib/rbs/cli/diff.rb +80 -0
  53. data/lib/rbs/cli.rb +151 -16
  54. data/lib/rbs/collection/config/lockfile.rb +0 -25
  55. data/lib/rbs/collection/config/lockfile_generator.rb +0 -6
  56. data/lib/rbs/collection/installer.rb +1 -1
  57. data/lib/rbs/collection/sources/git.rb +6 -4
  58. data/lib/rbs/collection/sources/local.rb +7 -5
  59. data/lib/rbs/diff.rb +104 -0
  60. data/lib/rbs/environment.rb +1 -1
  61. data/lib/rbs/method_type.rb +23 -0
  62. data/lib/rbs/prototype/rb.rb +2 -9
  63. data/lib/rbs/prototype/runtime/helpers.rb +59 -0
  64. data/lib/rbs/prototype/runtime/value_object_generator.rb +236 -0
  65. data/lib/rbs/prototype/runtime.rb +234 -150
  66. data/lib/rbs/sorter.rb +144 -117
  67. data/lib/rbs/test/guaranteed.rb +31 -0
  68. data/lib/rbs/test/type_check.rb +4 -4
  69. data/lib/rbs/test.rb +3 -0
  70. data/lib/rbs/types.rb +184 -3
  71. data/lib/rbs/version.rb +1 -1
  72. data/lib/rbs/writer.rb +4 -4
  73. data/lib/rbs.rb +1 -0
  74. data/rbs.gemspec +1 -0
  75. data/sig/annotate/formatter.rbs +2 -2
  76. data/sig/annotate/rdoc_annotater.rbs +1 -1
  77. data/sig/cli/colored_io.rbs +15 -0
  78. data/sig/cli/diff.rbs +21 -0
  79. data/sig/cli.rbs +2 -0
  80. data/sig/collection/config/lockfile.rbs +0 -6
  81. data/sig/diff.rbs +23 -0
  82. data/sig/errors.rbs +1 -5
  83. data/sig/method_types.rbs +6 -0
  84. data/sig/prototype/runtime.rbs +108 -0
  85. data/sig/rdoc/rbs.rbs +4 -0
  86. data/sig/shims/bundler.rbs +5 -0
  87. data/sig/sorter.rbs +23 -5
  88. data/sig/types.rbs +29 -0
  89. data/stdlib/benchmark/0/benchmark.rbs +1 -1
  90. data/stdlib/cgi/0/core.rbs +2 -2
  91. data/stdlib/did_you_mean/0/did_you_mean.rbs +2 -2
  92. data/stdlib/digest/0/digest.rbs +1 -1
  93. data/stdlib/fileutils/0/fileutils.rbs +1 -1
  94. data/stdlib/forwardable/0/forwardable.rbs +4 -4
  95. data/stdlib/io-console/0/io-console.rbs +1 -1
  96. data/stdlib/json/0/json.rbs +37 -0
  97. data/stdlib/logger/0/logger.rbs +2 -2
  98. data/stdlib/net-http/0/manifest.yaml +1 -1
  99. data/stdlib/net-http/0/net-http.rbs +16 -63
  100. data/stdlib/net-protocol/0/manifest.yaml +2 -0
  101. data/stdlib/net-protocol/0/net-protocol.rbs +56 -0
  102. data/stdlib/openssl/0/openssl.rbs +1 -1
  103. data/stdlib/pp/0/manifest.yaml +2 -0
  104. data/stdlib/pp/0/pp.rbs +301 -0
  105. data/stdlib/{yaml → psych}/0/dbm.rbs +3 -3
  106. data/stdlib/psych/0/manifest.yaml +3 -0
  107. data/stdlib/psych/0/psych.rbs +391 -0
  108. data/stdlib/{yaml → psych}/0/store.rbs +2 -2
  109. data/stdlib/rdoc/0/code_object.rbs +55 -0
  110. data/stdlib/rdoc/0/comment.rbs +60 -0
  111. data/stdlib/rdoc/0/context.rbs +153 -0
  112. data/stdlib/rdoc/0/markup.rbs +119 -0
  113. data/stdlib/rdoc/0/parser.rbs +56 -0
  114. data/stdlib/rdoc/0/rdoc.rbs +0 -372
  115. data/stdlib/rdoc/0/ri.rbs +17 -0
  116. data/stdlib/rdoc/0/store.rbs +48 -0
  117. data/stdlib/rdoc/0/top_level.rbs +97 -0
  118. data/stdlib/socket/0/basic_socket.rbs +1 -1
  119. data/stdlib/socket/0/socket.rbs +1 -1
  120. data/stdlib/uri/0/common.rbs +1 -1
  121. data/stdlib/yaml/0/manifest.yaml +1 -2
  122. data/stdlib/yaml/0/yaml.rbs +1 -199
  123. metadata +46 -9
  124. data/sig/shims/pp.rbs +0 -3
  125. 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
- `assert_send_type` method call asserts to be valid types and confirms to be able to execute without exceptions.
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
- @value: Hash[Symbol, Key]
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] # Type error
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
+ ```