rbs 1.8.1 → 2.0.0.pre1

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 (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +51 -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 +9 -0
  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 +20 -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 +56 -1
  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 +9 -0
  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 +1 -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/mailto.rbs +5 -0
  63. data/stdlib/uri/0/ws.rbs +10 -0
  64. data/stdlib/uri/0/wss.rbs +7 -0
  65. data/stdlib/yaml/0/manifest.yaml +3 -0
  66. metadata +19 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df1527cdbc750871d430d1c69bd4c9188b8814af7f3163d62adc5f9c1297e0dc
4
- data.tar.gz: 3d0cc66c78d043e5fd98b86b342a7e8944c1cee8bf31f8f876530caba4cd9bc9
3
+ metadata.gz: df2f2f119ca85f630c640b201ebf9ce72322db96223de2fb1d4ba77df68aee59
4
+ data.tar.gz: 82943e12963a10f2a4dbd30096fe40fb90ee4b23d6239bd515526a39d2af3b97
5
5
  SHA512:
6
- metadata.gz: 5c7f150ac87495a3ef064b5b499a5ef72c13eeb23985b84def107c111c6973a02ce6c3984bf22d1389882adf6d02f70fc82773d78383dd3992a2d7dbe58a7e59
7
- data.tar.gz: f99bd7edbf1b4cf1cb5f018538345fe8ea50653ad6fa908aa3cd98c46d45b095aabb0cc2314cfae064997c0db2c6a31c6f9ab4fda1b15d2890a6b9be8dc8244a
6
+ metadata.gz: 9dbd99091da5e5d116a5bb428383c3e63e8b8ee4207ea859e177577b33a1d1dc3d312bb31f03fcda61d90993b4a9ab1ccab69855e7ecb8e986f9696a638c618d
7
+ data.tar.gz: dc05caaa25da531bbe353eb0294fbe016a90039eef12a040f2c5b07f409683c9fbc6c3406beba8c8d76f25f5b0ed7bbc3501967cc84fcc000a2e49fda85e84a3
data/CHANGELOG.md CHANGED
@@ -2,6 +2,53 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 2.0.0 (pre1)
6
+
7
+ ### Bounded Generics
8
+
9
+ RBS 2.0 ships with _bounded generics_, which improves the expressiveness of the language by adding new syntax to define the constraint 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
+ `manifest.yaml` allows declaring the dependencies from your gems to standard libraries explicitly.
32
+
33
+ This version contains a bug fix, which potentially breaks the compatibility with older versions.
34
+ The super class names in class definitions are now resolved in _outer_ context.
35
+ It was an incompatibility with Ruby and [this PR](https://github.com/ruby/rbs/pull/856) fixed the problem.
36
+
37
+ ### Signature updates
38
+
39
+ * 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))
40
+
41
+ ### Language updates
42
+
43
+ * Bounded generics ([\#844](https://github.com/ruby/rbs/pull/844))
44
+ * Resolve super type names in outer context ([\#856](https://github.com/ruby/rbs/pull/856))
45
+
46
+ ### Library changes
47
+
48
+ * Add `manifest.yaml` for collection to specify stdlib dependencies ([\#808](https://github.com/ruby/rbs/pull/808))
49
+ * Remove experimental warning of `rbs collection` ([\#855](https://github.com/ruby/rbs/pull/855))
50
+ * Add the utility `#map_type` methods ([\#841](https://github.com/ruby/rbs/pull/841))
51
+
5
52
  ## 1.8.1 (2021-12-13)
6
53
 
7
54
  ### Library changes
@@ -11,7 +58,7 @@
11
58
 
12
59
  ## 1.8.0 (2021-12-02)
13
60
 
14
- RBS 1.8.0 ships with a language feature enchancement, _generic type alias_.
61
+ RBS 1.8.0 ships with a language feature enhancement, _generic type alias_.
15
62
  You can define a type alias with type parameters now.
16
63
 
17
64
  ```rbs
@@ -52,7 +99,7 @@ You can find the detail in the [PR](https://github.com/ruby/rbs/pull/823).
52
99
  This version replaces `RBS::Parser` implementation from pure Ruby code based on [Racc](https://github.com/ruby/racc) to C extension.
53
100
  It improves the RBS file parsing performance up to 5 times faster. :rocket:
54
101
 
55
- * There are some incompatibilties to drop obsolete syntax rules: `super` keyword and `any` type are no longer supported.
102
+ * There are some incompatibilities to drop obsolete syntax rules: `super` keyword and `any` type are no longer supported.
56
103
  * [re2c](https://github.com/skvadrik/re2c) is used to generate lexical generator.
57
104
 
58
105
  When you want to change the parser/lexer, change the files under `ext/rbs_extension` directory and run `rake compile` to compile the extension.
@@ -165,7 +212,7 @@ This release includes feature enhancements including recursive `type` definition
165
212
 
166
213
  ### Library changes
167
214
 
168
- * Add Recursiive type alias defnition validation ([\#719](https://github.com/ruby/rbs/pull/719))
215
+ * Add Recursive type alias definition validation ([\#719](https://github.com/ruby/rbs/pull/719))
169
216
  * Generate included modules with complete name ([\#731](https://github.com/ruby/rbs/pull/731))
170
217
  * Fix `rbs-prototype-rb` error when multi assign with const ([\#740](https://github.com/ruby/rbs/pull/740))
171
218
 
@@ -193,7 +240,7 @@ This release is to fix a bug introduced by parser update in 1.3.0.
193
240
 
194
241
  ### Summary
195
242
 
196
- RBS 1.3.0 includes bug fixees of the parser and class/module definition validations.
243
+ RBS 1.3.0 includes bug fixes of the parser and class/module definition validations.
197
244
 
198
245
  ### Signature updates
199
246
 
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;