Skip to content

Commit

Permalink
Merge pull request #189 from soutaro/op_asgn
Browse files Browse the repository at this point in the history
Revise op_asgn lvar_env handling
  • Loading branch information
soutaro committed Aug 17, 2020
2 parents 7929338 + d4d0937 commit d0c4b41
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
26 changes: 17 additions & 9 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1798,9 +1798,15 @@ def synthesize(node, hint: nil)

when :or_asgn, :and_asgn
yield_self do
_, rhs = node.children
rhs_type = synthesize(rhs).type
add_typing(node, type: rhs_type)
asgn, rhs = node.children
type, constr = synthesize(rhs, hint: hint)

case asgn.type
when :lvasgn
constr.lvasgn(asgn, type)
when :ivasgn
constr.ivasgn(asgn, type)
end
end

when :defined?
Expand Down Expand Up @@ -2009,6 +2015,8 @@ def ivasgn(node, type)
fallback_to_any node
end
end

add_typing(node, type: type)
end

def type_masgn(node)
Expand Down Expand Up @@ -2077,7 +2085,7 @@ def type_masgn(node)
when :lvasgn
_, constr = constr.lvasgn(asgn, type)
when :ivasgn
constr.ivasgn(asgn, type)
_, constr = constr.ivasgn(asgn, type)
end
end

Expand All @@ -2094,7 +2102,7 @@ def type_masgn(node)
when :lvasgn
_, constr = constr.lvasgn(asgn, type)
when :ivasgn
constr.ivasgn(asgn, type)
_, constr = constr.ivasgn(asgn, type)
end
end

Expand All @@ -2112,12 +2120,12 @@ def type_masgn(node)
when :lvasgn
_, constr = constr.lvasgn(asgn.children[0], array_type)
when :ivasgn
constr.ivasgn(asgn.children[0], array_type)
_, constr = constr.ivasgn(asgn.children[0], array_type)
end
when :lvasgn
_, constr = constr.lvasgn(asgn, element_type)
when :ivasgn
constr.ivasgn(asgn, element_type)
_,constr = constr.ivasgn(asgn, element_type)
end
end

Expand All @@ -2137,13 +2145,13 @@ def type_masgn(node)
_, constr = constr.lvasgn(assignment, element_type)

when :ivasgn
constr.ivasgn(assignment, element_type)
_, constr = constr.ivasgn(assignment, element_type)
when :splat
case assignment.children[0].type
when :lvasgn
_, constr = constr.lvasgn(assignment.children[0], unwrap_rhs_type)
when :ivasgn
constr.ivasgn(assignment.children[0], unwrap_rhs_type)
_, constr = constr.ivasgn(assignment.children[0], unwrap_rhs_type)
else
raise
end
Expand Down
42 changes: 42 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5110,4 +5110,46 @@ def preserve_empty_line(prev, decl)
end
end
end

def test_orasign_lvar
with_checker do |checker|
source = parse_ruby(<<-RUBY)
# @type var y: String
x = 1 ? "" : nil
x ||= ""
y = x
RUBY

with_standard_construction(checker, source) do |construction, typing|
construction.synthesize(source.node)

assert_no_error typing
end
end
end

def test_orasign_ivar
with_checker <<-RBS do |checker|
class IVar
@ivar: String?
def set: () -> String
end
RBS
source = parse_ruby(<<-RUBY)
class IVar
def set
@ivar ||= "foo"
end
end
RUBY

with_standard_construction(checker, source) do |construction, typing|
construction.synthesize(source.node)

assert_no_error typing
end
end
end
end

0 comments on commit d0c4b41

Please sign in to comment.