Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions lib/typeprof/core/ast/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,19 +265,39 @@ def install0(genv)
class ForNode < Node
def initialize(raw_node, lenv)
super(raw_node, lenv)
# XXX: tentative implementation
# raw_node.index
@index = AST.create_target_node(raw_node.index, lenv)
@expr = AST.create_node(raw_node.collection, lenv)
@body = raw_node.statements ? AST.create_node(raw_node.statements, lenv) : DummyNilNode.new(TypeProf::CodeRange.new(code_range.last, code_range.last), lenv)
end

attr_reader :expr, :body
attr_reader :index, :expr, :body

def subnodes = { expr:, body: }
def subnodes = { index:, expr:, body: }

def install0(genv)
@expr.install(genv)

vars = []
@index.modified_vars(@lenv.locals.keys, vars)
@body.modified_vars(@lenv.locals.keys, vars)
vars.uniq!

old_vtxs = {}
vars.each do |var|
vtx = @lenv.get_var(var)
nvtx = vtx.new_vertex(genv, self)
old_vtxs[var] = nvtx
@lenv.set_var(var, nvtx)
end

@index.install(genv)
@body.install(genv)

vars.each do |var|
@changes.add_edge(genv, @lenv.get_var(var), old_vtxs[var])
@lenv.set_var(var, old_vtxs[var])
end

Source.new(genv.nil_type)
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/typeprof/core/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,6 @@ def initialize(file_context, cref, locals, return_boxes)
attr_reader :file_context, :cref, :locals, :return_boxes, :break_vtx, :next_boxes, :strict_const_scope

def path = @file_context&.path
def code_units_cache = @file_context&.code_units_cache

def code_range_from_node(node)
TypeProf::CodeRange.from_node(node, @file_context&.code_units_cache)
end
Expand Down
13 changes: 13 additions & 0 deletions scenario/misc/for-variable-leak.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## update
def foo
x = "hello"
for i in [1, 2, 3]
x = 42
end
x
end

## assert
class Object
def foo: -> (Integer | String)
end
1 change: 0 additions & 1 deletion scenario/misc/for.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## update
def foo
for i in 1..100
# currently, TypeProf does not initialize i
return 1
end
end
Expand Down