steep 1.0.0 → 1.1.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby-windows.yml +34 -0
  3. data/.github/workflows/ruby.yml +7 -2
  4. data/.gitignore +1 -0
  5. data/CHANGELOG.md +59 -0
  6. data/Gemfile +7 -4
  7. data/Gemfile.lock +17 -21
  8. data/Gemfile.steep +3 -0
  9. data/Gemfile.steep.lock +49 -0
  10. data/Rakefile +5 -0
  11. data/Steepfile +6 -1
  12. data/bin/setup +2 -0
  13. data/bin/steep +19 -0
  14. data/lib/steep/ast/builtin.rb +2 -2
  15. data/lib/steep/ast/types/factory.rb +7 -3
  16. data/lib/steep/ast/types/proc.rb +2 -0
  17. data/lib/steep/cli.rb +3 -1
  18. data/lib/steep/diagnostic/ruby.rb +50 -4
  19. data/lib/steep/diagnostic/signature.rb +18 -0
  20. data/lib/steep/drivers/check.rb +3 -3
  21. data/lib/steep/drivers/watch.rb +3 -1
  22. data/lib/steep/method_name.rb +9 -3
  23. data/lib/steep/node_helper.rb +49 -0
  24. data/lib/steep/path_helper.rb +22 -0
  25. data/lib/steep/project.rb +3 -15
  26. data/lib/steep/server/base_worker.rb +1 -0
  27. data/lib/steep/server/change_buffer.rb +1 -1
  28. data/lib/steep/server/interaction_worker.rb +3 -5
  29. data/lib/steep/server/master.rb +61 -45
  30. data/lib/steep/server/type_check_worker.rb +10 -25
  31. data/lib/steep/services/completion_provider.rb +25 -18
  32. data/lib/steep/services/goto_service.rb +2 -4
  33. data/lib/steep/services/hover_provider/rbs.rb +1 -1
  34. data/lib/steep/services/hover_provider/ruby.rb +30 -12
  35. data/lib/steep/services/stats_calculator.rb +0 -1
  36. data/lib/steep/services/type_check_service.rb +15 -12
  37. data/lib/steep/shims/symbol_start_with.rb +18 -0
  38. data/lib/steep/signature/validator.rb +25 -1
  39. data/lib/steep/source.rb +1 -1
  40. data/lib/steep/subtyping/check.rb +0 -3
  41. data/lib/steep/subtyping/constraints.rb +43 -14
  42. data/lib/steep/type_construction.rb +721 -764
  43. data/lib/steep/type_inference/constant_env.rb +0 -2
  44. data/lib/steep/type_inference/context.rb +23 -17
  45. data/lib/steep/type_inference/logic_type_interpreter.rb +210 -117
  46. data/lib/steep/type_inference/method_call.rb +80 -6
  47. data/lib/steep/type_inference/multiple_assignment.rb +189 -0
  48. data/lib/steep/type_inference/send_args.rb +1 -2
  49. data/lib/steep/type_inference/type_env.rb +273 -116
  50. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  51. data/lib/steep/typing.rb +2 -0
  52. data/lib/steep/version.rb +1 -1
  53. data/lib/steep.rb +7 -5
  54. data/rbs_collection.steep.lock.yaml +112 -0
  55. data/rbs_collection.steep.yaml +19 -0
  56. data/sample/sig/conference.rbs +8 -0
  57. data/sig/shims/parser/source/map.rbs +146 -0
  58. data/sig/shims/parser/source/range.rbs +237 -0
  59. data/sig/shims/parser.rbs +17 -0
  60. data/sig/steep/ast/annotation/collection.rbs +75 -0
  61. data/sig/steep/ast/annotation.rbs +126 -0
  62. data/sig/steep/ast/builtin.rbs +69 -0
  63. data/sig/steep/ast/type_params.rbs +11 -0
  64. data/sig/steep/ast/types/any.rbs +29 -0
  65. data/sig/steep/ast/types/boolean.rbs +31 -0
  66. data/sig/steep/ast/types/bot.rbs +29 -0
  67. data/sig/steep/ast/types/class.rbs +29 -0
  68. data/sig/steep/ast/types/factory.rbs +76 -0
  69. data/sig/steep/ast/types/helper.rbs +19 -0
  70. data/sig/steep/ast/types/instance.rbs +29 -0
  71. data/sig/steep/ast/types/intersection.rbs +35 -0
  72. data/sig/steep/ast/types/literal.rbs +33 -0
  73. data/sig/steep/ast/types/logic.rbs +78 -0
  74. data/sig/steep/ast/types/name.rbs +71 -0
  75. data/sig/steep/ast/types/nil.rbs +31 -0
  76. data/sig/steep/ast/types/proc.rbs +46 -0
  77. data/sig/steep/ast/types/record.rbs +38 -0
  78. data/sig/steep/ast/types/self.rbs +29 -0
  79. data/sig/steep/ast/types/top.rbs +29 -0
  80. data/sig/steep/ast/types/tuple.rbs +34 -0
  81. data/sig/steep/ast/types/union.rbs +38 -0
  82. data/sig/steep/ast/types/var.rbs +37 -0
  83. data/sig/steep/ast/types/void.rbs +29 -0
  84. data/sig/steep/ast/types.rbs +37 -0
  85. data/sig/steep/diagnostic/deprecated/unknown_constant_assigned.rbs +15 -0
  86. data/sig/steep/diagnostic/helper.rbs +9 -0
  87. data/sig/steep/diagnostic/lsp_formatter.rbs +29 -0
  88. data/sig/steep/diagnostic/ruby.rbs +494 -0
  89. data/sig/steep/diagnostic/signature.rbs +215 -0
  90. data/sig/steep/interface/block.rbs +35 -0
  91. data/sig/steep/interface/function.rbs +253 -0
  92. data/sig/steep/interface/interface.rbs +23 -0
  93. data/sig/steep/interface/method_type.rbs +55 -0
  94. data/sig/steep/interface/substitution.rbs +53 -0
  95. data/sig/steep/interface/type_param.rbs +35 -0
  96. data/sig/steep/method_name.rbs +26 -0
  97. data/sig/steep/module_helper.rbs +7 -0
  98. data/sig/steep/node_helper.rbs +11 -0
  99. data/sig/steep/project/dsl.rbs +94 -0
  100. data/sig/steep/project/options.rbs +15 -0
  101. data/sig/steep/project/pattern.rbs +25 -0
  102. data/sig/steep/project/target.rbs +25 -0
  103. data/sig/steep/project.rbs +19 -0
  104. data/sig/steep/services/completion_provider.rbs +123 -0
  105. data/sig/steep/services/content_change.rbs +35 -0
  106. data/sig/steep/services/file_loader.rbs +13 -0
  107. data/sig/steep/services/goto_service.rbs +45 -0
  108. data/sig/steep/services/hover_provider/rbs.rbs +21 -0
  109. data/sig/steep/services/hover_provider/ruby.rbs +109 -0
  110. data/sig/steep/services/hover_provider/singleton_methods.rbs +11 -0
  111. data/sig/steep/services/path_assignment.rbs +21 -0
  112. data/sig/steep/services/signature_service.rbs +91 -0
  113. data/sig/steep/services/stats_calculator.rbs +17 -0
  114. data/sig/steep/services/type_check_service.rbs +93 -0
  115. data/sig/steep/source.rbs +55 -0
  116. data/sig/steep/subtyping/cache.rbs +17 -0
  117. data/sig/steep/subtyping/check.rbs +93 -0
  118. data/sig/steep/subtyping/constraints.rbs +111 -0
  119. data/sig/steep/subtyping/relation.rbs +51 -0
  120. data/sig/steep/subtyping/result.rbs +157 -0
  121. data/sig/steep/subtyping/variable_variance.rbs +23 -0
  122. data/sig/steep/type_construction.rbs +285 -0
  123. data/sig/steep/type_inference/block_params.rbs +52 -0
  124. data/sig/steep/type_inference/constant_env.rbs +27 -0
  125. data/sig/steep/type_inference/context.rbs +137 -0
  126. data/sig/steep/type_inference/logic_type_interpreter.rbs +72 -0
  127. data/sig/steep/type_inference/method_call.rbs +124 -0
  128. data/sig/steep/type_inference/method_params.rbs +104 -0
  129. data/sig/steep/type_inference/multiple_assignment.rbs +76 -0
  130. data/sig/steep/type_inference/type_env.rbs +158 -0
  131. data/sig/steep/type_inference/type_env_builder.rbs +77 -0
  132. data/sig/steep/typing.rbs +68 -0
  133. data/sig/steep.rbs +31 -0
  134. data/smoke/class/f.rb +1 -0
  135. data/smoke/class/test_expectations.yml +2 -2
  136. data/smoke/diagnostics/test_expectations.yml +4 -2
  137. data/smoke/regression/lambda.rb +3 -0
  138. data/smoke/regression/test_expectations.yml +12 -0
  139. data/steep.gemspec +1 -1
  140. metadata +95 -9
  141. data/lib/steep/subtyping/variable_occurrence.rb +0 -51
  142. data/lib/steep/type_inference/local_variable_type_env.rb +0 -249
  143. data/lib/steep/type_inference/logic.rb +0 -161
@@ -335,25 +335,29 @@ module Steep
335
335
  SourceFile.with_syntax_error(path: path, content: text, error: error)
336
336
  rescue EncodingError => exn
337
337
  SourceFile.no_data(path: path, content: "")
338
+ rescue RuntimeError => exn
339
+ Steep.log_error(exn)
340
+ SourceFile.no_data(path: path, content: text)
338
341
  end
339
342
 
340
343
  def self.type_check(source:, subtyping:)
341
344
  annotations = source.annotations(block: source.node, factory: subtyping.factory, context: nil)
345
+
346
+ definition = subtyping.factory.definition_builder.build_instance(AST::Builtin::Object.module_name)
347
+
342
348
  const_env = TypeInference::ConstantEnv.new(
343
349
  factory: subtyping.factory,
344
350
  context: nil,
345
351
  resolver: RBS::Resolver::ConstantResolver.new(builder: subtyping.factory.definition_builder)
346
352
  )
347
- type_env = TypeInference::TypeEnv.build(annotations: annotations,
348
- subtyping: subtyping,
349
- const_env: const_env,
350
- signatures: subtyping.factory.env)
351
- lvar_env = TypeInference::LocalVariableTypeEnv.empty(
352
- subtyping: subtyping,
353
- self_type: AST::Builtin::Object.instance_type,
354
- instance_type: AST::Builtin::Object.instance_type,
355
- class_type: AST::Builtin::Object.module_type
356
- ).annotate(annotations)
353
+ type_env = TypeInference::TypeEnv.new(const_env)
354
+ type_env = TypeInference::TypeEnvBuilder.new(
355
+ TypeInference::TypeEnvBuilder::Command::ImportConstantAnnotations.new(annotations),
356
+ TypeInference::TypeEnvBuilder::Command::ImportGlobalDeclarations.new(subtyping.factory),
357
+ TypeInference::TypeEnvBuilder::Command::ImportInstanceVariableDefinition.new(definition, subtyping.factory),
358
+ TypeInference::TypeEnvBuilder::Command::ImportInstanceVariableAnnotations.new(annotations),
359
+ TypeInference::TypeEnvBuilder::Command::ImportLocalVariableAnnotations.new(annotations)
360
+ ).build(type_env)
357
361
 
358
362
  context = TypeInference::Context.new(
359
363
  block_context: nil,
@@ -361,7 +365,7 @@ module Steep
361
365
  instance_type: AST::Builtin::Object.instance_type,
362
366
  module_type: AST::Builtin::Object.module_type,
363
367
  implement_name: nil,
364
- const_env: const_env,
368
+ nesting: nil,
365
369
  class_name: AST::Builtin::Object.module_name,
366
370
  instance_definition: subtyping.factory.definition_builder.build_instance(AST::Builtin::Object.module_name),
367
371
  module_definition: subtyping.factory.definition_builder.build_singleton(AST::Builtin::Object.module_name)
@@ -370,7 +374,6 @@ module Steep
370
374
  break_context: nil,
371
375
  self_type: AST::Builtin::Object.instance_type,
372
376
  type_env: type_env,
373
- lvar_env: lvar_env,
374
377
  call_context: TypeInference::MethodCall::TopLevelContext.new,
375
378
  variable_context: TypeInference::Context::TypeVariableContext.empty
376
379
  )
@@ -0,0 +1,18 @@
1
+ # Steep runs on Ruby 2.6 and it needs a shim of `Symbol#start_with?`
2
+
3
+ module Shims
4
+ module SymbolStartWith
5
+ def start_with?(*args)
6
+ to_s.start_with?(*args)
7
+ end
8
+
9
+ def end_with?(*args)
10
+ to_s.end_with?(*args)
11
+ end
12
+ end
13
+
14
+ unless Symbol.method_defined?(:start_with?)
15
+ Symbol.include(SymbolStartWith)
16
+ end
17
+ end
18
+
@@ -58,9 +58,14 @@ module Steep
58
58
 
59
59
  def validate_type_application_constraints(type_name, type_params, type_args, location:)
60
60
  if type_params.size == type_args.size
61
+ subst = Interface::Substitution.build(
62
+ type_params.map(&:name),
63
+ type_args.map {|type| factory.type(type) }
64
+ )
65
+
61
66
  type_params.zip(type_args).each do |param, arg|
62
67
  if param.upper_bound
63
- upper_bound_type = factory.type(param.upper_bound)
68
+ upper_bound_type = factory.type(param.upper_bound).subst(subst)
64
69
  arg_type = factory.type(arg)
65
70
 
66
71
  constraints = Subtyping::Constraints.empty
@@ -319,6 +324,25 @@ module Steep
319
324
  end
320
325
  end
321
326
 
327
+ definition.class_variables.each do |name, var|
328
+ if var.declared_in == definition.type_name
329
+ if (parent = var.parent_variable) && var.declared_in != parent.declared_in
330
+ class_var = definition.entry.decls.flat_map {|decl| decl.decl.members }.find do |member|
331
+ member.is_a?(RBS::AST::Members::ClassVariable) && member.name == name
332
+ end
333
+
334
+ if class_var
335
+ @errors << Diagnostic::Signature::ClassVariableDuplicationError.new(
336
+ class_name: definition.type_name,
337
+ other_class_name: parent.declared_in,
338
+ variable_name: name,
339
+ location: class_var.location[:name]
340
+ )
341
+ end
342
+ end
343
+ end
344
+ end
345
+
322
346
  ancestors = builder.ancestor_builder.one_singleton_ancestors(name)
323
347
  mixin_constraints(definition, ancestors.extended_modules, immediate_self_types: ancestors.self_types).each do |relation, ancestor|
324
348
  checker.check(
data/lib/steep/source.rb CHANGED
@@ -39,7 +39,7 @@ module Steep
39
39
  end
40
40
 
41
41
  def self.new_parser
42
- ::Parser::Ruby30.new(Builder.new).tap do |parser|
42
+ ::Parser::Ruby31.new(Builder.new).tap do |parser|
43
43
  parser.diagnostics.all_errors_are_fatal = true
44
44
  parser.diagnostics.ignore_warnings = true
45
45
  end
@@ -421,9 +421,6 @@ module Steep
421
421
  when relation.sub_type.is_a?(AST::Types::Proc) && relation.super_type.is_a?(AST::Types::Proc)
422
422
  name = :__proc__
423
423
 
424
- sub_type = relation.sub_type
425
- super_type = relation.super_type
426
-
427
424
  All(relation) do |result|
428
425
  result.add(relation.map {|p| p.type }) do |rel|
429
426
  check_function(name, rel)
@@ -124,10 +124,10 @@ module Steep
124
124
  skips << type if skip
125
125
  end
126
126
 
127
- super_fvs = supers.each.with_object(Set.new) do |type, fvs|
127
+ super_fvs = supers.each_with_object(Set.new) do |type, fvs|
128
128
  fvs.merge(type.free_variables)
129
129
  end
130
- sub_fvs = subs.each.with_object(Set.new) do |type, fvs|
130
+ sub_fvs = subs.each_with_object(Set.new) do |type, fvs|
131
131
  fvs.merge(type.free_variables)
132
132
  end
133
133
 
@@ -169,6 +169,18 @@ module Steep
169
169
  else
170
170
  type
171
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
+ location: type.location
176
+ )
177
+ when AST::Types::Record
178
+ AST::Types::Record.new(
179
+ elements: type.elements.transform_values {|ty| eliminate_variable(ty, to: AST::Builtin.any_type) },
180
+ location: type.location
181
+ )
182
+ when AST::Types::Proc
183
+ type.map_type {|ty| eliminate_variable(ty, to: AST::Builtin.any_type) }
172
184
  else
173
185
  type
174
186
  end
@@ -193,34 +205,36 @@ module Steep
193
205
  end
194
206
 
195
207
  def upper_bound(var, skip: false)
196
- _, upper_bound, skips = dictionary[var]
197
- upper_bound -= skips if skip
208
+ if skip
209
+ upper_bound = upper_bound_types(var)
210
+ else
211
+ _, upper_bound, _ = dictionary[var]
212
+ end
198
213
 
199
214
  case upper_bound.size
200
215
  when 0
201
216
  AST::Types::Top.new
202
217
  when 1
203
- upper_bound.first
218
+ upper_bound.first || raise
204
219
  else
205
220
  AST::Types::Intersection.build(types: upper_bound.to_a)
206
221
  end
207
222
  end
208
223
 
209
224
  def lower_bound(var, skip: false)
210
- lower_bound, _, skips = dictionary[var]
211
- lower_bound -= skips if skip
225
+ lower_bound = lower_bound_types(var)
212
226
 
213
227
  case lower_bound.size
214
228
  when 0
215
229
  AST::Types::Bot.new
216
230
  when 1
217
- lower_bound.first
231
+ lower_bound.first || raise
218
232
  else
219
233
  AST::Types::Union.build(types: lower_bound.to_a)
220
234
  end
221
235
  end
222
236
 
223
- Context = Struct.new(:variance, :self_type, :instance_type, :class_type, keyword_init: true)
237
+ Context = _ = Struct.new(:variance, :self_type, :instance_type, :class_type, keyword_init: true)
224
238
 
225
239
  def solution(checker, variance: nil, variables:, self_type: nil, instance_type: nil, class_type: nil, context: nil)
226
240
  if context
@@ -288,16 +302,13 @@ module Steep
288
302
  end
289
303
 
290
304
  def has_constraint?(var)
291
- lower, upper, skips = dictionary[var]
292
- lower -= skips
293
- upper -= skips
294
- !lower.empty? || !upper.empty?
305
+ !upper_bound_types(var).empty? || !lower_bound_types(var).empty?
295
306
  end
296
307
 
297
308
  def each
298
309
  if block_given?
299
310
  dictionary.each_key do |var|
300
- yield var, lower_bound(var), upper_bound(var)
311
+ yield [var, lower_bound(var), upper_bound(var)]
301
312
  end
302
313
  else
303
314
  enum_for :each
@@ -311,6 +322,24 @@ module Steep
311
322
 
312
323
  "#{unknowns.to_a.join(",")}/#{vars.to_a.join(",")} |- { #{strings.join(", ")} }"
313
324
  end
325
+
326
+ def lower_bound_types(var_name)
327
+ lower, _, _ = dictionary[var_name]
328
+ lower
329
+ end
330
+
331
+ def upper_bound_types(var_name)
332
+ _, upper, skips = dictionary[var_name]
333
+
334
+ case
335
+ when upper.empty?
336
+ skips
337
+ when skips.empty?
338
+ upper
339
+ else
340
+ upper - skips
341
+ end
342
+ end
314
343
  end
315
344
  end
316
345
  end