rbs 1.8.0 → 2.0.0

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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +82 -4
  3. data/docs/collection.md +23 -1
  4. data/docs/syntax.md +94 -41
  5. data/ext/rbs_extension/constants.c +2 -6
  6. data/ext/rbs_extension/constants.h +1 -2
  7. data/ext/rbs_extension/parser.c +212 -178
  8. data/ext/rbs_extension/parserstate.c +6 -2
  9. data/ext/rbs_extension/parserstate.h +10 -0
  10. data/ext/rbs_extension/ruby_objs.c +9 -11
  11. data/ext/rbs_extension/ruby_objs.h +1 -2
  12. data/lib/rbs/ast/declarations.rb +0 -97
  13. data/lib/rbs/ast/type_param.rb +134 -0
  14. data/lib/rbs/cli.rb +32 -4
  15. data/lib/rbs/collection/config/lockfile_generator.rb +26 -18
  16. data/lib/rbs/collection/sources/git.rb +18 -7
  17. data/lib/rbs/collection/sources/rubygems.rb +7 -0
  18. data/lib/rbs/collection/sources/stdlib.rb +6 -0
  19. data/lib/rbs/definition.rb +9 -0
  20. data/lib/rbs/definition_builder.rb +49 -14
  21. data/lib/rbs/environment.rb +32 -9
  22. data/lib/rbs/environment_loader.rb +0 -2
  23. data/lib/rbs/errors.rb +20 -7
  24. data/lib/rbs/location_aux.rb +2 -0
  25. data/lib/rbs/method_type.rb +29 -6
  26. data/lib/rbs/prototype/rb.rb +3 -3
  27. data/lib/rbs/prototype/rbi.rb +8 -6
  28. data/lib/rbs/prototype/runtime.rb +4 -4
  29. data/lib/rbs/types.rb +89 -0
  30. data/lib/rbs/validator.rb +62 -11
  31. data/lib/rbs/variance_calculator.rb +9 -8
  32. data/lib/rbs/version.rb +1 -1
  33. data/lib/rbs/writer.rb +1 -13
  34. data/lib/rbs.rb +1 -0
  35. data/schema/decls.json +16 -55
  36. data/schema/methodType.json +1 -1
  37. data/schema/typeParam.json +36 -0
  38. data/sig/collection/collections.rbs +11 -2
  39. data/sig/collection/config.rbs +2 -2
  40. data/sig/declarations.rbs +8 -58
  41. data/sig/definition.rbs +11 -1
  42. data/sig/definition_builder.rbs +8 -1
  43. data/sig/environment.rbs +7 -1
  44. data/sig/errors.rbs +19 -4
  45. data/sig/location.rbs +3 -1
  46. data/sig/locator.rbs +1 -1
  47. data/sig/method_types.rbs +25 -4
  48. data/sig/type_param.rbs +74 -0
  49. data/sig/types.rbs +27 -1
  50. data/sig/validator.rbs +31 -2
  51. data/sig/variance_calculator.rbs +1 -1
  52. data/sig/writer.rbs +1 -1
  53. data/stdlib/bigdecimal-math/0/manifest.yaml +2 -0
  54. data/stdlib/csv/0/manifest.yaml +2 -0
  55. data/stdlib/logger/0/manifest.yaml +2 -0
  56. data/stdlib/net-http/0/manifest.yaml +2 -0
  57. data/stdlib/openssl/0/manifest.yaml +2 -0
  58. data/stdlib/prime/0/manifest.yaml +2 -0
  59. data/stdlib/resolv/0/manifest.yaml +3 -0
  60. data/stdlib/uri/0/common.rbs +10 -5
  61. data/stdlib/uri/0/ftp.rbs +10 -0
  62. data/stdlib/uri/0/generic.rbs +34 -34
  63. data/stdlib/uri/0/mailto.rbs +5 -0
  64. data/stdlib/uri/0/ws.rbs +10 -0
  65. data/stdlib/uri/0/wss.rbs +7 -0
  66. data/stdlib/yaml/0/manifest.yaml +3 -0
  67. metadata +17 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b12d92ea4eeeb5dbd6de6c5d58351b0306bdcd017d0f1e51a36599766bfded3
4
- data.tar.gz: d60cfb836027441a5bb3f85fbebb0e2ab9bc03c49a21f00c3f10d4ab8dd1de65
3
+ metadata.gz: f6c23428bebbddb53710d5f77761d75bdcafa38d988b9fdc62b20563ff3eb60a
4
+ data.tar.gz: 65c065d547aed025238d0350800830e31e18a3fcd21db73406a7374202517b80
5
5
  SHA512:
6
- metadata.gz: 9ef926a62415189173d41204d1a16674a4a8331e3fdefafe00a4e52c9cbf0f3eb84281194a5b5c43027455334c13ea0ab14e1b3f0a6b8d5f5644cb80733a246b
7
- data.tar.gz: ce07016eee6b4142a66a28d8c7ffc356b3d4fdd747027f2dd521b347efcb6ac791385e903a1e3b9f8d468ec66e13995e341516137ceba990db511982c6d0d206
6
+ metadata.gz: c0fbada9d621cb2e1cd2a3793ad208d78a50f826c1c58e03eb5d256f8e6fa024446549f2943c85e37dc0ad1170672b66651dab66d5f8eb3d0a0f9b5757e9315e
7
+ data.tar.gz: 505fcfdf431ac0c904325494157fab4c5b1855b200ba1d416be0cf3680c173ac302517eb99a93d6657a0344a1fa198a2455498fec493f6b240a96d7b8cddf994
data/CHANGELOG.md CHANGED
@@ -2,9 +2,87 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 2.0.0 (2021-12-24)
6
+
7
+ ### Bounded Generics
8
+
9
+ RBS 2.0 ships with _bounded generics_, which improves the expressiveness of the language by adding a new syntax to define constraints on type parameters.
10
+
11
+ ```rbs
12
+ class PrettyPrint[T < _Output]
13
+ interface _Output
14
+ def <<: (String) -> void
15
+ end
16
+
17
+ attr_reader output: T
18
+
19
+ def initialize: (T output) -> void
20
+ end
21
+ ```
22
+
23
+ This is the motivating example I found in the [prettyprint library](https://github.com/ruby/prettyprint).
24
+ The `#initialize` receives a object of type `T` and it must have `#<<` method.
25
+ This is defined with `< _Output` syntax in the example.
26
+ It means _`T` has to be compatible with `_Output` interface._
27
+ `PrettyPrint[String]` is okay, but `PrettyPrint[Integer]` is a type error.
28
+
29
+ See [the PR for details](https://github.com/ruby/rbs/pull/844).
30
+
31
+ ### RBS Collection manager
32
+
33
+ RBS Collection feature is generally available on RBS 2.0. In short, it is Bundler for RBS. You can manage RBSs of standard libraries and third party gems with `rbs collection` subcommand.
34
+
35
+ ```bash
36
+ $ rbs collection init
37
+ created: rbs_collection.yaml
38
+
39
+ # The `install` command set up RBS files and creates `rbs_collection.lock.yaml` file
40
+ $ rbs collection install
41
+ Installing actionpack:6.0 (actionpack@ce6664cec73)
42
+ (...snip...)
43
+ Using tsort:0 (/path/to/rbs-2.0.0/stdlib/tsort/0)
44
+ It's done! 21 gems' RBSs now installed.
45
+
46
+ # Now you can use `rbs`, `typeprof` and `steep` commands with the dependencies out of the box!
47
+ $ rbs validate
48
+ $ typeprof app.rb
49
+ $ steep check
50
+ ```
51
+
52
+ RBS 2.0 also introduces `manifest.yaml` to declare the dependencies from your gems to standard libraries explicitly.
53
+ See [the documentation](https://github.com/ruby/rbs/blob/master/docs/collection.md) for more information.
54
+
55
+ ### Breaking changes
56
+
57
+ This version contains a bug fix, which potentially breaks the compatibility with older versions.
58
+ The super class names in class definitions are now resolved in _outer_ context.
59
+ It was an incompatibility with Ruby and [this PR](https://github.com/ruby/rbs/pull/856) fixed the problem.
60
+
61
+ ### Signature updates
62
+
63
+ * uri ([\#846](https://github.com/ruby/rbs/pull/846), [\#852](https://github.com/ruby/rbs/pull/852), [\#851](https://github.com/ruby/rbs/pull/851), [\#850](https://github.com/ruby/rbs/pull/850), [#849](https://github.com/ruby/rbs/pull/849))
64
+
65
+ ### Language updates
66
+
67
+ * Bounded generics ([\#844](https://github.com/ruby/rbs/pull/844))
68
+ * Resolve super type names in outer context ([\#856](https://github.com/ruby/rbs/pull/856))
69
+
70
+ ### Library changes
71
+
72
+ * Add `manifest.yaml` for collection to specify stdlib dependencies ([\#808](https://github.com/ruby/rbs/pull/808))
73
+ * Remove experimental warning of `rbs collection` ([\#855](https://github.com/ruby/rbs/pull/855))
74
+ * Add the utility `#map_type` methods ([\#841](https://github.com/ruby/rbs/pull/841))
75
+
76
+ ## 1.8.1 (2021-12-13)
77
+
78
+ ### Library changes
79
+
80
+ * Validate `extend` arguments ([\#840](https://github.com/ruby/rbs/pull/840))
81
+ * Allow a relative path as git remote in collection ([\#839](https://github.com/ruby/rbs/pull/839))
82
+
5
83
  ## 1.8.0 (2021-12-02)
6
84
 
7
- RBS 1.8.0 ships with a language feature enchancement, _generic type alias_.
85
+ RBS 1.8.0 ships with a language feature enhancement, _generic type alias_.
8
86
  You can define a type alias with type parameters now.
9
87
 
10
88
  ```rbs
@@ -45,7 +123,7 @@ You can find the detail in the [PR](https://github.com/ruby/rbs/pull/823).
45
123
  This version replaces `RBS::Parser` implementation from pure Ruby code based on [Racc](https://github.com/ruby/racc) to C extension.
46
124
  It improves the RBS file parsing performance up to 5 times faster. :rocket:
47
125
 
48
- * There are some incompatibilties to drop obsolete syntax rules: `super` keyword and `any` type are no longer supported.
126
+ * There are some incompatibilities to drop obsolete syntax rules: `super` keyword and `any` type are no longer supported.
49
127
  * [re2c](https://github.com/skvadrik/re2c) is used to generate lexical generator.
50
128
 
51
129
  When you want to change the parser/lexer, change the files under `ext/rbs_extension` directory and run `rake compile` to compile the extension.
@@ -158,7 +236,7 @@ This release includes feature enhancements including recursive `type` definition
158
236
 
159
237
  ### Library changes
160
238
 
161
- * Add Recursiive type alias defnition validation ([\#719](https://github.com/ruby/rbs/pull/719))
239
+ * Add Recursive type alias definition validation ([\#719](https://github.com/ruby/rbs/pull/719))
162
240
  * Generate included modules with complete name ([\#731](https://github.com/ruby/rbs/pull/731))
163
241
  * Fix `rbs-prototype-rb` error when multi assign with const ([\#740](https://github.com/ruby/rbs/pull/740))
164
242
 
@@ -186,7 +264,7 @@ This release is to fix a bug introduced by parser update in 1.3.0.
186
264
 
187
265
  ### Summary
188
266
 
189
- RBS 1.3.0 includes bug fixees of the parser and class/module definition validations.
267
+ RBS 1.3.0 includes bug fixes of the parser and class/module definition validations.
190
268
 
191
269
  ### Signature updates
192
270
 
data/docs/collection.md CHANGED
@@ -7,7 +7,6 @@
7
7
  * `git(1)`
8
8
  * `Gemfile.lock`
9
9
 
10
-
11
10
  ## Usage
12
11
 
13
12
  ### Setup
@@ -65,9 +64,13 @@ Finally the third party RBSs are available! `rbs` commands, such as `rbs validat
65
64
 
66
65
  ## Configuration
67
66
 
67
+ ### `rbs_collection.yaml`
68
+
68
69
  Configure `rbs collection` with editing `rbs_collection.yaml`.
69
70
 
70
71
  ```yaml
72
+ # rbs_collection.yaml
73
+
71
74
  # Download sources.
72
75
  # You can add own collection git repository.
73
76
  sources:
@@ -92,6 +95,25 @@ gems:
92
95
  ignore: true
93
96
  ```
94
97
 
98
+ ### `manifest.yaml`
99
+
100
+ If you are a gem maintainer, you can write `manifest.yaml`.
101
+ You need to put the file if the gem has implicit dependencies, which don't appear in `Gemfile.lock`. You have to write standard libraries' dependencies in most cases.
102
+ For example:
103
+
104
+ ```yaml
105
+ # manifest.yaml
106
+
107
+ dependencies:
108
+ # If your gem depends on pathname but the gemspec doesn't include pathname,
109
+ # you need to write the following.
110
+ - name: pathname
111
+ ```
112
+
113
+ If the gem's RBS is managed with [ruby/gem_rbs_collection](https://github.com/ruby/gem_rbs_collection), put it as `gems/GEM_NAME/VERSION/manifest.yaml`. For example, `gems/activesupport/6.0/manifest.yaml`.
114
+ If the gem's RBS is included in the gem package, put it as `sig/manifest.yaml`.
115
+
116
+
95
117
  ## Files / Directories
96
118
 
97
119
  * `rbs_collection.yaml`
data/docs/syntax.md CHANGED
@@ -288,12 +288,12 @@ _method-member_ ::= `def` _method-name_ `:` _method-types_ # Instance
288
288
  | `def self.` _method-name_ `:` _method-types_ # Singleton method
289
289
  | `def self?.` _method-name_ `:` _method-types_ # Singleton and instance method
290
290
 
291
- _method-types_ ::= # Empty
292
- | _type-parameters_ _method-type_ `|` _method-types_ # Overloading types
293
- | `...` # Overloading for duplicate definitions
291
+ _method-types_ ::= _method-type-parameters_ _method-type_ # Single method type
292
+ | _method-type-parameters_ _method-type_ `|` _method-types_ # Overloading types
293
+ | `...` # Overloading for duplicate definitions
294
294
 
295
- _type-parameters_ ::= # Empty
296
- | `[` _type-variable_ `,` etc. `]`
295
+ _method-type-parameters_ ::= # Empty
296
+ | `[` _type-variable_ `,` ... `]`
297
297
 
298
298
  _attribute-member_ ::= _attribute-type_ _method-name_ `:` _type_ # Attribute
299
299
  | _attribute-type_ _method-name_ `(` _ivar-name_ `) :` _type_ # Attribute with variable name specification
@@ -443,48 +443,13 @@ _const-name_ ::= _namespace_ /[A-Z]\w*/
443
443
  _global-name_ ::= /$[a-zA-Z]\w+/ | ...
444
444
 
445
445
  _module-type-parameters_ ::= # Empty
446
- | `[` _module-type-parameter_ `,` etc. `]`
447
-
448
- _module-type-parameter_ ::= _check_ _variance_ _type-variable_
449
- _variance_ ::= `out` | `in`
450
- _check_ ::= # Empty
451
- | `unchecked`
446
+ | `[` _module-type-parameter_ `,` ... `]`
452
447
  ```
453
448
 
454
449
  ### Class declaration
455
450
 
456
451
  Class declaration can have type parameters and superclass. When you omit superclass, `::Object` is assumed.
457
452
 
458
- ```
459
- class Ref[A] < Object
460
- attr_reader value: A
461
- def initialize: (value: A) -> void
462
- end
463
- ```
464
-
465
- For classes with type parameters, you may specify if they are "invariant" (default), "covariant" (`out`) or "contravariant" (`in`). See [this definition of covariance and contravariance](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)).
466
-
467
- For example, an `Array` of `String` can almost be considered to be an `Array` of `Object`, but not the reverse, so we can think of:
468
-
469
- ```
470
- class Array[out T]
471
- # etc.
472
- end
473
- ```
474
-
475
- There's a limitation with this is for mutable objects (like arrays): a mutation could invalidate this.
476
- If an array of String is passed to a method as an array of Objects, and that method adds an Integer to the array, the promise is broken.
477
-
478
- In those cases, one must use the `unchecked` keyword:
479
-
480
- ```
481
- class Array[unchecked out T]
482
- # etc.
483
- end
484
- ```
485
-
486
- This is how `Array` is actually defined in RBS.
487
-
488
453
  ### Module declaration
489
454
 
490
455
  Module declaration takes optional _self type_ parameter, which defines a constraint about a class when the module is mixed.
@@ -555,6 +520,94 @@ You can declare a global variable.
555
520
  $LOAD_PATH: Array[String]
556
521
  ```
557
522
 
523
+ ### Generics
524
+
525
+ ```md
526
+ _module-type-parameter_ ::= _generics-unchecked_ _generics-variance_ _type-variable_ _generics-bound_
527
+
528
+ _method-type-param_ ::= _type-variable_ _generics-bound_
529
+
530
+ _generics-bound_ ::= (No type bound)
531
+ | `<` _bound-type_ (The generics parameter is bounded)
532
+
533
+ _bound-type_ ::= _class-name_ _type-arguments_ (Class instance type)
534
+ | _interface-name_ _type-arguments_ (Interface type)
535
+ | `singleton(` _class-name_ `)` (Class singleton type)
536
+
537
+ _generics-variance_ ::= (Invariant)
538
+ | `out` (Covariant)
539
+ | `in` (Contravariant)
540
+
541
+ _generics-unchecked_ ::= (Empty)
542
+ | `unchecked` (Skips variance annotation validation)
543
+ ```
544
+
545
+ RBS allows class/module/interface/type alias definitions and methods to be generic.
546
+
547
+ ```rbs
548
+ # Simple generic class definition
549
+ class Stack[T]
550
+ def push: (T) -> void
551
+
552
+ def pop: () -> T
553
+ end
554
+ ```
555
+
556
+ For classes with type parameters, you may specify if they are "invariant" (default), "covariant" (`out`) or "contravariant" (`in`). See [this definition of covariance and contravariance](https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)).
557
+
558
+ For example, an `Array` of `String` can almost be considered to be an `Array` of `Object`, but not the reverse, so we can think of:
559
+
560
+ ```
561
+ # The `T` type parameter is covariant.
562
+ class Array[out T]
563
+ # etc.
564
+ end
565
+ ```
566
+
567
+ There's a limitation with this is for mutable objects (like arrays): a mutation could invalidate this.
568
+ If an array of `String` is passed to a method as an array of `Objects`, and that method adds an Integer to the array, the promise is broken.
569
+
570
+ In those cases, one must use the `unchecked` keyword:
571
+
572
+ ```rbs
573
+ # Skips the validation of variance of the type parameter `T`.
574
+ # The type safety prohibits `out` type parameters to appear at _negative_ position (== method parameter), but we want `Array` to have it.
575
+ class Array[unchecked out T]
576
+ def include?: (T) -> bool
577
+ end
578
+ ```
579
+
580
+ This is how `Array` is actually defined in RBS.
581
+
582
+ Note that RBS doesn't allow specifying variance related annotations to generic method types.
583
+
584
+ ```rbs
585
+ class Foo
586
+ def bar: [out T] () -> T # Syntax error
587
+ end
588
+ ```
589
+
590
+ You can also specify the _upper bound_ of the type parameter.
591
+
592
+ ```rbs
593
+ class PrettyPrint[T < _Output]
594
+ interface _Output
595
+ def <<: (String) -> void
596
+ end
597
+
598
+ attr_reader output: T
599
+ end
600
+ ```
601
+
602
+ If a type parameter has an upper bound, the type parameter must be instantiated with types that is a subclass of the upper bound.
603
+
604
+ ```rbs
605
+ type str_printer = PrettyPrint[String] # OK
606
+ type int_printer = PrettyPrint[Integer] # Type error
607
+ ```
608
+
609
+ The upper bound must be one of a class instance type, interface type, or class singleton type.
610
+
558
611
  ### Comments
559
612
 
560
613
  You can write single line comments. Comments must be on their own line. Comments can lead with whitespace.
@@ -6,12 +6,10 @@ VALUE RBS;
6
6
  VALUE RBS_AST;
7
7
  VALUE RBS_AST_Comment;
8
8
  VALUE RBS_AST_Annotation;
9
+ VALUE RBS_AST_TypeParam;
9
10
 
10
11
  VALUE RBS_AST_Declarations;
11
12
 
12
- VALUE RBS_AST_Declarations_ModuleTypeParams;
13
- VALUE RBS_AST_Declarations_ModuleTypeParams_TypeParam;
14
-
15
13
  VALUE RBS_AST_Declarations_Alias;
16
14
  VALUE RBS_AST_Declarations_Constant;
17
15
  VALUE RBS_AST_Declarations_Global;
@@ -77,12 +75,10 @@ void rbs__init_constants() {
77
75
  RBS_AST = rb_const_get(RBS, rb_intern("AST"));
78
76
  RBS_AST_Comment = rb_const_get(RBS_AST, rb_intern("Comment"));
79
77
  RBS_AST_Annotation = rb_const_get(RBS_AST, rb_intern("Annotation"));
78
+ RBS_AST_TypeParam = rb_const_get(RBS_AST, rb_intern("TypeParam"));
80
79
 
81
80
  RBS_AST_Declarations = rb_const_get(RBS_AST, rb_intern("Declarations"));
82
81
 
83
- RBS_AST_Declarations_ModuleTypeParams = rb_const_get(RBS_AST_Declarations, rb_intern("ModuleTypeParams"));
84
- RBS_AST_Declarations_ModuleTypeParams_TypeParam = rb_const_get(RBS_AST_Declarations_ModuleTypeParams, rb_intern("TypeParam"));
85
-
86
82
  RBS_AST_Declarations_Alias = rb_const_get(RBS_AST_Declarations, rb_intern("Alias"));
87
83
  RBS_AST_Declarations_Constant = rb_const_get(RBS_AST_Declarations, rb_intern("Constant"));
88
84
  RBS_AST_Declarations_Global = rb_const_get(RBS_AST_Declarations, rb_intern("Global"));
@@ -6,6 +6,7 @@ extern VALUE RBS;
6
6
  extern VALUE RBS_AST;
7
7
  extern VALUE RBS_AST_Annotation;
8
8
  extern VALUE RBS_AST_Comment;
9
+ extern VALUE RBS_AST_TypeParam;
9
10
 
10
11
  extern VALUE RBS_AST_Declarations;
11
12
  extern VALUE RBS_AST_Declarations_Alias;
@@ -16,8 +17,6 @@ extern VALUE RBS_AST_Declarations_Global;
16
17
  extern VALUE RBS_AST_Declarations_Interface;
17
18
  extern VALUE RBS_AST_Declarations_Module_Self;
18
19
  extern VALUE RBS_AST_Declarations_Module;
19
- extern VALUE RBS_AST_Declarations_ModuleTypeParams_TypeParam;
20
- extern VALUE RBS_AST_Declarations_ModuleTypeParams;
21
20
 
22
21
  extern VALUE RBS_AST_Members;
23
22
  extern VALUE RBS_AST_Members_Alias;