Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise op_asgn lvar_env handling #189

Merged
merged 1 commit into from
Aug 17, 2020
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
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