Skip to content

Commit d6c1caa

Browse files
committed
Fix a narrowing bug for a nested ivar
1 parent 9aaf4f7 commit d6c1caa

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

lib/typeprof/core/env.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ def pop_ivar_narrowing(name)
330330

331331
def apply_ivar_narrowing(genv, node, name, vtx)
332332
if @ivar_narrowings[name] && !@ivar_narrowings[name].empty?
333-
return @ivar_narrowings[name].last.narrow(genv, node, vtx)
333+
# Apply all accumulated narrowings in order
334+
@ivar_narrowings[name].each do |narrowing|
335+
vtx = narrowing.narrow(genv, node, vtx)
336+
end
337+
return vtx
334338
end
335339
vtx
336340
end

scenario/flow/ivar3.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## update
2+
class Foo
3+
def initialize(x)
4+
@x = x
5+
end
6+
7+
def accept_int(x) = nil
8+
def accept_str(x) = nil
9+
def accept_nil(x) = nil
10+
11+
def foo
12+
if @x
13+
if @x.is_a?(String)
14+
accept_str(@x)
15+
else
16+
accept_int(@x)
17+
end
18+
else
19+
accept_nil(@x)
20+
end
21+
end
22+
end
23+
24+
Foo.new(1)
25+
Foo.new("")
26+
Foo.new(nil)
27+
28+
## assert
29+
class Foo
30+
def initialize: ((Integer | String)?) -> (Integer | String)?
31+
def accept_int: (Integer) -> nil
32+
def accept_str: (String) -> nil
33+
def accept_nil: (nil) -> nil
34+
def foo: -> nil
35+
end

0 commit comments

Comments
 (0)