steep 0.50.0 → 0.51.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,33 +2,51 @@ module Steep
2
2
  module TypeInference
3
3
  class ConstantEnv
4
4
  attr_reader :context
5
- attr_reader :cache
6
5
  attr_reader :factory
7
- attr_reader :table
6
+ attr_reader :resolver
8
7
 
9
8
  # ConstantEnv receives an TypeName as a context, not a Namespace, because this is a simulation of Ruby.
10
9
  # Any namespace is a module or class.
11
- def initialize(factory:, context:)
10
+ def initialize(factory:, context:, resolver:)
12
11
  @cache = {}
13
12
  @factory = factory
14
13
  @context = context
15
- @table = RBS::ConstantTable.new(builder: factory.definition_builder)
14
+ @resolver = resolver
16
15
  end
17
16
 
18
- def lookup_constant(name)
19
- table.resolve_constant_reference(name, context: context)
17
+ def resolve(name)
18
+ decompose_constant(
19
+ resolver.resolve(name, context: context)
20
+ )
20
21
  end
21
22
 
22
- def lookup(name)
23
- cache[name] ||= begin
24
- constant = lookup_constant(name)
23
+ def toplevel(name)
24
+ decompose_constant(
25
+ resolver.table.toplevel[name]
26
+ )
27
+ end
28
+
29
+ def constants
30
+ resolver.constants(context).transform_values {|c| decompose_constant(c) }
31
+ end
32
+
33
+ def resolve_child(module_name, constant_name)
34
+ decompose_constant(
35
+ resolver.resolve_child(module_name, constant_name)
36
+ )
37
+ end
38
+
39
+ def children(module_name)
40
+ resolver.children(module_name).transform_values {|c| decompose_constant(c) }
41
+ end
25
42
 
26
- if constant
27
- factory.type(constant.type)
28
- end
29
- rescue => exn
30
- Steep.logger.debug "Looking up a constant failed: name=#{name}, context=[#{context.join(", ")}], error=#{exn.inspect}"
31
- nil
43
+ def decompose_constant(constant)
44
+ if constant
45
+ [
46
+ factory.type(constant.type),
47
+ constant.name,
48
+ constant.entry
49
+ ]
32
50
  end
33
51
  end
34
52
  end
@@ -48,26 +48,23 @@ module Steep
48
48
  attr_reader :defined_module_methods
49
49
  attr_reader :const_env
50
50
  attr_reader :implement_name
51
- attr_reader :namespaces
52
- attr_reader :current_namespace
53
51
  attr_reader :class_name
54
52
  attr_reader :instance_definition
55
53
  attr_reader :module_definition
56
54
 
57
- def initialize(instance_type:, module_type:, implement_name:, current_namespace:, const_env:, class_name:, instance_definition: nil, module_definition: nil)
55
+ def initialize(instance_type:, module_type:, implement_name:, const_env:, class_name:, instance_definition: nil, module_definition: nil)
58
56
  @instance_type = instance_type
59
57
  @module_type = module_type
60
58
  @defined_instance_methods = Set.new
61
59
  @defined_module_methods = Set.new
62
60
  @implement_name = implement_name
63
- @current_namespace = current_namespace
64
61
  @const_env = const_env
65
62
  @class_name = class_name
66
63
  @instance_definition = instance_definition
67
64
  @module_definition = module_definition
68
65
  end
69
66
 
70
- def const_context
67
+ def nesting
71
68
  const_env.context
72
69
  end
73
70
 
@@ -83,7 +80,6 @@ module Steep
83
80
  instance_type: self.instance_type,
84
81
  module_type: self.module_type,
85
82
  implement_name: self.implement_name,
86
- current_namespace: self.current_namespace,
87
83
  const_env: self.const_env,
88
84
  class_name: self.class_name,
89
85
  instance_definition: self.instance_definition,
@@ -93,7 +89,6 @@ module Steep
93
89
  instance_type: instance_type,
94
90
  module_type: module_type,
95
91
  implement_name: implement_name,
96
- current_namespace: current_namespace,
97
92
  const_env: const_env,
98
93
  class_name: class_name,
99
94
  instance_definition: instance_definition,
@@ -175,6 +170,10 @@ module Steep
175
170
  variable_context: variable_context
176
171
  )
177
172
  end
173
+
174
+ def env
175
+ type_env.subtyping.factory.env
176
+ end
178
177
  end
179
178
  end
180
179
  end
@@ -44,18 +44,6 @@ module Steep
44
44
  merge!(original_env: env.gvar_types, override_env: gvar_types, self_type: self_type, instance_type: instance_type, class_type: class_type, &block)
45
45
 
46
46
  const_types.each do |name, annotated_type|
47
- original_type = self.const_types[name] || const_env.lookup(name)
48
- if original_type
49
- assert_annotation(
50
- name,
51
- original_type: original_type,
52
- annotated_type: annotated_type,
53
- self_type: self_type,
54
- instance_type: instance_type,
55
- class_type: class_type,
56
- &block
57
- )
58
- end
59
47
  env.const_types[name] = annotated_type
60
48
  end
61
49
  end
@@ -70,14 +58,8 @@ module Steep
70
58
  if const_types.key?(const)
71
59
  const_types[const]
72
60
  else
73
- const_env.lookup(const).yield_self do |type|
74
- if type
75
- type
76
- else
77
- yield
78
- AST::Types::Any.new
79
- end
80
- end
61
+ yield
62
+ AST::Types::Any.new
81
63
  end
82
64
  else
83
65
  lookup_dictionary(ivar: ivar, gvar: gvar) do |var_name, dictionary|
@@ -105,40 +87,20 @@ module Steep
105
87
  # @type method assign: (const: TypeName, type: AST::Type) { (Subtyping::Result::Failure | nil) -> void } -> AST::Type
106
88
  # | (gvar: Symbol, type: AST::Type) { (Subtyping::Result::Failure | nil) -> void } -> AST::Type
107
89
  # | (ivar: Symbol, type: AST::Type) { (Subtyping::Result::Failure | nil) -> void } -> AST::Type
108
- def assign(const: nil, gvar: nil, ivar: nil, type:, self_type:, instance_type:, class_type:, &block)
109
- case
110
- when const
111
- yield_self do
112
- const_type = const_types[const] || const_env.lookup(const)
113
- if const_type
114
- assert_assign(
115
- var_type: const_type,
116
- lhs_type: type,
117
- self_type: self_type,
118
- instance_type: instance_type,
119
- class_type: class_type,
120
- &block
121
- )
122
- else
123
- yield nil
124
- AST::Types::Any.new
125
- end
126
- end
127
- else
128
- lookup_dictionary(ivar: ivar, gvar: gvar) do |var_name, dictionary|
129
- if dictionary.key?(var_name)
130
- assert_assign(
131
- var_type: dictionary[var_name],
132
- lhs_type: type,
133
- self_type: self_type,
134
- instance_type: instance_type,
135
- class_type: class_type,
136
- &block
137
- )
138
- else
139
- yield nil
140
- AST::Types::Any.new
141
- end
90
+ def assign(gvar: nil, ivar: nil, type:, self_type:, instance_type:, class_type:, &block)
91
+ lookup_dictionary(ivar: ivar, gvar: gvar) do |var_name, dictionary|
92
+ if dictionary.key?(var_name)
93
+ assert_assign(
94
+ var_type: dictionary[var_name],
95
+ lhs_type: type,
96
+ self_type: self_type,
97
+ instance_type: instance_type,
98
+ class_type: class_type,
99
+ &block
100
+ )
101
+ else
102
+ yield nil
103
+ AST::Types::Any.new
142
104
  end
143
105
  end
144
106
  end
data/lib/steep/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Steep
2
- VERSION = "0.50.0"
2
+ VERSION = "0.51.0"
3
3
  end
@@ -24,8 +24,18 @@
24
24
  line: 6
25
25
  character: 5
26
26
  severity: ERROR
27
- message: Cannot detect the type of the expression
28
- code: Ruby::FallbackAny
27
+ message: 'Cannot find the declaration of constant: `B`'
28
+ code: Ruby::UnknownConstant
29
+ - range:
30
+ start:
31
+ line: 8
32
+ character: 7
33
+ end:
34
+ line: 8
35
+ character: 8
36
+ severity: ERROR
37
+ message: 'Cannot find the declaration of module: `X`'
38
+ code: Ruby::UnknownConstant
29
39
  - range:
30
40
  start:
31
41
  line: 14
@@ -49,8 +59,8 @@
49
59
  line: 16
50
60
  character: 9
51
61
  severity: ERROR
52
- message: Cannot detect the type of the expression
53
- code: Ruby::FallbackAny
62
+ message: 'Cannot find the declaration of constant: `B`'
63
+ code: Ruby::UnknownConstant
54
64
  - range:
55
65
  start:
56
66
  line: 23
@@ -69,23 +79,18 @@
69
79
  - range:
70
80
  start:
71
81
  line: 26
72
- character: 4
73
- end:
74
- line: 26
75
- character: 10
76
- severity: ERROR
77
- message: Cannot detect the type of the expression
78
- code: Ruby::FallbackAny
79
- - range:
80
- start:
81
- line: 26
82
- character: 4
82
+ character: 0
83
83
  end:
84
84
  line: 26
85
85
  character: 15
86
86
  severity: ERROR
87
- message: Cannot detect the type of the expression
88
- code: Ruby::FallbackAny
87
+ message: |-
88
+ Cannot assign a value of type `::Integer` to a variable of type `::String`
89
+ ::Integer <: ::String
90
+ ::Numeric <: ::String
91
+ ::Object <: ::String
92
+ ::BasicObject <: ::String
93
+ code: Ruby::IncompatibleAssignment
89
94
  - range:
90
95
  start:
91
96
  line: 27
@@ -125,5 +130,5 @@
125
130
  line: 5
126
131
  character: 8
127
132
  severity: ERROR
128
- message: Cannot find the declaration of constant `Baz2`
129
- code: Ruby::UnknownConstantAssigned
133
+ message: 'Cannot find the declaration of constant: `Baz2`'
134
+ code: Ruby::UnknownConstant
@@ -65,6 +65,16 @@
65
65
  code: Ruby::BreakTypeMismatch
66
66
  - file: different_method_parameter_kind.rb
67
67
  diagnostics:
68
+ - range:
69
+ start:
70
+ line: 1
71
+ character: 6
72
+ end:
73
+ line: 1
74
+ character: 34
75
+ severity: ERROR
76
+ message: 'Cannot find the declaration of class: `DifferentMethodParameterKind`'
77
+ code: Ruby::UnknownConstant
68
78
  - range:
69
79
  start:
70
80
  line: 3
@@ -207,6 +217,16 @@
207
217
  code: Ruby::IncompatibleAssignment
208
218
  - file: method_arity_mismatch.rb
209
219
  diagnostics:
220
+ - range:
221
+ start:
222
+ line: 1
223
+ character: 6
224
+ end:
225
+ line: 1
226
+ character: 25
227
+ severity: ERROR
228
+ message: 'Cannot find the declaration of class: `MethodArityMismatch`'
229
+ code: Ruby::UnknownConstant
210
230
  - range:
211
231
  start:
212
232
  line: 3
@@ -230,6 +250,16 @@
230
250
  code: Ruby::MethodArityMismatch
231
251
  - file: method_body_type_mismatch.rb
232
252
  diagnostics:
253
+ - range:
254
+ start:
255
+ line: 1
256
+ character: 6
257
+ end:
258
+ line: 1
259
+ character: 28
260
+ severity: ERROR
261
+ message: 'Cannot find the declaration of class: `MethodBodyTypeMismatch`'
262
+ code: Ruby::UnknownConstant
233
263
  - range:
234
264
  start:
235
265
  line: 3
@@ -258,6 +288,16 @@
258
288
  code: Ruby::MethodDefinitionMissing
259
289
  - file: method_parameter_mismatch.rb
260
290
  diagnostics:
291
+ - range:
292
+ start:
293
+ line: 1
294
+ character: 6
295
+ end:
296
+ line: 1
297
+ character: 29
298
+ severity: ERROR
299
+ message: 'Cannot find the declaration of class: `MethodParameterMismatch`'
300
+ code: Ruby::UnknownConstant
261
301
  - range:
262
302
  start:
263
303
  line: 3
@@ -293,6 +333,16 @@
293
333
  code: Ruby::MethodParameterMismatch
294
334
  - file: method_return_type_annotation_mismatch.rb
295
335
  diagnostics:
336
+ - range:
337
+ start:
338
+ line: 1
339
+ character: 6
340
+ end:
341
+ line: 1
342
+ character: 40
343
+ severity: ERROR
344
+ message: 'Cannot find the declaration of class: `MethodReturnTypeAnnotationMismatch`'
345
+ code: Ruby::UnknownConstant
296
346
  - range:
297
347
  start:
298
348
  line: 3
@@ -358,6 +408,16 @@
358
408
  code: Ruby::RequiredBlockMissing
359
409
  - file: return_type_mismatch.rb
360
410
  diagnostics:
411
+ - range:
412
+ start:
413
+ line: 1
414
+ character: 6
415
+ end:
416
+ line: 1
417
+ character: 24
418
+ severity: ERROR
419
+ message: 'Cannot find the declaration of class: `ReturnTypeMismatch`'
420
+ code: Ruby::UnknownConstant
361
421
  - range:
362
422
  start:
363
423
  line: 4
@@ -445,6 +505,16 @@
445
505
  code: Ruby::UnexpectedKeywordArgument
446
506
  - file: unexpected_yield.rb
447
507
  diagnostics:
508
+ - range:
509
+ start:
510
+ line: 1
511
+ character: 6
512
+ end:
513
+ line: 1
514
+ character: 21
515
+ severity: ERROR
516
+ message: 'Cannot find the declaration of class: `UnexpectedYield`'
517
+ code: Ruby::UnknownConstant
448
518
  - range:
449
519
  start:
450
520
  line: 4
@@ -475,8 +545,8 @@
475
545
  line: 2
476
546
  character: 5
477
547
  severity: ERROR
478
- message: Cannot find the declaration of constant `FOO`
479
- code: Ruby::UnknownConstantAssigned
548
+ message: 'Cannot find the declaration of constant: `FOO`'
549
+ code: Ruby::UnknownConstant
480
550
  - range:
481
551
  start:
482
552
  line: 4
@@ -485,28 +555,28 @@
485
555
  line: 4
486
556
  character: 5
487
557
  severity: ERROR
488
- message: Cannot detect the type of the expression
489
- code: Ruby::FallbackAny
558
+ message: 'Cannot find the declaration of constant: `FOO`'
559
+ code: Ruby::UnknownConstant
490
560
  - range:
491
561
  start:
492
562
  line: 4
493
- character: 2
563
+ character: 7
494
564
  end:
495
565
  line: 4
496
566
  character: 10
497
567
  severity: ERROR
498
- message: Cannot find the declaration of constant `FOO::BAR`
499
- code: Ruby::UnknownConstantAssigned
568
+ message: 'Cannot find the declaration of constant: `BAR`'
569
+ code: Ruby::UnknownConstant
500
570
  - range:
501
571
  start:
502
572
  line: 6
503
- character: 2
573
+ character: 4
504
574
  end:
505
575
  line: 6
506
576
  character: 7
507
577
  severity: ERROR
508
- message: Cannot find the declaration of constant `::FOO`
509
- code: Ruby::UnknownConstantAssigned
578
+ message: 'Cannot find the declaration of constant: `FOO`'
579
+ code: Ruby::UnknownConstant
510
580
  - file: unresolved_overloading.rb
511
581
  diagnostics:
512
582
  - range:
@@ -21,6 +21,16 @@
21
21
  severity: ERROR
22
22
  message: Type `::Integer` does not have method `foo`
23
23
  code: Ruby::NoMethod
24
+ - range:
25
+ start:
26
+ line: 9
27
+ character: 6
28
+ end:
29
+ line: 9
30
+ character: 15
31
+ severity: ERROR
32
+ message: 'Cannot find the declaration of class: `WithToInt`'
33
+ code: Ruby::UnknownConstant
24
34
  - range:
25
35
  start:
26
36
  line: 12
@@ -29,8 +39,8 @@
29
39
  line: 12
30
40
  character: 29
31
41
  severity: ERROR
32
- message: Cannot detect the type of the expression
33
- code: Ruby::FallbackAny
42
+ message: 'Cannot find the declaration of constant: `WithToInt`'
43
+ code: Ruby::UnknownConstant
34
44
  - range:
35
45
  start:
36
46
  line: 13
@@ -41,6 +51,16 @@
41
51
  severity: ERROR
42
52
  message: Type `::Integer` does not have method `foo`
43
53
  code: Ruby::NoMethod
54
+ - range:
55
+ start:
56
+ line: 15
57
+ character: 6
58
+ end:
59
+ line: 15
60
+ character: 13
61
+ severity: ERROR
62
+ message: 'Cannot find the declaration of class: `WithToI`'
63
+ code: Ruby::UnknownConstant
44
64
  - range:
45
65
  start:
46
66
  line: 18
@@ -49,8 +69,8 @@
49
69
  line: 18
50
70
  character: 27
51
71
  severity: ERROR
52
- message: Cannot detect the type of the expression
53
- code: Ruby::FallbackAny
72
+ message: 'Cannot find the declaration of constant: `WithToI`'
73
+ code: Ruby::UnknownConstant
54
74
  - range:
55
75
  start:
56
76
  line: 19
@@ -74,6 +74,16 @@
74
74
  code: Ruby::IncompatibleAssignment
75
75
  - file: b.rb
76
76
  diagnostics:
77
+ - range:
78
+ start:
79
+ line: 1
80
+ character: 6
81
+ end:
82
+ line: 1
83
+ character: 7
84
+ severity: ERROR
85
+ message: 'Cannot find the declaration of class: `A`'
86
+ code: Ruby::UnknownConstant
77
87
  - range:
78
88
  start:
79
89
  line: 4
@@ -89,3 +99,23 @@
89
99
  ::Object <: ::Integer
90
100
  ::BasicObject <: ::Integer
91
101
  code: Ruby::MethodBodyTypeMismatch
102
+ - range:
103
+ start:
104
+ line: 9
105
+ character: 6
106
+ end:
107
+ line: 9
108
+ character: 7
109
+ severity: ERROR
110
+ message: 'Cannot find the declaration of class: `B`'
111
+ code: Ruby::UnknownConstant
112
+ - range:
113
+ start:
114
+ line: 18
115
+ character: 6
116
+ end:
117
+ line: 18
118
+ character: 7
119
+ severity: ERROR
120
+ message: 'Cannot find the declaration of class: `C`'
121
+ code: Ruby::UnknownConstant
@@ -1,4 +1,28 @@
1
1
  ---
2
+ - file: array.rb
3
+ diagnostics:
4
+ - range:
5
+ start:
6
+ line: 1
7
+ character: 6
8
+ end:
9
+ line: 1
10
+ character: 9
11
+ severity: ERROR
12
+ message: 'Cannot find the declaration of class: `Foo`'
13
+ code: Ruby::UnknownConstant
14
+ - file: hash.rb
15
+ diagnostics:
16
+ - range:
17
+ start:
18
+ line: 1
19
+ character: 6
20
+ end:
21
+ line: 1
22
+ character: 9
23
+ severity: ERROR
24
+ message: 'Cannot find the declaration of class: `Foo`'
25
+ code: Ruby::UnknownConstant
2
26
  - file: issue_332.rb
3
27
  diagnostics:
4
28
  - range:
@@ -1,6 +1,16 @@
1
1
  ---
2
2
  - file: a.rb
3
3
  diagnostics:
4
+ - range:
5
+ start:
6
+ line: 1
7
+ character: 6
8
+ end:
9
+ line: 1
10
+ character: 7
11
+ severity: ERROR
12
+ message: 'Cannot find the declaration of class: `A`'
13
+ code: Ruby::UnknownConstant
4
14
  - range:
5
15
  start:
6
16
  line: 6
@@ -52,6 +62,16 @@
52
62
  code: Ruby::UnexpectedYield
53
63
  - file: b.rb
54
64
  diagnostics:
65
+ - range:
66
+ start:
67
+ line: 1
68
+ character: 6
69
+ end:
70
+ line: 1
71
+ character: 9
72
+ severity: ERROR
73
+ message: 'Cannot find the declaration of class: `Foo`'
74
+ code: Ruby::UnknownConstant
55
75
  - range:
56
76
  start:
57
77
  line: 4
data/steep.gemspec CHANGED
@@ -33,7 +33,7 @@ Gem::Specification.new do |spec|
33
33
  spec.add_runtime_dependency "rainbow", ">= 2.2.2", "< 4.0"
34
34
  spec.add_runtime_dependency "listen", "~> 3.0"
35
35
  spec.add_runtime_dependency "language_server-protocol", ">= 3.15", "< 4.0"
36
- spec.add_runtime_dependency "rbs", ">= 2.2.0"
36
+ spec.add_runtime_dependency "rbs", ">= 2.3.0"
37
37
  spec.add_runtime_dependency "parallel", ">= 1.0.0"
38
38
  spec.add_runtime_dependency "terminal-table", ">= 2", "< 4"
39
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.50.0
4
+ version: 0.51.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-22 00:00:00.000000000 Z
11
+ date: 2022-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -98,14 +98,14 @@ dependencies:
98
98
  requirements:
99
99
  - - ">="
100
100
  - !ruby/object:Gem::Version
101
- version: 2.2.0
101
+ version: 2.3.0
102
102
  type: :runtime
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - ">="
107
107
  - !ruby/object:Gem::Version
108
- version: 2.2.0
108
+ version: 2.3.0
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: parallel
111
111
  requirement: !ruby/object:Gem::Requirement
@@ -193,6 +193,7 @@ files:
193
193
  - lib/steep/ast/types/var.rb
194
194
  - lib/steep/ast/types/void.rb
195
195
  - lib/steep/cli.rb
196
+ - lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb
196
197
  - lib/steep/diagnostic/helper.rb
197
198
  - lib/steep/diagnostic/lsp_formatter.rb
198
199
  - lib/steep/diagnostic/ruby.rb