steep 0.19.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2c1abcf64958fd547dbb336a0489d9e310e71bc66d208a67c6328c1ac09e62b
4
- data.tar.gz: eecd4d437442a85356525c31d10697d73f06fd319930371a7f2710b77f3e86a3
3
+ metadata.gz: 566e77aa4db03cb06ae80569f4db9f5409e65199b1af4af9fc6f2598e911f694
4
+ data.tar.gz: 41f1d54c5196fdec7cc58751899aec208066d845eef717234bb37266313c7d8d
5
5
  SHA512:
6
- metadata.gz: 57957103491d678aa843f9155a757d9f903968d272e2d117b659bcb46659ada397cf82db1bf4e54e25f77a5548db32b6ea2da7be0fba6920d033340c44448c19
7
- data.tar.gz: 94eeb79af2e1fdab28b5f2c6237e343d9f174d7ff5a65c9d25d3860e042820027c1f4fab8c6571492967c74b37161572b8ab1dea91d8d60d0e7f2c1cbf4c31fe
6
+ metadata.gz: 46366ed11a6328a6a237c52042ef49eb12d5ce3db111fb55c5038109b62aa471ba6634c8c060575f6862658dcc0f388684a083f9a691becd6d90f076acb05908
7
+ data.tar.gz: 9cb15861e6de6b5209dd82745f1c7cf67c212e3e15cc9c1f92956b84089801f994c7c1cd79ff8972f37da229530e7354d237ce4a74ce844083daf656936286dc
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.20.0 (2020-07-17)
6
+
7
+ * Support singleton class definitions ([#166](https://github.com/soutaro/steep/pull/166))
8
+
5
9
  ## 0.19.0 (2020-07-12)
6
10
 
7
11
  * Update RBS. ([#157](https://github.com/soutaro/steep/pull/157))
@@ -230,6 +230,39 @@ module Steep
230
230
  )
231
231
  end
232
232
 
233
+ def implement_module(module_name:, super_name: nil, annotations:)
234
+ if (annotation = annotations.implement_module_annotation)
235
+ absolute_name(annotation.name.name).yield_self do |absolute_name|
236
+ if checker.factory.class_name?(absolute_name) || checker.factory.module_name?(absolute_name)
237
+ AST::Annotation::Implements::Module.new(
238
+ name: absolute_name,
239
+ args: annotation.name.args
240
+ )
241
+ else
242
+ Steep.logger.error "Unknown class name given to @implements: #{annotation.name.name}"
243
+ nil
244
+ end
245
+ end
246
+ else
247
+ name = nil
248
+ name ||= absolute_name(module_name).yield_self do |absolute_name|
249
+ absolute_name if checker.factory.class_name?(absolute_name) || checker.factory.module_name?(absolute_name)
250
+ end
251
+ name ||= super_name && absolute_name(super_name).yield_self do |absolute_name|
252
+ absolute_name if checker.factory.class_name?(absolute_name) || checker.factory.module_name?(absolute_name)
253
+ end
254
+
255
+ if name
256
+ absolute_name_ = checker.factory.type_name_1(name)
257
+ entry = checker.factory.env.class_decls[absolute_name_]
258
+ AST::Annotation::Implements::Module.new(
259
+ name: name,
260
+ args: entry.type_params.each.map(&:name)
261
+ )
262
+ end
263
+ end
264
+ end
265
+
233
266
  def for_module(node)
234
267
  new_module_name = Names::Module.from_node(node.children.first) or raise "Unexpected module name: #{node.children.first}"
235
268
  new_namespace = nested_namespace_for_module(new_module_name)
@@ -240,28 +273,7 @@ module Steep
240
273
  annots = source.annotations(block: node, factory: checker.factory, current_module: new_namespace)
241
274
  module_type = AST::Builtin::Module.instance_type
242
275
 
243
- implement_module_name = yield_self do
244
- if (annotation = annots.implement_module_annotation)
245
- absolute_name(annotation.name.name).yield_self do |absolute_name|
246
- if checker.factory.module_name?(absolute_name)
247
- AST::Annotation::Implements::Module.new(name: absolute_name,
248
- args: annotation.name.args)
249
- else
250
- Steep.logger.error "Unknown module name given to @implements: #{annotation.name.name}"
251
- nil
252
- end
253
- end
254
- else
255
- absolute_name(new_module_name).yield_self do |absolute_name|
256
- if checker.factory.module_name?(absolute_name)
257
- absolute_name_ = checker.factory.type_name_1(absolute_name)
258
- entry = checker.factory.env.class_decls[absolute_name_]
259
- AST::Annotation::Implements::Module.new(name: absolute_name,
260
- args: entry.type_params.each.map(&:name))
261
- end
262
- end
263
- end
264
- end
276
+ implement_module_name = implement_module(module_name: new_module_name, annotations: annots)
265
277
 
266
278
  if implement_module_name
267
279
  module_name = implement_module_name.name
@@ -352,36 +364,7 @@ module Steep
352
364
 
353
365
  annots = source.annotations(block: node, factory: checker.factory, current_module: new_namespace)
354
366
 
355
- implement_module_name = yield_self do
356
- if (annotation = annots.implement_module_annotation)
357
- absolute_name(annotation.name.name).yield_self do |absolute_name|
358
- if checker.factory.class_name?(absolute_name)
359
- AST::Annotation::Implements::Module.new(name: absolute_name,
360
- args: annotation.name.args)
361
- else
362
- Steep.logger.error "Unknown class name given to @implements: #{annotation.name.name}"
363
- nil
364
- end
365
- end
366
- else
367
- name = nil
368
- name ||= absolute_name(new_class_name).yield_self do |absolute_name|
369
- absolute_name if checker.factory.class_name?(absolute_name)
370
- end
371
- name ||= super_class_name && absolute_name(super_class_name).yield_self do |absolute_name|
372
- absolute_name if checker.factory.class_name?(absolute_name)
373
- end
374
-
375
- if name
376
- absolute_name_ = checker.factory.type_name_1(name)
377
- entry = checker.factory.env.class_decls[absolute_name_]
378
- AST::Annotation::Implements::Module.new(
379
- name: name,
380
- args: entry.type_params.each.map(&:name)
381
- )
382
- end
383
- end
384
- end
367
+ implement_module_name = implement_module(module_name: new_class_name, super_name: super_class_name, annotations: annots)
385
368
 
386
369
  if annots.implement_module_annotation
387
370
  new_class_name = implement_module_name.name
@@ -450,6 +433,83 @@ module Steep
450
433
  )
451
434
  end
452
435
 
436
+ def for_sclass(node, type)
437
+ annots = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
438
+
439
+ instance_type = if type.is_a?(AST::Types::Self)
440
+ context.self_type
441
+ else
442
+ type
443
+ end
444
+
445
+ module_type = case instance_type
446
+ when AST::Types::Name::Class
447
+ AST::Builtin::Class.instance_type
448
+ when AST::Types::Name::Module
449
+ AST::Builtin::Module.instance_type
450
+ when AST::Types::Name::Instance
451
+ instance_type.to_class(constructor: nil)
452
+ else
453
+ raise "Unexpected type for sclass node: #{type}"
454
+ end
455
+
456
+ instance_definition = case instance_type
457
+ when AST::Types::Name::Class, AST::Types::Name::Module
458
+ type_name = checker.factory.type_name_1(instance_type.name)
459
+ checker.factory.definition_builder.build_singleton(type_name)
460
+ when AST::Types::Name::Instance
461
+ type_name = checker.factory.type_name_1(instance_type.name)
462
+ checker.factory.definition_builder.build_instance(type_name)
463
+ end
464
+
465
+ module_definition = case module_type
466
+ when AST::Types::Name::Class, AST::Types::Name::Module
467
+ type_name = checker.factory.type_name_1(instance_type.name)
468
+ checker.factory.definition_builder.build_singleton(type_name)
469
+ else
470
+ nil
471
+ end
472
+
473
+ module_context = TypeInference::Context::ModuleContext.new(
474
+ instance_type: annots.instance_type || instance_type,
475
+ module_type: annots.self_type || annots.module_type || module_type,
476
+ implement_name: nil,
477
+ current_namespace: current_namespace,
478
+ const_env: self.module_context.const_env,
479
+ class_name: self.module_context.class_name,
480
+ module_definition: module_definition,
481
+ instance_definition: instance_definition
482
+ )
483
+
484
+ type_env = TypeInference::TypeEnv.build(annotations: annots,
485
+ subtyping: checker,
486
+ const_env: self.module_context.const_env,
487
+ signatures: checker.factory.env)
488
+
489
+ lvar_env = TypeInference::LocalVariableTypeEnv.empty(
490
+ subtyping: checker,
491
+ self_type: module_context.module_type
492
+ ).annotate(annots)
493
+
494
+ body_context = TypeInference::Context.new(
495
+ method_context: nil,
496
+ block_context: nil,
497
+ module_context: module_context,
498
+ break_context: nil,
499
+ self_type: module_context.module_type,
500
+ type_env: type_env,
501
+ lvar_env: lvar_env
502
+ )
503
+
504
+ self.class.new(
505
+ checker: checker,
506
+ source: source,
507
+ annotations: annots,
508
+ typing: typing,
509
+ context: body_context
510
+ )
511
+ end
512
+
453
513
  def for_branch(node, truthy_vars: Set.new, type_case_override: nil, break_context: context.break_context)
454
514
  annots = source.annotations(block: node, factory: checker.factory, current_module: current_namespace)
455
515
 
@@ -1130,6 +1190,25 @@ module Steep
1130
1190
  add_typing(node, type: AST::Builtin.nil_type)
1131
1191
  end
1132
1192
 
1193
+ when :sclass
1194
+ yield_self do
1195
+ type, constr = synthesize(node.children[0])
1196
+ constructor = constr.for_sclass(node, type)
1197
+
1198
+ constructor.typing.add_context_for_node(node, context: constructor.context)
1199
+ constructor.typing.add_context_for_body(node, context: constructor.context)
1200
+
1201
+ constructor.synthesize(node.children[1]) if node.children[1]
1202
+
1203
+ if constructor.module_context.instance_definition && module_context.module_definition
1204
+ if constructor.module_context.instance_definition.type_name == module_context.module_definition.type_name
1205
+ module_context.defined_module_methods.merge(constructor.module_context.defined_instance_methods)
1206
+ end
1207
+ end
1208
+
1209
+ add_typing(node, type: AST::Builtin.nil_type)
1210
+ end
1211
+
1133
1212
  when :self
1134
1213
  add_typing node, type: AST::Types::Self.new
1135
1214
 
@@ -95,6 +95,12 @@ module Steep
95
95
  end_pos = node.loc.end.begin_pos
96
96
  add_context(begin_pos..end_pos, context: context)
97
97
 
98
+ when :sclass
99
+ name_node = node.children[0]
100
+ begin_pos = name_node.loc.expression.end_pos
101
+ end_pos = node.loc.end.begin_pos
102
+ add_context(begin_pos..end_pos, context: context)
103
+
98
104
  when :def, :defs
99
105
  args_node = case node.type
100
106
  when :def
@@ -1,3 +1,3 @@
1
1
  module Steep
2
- VERSION = "0.19.0"
2
+ VERSION = "0.20.0"
3
3
  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.19.0
4
+ version: 0.20.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: 2020-07-12 00:00:00.000000000 Z
11
+ date: 2020-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler