rbs 1.0.6 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +2 -2
  3. data/CHANGELOG.md +37 -0
  4. data/Rakefile +3 -2
  5. data/Steepfile +2 -0
  6. data/bin/rbs-prof +1 -1
  7. data/core/array.rbs +8 -4
  8. data/core/thread.rbs +14 -1
  9. data/lib/rbs.rb +3 -0
  10. data/lib/rbs/ancestor_graph.rb +90 -0
  11. data/lib/rbs/char_scanner.rb +20 -0
  12. data/lib/rbs/definition_builder.rb +38 -20
  13. data/lib/rbs/definition_builder/method_builder.rb +14 -0
  14. data/lib/rbs/environment.rb +42 -5
  15. data/lib/rbs/environment_walker.rb +4 -4
  16. data/lib/rbs/errors.rb +32 -17
  17. data/lib/rbs/parser.rb +437 -416
  18. data/lib/rbs/parser.y +29 -16
  19. data/lib/rbs/test/type_check.rb +7 -3
  20. data/lib/rbs/version.rb +1 -1
  21. data/sig/ancestor_graph.rbs +40 -0
  22. data/sig/char_scanner.rbs +9 -0
  23. data/sig/definition_builder.rbs +5 -1
  24. data/sig/environment.rbs +22 -2
  25. data/sig/environment_walker.rbs +39 -0
  26. data/sig/errors.rbs +42 -17
  27. data/sig/method_builder.rbs +2 -0
  28. data/sig/parser.rbs +11 -4
  29. data/sig/polyfill.rbs +0 -14
  30. data/stdlib/cgi/0/core.rbs +595 -0
  31. data/stdlib/rubygems/0/basic_specification.rbs +3 -0
  32. data/stdlib/rubygems/0/config_file.rbs +3 -0
  33. data/stdlib/rubygems/0/dependency_installer.rbs +5 -0
  34. data/stdlib/rubygems/0/installer.rbs +3 -0
  35. data/stdlib/rubygems/0/path_support.rbs +3 -0
  36. data/stdlib/rubygems/0/platform.rbs +3 -0
  37. data/stdlib/rubygems/0/request_set.rbs +7 -0
  38. data/stdlib/rubygems/0/requirement.rbs +3 -0
  39. data/stdlib/rubygems/0/rubygems.rbs +710 -0
  40. data/stdlib/rubygems/0/source_list.rbs +2 -0
  41. data/stdlib/rubygems/0/specification.rbs +3 -0
  42. data/stdlib/rubygems/0/stream_ui.rbs +3 -0
  43. data/stdlib/rubygems/0/uninstaller.rbs +3 -0
  44. data/stdlib/rubygems/0/version.rbs +228 -0
  45. data/stdlib/strscan/0/string_scanner.rbs +582 -0
  46. metadata +23 -2
data/lib/rbs/parser.y CHANGED
@@ -760,8 +760,8 @@ rule
760
760
  location = val[0].location + val[1].location
761
761
  result = Types::Tuple.new(types: [], location: location)
762
762
  }
763
- | kLBRACKET type_list kRBRACKET {
764
- location = val[0].location + val[2].location
763
+ | kLBRACKET type_list comma_opt kRBRACKET {
764
+ location = val[0].location + val[3].location
765
765
  types = val[1]
766
766
  result = Types::Tuple.new(types: types, location: location)
767
767
  }
@@ -794,10 +794,10 @@ rule
794
794
  }
795
795
 
796
796
  record_type:
797
- kLBRACE record_fields kRBRACE {
797
+ kLBRACE record_fields comma_opt kRBRACE {
798
798
  result = Types::Record.new(
799
799
  fields: val[1],
800
- location: val[0].location + val[2].location
800
+ location: val[0].location + val[3].location
801
801
  )
802
802
  }
803
803
 
@@ -805,7 +805,7 @@ rule
805
805
  record_field {
806
806
  result = val[0]
807
807
  }
808
- | record_field kCOMMA record_fields {
808
+ | record_fields kCOMMA record_field {
809
809
  result = val[0].merge!(val[2])
810
810
  }
811
811
 
@@ -1062,6 +1062,10 @@ rule
1062
1062
  namespace = Namespace.parse(val[0].value)
1063
1063
  result = LocatedValue.new(value: namespace, location: val[0].location)
1064
1064
  }
1065
+
1066
+ comma_opt:
1067
+ kCOMMA | # empty
1068
+
1065
1069
  end
1066
1070
 
1067
1071
  ---- inner
@@ -1084,8 +1088,6 @@ class LocatedValue
1084
1088
  end
1085
1089
  end
1086
1090
 
1087
- require "strscan"
1088
-
1089
1091
  attr_reader :input
1090
1092
  attr_reader :buffer
1091
1093
  attr_reader :eof_re
@@ -1094,7 +1096,7 @@ def initialize(type, buffer:, eof_re:)
1094
1096
  super()
1095
1097
  @type = type
1096
1098
  @buffer = buffer
1097
- @input = StringScanner.new(buffer.content)
1099
+ @input = CharScanner.new(buffer.content)
1098
1100
  @eof_re = eof_re
1099
1101
  @eof = false
1100
1102
  @bound_variables_stack = []
@@ -1210,11 +1212,7 @@ def new_token(type, value = input.matched)
1210
1212
  end
1211
1213
 
1212
1214
  def charpos(scanner)
1213
- if @ascii_only
1214
- scanner.pos
1215
- else
1216
- scanner.charpos
1217
- end
1215
+ scanner.charpos
1218
1216
  end
1219
1217
 
1220
1218
  def empty_params_result
@@ -1421,7 +1419,11 @@ def next_token
1421
1419
  s = input.matched.yield_self {|s| s[1, s.length - 2] }.gsub(/\\'/, "'")
1422
1420
  new_token(:tSTRING, s)
1423
1421
  else
1424
- raise "Unexpected token: #{input.peek(10)}..."
1422
+ text = input.peek(10)
1423
+ start_index = charpos(input)
1424
+ end_index = start_index + text.length
1425
+ location = RBS::Location.new(buffer: buffer, start_pos: start_index, end_pos: end_index)
1426
+ raise LexerError.new(input: text, location: location)
1425
1427
  end
1426
1428
  end
1427
1429
 
@@ -1429,7 +1431,7 @@ def on_error(token_id, error_value, value_stack)
1429
1431
  raise SyntaxError.new(token_str: token_to_str(token_id), error_value: error_value, value_stack: value_stack)
1430
1432
  end
1431
1433
 
1432
- class SyntaxError < StandardError
1434
+ class SyntaxError < ParsingError
1433
1435
  attr_reader :token_str, :error_value, :value_stack
1434
1436
 
1435
1437
  def initialize(token_str:, error_value:, value_stack: nil)
@@ -1441,7 +1443,7 @@ class SyntaxError < StandardError
1441
1443
  end
1442
1444
  end
1443
1445
 
1444
- class SemanticsError < StandardError
1446
+ class SemanticsError < ParsingError
1445
1447
  attr_reader :subject, :location, :original_message
1446
1448
 
1447
1449
  def initialize(message, subject:, location:)
@@ -1453,4 +1455,15 @@ class SemanticsError < StandardError
1453
1455
  end
1454
1456
  end
1455
1457
 
1458
+ class LexerError < ParsingError
1459
+ attr_reader :location, :input
1460
+
1461
+ def initialize(input:, location:)
1462
+ @input = input
1463
+ @location = location
1464
+
1465
+ super "Unexpected string: #{input}..."
1466
+ end
1467
+ end
1468
+
1456
1469
  ---- footer
@@ -5,13 +5,17 @@ module RBS
5
5
  attr_reader :builder
6
6
  attr_reader :sample_size
7
7
  attr_reader :unchecked_classes
8
+ attr_reader :instance_class
9
+ attr_reader :class_class
8
10
 
9
11
  attr_reader :const_cache
10
12
 
11
13
  DEFAULT_SAMPLE_SIZE = 100
12
14
 
13
- def initialize(self_class:, builder:, sample_size:, unchecked_classes:)
15
+ def initialize(self_class:, builder:, sample_size:, unchecked_classes:, instance_class: Object, class_class: Module)
14
16
  @self_class = self_class
17
+ @instance_class = instance_class
18
+ @class_class = class_class
15
19
  @builder = builder
16
20
  @sample_size = sample_size
17
21
  @unchecked_classes = unchecked_classes.uniq
@@ -237,9 +241,9 @@ module RBS
237
241
  when Types::Bases::Nil
238
242
  Test.call(val, IS_AP, ::NilClass)
239
243
  when Types::Bases::Class
240
- Test.call(val, IS_AP, Class)
244
+ Test.call(val, IS_AP, class_class)
241
245
  when Types::Bases::Instance
242
- Test.call(val, IS_AP, self_class)
246
+ Test.call(val, IS_AP, instance_class)
243
247
  when Types::ClassInstance
244
248
  klass = get_class(type.name) or return false
245
249
  case
data/lib/rbs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module RBS
2
- VERSION = "1.0.6"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,40 @@
1
+ module RBS
2
+ class AncestorGraph
3
+ class InstanceNode
4
+ attr_reader type_name: TypeName
5
+ def initialize: (type_name: TypeName) -> void
6
+ end
7
+
8
+ class SingletonNode
9
+ attr_reader type_name: TypeName
10
+ def initialize: (type_name: TypeName) -> void
11
+ end
12
+
13
+ type node = InstanceNode | SingletonNode
14
+
15
+ attr_reader env: Environment
16
+ attr_reader ancestor_builder: DefinitionBuilder::AncestorBuilder
17
+ attr_reader parents: Hash[node, Set[node]]
18
+ attr_reader children: Hash[node, Set[node]]
19
+
20
+ def initialize: (env: Environment, ?ancestor_builder: DefinitionBuilder::AncestorBuilder) -> void
21
+
22
+ def build: () -> void
23
+
24
+ def each_parent: (node) { (node) -> void } -> void
25
+ | (node) -> Enumerator[node, void]
26
+
27
+ def each_ancestor: (node, ?yielded: Set[node]) { (node) -> void } -> void
28
+ | (node) -> Enumerator[node, void]
29
+
30
+ def each_child: (node) { (node) -> void } -> void
31
+ | (node) -> Enumerator[node, void]
32
+
33
+ def each_descendant: (node, ?yielded: Set[node]) { (node) -> void } -> void
34
+ | (node) -> Enumerator[node, void]
35
+
36
+ def build_ancestors: (node, DefinitionBuilder::AncestorBuilder::OneAncestors) -> void
37
+
38
+ def register: (parent: node, child: node) -> void
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ module RBS
2
+ class CharScanner < StringScanner
3
+ def initialize: (String) -> void
4
+
5
+ def original_charpos: () -> Integer
6
+
7
+ @charpos: Integer
8
+ end
9
+ end
@@ -10,7 +10,7 @@ module RBS
10
10
  attr_reader singleton0_cache: Hash[TypeName, Definition | false | nil]
11
11
  attr_reader interface_cache: Hash[TypeName, Definition | false | nil]
12
12
 
13
- def initialize: (env: Environment) -> void
13
+ def initialize: (env: Environment, ?ancestor_builder: AncestorBuilder?, ?method_builder: MethodBuilder?) -> void
14
14
 
15
15
  def validate_super_class!: (TypeName, Environment::ClassEntry) -> void
16
16
 
@@ -42,5 +42,9 @@ module RBS
42
42
  def insert_variable: (TypeName, Hash[Symbol, Definition::Variable], name: Symbol, type: Types::t) -> void
43
43
 
44
44
  def define_methods: (Definition, interface_methods: Hash[Symbol, Definition::Method], methods: MethodBuilder::Methods, super_interface_method: bool) -> void
45
+
46
+ def expand_alias: (TypeName) -> Types::t
47
+
48
+ def update: (env: Environment, ancestor_builder: AncestorBuilder, except: _Each[TypeName]) -> DefinitionBuilder
45
49
  end
46
50
  end
data/sig/environment.rbs CHANGED
@@ -58,7 +58,7 @@ module RBS
58
58
  def initialize: (name: N, decl: D, outer: Array[module_decl]) -> void
59
59
  end
60
60
 
61
- attr_reader buffers: Array[Buffer]
61
+ # Top level declarations.
62
62
  attr_reader declarations: Array[AST::Declarations::t]
63
63
 
64
64
  attr_reader class_decls: Hash[TypeName, ModuleEntry | ClassEntry]
@@ -77,9 +77,20 @@ module RBS
77
77
 
78
78
  def insert_decl: (AST::Declarations::t, outer: Array[module_decl], namespace: Namespace) -> void
79
79
 
80
+ # Insert a toplevel declaration into the env.
81
+ #
80
82
  def <<: (AST::Declarations::t decl) -> self
81
83
 
82
- def resolve_type_names: () -> Environment
84
+ # Runs generics type params validation over each class definitions.
85
+ def validate_type_params: () -> void
86
+
87
+ # Resolve all type names in the environment to absolute type names.
88
+ # Relative type name will be left if absolute type name cannot be found.
89
+ #
90
+ # When `only` is given, it skips other _top-level_ declarations not included in the collection.
91
+ # This helps running resolution faster in the case of _partial updates_.
92
+ #
93
+ def resolve_type_names: (?only: Set[AST::Declarations::t]?) -> Environment
83
94
 
84
95
  def resolve_declaration: (TypeNameResolver resolver, AST::Declarations::t decl, outer: Array[module_decl], prefix: Namespace) -> AST::Declarations::t
85
96
 
@@ -90,5 +101,14 @@ module RBS
90
101
  def absolute_type_name: (TypeNameResolver, TypeName, context: Array[Namespace]) -> TypeName
91
102
 
92
103
  def inspect: () -> String
104
+
105
+ def buffers: () -> Array[Buffer]
106
+
107
+ def buffers_decls: () -> Hash[Buffer, Array[AST::Declarations::t]]
108
+
109
+ # Construct new environment without declarations tested `true` by block.
110
+ # Construction of new environment is done with `<<` so that nested declarations will work well.
111
+ #
112
+ def reject: () { (AST::Declarations::t) -> boolish } -> Environment
93
113
  end
94
114
  end
@@ -0,0 +1,39 @@
1
+ module RBS
2
+ class EnvironmentWalker
3
+ class InstanceNode
4
+ attr_reader type_name: TypeName
5
+ def initialize: (type_name: TypeName) -> void
6
+ end
7
+
8
+ class SingletonNode
9
+ attr_reader type_name: TypeName
10
+ def initialize: (type_name: TypeName) -> void
11
+ end
12
+
13
+ class TypeNameNode
14
+ attr_reader type_name: TypeName
15
+ def initialize: (type_name: TypeName) -> void
16
+ end
17
+
18
+ attr_reader env: Environment
19
+ attr_reader only_ancestors: bool
20
+ attr_reader builder: DefinitionBuilder
21
+
22
+ def initialize: (env: Environment) -> void
23
+
24
+ def only_ancestors!: (?bool only) -> self
25
+
26
+ def only_ancestors?: () -> bool
27
+
28
+ type node = InstanceNode | SingletonNode | TypeNameNode
29
+ include TSort[node]
30
+
31
+ def tsort_each_node: () { (node) -> void } -> void
32
+
33
+ def tsort_each_child: (node) { (node) -> void } -> void
34
+
35
+ def each_type_name: (Types::t) { (TypeName) -> void } -> void
36
+
37
+ def each_type_node: (Types::t) { (node) -> void } -> void
38
+ end
39
+ end
data/sig/errors.rbs CHANGED
@@ -11,7 +11,27 @@ module RBS
11
11
  def method_name_string: () -> String
12
12
  end
13
13
 
14
- class InvalidTypeApplicationError < StandardError
14
+ # Error class for errors defined in RBS.
15
+ #
16
+ class BaseError < StandardError
17
+ end
18
+
19
+ # Error class for errors raised during parsing.
20
+ #
21
+ class ParsingError < BaseError
22
+ end
23
+
24
+ # Error class for errors raised during loading environments.
25
+ #
26
+ class LoadingError < BaseError
27
+ end
28
+
29
+ # Error class for errors raised during building definitions.
30
+ #
31
+ class DefinitionError < BaseError
32
+ end
33
+
34
+ class InvalidTypeApplicationError < DefinitionError
15
35
  attr_reader type_name: TypeName
16
36
  attr_reader args: Array[Types::t]
17
37
  attr_reader params: Array[Symbol]
@@ -22,16 +42,16 @@ module RBS
22
42
  def self.check!: (type_name: TypeName, args: Array[Types::t], params: Array[Symbol], location: Location?) -> void
23
43
  end
24
44
 
25
- class RecursiveAncestorError < StandardError
45
+ class RecursiveAncestorError < DefinitionError
26
46
  attr_reader ancestors: Array[Definition::Ancestor::t]
27
- attr_reader location: Location
47
+ attr_reader location: Location?
28
48
 
29
49
  def initialize: (ancestors: Array[Definition::Ancestor::t], location: Location?) -> void
30
50
 
31
51
  def self.check!: (Definition::Ancestor::t, ancestors: Array[Definition::Ancestor::t], location: Location?) -> void
32
52
  end
33
53
 
34
- class NoTypeFoundError < StandardError
54
+ class NoTypeFoundError < DefinitionError
35
55
  attr_reader type_name: TypeName
36
56
  attr_reader location: Location?
37
57
 
@@ -40,7 +60,7 @@ module RBS
40
60
  def self.check!: (TypeName, env: Environment, location: Location?) -> TypeName
41
61
  end
42
62
 
43
- class NoSuperclassFoundError < StandardError
63
+ class NoSuperclassFoundError < DefinitionError
44
64
  attr_reader type_name: TypeName
45
65
  attr_reader location: Location?
46
66
 
@@ -49,7 +69,7 @@ module RBS
49
69
  def self.check!: (TypeName, env: Environment, location: Location?) -> void
50
70
  end
51
71
 
52
- class NoSelfTypeFoundError < StandardError
72
+ class NoSelfTypeFoundError < DefinitionError
53
73
  attr_reader type_name: TypeName
54
74
  attr_reader location: Location?
55
75
 
@@ -58,7 +78,7 @@ module RBS
58
78
  def self.check!: (AST::Declarations::Module::Self, env: Environment) -> void
59
79
  end
60
80
 
61
- class NoMixinFoundError < StandardError
81
+ class NoMixinFoundError < DefinitionError
62
82
  attr_reader type_name: TypeName
63
83
  attr_reader member: AST::Members::t
64
84
 
@@ -69,7 +89,7 @@ module RBS
69
89
  def self.check!: (TypeName, env: Environment, member: AST::Members::t) -> void
70
90
  end
71
91
 
72
- class DuplicatedMethodDefinitionError < StandardError
92
+ class DuplicatedMethodDefinitionError < DefinitionError
73
93
  type ty = Types::ClassSingleton | Types::ClassInstance | Types::Interface
74
94
  type original = DefinitionBuilder::MethodBuilder::Methods::Definition::original
75
95
 
@@ -79,6 +99,8 @@ module RBS
79
99
 
80
100
  def initialize: (type: ty, method_name: Symbol, members: Array[original]) -> void
81
101
 
102
+ def type_name: () -> TypeName
103
+
82
104
  def qualified_method_name: () -> String
83
105
 
84
106
  def location: () -> Location?
@@ -86,7 +108,7 @@ module RBS
86
108
  def other_locations: () -> Array[Location?]
87
109
  end
88
110
 
89
- class DuplicatedInterfaceMethodDefinitionError < StandardError
111
+ class DuplicatedInterfaceMethodDefinitionError < DefinitionError
90
112
  type ty = Types::ClassSingleton | Types::ClassInstance | Types::Interface
91
113
  type mixin_member = AST::Members::Include | AST::Members::Extend
92
114
 
@@ -96,25 +118,28 @@ module RBS
96
118
 
97
119
  def initialize: (type: ty, method_name: Symbol, member: mixin_member) -> void
98
120
 
121
+ def type_name: () -> TypeName
122
+
99
123
  def qualified_method_name: () -> String
100
124
  end
101
125
 
102
- class UnknownMethodAliasError < StandardError
126
+ class UnknownMethodAliasError < DefinitionError
127
+ attr_reader type_name: TypeName
103
128
  attr_reader original_name: Symbol
104
129
  attr_reader aliased_name: Symbol
105
130
  attr_reader location: Location?
106
131
 
107
- def initialize: (original_name: Symbol, aliased_name: Symbol, location: Location?) -> void
132
+ def initialize: (type_name: TypeName, original_name: Symbol, aliased_name: Symbol, location: Location?) -> void
108
133
  end
109
134
 
110
- class SuperclassMismatchError < StandardError
135
+ class SuperclassMismatchError < DefinitionError
111
136
  attr_reader name: TypeName
112
137
  attr_reader entry: Environment::ClassEntry
113
138
 
114
139
  def initialize: (name: TypeName, entry: Environment::ClassEntry) -> void
115
140
  end
116
141
 
117
- class InvalidOverloadMethodError < StandardError
142
+ class InvalidOverloadMethodError < DefinitionError
118
143
  attr_reader type_name: TypeName
119
144
  attr_reader method_name: Symbol
120
145
  attr_reader kind: :instance | :singleton
@@ -123,21 +148,21 @@ module RBS
123
148
  def initialize: (type_name: TypeName, method_name: Symbol, kind: :instance | :singleton, members: Array[AST::Members::MethodDefinition]) -> void
124
149
  end
125
150
 
126
- class GenericParameterMismatchError < StandardError
151
+ class GenericParameterMismatchError < LoadingError
127
152
  attr_reader name: TypeName
128
153
  attr_reader decl: AST::Declarations::Class | AST::Declarations::Module
129
154
 
130
155
  def initialize: (name: TypeName, decl: AST::Declarations::Class | AST::Declarations::Module) -> void
131
156
  end
132
157
 
133
- class DuplicatedDeclarationError < StandardError
158
+ class DuplicatedDeclarationError < LoadingError
134
159
  attr_reader name: TypeName | Symbol
135
160
  attr_reader decls: Array[AST::Declarations::t]
136
161
 
137
162
  def initialize: (TypeName | Symbol, *AST::Declarations::t) -> void
138
163
  end
139
164
 
140
- class InvalidVarianceAnnotationError < StandardError
165
+ class InvalidVarianceAnnotationError < DefinitionError
141
166
  attr_reader type_name: TypeName
142
167
  attr_reader param: AST::Declarations::ModuleTypeParams::TypeParam
143
168
  attr_reader location: Location?
@@ -145,7 +170,7 @@ module RBS
145
170
  def initialize: (type_name: TypeName, param: AST::Declarations::ModuleTypeParams::TypeParam, location: Location?) -> void
146
171
  end
147
172
 
148
- class RecursiveAliasDefinitionError < StandardError
173
+ class RecursiveAliasDefinitionError < DefinitionError
149
174
  type ty = Types::ClassInstance | Types::ClassSingleton | Types::Interface
150
175
  type defn = DefinitionBuilder::MethodBuilder::Methods::Definition
151
176