Skip to content

Commit

Permalink
Fix double-edge error when the same keyword hash is passed to a method
Browse files Browse the repository at this point in the history
This pattern was found in optparse
  • Loading branch information
mame committed Aug 8, 2024
1 parent b82a9e0 commit 7a6455d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 24 deletions.
17 changes: 17 additions & 0 deletions lib/typeprof/core/env/method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def get_rest_args(genv, start_rest, end_rest)

vtxs.uniq
end

def get_keyword_arg(genv, changes, name)
vtx = Vertex.new(self)
@keywords.each_type do |ty|
case ty
when Type::Hash
changes.add_edge(genv, ty.get_value(name), vtx)
when Type::Instance
if ty.mod == genv.mod_hash
changes.add_edge(genv, ty.args[1], vtx)
end
else
# what to do?
end
end
vtx
end
end

class Block
Expand Down
26 changes: 2 additions & 24 deletions lib/typeprof/core/graph/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -530,33 +530,11 @@ def pass_arguments(changes, genv, a_args)
if a_args.keywords
# TODO: support diagnostics
@node.req_keywords.zip(@f_args.req_keywords) do |name, f_vtx|
a_args.keywords.each_type do |ty|
case ty
when Type::Hash
changes.add_edge(genv, ty.get_value(name), f_vtx)
when Type::Instance
if ty.mod == genv.mod_hash
changes.add_edge(genv, ty.args[1], f_vtx)
end
else
# what to do?
end
end
changes.add_edge(genv, a_args.get_keyword_arg(genv, changes, name), f_vtx)
end

@node.opt_keywords.zip(@f_args.opt_keywords).each do |name, f_vtx|
a_args.keywords.each_type do |ty|
case ty
when Type::Hash
changes.add_edge(genv, ty.get_value(name), f_vtx)
when Type::Instance
if ty.mod == genv.mod_hash
changes.add_edge(genv, ty.args[1], f_vtx)
end
else
# what to do?
end
end
changes.add_edge(genv, a_args.get_keyword_arg(genv, changes, name), f_vtx)
end

if @node.rest_keywords
Expand Down
37 changes: 37 additions & 0 deletions scenario/args/keyword-twice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## update
class Foo
def initialize(k:)
end
end

class Bar < Foo
end

obj = rand < 0.5 ? Foo : Bar
obj.new(k: 1)

## assert
class Foo
def initialize: (k: Integer) -> nil
end
class Bar < Foo
end

## update
class Foo
def initialize(k: :default)
end
end

class Bar < Foo
end

obj = rand < 0.5 ? Foo : Bar
obj.new(k: 1)

## assert
class Foo
def initialize: (?k: :default | Integer) -> nil
end
class Bar < Foo
end

0 comments on commit 7a6455d

Please sign in to comment.