steep 0.50.0 → 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/Gemfile +1 -0
- data/Gemfile.lock +8 -5
- data/lib/steep/ast/annotation/collection.rb +10 -8
- data/lib/steep/ast/types/factory.rb +5 -5
- data/lib/steep/cli.rb +8 -0
- data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
- data/lib/steep/diagnostic/ruby.rb +21 -15
- data/lib/steep/index/source_index.rb +18 -1
- data/lib/steep/module_helper.rb +4 -7
- data/lib/steep/server/interaction_worker.rb +45 -2
- data/lib/steep/server/master.rb +1 -1
- data/lib/steep/services/completion_provider.rb +109 -1
- data/lib/steep/services/goto_service.rb +4 -6
- data/lib/steep/services/hover_content.rb +52 -4
- data/lib/steep/services/type_check_service.rb +6 -3
- data/lib/steep/source.rb +19 -13
- data/lib/steep/type_construction.rb +166 -148
- data/lib/steep/type_inference/constant_env.rb +33 -15
- data/lib/steep/type_inference/context.rb +6 -7
- data/lib/steep/type_inference/type_env.rb +16 -54
- data/lib/steep/version.rb +1 -1
- data/smoke/const/test_expectations.yml +24 -19
- data/smoke/diagnostics/test_expectations.yml +80 -10
- data/smoke/integer/test_expectations.yml +24 -4
- data/smoke/method/test_expectations.yml +30 -0
- data/smoke/regression/test_expectations.yml +24 -0
- data/smoke/yield/test_expectations.yml +20 -0
- data/steep.gemspec +1 -1
- metadata +5 -4
@@ -5,16 +5,42 @@ module Steep
|
|
5
5
|
VariableContent = Struct.new(:node, :name, :type, :location, keyword_init: true)
|
6
6
|
MethodCallContent = Struct.new(:node, :method_name, :type, :definition, :location, keyword_init: true)
|
7
7
|
DefinitionContent = Struct.new(:node, :method_name, :method_type, :definition, :location, keyword_init: true) do
|
8
|
-
TypeAliasContent = Struct.new(:location, :decl, keyword_init: true)
|
9
|
-
ClassContent = Struct.new(:location, :decl, keyword_init: true)
|
10
|
-
InterfaceContent = Struct.new(:location, :decl, keyword_init: true)
|
11
|
-
|
12
8
|
def comment_string
|
13
9
|
if comments = definition&.comments
|
14
10
|
comments.map {|c| c.string.chomp }.uniq.join("\n----\n")
|
15
11
|
end
|
16
12
|
end
|
17
13
|
end
|
14
|
+
ConstantContent = Struct.new(:location, :full_name, :type, :decl, keyword_init: true) do
|
15
|
+
def comment_string
|
16
|
+
if class_or_module?
|
17
|
+
comments = decl.decls.filter_map {|d| d.decl.comment&.string }
|
18
|
+
unless comments.empty?
|
19
|
+
return comments.join("\n----\n")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
if constant?
|
24
|
+
if comment = decl.decl.comment
|
25
|
+
return comment.string
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def constant?
|
33
|
+
decl.is_a?(RBS::Environment::SingleEntry)
|
34
|
+
end
|
35
|
+
|
36
|
+
def class_or_module?
|
37
|
+
decl.is_a?(RBS::Environment::MultiEntry)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
TypeAliasContent = Struct.new(:location, :decl, keyword_init: true)
|
42
|
+
ClassContent = Struct.new(:location, :decl, keyword_init: true)
|
43
|
+
InterfaceContent = Struct.new(:location, :decl, keyword_init: true)
|
18
44
|
|
19
45
|
InstanceMethodName = Struct.new(:class_name, :method_name)
|
20
46
|
SingletonMethodName = Struct.new(:class_name, :method_name)
|
@@ -190,6 +216,28 @@ module Steep
|
|
190
216
|
location: node.loc.expression
|
191
217
|
)
|
192
218
|
end
|
219
|
+
when :const, :casgn
|
220
|
+
context = typing.context_at(line: line, column: column)
|
221
|
+
|
222
|
+
type = typing.type_of(node: node)
|
223
|
+
const_name = typing.source_index.reference(constant_node: node)
|
224
|
+
|
225
|
+
if const_name
|
226
|
+
decl = context.env.class_decls[const_name] || context.env.constant_decls[const_name]
|
227
|
+
|
228
|
+
ConstantContent.new(
|
229
|
+
location: node.location.name,
|
230
|
+
full_name: const_name,
|
231
|
+
type: type,
|
232
|
+
decl: decl
|
233
|
+
)
|
234
|
+
else
|
235
|
+
TypeContent.new(
|
236
|
+
node: node,
|
237
|
+
type: type,
|
238
|
+
location: node.location.expression
|
239
|
+
)
|
240
|
+
end
|
193
241
|
else
|
194
242
|
type = typing.type_of(node: node)
|
195
243
|
|
@@ -338,8 +338,12 @@ module Steep
|
|
338
338
|
end
|
339
339
|
|
340
340
|
def self.type_check(source:, subtyping:)
|
341
|
-
annotations = source.annotations(block: source.node, factory: subtyping.factory,
|
342
|
-
const_env = TypeInference::ConstantEnv.new(
|
341
|
+
annotations = source.annotations(block: source.node, factory: subtyping.factory, context: nil)
|
342
|
+
const_env = TypeInference::ConstantEnv.new(
|
343
|
+
factory: subtyping.factory,
|
344
|
+
context: nil,
|
345
|
+
resolver: RBS::Resolver::ConstantResolver.new(builder: subtyping.factory.definition_builder)
|
346
|
+
)
|
343
347
|
type_env = TypeInference::TypeEnv.build(annotations: annotations,
|
344
348
|
subtyping: subtyping,
|
345
349
|
const_env: const_env,
|
@@ -357,7 +361,6 @@ module Steep
|
|
357
361
|
instance_type: AST::Builtin::Object.instance_type,
|
358
362
|
module_type: AST::Builtin::Object.module_type,
|
359
363
|
implement_name: nil,
|
360
|
-
current_namespace: RBS::Namespace.root,
|
361
364
|
const_env: const_env,
|
362
365
|
class_name: AST::Builtin::Object.module_name,
|
363
366
|
instance_definition: subtyping.factory.definition_builder.build_instance(AST::Builtin::Object.module_name),
|
data/lib/steep/source.rb
CHANGED
@@ -283,11 +283,11 @@ module Steep
|
|
283
283
|
node.updated(nil, children)
|
284
284
|
end
|
285
285
|
|
286
|
-
def annotations(block:, factory:,
|
286
|
+
def annotations(block:, factory:, context:)
|
287
287
|
AST::Annotation::Collection.new(
|
288
288
|
annotations: (mapping[block] || []).map(&:annotation),
|
289
289
|
factory: factory,
|
290
|
-
|
290
|
+
context: context
|
291
291
|
)
|
292
292
|
end
|
293
293
|
|
@@ -302,6 +302,8 @@ module Steep
|
|
302
302
|
end
|
303
303
|
|
304
304
|
def find_nodes(line:, column:, node: self.node, position: nil, parents: [])
|
305
|
+
return [] unless node
|
306
|
+
|
305
307
|
position ||= (line-1).times.sum do |i|
|
306
308
|
node.location.expression.source_buffer.source_line(i+1).size + 1
|
307
309
|
end + column
|
@@ -345,22 +347,26 @@ module Steep
|
|
345
347
|
end
|
346
348
|
|
347
349
|
def without_unrelated_defs(line:, column:)
|
348
|
-
|
349
|
-
|
350
|
+
if node
|
351
|
+
nodes = find_nodes(line: line, column: column) || []
|
352
|
+
defs = Set[].compare_by_identity.merge(nodes.select {|node| node.type == :def || node.type == :defs })
|
350
353
|
|
351
|
-
|
354
|
+
node_ = Source.delete_defs(node, defs)
|
352
355
|
|
353
|
-
|
356
|
+
mapping = {}.compare_by_identity
|
354
357
|
|
355
|
-
|
356
|
-
|
358
|
+
annotations = self.mapping.values.flatten
|
359
|
+
Source.construct_mapping(node: node_, annotations: annotations, mapping: mapping)
|
357
360
|
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
361
|
+
annotations.each do |annot|
|
362
|
+
mapping[node] ||= []
|
363
|
+
mapping[node] << annot
|
364
|
+
end
|
362
365
|
|
363
|
-
|
366
|
+
Source.new(path: path, node: node_, mapping: mapping)
|
367
|
+
else
|
368
|
+
self
|
369
|
+
end
|
364
370
|
end
|
365
371
|
|
366
372
|
def compact_siblings(node)
|
@@ -130,9 +130,8 @@ module Steep
|
|
130
130
|
end
|
131
131
|
|
132
132
|
def for_new_method(method_name, node, args:, self_type:, definition:)
|
133
|
-
annots = source.annotations(block: node, factory: checker.factory,
|
134
|
-
type_env = TypeInference::TypeEnv.new(subtyping: checker,
|
135
|
-
const_env: module_context&.const_env || self.type_env.const_env)
|
133
|
+
annots = source.annotations(block: node, factory: checker.factory, context: nesting)
|
134
|
+
type_env = TypeInference::TypeEnv.new(subtyping: checker, const_env: module_context&.const_env || self.type_env.const_env)
|
136
135
|
|
137
136
|
self.type_env.const_types.each do |name, type|
|
138
137
|
type_env.set(const: name, type: type)
|
@@ -285,17 +284,9 @@ module Steep
|
|
285
284
|
end
|
286
285
|
end
|
287
286
|
else
|
288
|
-
name =
|
289
|
-
name ||= absolute_name(module_name).yield_self do |absolute_name|
|
290
|
-
absolute_name if checker.factory.class_name?(absolute_name) || checker.factory.module_name?(absolute_name)
|
291
|
-
end
|
292
|
-
name ||= super_name && absolute_name(super_name).yield_self do |absolute_name|
|
293
|
-
absolute_name if checker.factory.class_name?(absolute_name) || checker.factory.module_name?(absolute_name)
|
294
|
-
end
|
287
|
+
name = module_name || super_name
|
295
288
|
|
296
|
-
if name
|
297
|
-
absolute_name_ = name
|
298
|
-
entry = checker.factory.env.class_decls[absolute_name_]
|
289
|
+
if name && entry = checker.factory.env.class_decls[name]
|
299
290
|
AST::Annotation::Implements::Module.new(
|
300
291
|
name: name,
|
301
292
|
args: entry.type_params.each.map(&:name)
|
@@ -304,9 +295,9 @@ module Steep
|
|
304
295
|
end
|
305
296
|
end
|
306
297
|
|
307
|
-
def default_module_context(implement_module_name, const_env
|
298
|
+
def default_module_context(implement_module_name, const_env:)
|
308
299
|
if implement_module_name
|
309
|
-
module_name = checker.factory.absolute_type_name(implement_module_name.name,
|
300
|
+
module_name = checker.factory.absolute_type_name(implement_module_name.name, context: const_env.context)
|
310
301
|
module_args = implement_module_name.args.map {|name| AST::Types::Var.new(name: name) }
|
311
302
|
|
312
303
|
instance_def = checker.factory.definition_builder.build_instance(module_name)
|
@@ -319,7 +310,6 @@ module Steep
|
|
319
310
|
instance_type: instance_type,
|
320
311
|
module_type: module_type,
|
321
312
|
implement_name: implement_module_name,
|
322
|
-
current_namespace: current_namespace,
|
323
313
|
const_env: const_env,
|
324
314
|
class_name: module_name,
|
325
315
|
instance_definition: instance_def,
|
@@ -330,7 +320,6 @@ module Steep
|
|
330
320
|
instance_type: nil,
|
331
321
|
module_type: nil,
|
332
322
|
implement_name: nil,
|
333
|
-
current_namespace: current_namespace,
|
334
323
|
const_env: self.module_context.const_env,
|
335
324
|
class_name: self.module_context.class_name,
|
336
325
|
module_definition: nil,
|
@@ -339,17 +328,19 @@ module Steep
|
|
339
328
|
end
|
340
329
|
end
|
341
330
|
|
342
|
-
def for_module(node)
|
343
|
-
|
344
|
-
new_namespace = nested_namespace_for_module(new_module_name)
|
331
|
+
def for_module(node, new_module_name)
|
332
|
+
new_nesting = [nesting, new_module_name || false]
|
345
333
|
|
346
|
-
|
347
|
-
|
334
|
+
module_const_env = TypeInference::ConstantEnv.new(
|
335
|
+
factory: checker.factory,
|
336
|
+
context: new_nesting,
|
337
|
+
resolver: self.module_context.const_env.resolver
|
338
|
+
)
|
348
339
|
|
349
|
-
annots = source.annotations(block: node, factory: checker.factory,
|
340
|
+
annots = source.annotations(block: node, factory: checker.factory, context: new_nesting)
|
350
341
|
|
351
342
|
implement_module_name = implement_module(module_name: new_module_name, annotations: annots)
|
352
|
-
module_context = default_module_context(implement_module_name, const_env: module_const_env
|
343
|
+
module_context = default_module_context(implement_module_name, const_env: module_const_env)
|
353
344
|
|
354
345
|
unless implement_module_name
|
355
346
|
module_context = module_context.update(
|
@@ -446,25 +437,25 @@ module Steep
|
|
446
437
|
)
|
447
438
|
end
|
448
439
|
|
449
|
-
def with_module_constr(node)
|
450
|
-
constr = for_module(node)
|
440
|
+
def with_module_constr(node, module_name)
|
441
|
+
constr = for_module(node, module_name)
|
451
442
|
constr.checker.push_variable_bounds(constr.variable_context.upper_bounds) do
|
452
443
|
yield constr
|
453
444
|
end
|
454
445
|
end
|
455
446
|
|
456
|
-
def for_class(node)
|
457
|
-
|
458
|
-
|
459
|
-
new_namespace = nested_namespace_for_module(new_class_name)
|
447
|
+
def for_class(node, new_class_name, super_class_name)
|
448
|
+
new_nesting = [nesting, new_class_name || false]
|
449
|
+
annots = source.annotations(block: node, factory: checker.factory, context: new_nesting)
|
460
450
|
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
451
|
+
class_const_env = TypeInference::ConstantEnv.new(
|
452
|
+
factory: checker.factory,
|
453
|
+
context: new_nesting,
|
454
|
+
resolver: self.module_context.const_env.resolver
|
455
|
+
)
|
465
456
|
|
466
457
|
implement_module_name = implement_module(module_name: new_class_name, super_name: super_class_name, annotations: annots)
|
467
|
-
module_context = default_module_context(implement_module_name, const_env: class_const_env
|
458
|
+
module_context = default_module_context(implement_module_name, const_env: class_const_env)
|
468
459
|
|
469
460
|
if implement_module_name
|
470
461
|
if super_class_name && implement_module_name.name == absolute_name(super_class_name)
|
@@ -534,8 +525,8 @@ module Steep
|
|
534
525
|
)
|
535
526
|
end
|
536
527
|
|
537
|
-
def with_class_constr(node)
|
538
|
-
constr = for_class(node)
|
528
|
+
def with_class_constr(node, new_class_name, super_class_name)
|
529
|
+
constr = for_class(node, new_class_name, super_class_name)
|
539
530
|
|
540
531
|
constr.checker.push_variable_bounds(constr.variable_context.upper_bounds) do
|
541
532
|
yield constr
|
@@ -553,7 +544,7 @@ module Steep
|
|
553
544
|
end
|
554
545
|
|
555
546
|
def for_sclass(node, type)
|
556
|
-
annots = source.annotations(block: node, factory: checker.factory,
|
547
|
+
annots = source.annotations(block: node, factory: checker.factory, context: nesting)
|
557
548
|
|
558
549
|
instance_type = if type.is_a?(AST::Types::Self)
|
559
550
|
context.self_type
|
@@ -601,7 +592,6 @@ module Steep
|
|
601
592
|
instance_type: annots.instance_type || instance_type,
|
602
593
|
module_type: annots.self_type || annots.module_type || module_type,
|
603
594
|
implement_name: nil,
|
604
|
-
current_namespace: current_namespace,
|
605
595
|
const_env: self.module_context.const_env,
|
606
596
|
class_name: self.module_context.class_name,
|
607
597
|
module_definition: module_definition,
|
@@ -642,7 +632,7 @@ module Steep
|
|
642
632
|
end
|
643
633
|
|
644
634
|
def for_branch(node, truthy_vars: Set.new, type_case_override: nil, break_context: context.break_context)
|
645
|
-
annots = source.annotations(block: node, factory: checker.factory,
|
635
|
+
annots = source.annotations(block: node, factory: checker.factory, context: nesting)
|
646
636
|
|
647
637
|
lvar_env = context.lvar_env
|
648
638
|
|
@@ -1497,33 +1487,37 @@ module Steep
|
|
1497
1487
|
yield_self do
|
1498
1488
|
constr = self
|
1499
1489
|
|
1500
|
-
|
1501
|
-
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1490
|
+
name_node, super_node, _ = node.children
|
1491
|
+
_, constr, class_name = synthesize_constant(name_node, name_node.children[0], name_node.children[1]) do
|
1492
|
+
typing.add_error(
|
1493
|
+
Diagnostic::Ruby::UnknownConstant.new(node: name_node, name: name_node.children[1]).class!
|
1494
|
+
)
|
1495
|
+
end
|
1496
|
+
if class_name
|
1497
|
+
typing.source_index.add_definition(constant: class_name, definition: name_node)
|
1498
|
+
end
|
1499
|
+
|
1500
|
+
if super_node
|
1501
|
+
_, constr, super_name = constr.synthesize_constant(super_node, super_node.children[0], super_node.children[1]) do
|
1502
|
+
typing.add_error(
|
1503
|
+
Diagnostic::Ruby::UnknownConstant.new(node: super_node, name: super_node.children[1]).class!
|
1504
|
+
)
|
1505
|
+
end
|
1506
|
+
if super_name
|
1507
|
+
typing.source_index.add_reference(constant: super_name, ref: super_node)
|
1505
1508
|
end
|
1506
|
-
else
|
1507
|
-
_, constr = constr.synthesize(name)
|
1508
1509
|
end
|
1509
|
-
_, constr = constr.synthesize(sup) if sup
|
1510
1510
|
|
1511
|
-
with_class_constr(node) do |constructor|
|
1511
|
+
with_class_constr(node, class_name, super_name) do |constructor|
|
1512
1512
|
if module_type = constructor.module_context&.module_type
|
1513
1513
|
_, constructor = constructor.add_typing(name, type: module_type)
|
1514
1514
|
else
|
1515
1515
|
_, constructor = constructor.fallback_to_any(name)
|
1516
1516
|
end
|
1517
1517
|
|
1518
|
-
constructor.typing.source_index.add_definition(
|
1519
|
-
constant: constructor.module_context.class_name,
|
1520
|
-
definition: node
|
1521
|
-
)
|
1522
|
-
|
1523
1518
|
constructor.typing.add_context_for_node(node, context: constructor.context)
|
1524
1519
|
constructor.typing.add_context_for_body(node, context: constructor.context)
|
1525
1520
|
|
1526
|
-
constructor.synthesize(node.children[1]) if node.children[1]
|
1527
1521
|
constructor.synthesize(node.children[2]) if node.children[2]
|
1528
1522
|
|
1529
1523
|
if constructor.module_context&.implement_name && !namespace_module?(node)
|
@@ -1538,28 +1532,22 @@ module Steep
|
|
1538
1532
|
yield_self do
|
1539
1533
|
constr = self
|
1540
1534
|
|
1541
|
-
|
1542
|
-
|
1543
|
-
|
1544
|
-
|
1545
|
-
|
1546
|
-
|
1547
|
-
|
1548
|
-
_, constr = constr.synthesize(name)
|
1535
|
+
name_node, _ = node.children
|
1536
|
+
_, constr, module_name = synthesize_constant(name_node, name_node.children[0], name_node.children[1]) do
|
1537
|
+
typing.add_error Diagnostic::Ruby::UnknownConstant.new(node: name_node, name: name_node.children[1]).module!
|
1538
|
+
end
|
1539
|
+
|
1540
|
+
if module_name
|
1541
|
+
constr.typing.source_index.add_definition(constant: module_name, definition: name_node)
|
1549
1542
|
end
|
1550
1543
|
|
1551
|
-
with_module_constr(node) do |constructor|
|
1544
|
+
with_module_constr(node, module_name) do |constructor|
|
1552
1545
|
if module_type = constructor.module_context&.module_type
|
1553
1546
|
_, constructor = constructor.add_typing(name, type: module_type)
|
1554
1547
|
else
|
1555
1548
|
_, constructor = constructor.fallback_to_any(name)
|
1556
1549
|
end
|
1557
1550
|
|
1558
|
-
constructor.typing.source_index.add_definition(
|
1559
|
-
constant: constructor.module_context.class_name,
|
1560
|
-
definition: node
|
1561
|
-
)
|
1562
|
-
|
1563
1551
|
constructor.typing.add_context_for_node(node, context: constructor.context)
|
1564
1552
|
constructor.typing.add_context_for_body(node, context: constructor.context)
|
1565
1553
|
|
@@ -1611,75 +1599,47 @@ module Steep
|
|
1611
1599
|
add_typing node, type: AST::Types::Void.new
|
1612
1600
|
|
1613
1601
|
when :const
|
1614
|
-
|
1615
|
-
|
1616
|
-
_, constr = synthesize(parent)
|
1617
|
-
else
|
1618
|
-
constr = self
|
1619
|
-
end
|
1620
|
-
|
1621
|
-
const_name = constr.module_name_from_node(node)
|
1602
|
+
yield_self do
|
1603
|
+
type, constr, name = synthesize_constant(node, node.children[0], node.children[1])
|
1622
1604
|
|
1623
|
-
|
1624
|
-
|
1625
|
-
typing.source_index.add_reference(constant: constant.name, ref: node)
|
1605
|
+
if name
|
1606
|
+
typing.source_index.add_reference(constant: name, ref: node)
|
1626
1607
|
end
|
1627
1608
|
|
1628
|
-
type
|
1629
|
-
constr.fallback_to_any(node)
|
1630
|
-
end
|
1631
|
-
constr.add_typing(node, type: type)
|
1632
|
-
else
|
1633
|
-
constr.fallback_to_any(node)
|
1609
|
+
Pair.new(type: type, constr: constr)
|
1634
1610
|
end
|
1635
1611
|
|
1636
1612
|
when :casgn
|
1637
1613
|
yield_self do
|
1638
|
-
constr =
|
1614
|
+
constant_type, constr, constant_name = synthesize_constant(nil, node.children[0], node.children[1]) do
|
1615
|
+
typing.add_error(
|
1616
|
+
Diagnostic::Ruby::UnknownConstant.new(
|
1617
|
+
node: node,
|
1618
|
+
name: node.children[1]
|
1619
|
+
)
|
1620
|
+
)
|
1621
|
+
end
|
1639
1622
|
|
1640
|
-
|
1641
|
-
|
1642
|
-
|
1623
|
+
if constant_name
|
1624
|
+
typing.source_index.add_definition(constant: constant_name, definition: node)
|
1625
|
+
end
|
1643
1626
|
|
1644
|
-
|
1645
|
-
if constant = module_context.const_env.lookup_constant(const_name)
|
1646
|
-
typing.source_index.add_definition(constant: constant.name, definition: node)
|
1647
|
-
end
|
1627
|
+
value_type, constr = constr.synthesize(node.children.last, hint: constant_type)
|
1648
1628
|
|
1649
|
-
|
1650
|
-
|
1651
|
-
|
1652
|
-
|
1653
|
-
|
1654
|
-
|
1655
|
-
|
1656
|
-
|
1657
|
-
|
1658
|
-
|
1659
|
-
const_type = type_env.get(const: const_name)
|
1660
|
-
typing.add_error(
|
1661
|
-
Diagnostic::Ruby::IncompatibleAssignment.new(
|
1662
|
-
node: node,
|
1663
|
-
lhs_type: const_type,
|
1664
|
-
rhs_type: value_type,
|
1665
|
-
result: error
|
1666
|
-
)
|
1667
|
-
)
|
1668
|
-
else
|
1669
|
-
typing.add_error(
|
1670
|
-
Diagnostic::Ruby::UnknownConstantAssigned.new(
|
1671
|
-
node: node,
|
1672
|
-
name: const_name,
|
1673
|
-
context: module_context
|
1674
|
-
)
|
1675
|
-
)
|
1676
|
-
end
|
1677
|
-
end
|
1629
|
+
result = check_relation(sub_type: value_type, super_type: constant_type)
|
1630
|
+
if result.failure?
|
1631
|
+
typing.add_error(
|
1632
|
+
Diagnostic::Ruby::IncompatibleAssignment.new(
|
1633
|
+
node: node,
|
1634
|
+
lhs_type: constant_type,
|
1635
|
+
rhs_type: value_type,
|
1636
|
+
result: result
|
1637
|
+
)
|
1638
|
+
)
|
1678
1639
|
|
1679
|
-
constr.add_typing(node, type:
|
1640
|
+
constr.add_typing(node, type: constant_type)
|
1680
1641
|
else
|
1681
|
-
|
1682
|
-
constr.fallback_to_any(node)
|
1642
|
+
constr.add_typing(node, type: value_type)
|
1683
1643
|
end
|
1684
1644
|
end
|
1685
1645
|
|
@@ -2724,6 +2684,70 @@ module Steep
|
|
2724
2684
|
end
|
2725
2685
|
end
|
2726
2686
|
|
2687
|
+
def synthesize_constant(node, parent_node, constant_name)
|
2688
|
+
const_name = module_name_from_node(parent_node, constant_name)
|
2689
|
+
|
2690
|
+
if const_name && type = type_env.get(const: const_name) { break }
|
2691
|
+
# const-type annotation wins
|
2692
|
+
if node
|
2693
|
+
constr = synthesize_children(node)
|
2694
|
+
type, constr = constr.add_typing(node, type: type)
|
2695
|
+
[type, constr, nil]
|
2696
|
+
else
|
2697
|
+
[type, self, nil]
|
2698
|
+
end
|
2699
|
+
else
|
2700
|
+
case
|
2701
|
+
when !parent_node
|
2702
|
+
constr = self
|
2703
|
+
|
2704
|
+
if (type, name = module_context.const_env.resolve(constant_name))
|
2705
|
+
if node
|
2706
|
+
_, constr = add_typing(node, type: type)
|
2707
|
+
end
|
2708
|
+
|
2709
|
+
return [type, constr, name]
|
2710
|
+
end
|
2711
|
+
when parent_node.type == :cbase
|
2712
|
+
_, constr = add_typing(parent_node, type: AST::Builtin.nil_type)
|
2713
|
+
|
2714
|
+
if (type, name = constr.module_context.const_env.toplevel(constant_name))
|
2715
|
+
if node
|
2716
|
+
_, constr = constr.add_typing(node, type: type)
|
2717
|
+
end
|
2718
|
+
|
2719
|
+
return [type, constr, name]
|
2720
|
+
end
|
2721
|
+
else
|
2722
|
+
parent_type, constr = synthesize(parent_node)
|
2723
|
+
parent_type = deep_expand_alias(parent_type)
|
2724
|
+
|
2725
|
+
case parent_type
|
2726
|
+
when AST::Types::Name::Singleton
|
2727
|
+
if (type, name = module_context.const_env.resolve_child(parent_type.name, constant_name))
|
2728
|
+
if node
|
2729
|
+
_, constr = add_typing(node, type: type)
|
2730
|
+
end
|
2731
|
+
|
2732
|
+
return [type, constr, name]
|
2733
|
+
end
|
2734
|
+
end
|
2735
|
+
end
|
2736
|
+
|
2737
|
+
if block_given?
|
2738
|
+
yield
|
2739
|
+
else
|
2740
|
+
if node
|
2741
|
+
constr.typing.add_error(
|
2742
|
+
Diagnostic::Ruby::UnknownConstant.new(node: node, name: constant_name)
|
2743
|
+
)
|
2744
|
+
end
|
2745
|
+
end
|
2746
|
+
|
2747
|
+
[AST::Builtin.any_type, constr, nil]
|
2748
|
+
end
|
2749
|
+
end
|
2750
|
+
|
2727
2751
|
def optional_proc?(type)
|
2728
2752
|
if type.is_a?(AST::Types::Union)
|
2729
2753
|
if type.types.size == 2
|
@@ -2737,7 +2761,7 @@ module Steep
|
|
2737
2761
|
end
|
2738
2762
|
|
2739
2763
|
def type_lambda(node, params_node:, body_node:, type_hint:)
|
2740
|
-
block_annotations = source.annotations(block: node, factory: checker.factory,
|
2764
|
+
block_annotations = source.annotations(block: node, factory: checker.factory, context: nesting)
|
2741
2765
|
params = TypeInference::BlockParams.from_node(params_node, annotations: block_annotations)
|
2742
2766
|
|
2743
2767
|
type_hint = deep_expand_alias(type_hint) if type_hint
|
@@ -2894,7 +2918,7 @@ module Steep
|
|
2894
2918
|
|
2895
2919
|
constr = synthesize_children(node, skips: skips)
|
2896
2920
|
if block_params
|
2897
|
-
block_annotations = source.annotations(block: node, factory: checker.factory,
|
2921
|
+
block_annotations = source.annotations(block: node, factory: checker.factory, context: nesting)
|
2898
2922
|
|
2899
2923
|
constr.type_block_without_hint(
|
2900
2924
|
node: node,
|
@@ -2915,7 +2939,7 @@ module Steep
|
|
2915
2939
|
|
2916
2940
|
constr = synthesize_children(node, skips: skips)
|
2917
2941
|
if block_params
|
2918
|
-
block_annotations = source.annotations(block: node, factory: checker.factory,
|
2942
|
+
block_annotations = source.annotations(block: node, factory: checker.factory, context: nesting)
|
2919
2943
|
|
2920
2944
|
constr.type_block_without_hint(
|
2921
2945
|
node: node,
|
@@ -3296,7 +3320,7 @@ module Steep
|
|
3296
3320
|
|
3297
3321
|
if block_params
|
3298
3322
|
# block is given
|
3299
|
-
block_annotations = source.annotations(block: node, factory: checker.factory,
|
3323
|
+
block_annotations = source.annotations(block: node, factory: checker.factory, context: nesting)
|
3300
3324
|
block_params_ = TypeInference::BlockParams.from_node(block_params, annotations: block_annotations)
|
3301
3325
|
|
3302
3326
|
if method_type.block
|
@@ -3638,8 +3662,7 @@ module Steep
|
|
3638
3662
|
if implements = block_annotations.implement_module_annotation
|
3639
3663
|
module_context = default_module_context(
|
3640
3664
|
implements.name,
|
3641
|
-
const_env: self.module_context.const_env
|
3642
|
-
current_namespace: current_namespace
|
3665
|
+
const_env: self.module_context.const_env
|
3643
3666
|
)
|
3644
3667
|
|
3645
3668
|
self_type = module_context.module_type
|
@@ -3693,30 +3716,25 @@ module Steep
|
|
3693
3716
|
end
|
3694
3717
|
end
|
3695
3718
|
|
3696
|
-
def
|
3697
|
-
module_context&.
|
3719
|
+
def nesting
|
3720
|
+
module_context&.nesting
|
3698
3721
|
end
|
3699
3722
|
|
3700
|
-
def
|
3701
|
-
|
3702
|
-
|
3703
|
-
|
3704
|
-
module_name.to_namespace
|
3723
|
+
def absolute_nested_module_name(module_name)
|
3724
|
+
n = nesting
|
3725
|
+
while n && !n[1]
|
3726
|
+
n = n[0]
|
3705
3727
|
end
|
3706
|
-
end
|
3707
3728
|
|
3708
|
-
|
3709
|
-
if current_namespace
|
3710
|
-
module_name.with_prefix(current_namespace)
|
3711
|
-
else
|
3729
|
+
if n
|
3712
3730
|
module_name.absolute!
|
3731
|
+
else
|
3732
|
+
n + module_name
|
3713
3733
|
end
|
3714
3734
|
end
|
3715
3735
|
|
3716
|
-
def
|
3717
|
-
|
3718
|
-
checker.builder.absolute_type(type, current: current_namespace)
|
3719
|
-
end
|
3736
|
+
def absolute_name(name)
|
3737
|
+
checker.factory.absolute_type_name(name, context: nesting)
|
3720
3738
|
end
|
3721
3739
|
|
3722
3740
|
def union_type(*types)
|