steep-activesupport-4 1.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +1032 -0
  5. data/LICENSE +21 -0
  6. data/README.md +260 -0
  7. data/Rakefile +227 -0
  8. data/Steepfile +68 -0
  9. data/bin/console +14 -0
  10. data/bin/generate-diagnostics-docs.rb +112 -0
  11. data/bin/mem_graph.rb +67 -0
  12. data/bin/mem_prof.rb +102 -0
  13. data/bin/output_rebaseline.rb +34 -0
  14. data/bin/output_test.rb +60 -0
  15. data/bin/rbs +20 -0
  16. data/bin/rbs-inline +19 -0
  17. data/bin/setup +9 -0
  18. data/bin/stackprof_test.rb +19 -0
  19. data/bin/steep +19 -0
  20. data/bin/steep-check.rb +251 -0
  21. data/bin/steep-prof +16 -0
  22. data/doc/narrowing.md +195 -0
  23. data/doc/shape.md +194 -0
  24. data/exe/steep +18 -0
  25. data/guides/README.md +5 -0
  26. data/guides/src/gem-rbs-collection/gem-rbs-collection.md +126 -0
  27. data/guides/src/getting-started/getting-started.md +163 -0
  28. data/guides/src/nil-optional/nil-optional.md +195 -0
  29. data/lib/steep/annotation_parser.rb +199 -0
  30. data/lib/steep/ast/annotation/collection.rb +172 -0
  31. data/lib/steep/ast/annotation.rb +137 -0
  32. data/lib/steep/ast/builtin.rb +104 -0
  33. data/lib/steep/ast/ignore.rb +148 -0
  34. data/lib/steep/ast/node/type_application.rb +88 -0
  35. data/lib/steep/ast/node/type_assertion.rb +81 -0
  36. data/lib/steep/ast/types/any.rb +35 -0
  37. data/lib/steep/ast/types/boolean.rb +45 -0
  38. data/lib/steep/ast/types/bot.rb +35 -0
  39. data/lib/steep/ast/types/class.rb +43 -0
  40. data/lib/steep/ast/types/factory.rb +557 -0
  41. data/lib/steep/ast/types/helper.rb +40 -0
  42. data/lib/steep/ast/types/instance.rb +42 -0
  43. data/lib/steep/ast/types/intersection.rb +93 -0
  44. data/lib/steep/ast/types/literal.rb +59 -0
  45. data/lib/steep/ast/types/logic.rb +84 -0
  46. data/lib/steep/ast/types/name.rb +128 -0
  47. data/lib/steep/ast/types/nil.rb +41 -0
  48. data/lib/steep/ast/types/proc.rb +117 -0
  49. data/lib/steep/ast/types/record.rb +79 -0
  50. data/lib/steep/ast/types/self.rb +43 -0
  51. data/lib/steep/ast/types/shared_instance.rb +11 -0
  52. data/lib/steep/ast/types/top.rb +35 -0
  53. data/lib/steep/ast/types/tuple.rb +60 -0
  54. data/lib/steep/ast/types/union.rb +97 -0
  55. data/lib/steep/ast/types/var.rb +65 -0
  56. data/lib/steep/ast/types/void.rb +35 -0
  57. data/lib/steep/cli.rb +401 -0
  58. data/lib/steep/diagnostic/deprecated/else_on_exhaustive_case.rb +20 -0
  59. data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
  60. data/lib/steep/diagnostic/helper.rb +18 -0
  61. data/lib/steep/diagnostic/lsp_formatter.rb +78 -0
  62. data/lib/steep/diagnostic/result_printer2.rb +48 -0
  63. data/lib/steep/diagnostic/ruby.rb +1221 -0
  64. data/lib/steep/diagnostic/signature.rb +570 -0
  65. data/lib/steep/drivers/annotations.rb +52 -0
  66. data/lib/steep/drivers/check.rb +339 -0
  67. data/lib/steep/drivers/checkfile.rb +210 -0
  68. data/lib/steep/drivers/diagnostic_printer.rb +105 -0
  69. data/lib/steep/drivers/init.rb +66 -0
  70. data/lib/steep/drivers/langserver.rb +56 -0
  71. data/lib/steep/drivers/print_project.rb +113 -0
  72. data/lib/steep/drivers/stats.rb +203 -0
  73. data/lib/steep/drivers/utils/driver_helper.rb +143 -0
  74. data/lib/steep/drivers/utils/jobs_option.rb +26 -0
  75. data/lib/steep/drivers/vendor.rb +27 -0
  76. data/lib/steep/drivers/watch.rb +194 -0
  77. data/lib/steep/drivers/worker.rb +58 -0
  78. data/lib/steep/equatable.rb +23 -0
  79. data/lib/steep/expectations.rb +228 -0
  80. data/lib/steep/index/rbs_index.rb +350 -0
  81. data/lib/steep/index/signature_symbol_provider.rb +185 -0
  82. data/lib/steep/index/source_index.rb +167 -0
  83. data/lib/steep/interface/block.rb +103 -0
  84. data/lib/steep/interface/builder.rb +843 -0
  85. data/lib/steep/interface/function.rb +1090 -0
  86. data/lib/steep/interface/method_type.rb +330 -0
  87. data/lib/steep/interface/shape.rb +239 -0
  88. data/lib/steep/interface/substitution.rb +159 -0
  89. data/lib/steep/interface/type_param.rb +115 -0
  90. data/lib/steep/located_value.rb +20 -0
  91. data/lib/steep/method_name.rb +42 -0
  92. data/lib/steep/module_helper.rb +24 -0
  93. data/lib/steep/node_helper.rb +273 -0
  94. data/lib/steep/path_helper.rb +30 -0
  95. data/lib/steep/project/dsl.rb +268 -0
  96. data/lib/steep/project/group.rb +31 -0
  97. data/lib/steep/project/options.rb +63 -0
  98. data/lib/steep/project/pattern.rb +59 -0
  99. data/lib/steep/project/target.rb +92 -0
  100. data/lib/steep/project.rb +78 -0
  101. data/lib/steep/rake_task.rb +132 -0
  102. data/lib/steep/range_extension.rb +29 -0
  103. data/lib/steep/server/base_worker.rb +97 -0
  104. data/lib/steep/server/change_buffer.rb +73 -0
  105. data/lib/steep/server/custom_methods.rb +77 -0
  106. data/lib/steep/server/delay_queue.rb +45 -0
  107. data/lib/steep/server/interaction_worker.rb +492 -0
  108. data/lib/steep/server/lsp_formatter.rb +455 -0
  109. data/lib/steep/server/master.rb +912 -0
  110. data/lib/steep/server/target_group_files.rb +205 -0
  111. data/lib/steep/server/type_check_controller.rb +366 -0
  112. data/lib/steep/server/type_check_worker.rb +303 -0
  113. data/lib/steep/server/work_done_progress.rb +64 -0
  114. data/lib/steep/server/worker_process.rb +176 -0
  115. data/lib/steep/services/completion_provider.rb +802 -0
  116. data/lib/steep/services/content_change.rb +61 -0
  117. data/lib/steep/services/file_loader.rb +74 -0
  118. data/lib/steep/services/goto_service.rb +441 -0
  119. data/lib/steep/services/hover_provider/rbs.rb +88 -0
  120. data/lib/steep/services/hover_provider/ruby.rb +221 -0
  121. data/lib/steep/services/hover_provider/singleton_methods.rb +20 -0
  122. data/lib/steep/services/path_assignment.rb +46 -0
  123. data/lib/steep/services/signature_help_provider.rb +202 -0
  124. data/lib/steep/services/signature_service.rb +428 -0
  125. data/lib/steep/services/stats_calculator.rb +68 -0
  126. data/lib/steep/services/type_check_service.rb +394 -0
  127. data/lib/steep/services/type_name_completion.rb +236 -0
  128. data/lib/steep/signature/validator.rb +651 -0
  129. data/lib/steep/source/ignore_ranges.rb +69 -0
  130. data/lib/steep/source.rb +691 -0
  131. data/lib/steep/subtyping/cache.rb +30 -0
  132. data/lib/steep/subtyping/check.rb +1113 -0
  133. data/lib/steep/subtyping/constraints.rb +341 -0
  134. data/lib/steep/subtyping/relation.rb +101 -0
  135. data/lib/steep/subtyping/result.rb +324 -0
  136. data/lib/steep/subtyping/variable_variance.rb +89 -0
  137. data/lib/steep/test.rb +9 -0
  138. data/lib/steep/thread_waiter.rb +43 -0
  139. data/lib/steep/type_construction.rb +5183 -0
  140. data/lib/steep/type_inference/block_params.rb +416 -0
  141. data/lib/steep/type_inference/case_when.rb +303 -0
  142. data/lib/steep/type_inference/constant_env.rb +56 -0
  143. data/lib/steep/type_inference/context.rb +195 -0
  144. data/lib/steep/type_inference/logic_type_interpreter.rb +613 -0
  145. data/lib/steep/type_inference/method_call.rb +193 -0
  146. data/lib/steep/type_inference/method_params.rb +531 -0
  147. data/lib/steep/type_inference/multiple_assignment.rb +194 -0
  148. data/lib/steep/type_inference/send_args.rb +712 -0
  149. data/lib/steep/type_inference/type_env.rb +341 -0
  150. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  151. data/lib/steep/typing.rb +321 -0
  152. data/lib/steep/version.rb +3 -0
  153. data/lib/steep.rb +369 -0
  154. data/manual/annotations.md +181 -0
  155. data/manual/ignore.md +20 -0
  156. data/manual/ruby-diagnostics.md +1879 -0
  157. data/sample/Steepfile +22 -0
  158. data/sample/lib/conference.rb +49 -0
  159. data/sample/lib/length.rb +35 -0
  160. data/sample/sig/conference.rbs +42 -0
  161. data/sample/sig/generics.rbs +15 -0
  162. data/sample/sig/length.rbs +34 -0
  163. data/steep-activesupport-4.gemspec +55 -0
  164. metadata +437 -0
@@ -0,0 +1,341 @@
1
+ module Steep
2
+ module Subtyping
3
+ class Constraints
4
+ class UnsatisfiedInvariantError < StandardError
5
+ attr_reader :constraints
6
+ attr_reader :reason
7
+
8
+ def initialize(reason:, constraints:)
9
+ @reason = reason
10
+ @constraints = constraints
11
+ super "Invalid constraint: reason=#{reason.message}, constraints=#{constraints.to_s}"
12
+ end
13
+
14
+ class VariablesUnknownsNotDisjoint
15
+ attr_reader :vars
16
+
17
+ def initialize(vars:)
18
+ @vars = vars
19
+ end
20
+
21
+ def message
22
+ "Variables and unknowns should be disjoint (#{vars})"
23
+ end
24
+ end
25
+
26
+ class VariablesFreeVariablesNotDisjoint
27
+ attr_reader :var
28
+ attr_reader :lower_bound
29
+ attr_reader :upper_bound
30
+
31
+ def initialize(var:, lower_bound:, upper_bound:)
32
+ @var = var
33
+ @lower_bound = lower_bound
34
+ @upper_bound = upper_bound
35
+ end
36
+
37
+ def message
38
+ "Variables and FV(constraints) should be disjoint (#{var}, #{lower_bound}, #{upper_bound})"
39
+ end
40
+ end
41
+
42
+ class UnknownsFreeVariableNotDisjoint
43
+ attr_reader :var
44
+ attr_reader :upper_bound
45
+ attr_reader :lower_bound
46
+
47
+ def initialize(var:, lower_bound:, upper_bound:)
48
+ @var = var
49
+ @lower_bound = lower_bound
50
+ @upper_bound = upper_bound
51
+ end
52
+
53
+ def message
54
+ "Unknowns and FV(constraints) should be disjoint (#{var}, #{lower_bound}, #{upper_bound})"
55
+ end
56
+ end
57
+ end
58
+
59
+ class UnsatisfiableConstraint < StandardError
60
+ attr_reader :var
61
+ attr_reader :sub_type
62
+ attr_reader :super_type
63
+ attr_reader :result
64
+
65
+ def initialize(var:, sub_type:, super_type:, result:)
66
+ @var = var
67
+ @sub_type = sub_type
68
+ @super_type = super_type
69
+ @result = result
70
+
71
+ super "Unsatisfiable constraint on #{var}: #{sub_type} <: #{super_type}"
72
+ end
73
+ end
74
+
75
+ attr_reader :dictionary
76
+ attr_reader :vars
77
+
78
+ def initialize(unknowns:)
79
+ @dictionary = {}
80
+ @vars = Set.new
81
+
82
+ unknowns.each do |var|
83
+ dictionary[var] = [Set.new, Set.new, Set.new]
84
+ end
85
+ end
86
+
87
+ def self.empty
88
+ new(unknowns: [])
89
+ end
90
+
91
+ def add_var(*vars)
92
+ vars.each do |var|
93
+ self.vars << var
94
+ end
95
+
96
+ unless Set.new(vars).disjoint?(unknowns)
97
+ raise UnsatisfiedInvariantError.new(
98
+ reason: UnsatisfiedInvariantError::VariablesUnknownsNotDisjoint.new(vars: vars),
99
+ constraints: self
100
+ )
101
+ end
102
+ end
103
+
104
+ def add(var, sub_type: nil, super_type: nil, skip: false)
105
+ subs, supers, skips = dictionary.fetch(var)
106
+
107
+ if sub_type.is_a?(AST::Types::Logic::Base)
108
+ sub_type = AST::Builtin.bool_type
109
+ end
110
+
111
+ if super_type.is_a?(AST::Types::Logic::Base)
112
+ super_type = AST::Builtin.bool_type
113
+ end
114
+
115
+ if super_type && !super_type.is_a?(AST::Types::Top)
116
+ type = eliminate_variable(super_type, to: AST::Types::Top.instance)
117
+ supers << type
118
+ skips << type if skip
119
+ end
120
+
121
+ if sub_type && !sub_type.is_a?(AST::Types::Bot)
122
+ type = eliminate_variable(sub_type, to: AST::Types::Bot.instance)
123
+ subs << type
124
+ skips << type if skip
125
+ end
126
+
127
+ super_fvs = supers.each_with_object(Set.new) do |type, fvs|
128
+ fvs.merge(type.free_variables)
129
+ end
130
+ sub_fvs = subs.each_with_object(Set.new) do |type, fvs|
131
+ fvs.merge(type.free_variables)
132
+ end
133
+
134
+ unless super_fvs.disjoint?(unknowns) || sub_fvs.disjoint?(unknowns)
135
+ raise UnsatisfiedInvariantError.new(
136
+ reason: UnsatisfiedInvariantError::UnknownsFreeVariableNotDisjoint.new(
137
+ var: var,
138
+ lower_bound: sub_type,
139
+ upper_bound: super_type
140
+ ),
141
+ constraints: self
142
+ )
143
+ end
144
+ end
145
+
146
+ def eliminate_variable(type, to:)
147
+ case type
148
+ when AST::Types::Name::Instance, AST::Types::Name::Alias, AST::Types::Name::Interface
149
+ type.args.map do |ty|
150
+ eliminate_variable(ty, to: AST::Types::Any.instance)
151
+ end.yield_self do |args|
152
+ type.class.new(name: type.name, args: args)
153
+ end
154
+ when AST::Types::Union
155
+ type.types.map do |ty|
156
+ eliminate_variable(ty, to: AST::Types::Any.instance)
157
+ end.yield_self do |types|
158
+ AST::Types::Union.build(types: types)
159
+ end
160
+ when AST::Types::Intersection
161
+ type.types.map do |ty|
162
+ eliminate_variable(ty, to: AST::Types::Any.instance)
163
+ end.yield_self do |types|
164
+ AST::Types::Intersection.build(types: types)
165
+ end
166
+ when AST::Types::Var
167
+ if vars.member?(type.name)
168
+ to
169
+ else
170
+ type
171
+ end
172
+ when AST::Types::Tuple
173
+ AST::Types::Tuple.new(
174
+ types: type.types.map {|ty| eliminate_variable(ty, to: AST::Builtin.any_type) }
175
+ )
176
+ when AST::Types::Record
177
+ type.map_type { eliminate_variable(_1, to: AST::Builtin.any_type) }
178
+ when AST::Types::Proc
179
+ type.map_type {|ty| eliminate_variable(ty, to: AST::Builtin.any_type) }
180
+ else
181
+ type
182
+ end
183
+ end
184
+
185
+ def unknown?(var)
186
+ dictionary.key?(var)
187
+ end
188
+
189
+ def unknowns
190
+ Set.new(dictionary.keys)
191
+ end
192
+
193
+ def unknown!(var)
194
+ unless unknown?(var)
195
+ dictionary[var] = [Set.new, Set.new, Set.new]
196
+ end
197
+ end
198
+
199
+ def empty?
200
+ dictionary.keys.empty?
201
+ end
202
+
203
+ def upper_bound(var, skip: false)
204
+ if skip
205
+ upper_bound = upper_bound_types(var)
206
+ else
207
+ _, upper_bound, _ = dictionary.fetch(var)
208
+ end
209
+
210
+ case upper_bound.size
211
+ when 0
212
+ AST::Types::Top.instance
213
+ when 1
214
+ upper_bound.first || raise
215
+ else
216
+ AST::Types::Intersection.build(types: upper_bound.to_a)
217
+ end
218
+ end
219
+
220
+ def lower_bound(var, skip: false)
221
+ lower_bound = lower_bound_types(var)
222
+
223
+ case lower_bound.size
224
+ when 0
225
+ AST::Types::Bot.instance
226
+ when 1
227
+ lower_bound.first || raise
228
+ else
229
+ AST::Types::Union.build(types: lower_bound.to_a)
230
+ end
231
+ end
232
+
233
+ Context = _ = Struct.new(:variance, :self_type, :instance_type, :class_type, keyword_init: true)
234
+
235
+ def solution(checker, variance: nil, variables:, self_type: nil, instance_type: nil, class_type: nil, context: nil)
236
+ if context
237
+ raise if variance
238
+ raise if self_type
239
+ raise if instance_type
240
+ raise if class_type
241
+
242
+ variance = context.variance
243
+ self_type = context.self_type
244
+ instance_type = context.instance_type
245
+ class_type = context.class_type
246
+ end
247
+
248
+ vars = [] #: Array[Symbol]
249
+ types = [] #: Array[AST::Types::t]
250
+
251
+ dictionary.each_key do |var|
252
+ if variables.include?(var)
253
+ if has_constraint?(var)
254
+ relation = Relation.new(
255
+ sub_type: lower_bound(var, skip: false),
256
+ super_type: upper_bound(var, skip: false)
257
+ )
258
+
259
+ checker.check(relation, self_type: self_type, instance_type: instance_type, class_type: class_type, constraints: self.class.empty).yield_self do |result|
260
+ if result.success?
261
+ vars << var
262
+
263
+ upper_bound = upper_bound(var, skip: true)
264
+ lower_bound = lower_bound(var, skip: true)
265
+
266
+ type =
267
+ case
268
+ when variance.contravariant?(var)
269
+ upper_bound
270
+ when variance.covariant?(var)
271
+ lower_bound
272
+ else
273
+ if lower_bound.level.join > upper_bound.level.join
274
+ upper_bound
275
+ else
276
+ lower_bound
277
+ end
278
+ end
279
+
280
+ types << type
281
+ else
282
+ raise UnsatisfiableConstraint.new(
283
+ var: var,
284
+ sub_type: result.relation.sub_type,
285
+ super_type: result.relation.super_type,
286
+ result: result
287
+ )
288
+ end
289
+ end
290
+ else
291
+ vars << var
292
+ types << AST::Types::Any.instance
293
+ end
294
+ end
295
+ end
296
+
297
+ Interface::Substitution.build(vars, types)
298
+ end
299
+
300
+ def has_constraint?(var)
301
+ !upper_bound_types(var).empty? || !lower_bound_types(var).empty?
302
+ end
303
+
304
+ def each
305
+ if block_given?
306
+ dictionary.each_key do |var|
307
+ yield [var, lower_bound(var), upper_bound(var)]
308
+ end
309
+ else
310
+ enum_for :each
311
+ end
312
+ end
313
+
314
+ def to_s
315
+ strings = each.map do |var, lower_bound, upper_bound|
316
+ "#{lower_bound} <: #{var} <: #{upper_bound}"
317
+ end
318
+
319
+ "#{unknowns.to_a.join(",")}/#{vars.to_a.join(",")} |- { #{strings.join(", ")} }"
320
+ end
321
+
322
+ def lower_bound_types(var_name)
323
+ lower, _, _ = dictionary.fetch(var_name)
324
+ lower
325
+ end
326
+
327
+ def upper_bound_types(var_name)
328
+ _, upper, skips = dictionary.fetch(var_name)
329
+
330
+ case
331
+ when upper.empty?
332
+ skips
333
+ when skips.empty?
334
+ upper
335
+ else
336
+ upper - skips
337
+ end
338
+ end
339
+ end
340
+ end
341
+ end
@@ -0,0 +1,101 @@
1
+ module Steep
2
+ module Subtyping
3
+ class Relation
4
+ attr_reader :sub_type
5
+ attr_reader :super_type
6
+
7
+ def initialize(sub_type:, super_type:)
8
+ @sub_type = sub_type
9
+ @super_type = super_type
10
+ end
11
+
12
+ def hash
13
+ self.class.hash ^ sub_type.hash ^ super_type.hash
14
+ end
15
+
16
+ def ==(other)
17
+ other.is_a?(self.class) && other.sub_type == sub_type && other.super_type == super_type
18
+ end
19
+
20
+ alias eql? ==
21
+
22
+ def to_s
23
+ "#{sub_type} <: #{super_type}"
24
+ end
25
+
26
+ def to_ary
27
+ [sub_type, super_type]
28
+ end
29
+
30
+ def type?
31
+ !interface? && !method? && !function? && !params? && !block?
32
+ end
33
+
34
+ def interface?
35
+ sub_type.is_a?(Interface::Shape) && super_type.is_a?(Interface::Shape)
36
+ end
37
+
38
+ def method?
39
+ (sub_type.is_a?(Interface::Shape::Entry) || sub_type.is_a?(Interface::MethodType)) &&
40
+ (super_type.is_a?(Interface::Shape::Entry) || super_type.is_a?(Interface::MethodType))
41
+ end
42
+
43
+ def function?
44
+ sub_type.is_a?(Interface::Function) && super_type.is_a?(Interface::Function)
45
+ end
46
+
47
+ def params?
48
+ sub_type.is_a?(Interface::Function::Params) && super_type.is_a?(Interface::Function::Params)
49
+ end
50
+
51
+ def block?
52
+ (sub_type.is_a?(Interface::Block) || !sub_type) &&
53
+ (!super_type || super_type.is_a?(Interface::Block))
54
+ end
55
+
56
+ def assert_type(type)
57
+ unless __send__(:"#{type}?")
58
+ raise "#{type}? is expected but: sub_type=#{sub_type.class}, super_type=#{super_type.class}"
59
+ end
60
+ end
61
+
62
+ def type!
63
+ assert_type(:type)
64
+ end
65
+
66
+ def interface!
67
+ assert_type(:interface)
68
+ end
69
+
70
+ def method!
71
+ assert_type(:method)
72
+ end
73
+
74
+ def function!
75
+ assert_type(:function)
76
+ end
77
+
78
+ def params!
79
+ assert_type(:params)
80
+ end
81
+
82
+ def block!
83
+ assert_type(:block)
84
+ end
85
+
86
+ def map
87
+ self.class.new(
88
+ sub_type: yield(sub_type),
89
+ super_type: yield(super_type)
90
+ )
91
+ end
92
+
93
+ def flip
94
+ self.class.new(
95
+ sub_type: super_type,
96
+ super_type: sub_type
97
+ )
98
+ end
99
+ end
100
+ end
101
+ end