typeprof 0.33.0 → 0.33.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8dc7b580d84d82c2a49d4a2d24ddf32ddcafc938d4f9511fc4b6d4543f0b8e19
4
- data.tar.gz: eed64be210440c2ea2627a7b21c95c7eedee374b760e9e6aa479dfa4c27df54f
3
+ metadata.gz: d5fa5ffcf2f4e98328b77be29d4e14574083acd097b9af8bac36c2f8e1113626
4
+ data.tar.gz: e4a075b8befec5fbf855d4c83cb17c66db0e0441d1e964249b82072a21b40f93
5
5
  SHA512:
6
- metadata.gz: b314eccaecb06cac94df1af7ab96084b06d1d70710c9ece6934c98ca5379ca98e82ab93838322081357d13c0200681beb24d6108f699aac4a8904952333a7d83
7
- data.tar.gz: 7ebcd55f8c8d91b1eac583c7d585723de31a83da5fa85c7485cda77e520e24d09f1652b1dde7657db62c8151166cbfdc71d3563647ccd60871bb31e74fc6055a
6
+ metadata.gz: 15d81769ed88b1f1ae097a913a8bc2f4912a36fb792900f86ef4071fe8e04edf1268fb84540e0a56bbc0c30ddeed12b7965aceb1e14fe5917557909146a936f5
7
+ data.tar.gz: a1ceade33cf5282b8d117dba231a0a59121a2d32865a8bdea17b5b44adb25f4eee53534a4c14b67a3442a1538557430408662b1802aa6769085a37a39a4c7db5
@@ -14,16 +14,20 @@ module TypeProf::Core
14
14
  @cpath = AST.resolve_rbs_name(raw_decl.name, lenv)
15
15
  # TODO: decl.type_params
16
16
  # TODO: decl.super_class.args
17
+ # TODO?: param.variance, param.unchecked, param.upper_bound
18
+ @params = raw_decl.type_params.map {|param| param.name }
19
+ sig_type_params = [@cpath, @params]
20
+ # The header (default types, self types, and superclass arguments) is
21
+ # resolved in the outer scope but may refer to the type parameters
22
+ @header_lenv = LocalEnv.new(@lenv.file_context, lenv.cref, {}, [], sig_type_params:)
17
23
  ncref = CRef.new(@cpath, :class, nil, lenv.cref)
18
- nlenv = LocalEnv.new(@lenv.file_context, ncref, {}, [])
24
+ nlenv = LocalEnv.new(@lenv.file_context, ncref, {}, [], sig_type_params:)
19
25
  @members = raw_decl.members.map do |member|
20
26
  AST.create_rbs_member(member, nlenv)
21
27
  end.compact
22
- # TODO?: param.variance, param.unchecked, param.upper_bound
23
- @params = raw_decl.type_params.map {|param| param.name }
24
28
  @params_default_types = raw_decl.type_params.map do |param|
25
29
  ty = param.default_type
26
- ty ? AST.create_rbs_type(ty, lenv) : nil
30
+ ty ? AST.create_rbs_type(ty, @header_lenv) : nil
27
31
  end
28
32
  end
29
33
 
@@ -81,7 +85,7 @@ module TypeProf::Core
81
85
  cpath = name.namespace.path + [self_type.name.name]
82
86
  toplevel = name.namespace.absolute?
83
87
  @self_types << [cpath, toplevel]
84
- @self_type_args << self_type.args.map {|arg| AST.create_rbs_type(arg, lenv) }
88
+ @self_type_args << self_type.args.map {|arg| AST.create_rbs_type(arg, @header_lenv) }
85
89
  end
86
90
  end
87
91
 
@@ -138,7 +142,7 @@ module TypeProf::Core
138
142
  name = superclass.name
139
143
  @superclass_cpath = name.namespace.path + [name.name]
140
144
  @superclass_toplevel = name.namespace.absolute?
141
- @superclass_args = superclass.args.map {|arg| AST.create_rbs_type(arg, lenv) }
145
+ @superclass_args = superclass.args.map {|arg| AST.create_rbs_type(arg, @header_lenv) }
142
146
  else
143
147
  @superclass_cpath = nil
144
148
  @superclass_toplevel = nil
@@ -883,18 +883,32 @@ module TypeProf::Core
883
883
 
884
884
  def attrs = { var: }
885
885
 
886
+ # A reopened declaration may rename the type parameters (e.g., `Enumerable[E]`
887
+ # and `Enumerable[Elem]`), but the subst is keyed by the names of the module
888
+ # entity, so fall back to matching them by position
889
+ def resolve_var(genv, subst)
890
+ decl_cpath, decl_params = @lenv.sig_type_params
891
+ return subst[@var] unless decl_params
892
+ idx = decl_params.index(@var)
893
+ return subst[@var] unless idx
894
+ subst[genv.resolve_cpath(decl_cpath).type_params.keys[idx]]
895
+ end
896
+
886
897
  def covariant_vertex0(genv, changes, vtx, subst)
887
- raise "unknown type variable: #{ @var }" unless subst[@var]
888
- changes.add_edge(genv, subst[@var], vtx)
898
+ var_vtx = resolve_var(genv, subst)
899
+ raise "unknown type variable: #{ @var }" unless var_vtx
900
+ changes.add_edge(genv, var_vtx, vtx)
889
901
  end
890
902
 
891
903
  def contravariant_vertex0(genv, changes, vtx, subst)
892
- raise "unknown type variable: #{ @var }" unless subst[@var]
893
- changes.add_edge(genv, Source.new(Type::Var.new(genv, @var, subst[@var])), vtx)
904
+ var_vtx = resolve_var(genv, subst)
905
+ raise "unknown type variable: #{ @var }" unless var_vtx
906
+ changes.add_edge(genv, Source.new(Type::Var.new(genv, @var, var_vtx)), vtx)
894
907
  end
895
908
 
896
909
  def typecheck(genv, changes, vtx, subst)
897
- changes.add_edge(genv, vtx.new_vertex(genv, self), subst[@var]) unless vtx == subst[@var]
910
+ var_vtx = resolve_var(genv, subst)
911
+ changes.add_edge(genv, vtx.new_vertex(genv, self), var_vtx) unless vtx == var_vtx
898
912
  true
899
913
  end
900
914
 
@@ -1,14 +1,24 @@
1
1
  module TypeProf::Core
2
2
  class AST
3
3
  def self.parse_rb(path, src, position_encoding)
4
- result = Prism.parse(src)
4
+ build_rb(path, Prism.parse(src), position_encoding)
5
+ end
6
+
7
+ # This needs the comments and the Prism::Source, which a ParseResult has but a
8
+ # node does not.
9
+ def self.build_rb(path, result, position_encoding)
10
+ unless result.is_a?(Prism::ParseResult)
11
+ raise ArgumentError, "expected Prism::ParseResult, got #{ result.class }"
12
+ end
5
13
 
6
14
  return nil unless result.errors.empty?
7
15
 
8
16
  # comments, errors, magic_comments
9
17
  raw_scope = result.value
10
18
 
11
- raise unless raw_scope.type == :program_node
19
+ unless raw_scope.is_a?(Prism::ProgramNode)
20
+ raise ArgumentError, "expected Prism::ProgramNode, got #{ raw_scope.class }"
21
+ end
12
22
 
13
23
  prism_source = result.source
14
24
  file_context = FileContext.new(path, position_encoding, prism_source, result.comments)
@@ -283,17 +283,6 @@ module TypeProf::Core
283
283
  mod.get_type_alias(name)
284
284
  end
285
285
 
286
- # Returns the type parameter names of the first generic declaration of cpath
287
- def find_type_params(decls, cpath)
288
- decls.each do |decl|
289
- next unless decl.cpath == cpath
290
- next unless decl.respond_to?(:params)
291
- params = decl.params
292
- return params if params && !params.empty?
293
- end
294
- nil
295
- end
296
-
297
286
  def load_core_rbs(raw_decls, position_encoding)
298
287
  file_context = FileContext.new(nil, position_encoding)
299
288
  lenv = LocalEnv.new(file_context, CRef::Toplevel, {}, [])
@@ -301,11 +290,6 @@ module TypeProf::Core
301
290
  AST.create_rbs_decl(raw_decl, lenv)
302
291
  end.compact
303
292
 
304
- # A module entity has one set of parameter names shared by all its declarations,
305
- # so the shim must reuse the core's ones (Array's was `Elem`, and is `E` since RBS 4.1)
306
- ary_elem, = find_type_params(decls, [:Array]) || [:Elem]
307
- hash_key, hash_val = find_type_params(decls, [:Hash]) || [:K, :V]
308
-
309
293
  decls += AST.parse_rbs("typeprof-rbs-shim.rbs", <<-RBS, position_encoding)
310
294
  class Exception
311
295
  include _Exception
@@ -314,12 +298,12 @@ module TypeProf::Core
314
298
  include _ToS
315
299
  include _ToStr
316
300
  end
317
- class Array[#{ ary_elem }]
318
- include _ToAry[#{ ary_elem }]
319
- include _Each[#{ ary_elem }]
301
+ class Array[Elem]
302
+ include _ToAry[Elem]
303
+ include _Each[Elem]
320
304
  end
321
- class Hash[#{ hash_key }, #{ hash_val }]
322
- include _Each[[#{ hash_key }, #{ hash_val }]]
305
+ class Hash[K, V]
306
+ include _Each[[K, V]]
323
307
  end
324
308
  class Object
325
309
  include Hash::_Key
@@ -380,7 +364,7 @@ module TypeProf::Core
380
364
  end
381
365
 
382
366
  class LocalEnv
383
- def initialize(file_context, cref, locals, return_boxes, forward_args = nil)
367
+ def initialize(file_context, cref, locals, return_boxes, forward_args = nil, sig_type_params: nil)
384
368
  @file_context = file_context
385
369
  @cref = cref
386
370
  @locals = locals
@@ -390,9 +374,11 @@ module TypeProf::Core
390
374
  @ivar_narrowings = {}
391
375
  @strict_const_scope = false
392
376
  @forward_args = forward_args
377
+ # [cpath, names] of the type parameters of the enclosing RBS declaration
378
+ @sig_type_params = sig_type_params
393
379
  end
394
380
 
395
- attr_reader :file_context, :cref, :locals, :return_boxes, :break_vtx, :next_boxes, :strict_const_scope
381
+ attr_reader :file_context, :cref, :locals, :return_boxes, :break_vtx, :next_boxes, :strict_const_scope, :sig_type_params
396
382
  attr_accessor :module_function, :forward_args
397
383
 
398
384
  def path = @file_context&.path
@@ -55,10 +55,15 @@ module TypeProf::Core
55
55
  end
56
56
 
57
57
  def update_rb_file(path, code)
58
+ code = File.read(path) unless code
59
+ update_rb_ast(path, Prism.parse(code))
60
+ end
61
+
62
+ # path is used only as a key to find the previous analysis; the file is not read.
63
+ def update_rb_ast(path, parse_result)
58
64
  prev_node = @rb_text_nodes[path]
59
65
 
60
- code = File.read(path) unless code
61
- node = AST.parse_rb(path, code, @options[:position_encoding])
66
+ node = AST.build_rb(path, parse_result, @options[:position_encoding])
62
67
  return false unless node
63
68
 
64
69
  node.diff(@rb_text_nodes[path]) if prev_node
@@ -1,3 +1,3 @@
1
1
  module TypeProf
2
- VERSION = "0.33.0"
2
+ VERSION = "0.33.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typeprof
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.33.0
4
+ version: 0.33.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yusuke Endoh