rbs 1.7.0.beta.5 → 1.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/CHANGELOG.md +64 -3
  4. data/Steepfile +0 -1
  5. data/core/array.rbs +3 -3
  6. data/core/builtin.rbs +4 -0
  7. data/core/enumerable.rbs +3 -3
  8. data/core/env.rbs +881 -0
  9. data/core/false_class.rbs +2 -0
  10. data/core/nil_class.rbs +2 -0
  11. data/core/symbol.rbs +2 -0
  12. data/core/thread.rbs +1 -1
  13. data/core/true_class.rbs +2 -0
  14. data/docs/syntax.md +23 -20
  15. data/ext/rbs_extension/parser.c +97 -95
  16. data/ext/rbs_extension/ruby_objs.c +8 -6
  17. data/ext/rbs_extension/ruby_objs.h +2 -2
  18. data/lib/rbs/ast/declarations.rb +6 -2
  19. data/lib/rbs/cli.rb +1 -1
  20. data/lib/rbs/collection/sources/git.rb +9 -7
  21. data/lib/rbs/definition_builder.rb +58 -2
  22. data/lib/rbs/environment.rb +1 -0
  23. data/lib/rbs/environment_walker.rb +4 -1
  24. data/lib/rbs/errors.rb +12 -0
  25. data/lib/rbs/test/setup.rb +1 -0
  26. data/lib/rbs/type_alias_regularity.rb +115 -0
  27. data/lib/rbs/types.rb +11 -23
  28. data/lib/rbs/validator.rb +44 -15
  29. data/lib/rbs/variance_calculator.rb +52 -24
  30. data/lib/rbs/version.rb +1 -1
  31. data/lib/rbs/writer.rb +1 -1
  32. data/lib/rbs.rb +1 -0
  33. data/schema/decls.json +13 -1
  34. data/schema/types.json +8 -2
  35. data/sig/collection/collections.rbs +4 -2
  36. data/sig/declarations.rbs +9 -6
  37. data/sig/definition_builder.rbs +36 -0
  38. data/sig/environment_walker.rbs +26 -0
  39. data/sig/errors.rbs +10 -0
  40. data/sig/type_alias_regularity.rbs +92 -0
  41. data/sig/types.rbs +11 -8
  42. data/sig/validator.rbs +7 -0
  43. data/sig/variance_calculator.rbs +50 -0
  44. data/stdlib/bigdecimal/0/big_decimal.rbs +44 -0
  45. data/stdlib/csv/0/csv.rbs +49 -3
  46. data/stdlib/date/0/date.rbs +2 -2
  47. data/stdlib/set/0/set.rbs +3 -3
  48. data/steep/Gemfile.lock +10 -10
  49. metadata +7 -6
  50. data/lib/rbs/parser.y +0 -1805
  51. data/lib/ruby/signature.rb +0 -7
@@ -0,0 +1,92 @@
1
+ module RBS
2
+ # `TypeAliasRegularity` validates if a type alias is regular or not.
3
+ #
4
+ # Generic and recursive type alias cannot be polymorphic in their definitions.
5
+ #
6
+ # ```rbs
7
+ # type foo[T] = Integer
8
+ # | foo[T]? # Allowed. The type argument of `foo` doesn't change.
9
+ #
10
+ # type bar[T] = Integer
11
+ # | foo[T]
12
+ # | foo[Array[T]] # Allowed. There are two type arguments `T` and `Array[T]` of `foo`, but it's not definition of `foo`.
13
+ #
14
+ # type baz[T] = Integer
15
+ # | baz[Array[T]] # Error. Recursive definition of `baz` has different type argument from the definition.
16
+ # ```
17
+ #
18
+ # The `#nonregular?` method can be used to test if given type name is regular or not.
19
+ #
20
+ # ```rb
21
+ # validator = RBS::TypeAliasRegularity.validate(env: env)
22
+ #
23
+ # validator.nonregular?(TypeName("::foo")) # => nil
24
+ # validator.nonregular?(TypeName("::bar")) # => nil
25
+ # validator.nonregular?(TypeName("::baz")) # => TypeAliasRegularity::Diagnostic
26
+ # ```
27
+ #
28
+ # A special case is when the type argument is `untyped`.
29
+ #
30
+ # ```rbs
31
+ # type foo[T] = Integer | foo[untyped] # This is allowed.
32
+ # ```
33
+ #
34
+ class TypeAliasRegularity
35
+ attr_reader env: Environment
36
+
37
+ attr_reader builder: DefinitionBuilder
38
+
39
+ attr_reader diagnostics: Hash[TypeName, Diagnostic]
40
+
41
+ # `Diagnostic` represents an non-regular type alias declaration error.
42
+ # It consists of the name of the alias type and a type on which the nonregularity is detected.
43
+ #
44
+ # ```rbs
45
+ # type t[T] = Integer | t[T?]
46
+ # ```
47
+ #
48
+ # The type `t` is nonregular because it contains `t[T?]` on it's right hand side.
49
+ #
50
+ # ```
51
+ # diagnostic = validator.nonregular?(TypeName("::t"))
52
+ # diagnostic.type_name # => TypeName("::t")
53
+ # diagnostic.nonregular_type # => t[T?]
54
+ # ```
55
+ #
56
+ class Diagnostic
57
+ attr_reader type_name: TypeName
58
+
59
+ attr_reader nonregular_type: Types::Alias
60
+
61
+ def initialize: (type_name: TypeName, nonregular_type: Types::Alias) -> void
62
+ end
63
+
64
+ # Returns new instance which already run `#validate`.
65
+ #
66
+ def self.validate: (env: Environment) -> TypeAliasRegularity
67
+
68
+ def initialize: (env: Environment) -> void
69
+
70
+ # Returns `Diagnostic` instance if the alias type is nonregular.
71
+ # Regurns `nil` if the alias type is regular.
72
+ #
73
+ def nonregular?: (TypeName) -> Diagnostic?
74
+
75
+ def validate: () -> void
76
+
77
+ private
78
+
79
+ def validate_alias_type: (Types::Alias, Set[TypeName], Hash[TypeName, Types::Alias]) -> void
80
+
81
+ # Returns alias type for given type name, if the alias is generic.
82
+ # Returns nil if the type alias is not generic.
83
+ #
84
+ def build_alias_type: (TypeName) -> Types::Alias?
85
+
86
+ def compatible_args?: (Array[Types::t], Array[Types::t]) -> boolish
87
+
88
+ def each_alias_type: (Types::t) { (Types::Alias) -> void } -> void
89
+
90
+ def each_mutual_alias_defs: () { (Set[TypeName]) -> void } -> void
91
+ end
92
+ end
data/sig/types.rbs CHANGED
@@ -227,18 +227,21 @@ module RBS
227
227
  end
228
228
 
229
229
  class Alias
230
- attr_reader name: TypeName
230
+ # foo
231
+ # ^^^ => name
232
+ #
233
+ # foo[bar, baz]
234
+ # ^^^ => name
235
+ # ^^^^^^^^^^ => args
236
+ #
237
+ type loc = Location[:name, :args]
231
238
 
232
- type loc = Location[bot, bot]
239
+ attr_reader location: loc?
233
240
 
234
- def initialize: (name: TypeName, location: loc?) -> void
241
+ def initialize: (name: TypeName, args: Array[t], location: loc?) -> void
235
242
 
236
243
  include _TypeBase
237
- include NoFreeVariables
238
- include NoSubst
239
- include EmptyEachType
240
-
241
- attr_reader location: loc?
244
+ include Application
242
245
  end
243
246
 
244
247
  class Tuple
data/sig/validator.rbs CHANGED
@@ -1,8 +1,15 @@
1
1
  module RBS
2
2
  class Validator
3
3
  attr_reader env: Environment
4
+
4
5
  attr_reader resolver: TypeNameResolver
5
6
 
7
+ attr_reader definition_builder: DefinitionBuilder
8
+
9
+ attr_reader type_alias_dependency: TypeAliasDependency
10
+
11
+ attr_reader type_alias_regularity: TypeAliasRegularity
12
+
6
13
  def initialize: (env: Environment, resolver: TypeNameResolver) -> void
7
14
 
8
15
  def absolute_type: (Types::t, context: TypeNameResolver::context) { (Types::t) -> TypeName } -> Types::t
@@ -1,7 +1,47 @@
1
1
  module RBS
2
+ # Calculate the use variances of type variables in declaration.
3
+ #
4
+ # ```rb
5
+ # calculator = VarianceCalculator.new(builder: builder)
6
+ #
7
+ # # Calculates variances in a method type
8
+ # result = calculator.in_method_type(method_type: method_type, variables: variables)
9
+ #
10
+ # # Calculates variances in a inheritance/mixin/...
11
+ # result = calculator.in_inherit(name: name, args: args, variables: variables)
12
+ #
13
+ # # Calculates variances in a type alias
14
+ # result = calculator.in_type_alias(name: name, args: args, variables: variables)
15
+ # ```
16
+ #
17
+ # See `RBS::VarianceCaluculator::Result` for information recorded in the `Result` object.
18
+ #
2
19
  class VarianceCalculator
3
20
  type variance = :unused | :covariant | :contravariant | :invariant
4
21
 
22
+ # Result contains the set of type variables and it's variance in a occurrence.
23
+ #
24
+ # ```rb
25
+ # # Enumerates recorded type variables
26
+ # result.each do |name, variance|
27
+ # # name is the name of a type variable
28
+ # # variance is one of :unused | :covariant | :contravariant | :invariant
29
+ # end
30
+ # ```
31
+ #
32
+ # You can test with `compatible?` method if the type variable occurrences are compatible with specified (annotated) variance.
33
+ #
34
+ # ```rb
35
+ # # When T is declared as `out T`
36
+ # result.compatible?(:T, with_annotation: :covariant)
37
+ #
38
+ # # When T is declared as `in T`
39
+ # result.compatible?(:T, with_annotation: :contravariant)
40
+ #
41
+ # # When T is declared as `T`
42
+ # result.compatible?(:T, with_annotation: :invariant)
43
+ # ```
44
+ #
5
45
  class Result
6
46
  attr_reader result: Hash[Symbol, variance]
7
47
 
@@ -18,6 +58,8 @@ module RBS
18
58
  def include?: (Symbol) -> bool
19
59
 
20
60
  def compatible?: (Symbol, with_annotation: variance) -> bool
61
+
62
+ def incompatible?: (AST::Declarations::ModuleTypeParams) -> Set[Symbol]?
21
63
  end
22
64
 
23
65
  attr_reader builder: DefinitionBuilder
@@ -30,6 +72,14 @@ module RBS
30
72
 
31
73
  def in_inherit: (name: TypeName, args: Array[Types::t], variables: Array[Symbol]) -> Result
32
74
 
75
+ def in_type_alias: (name: TypeName) -> Result
76
+
77
+ private
78
+
33
79
  def type: (Types::t, result: Result, context: variance) -> void
80
+
81
+ def function: (Types::Function, result: Result, context: variance) -> void
82
+
83
+ def negate: (variance) -> variance
34
84
  end
35
85
  end
@@ -885,3 +885,47 @@ BigDecimal::SIGN_POSITIVE_ZERO: Integer
885
885
  # The version of bigdecimal library
886
886
  #
887
887
  BigDecimal::VERSION: String
888
+
889
+ module Kernel
890
+ private
891
+
892
+ # Create a new BigDecimal object.
893
+ #
894
+ # initial
895
+ # : The initial value, as an Integer, a Float, a Rational, a BigDecimal, or a
896
+ # String.
897
+ #
898
+ # If it is a String, spaces are ignored and unrecognized characters
899
+ # terminate the value.
900
+ #
901
+ # digits
902
+ # : The number of significant digits, as an Integer. If omitted or 0, the
903
+ # number of significant digits is determined from the initial value.
904
+ #
905
+ # The actual number of significant digits used in computation is usually
906
+ # larger than the specified number.
907
+ #
908
+ # exception
909
+ # : Whether an exception should be raised on invalid arguments. `true` by
910
+ # default, if passed `false`, just returns `nil` for invalid.
911
+ #
912
+ #
913
+ # #### Exceptions
914
+ #
915
+ # TypeError
916
+ # : If the `initial` type is neither Integer, Float, Rational, nor BigDecimal,
917
+ # this exception is raised.
918
+ #
919
+ # TypeError
920
+ # : If the `digits` is not an Integer, this exception is raised.
921
+ #
922
+ # ArgumentError
923
+ # : If `initial` is a Float, and the `digits` is larger than Float::DIG + 1,
924
+ # this exception is raised.
925
+ #
926
+ # ArgumentError
927
+ # : If the `initial` is a Float or Rational, and the `digits` value is
928
+ # omitted, this exception is raised.
929
+ #
930
+ def self?.BigDecimal: ((real | string | BigDecimal) initial, ?int digits, ?exception: bool) -> BigDecimal
931
+ end
data/stdlib/csv/0/csv.rbs CHANGED
@@ -360,6 +360,49 @@ class CSV < Object
360
360
  # output non-ASCII compatible data.
361
361
  #
362
362
  def self.generate: (?String str, **untyped options) { (CSV csv) -> void } -> String
363
+
364
+ # :call-seq:
365
+ # csv.each -> enumerator
366
+ # csv.each {|row| ...}
367
+ #
368
+ # Calls the block with each successive row.
369
+ # The data source must be opened for reading.
370
+ #
371
+ # Without headers:
372
+ # string = "foo,0\nbar,1\nbaz,2\n"
373
+ # csv = CSV.new(string)
374
+ # csv.each do |row|
375
+ # p row
376
+ # end
377
+ # Output:
378
+ # ["foo", "0"]
379
+ # ["bar", "1"]
380
+ # ["baz", "2"]
381
+ #
382
+ # With headers:
383
+ # string = "Name,Value\nfoo,0\nbar,1\nbaz,2\n"
384
+ # csv = CSV.new(string, headers: true)
385
+ # csv.each do |row|
386
+ # p row
387
+ # end
388
+ # Output:
389
+ # <CSV::Row "Name":"foo" "Value":"0">
390
+ # <CSV::Row "Name":"bar" "Value":"1">
391
+ # <CSV::Row "Name":"baz" "Value":"2">
392
+ #
393
+ # ---
394
+ #
395
+ # Raises an exception if the source is not opened for reading:
396
+ # string = "foo,0\nbar,1\nbaz,2\n"
397
+ # csv = CSV.new(string)
398
+ # csv.close
399
+ # # Raises IOError (not opened for reading)
400
+ # csv.each do |row|
401
+ # p row
402
+ # end
403
+ def each: () -> Enumerator[untyped, Integer]
404
+ | () { (untyped) -> void } -> Integer
405
+
363
406
  end
364
407
 
365
408
  # The options used when no overrides are given by calling code. They are:
@@ -408,7 +451,7 @@ CSV::VERSION: String
408
451
  # processing is activated.
409
452
  #
410
453
  class CSV::Row < Object
411
- include Enumerable[untyped]
454
+ include Enumerable[Array[String]]
412
455
  extend Forwardable
413
456
 
414
457
  # If a two-element Array is provided, it is assumed to be a header and field and
@@ -464,7 +507,8 @@ class CSV::Row < Object
464
507
  #
465
508
  # Support for Enumerable.
466
509
  #
467
- def each: () { (*untyped) -> untyped } -> untyped
510
+ def each: () -> Enumerator[Array[String], self]
511
+ | () { (Array[String]) -> void } -> self
468
512
 
469
513
  alias each_pair each
470
514
 
@@ -717,7 +761,9 @@ class CSV::Table[out Elem] < Object
717
761
  #
718
762
  # If no block is given, an Enumerator is returned.
719
763
  #
720
- def each: () { (*untyped) -> untyped } -> untyped
764
+ def each: () -> Enumerator[untyped, self]
765
+ | () { (untyped) -> void } -> self
766
+ | () { (*untyped) -> void } -> self
721
767
 
722
768
  def empty?: (*untyped args) { (*untyped) -> untyped } -> untyped
723
769
 
@@ -446,7 +446,7 @@ class Date
446
446
  # DateTime.jd(0,12) + DateTime.new(2001,2,3).ajd
447
447
  # #=> #<DateTime: 2001-02-03T00:00:00+00:00 ...>
448
448
  #
449
- def +: (Integer | Rational other) -> Date
449
+ def +: ((Numeric & _ToR) other) -> Date
450
450
 
451
451
  # Returns the difference between the two dates if the other is a date object.
452
452
  # If the other is a numeric value, returns a date object pointing `other` days
@@ -461,7 +461,7 @@ class Date
461
461
  # DateTime.new(2001,2,3) - DateTime.new(2001,2,2,12)
462
462
  # #=> (1/2)
463
463
  #
464
- def -: (Integer | Rational other) -> Date
464
+ def -: ((Numeric & _ToR) other) -> Date
465
465
  | (Date other) -> Rational
466
466
 
467
467
  # Returns a date object pointing `n` months before self. The argument `n` should
data/stdlib/set/0/set.rbs CHANGED
@@ -122,7 +122,7 @@ class Set[A]
122
122
  #
123
123
  # See also Enumerable#include?
124
124
  #
125
- def include?: (untyped) -> bool
125
+ def include?: (A) -> bool
126
126
 
127
127
  alias member? include?
128
128
 
@@ -168,12 +168,12 @@ class Set[A]
168
168
  # Deletes the given object from the set and returns self. Use `subtract` to
169
169
  # delete many items at once.
170
170
  #
171
- def delete: (untyped) -> self
171
+ def delete: (A) -> self
172
172
 
173
173
  # Deletes the given object from the set and returns self. If the object is not
174
174
  # in the set, returns nil.
175
175
  #
176
- def delete?: (untyped) -> self?
176
+ def delete?: (A) -> self?
177
177
 
178
178
  # Deletes every element of the set for which block evaluates to true, and
179
179
  # returns self. Returns an enumerator if no block is given.
data/steep/Gemfile.lock CHANGED
@@ -9,37 +9,37 @@ GEM
9
9
  zeitwerk (~> 2.3)
10
10
  ast (2.4.2)
11
11
  concurrent-ruby (1.1.9)
12
- ffi (1.15.3)
13
- i18n (1.8.10)
12
+ ffi (1.15.4)
13
+ i18n (1.8.11)
14
14
  concurrent-ruby (~> 1.0)
15
15
  language_server-protocol (3.16.0.3)
16
16
  listen (3.7.0)
17
17
  rb-fsevent (~> 0.10, >= 0.10.3)
18
18
  rb-inotify (~> 0.9, >= 0.9.10)
19
19
  minitest (5.14.4)
20
- parallel (1.20.1)
21
- parser (3.0.2.0)
20
+ parallel (1.21.0)
21
+ parser (3.0.3.1)
22
22
  ast (~> 2.4.1)
23
23
  rainbow (3.0.0)
24
24
  rb-fsevent (0.11.0)
25
25
  rb-inotify (0.10.1)
26
26
  ffi (~> 1.0)
27
- rbs (1.5.1)
28
- steep (0.46.0)
27
+ rbs (1.7.1)
28
+ steep (0.47.0)
29
29
  activesupport (>= 5.1)
30
30
  language_server-protocol (>= 3.15, < 4.0)
31
31
  listen (~> 3.0)
32
32
  parallel (>= 1.0.0)
33
33
  parser (>= 3.0)
34
34
  rainbow (>= 2.2.2, < 4.0)
35
- rbs (>= 1.2.0)
35
+ rbs (~> 1.7.0)
36
36
  terminal-table (>= 2, < 4)
37
- terminal-table (3.0.1)
37
+ terminal-table (3.0.2)
38
38
  unicode-display_width (>= 1.1.1, < 3)
39
39
  tzinfo (2.0.4)
40
40
  concurrent-ruby (~> 1.0)
41
- unicode-display_width (2.0.0)
42
- zeitwerk (2.4.2)
41
+ unicode-display_width (2.1.0)
42
+ zeitwerk (2.5.1)
43
43
 
44
44
  PLATFORMS
45
45
  ruby
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0.beta.5
4
+ version: 1.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-07 00:00:00.000000000 Z
11
+ date: 2021-12-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: RBS is the language for type signatures for Ruby and standard library
14
14
  definitions.
@@ -44,6 +44,7 @@ files:
44
44
  - core/encoding.rbs
45
45
  - core/enumerable.rbs
46
46
  - core/enumerator.rbs
47
+ - core/env.rbs
47
48
  - core/errno.rbs
48
49
  - core/errors.rbs
49
50
  - core/exception.rbs
@@ -152,7 +153,6 @@ files:
152
153
  - lib/rbs/locator.rb
153
154
  - lib/rbs/method_type.rb
154
155
  - lib/rbs/namespace.rb
155
- - lib/rbs/parser.y
156
156
  - lib/rbs/parser_aux.rb
157
157
  - lib/rbs/parser_compat/lexer_error.rb
158
158
  - lib/rbs/parser_compat/located_value.rb
@@ -174,6 +174,7 @@ files:
174
174
  - lib/rbs/test/tester.rb
175
175
  - lib/rbs/test/type_check.rb
176
176
  - lib/rbs/type_alias_dependency.rb
177
+ - lib/rbs/type_alias_regularity.rb
177
178
  - lib/rbs/type_name.rb
178
179
  - lib/rbs/type_name_resolver.rb
179
180
  - lib/rbs/types.rb
@@ -182,7 +183,6 @@ files:
182
183
  - lib/rbs/vendorer.rb
183
184
  - lib/rbs/version.rb
184
185
  - lib/rbs/writer.rb
185
- - lib/ruby/signature.rb
186
186
  - rbs.gemspec
187
187
  - schema/annotation.json
188
188
  - schema/comment.json
@@ -226,6 +226,7 @@ files:
226
226
  - sig/repository.rbs
227
227
  - sig/substitution.rbs
228
228
  - sig/type_alias_dependency.rbs
229
+ - sig/type_alias_regularity.rbs
229
230
  - sig/type_name_resolver.rbs
230
231
  - sig/typename.rbs
231
232
  - sig/types.rbs
@@ -341,9 +342,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
341
342
  version: '2.6'
342
343
  required_rubygems_version: !ruby/object:Gem::Requirement
343
344
  requirements:
344
- - - ">"
345
+ - - ">="
345
346
  - !ruby/object:Gem::Version
346
- version: 1.3.1
347
+ version: '0'
347
348
  requirements: []
348
349
  rubygems_version: 3.2.22
349
350
  signing_key: