From d4d0937097b8df300a4f4bfe1466c5cd6d51ac03 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Sun, 16 Aug 2020 23:41:48 +0900 Subject: [PATCH] Revise op_asgn lvar_env handling --- lib/steep/type_construction.rb | 26 +++++++++++++-------- test/type_construction_test.rb | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/lib/steep/type_construction.rb b/lib/steep/type_construction.rb index 89f78bcb3..23806809e 100644 --- a/lib/steep/type_construction.rb +++ b/lib/steep/type_construction.rb @@ -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? @@ -2009,6 +2015,8 @@ def ivasgn(node, type) fallback_to_any node end end + + add_typing(node, type: type) end def type_masgn(node) @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/type_construction_test.rb b/test/type_construction_test.rb index 52e70a272..e65736a18 100644 --- a/test/type_construction_test.rb +++ b/test/type_construction_test.rb @@ -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