Skip to content

Commit

Permalink
Bidirectional typing on true/false
Browse files Browse the repository at this point in the history
They may have type of `true` and `false`.
  • Loading branch information
soutaro committed Jul 24, 2020
1 parent 8ff2b3a commit c80cd43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,13 @@ def synthesize(node, hint: nil)
end

when :true, :false
add_typing(node, type: AST::Types::Boolean.new)
ty = node.type == :true ? AST::Types::Literal.new(value: true) : AST::Types::Literal.new(value: false)

if hint && check_relation(sub_type: ty, super_type: hint).success?
add_typing(node, type: hint)
else
add_typing(node, type: AST::Types::Boolean.new)
end

when :hash
yield_self do
Expand Down
20 changes: 20 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2656,6 +2656,26 @@ def test_rescue_bidning_typing
end
end

def test_string_or_true_false
with_checker do |checker|
source = parse_ruby(<<EOF)
# @type var x: String | FalseClass
x = false
# @type var y: String | true
y = true
EOF

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

assert_no_error typing
assert_equal parse_type("::String | ::FalseClass"), context.lvar_env[:x]
assert_equal parse_type("::String | true"), context.lvar_env[:y]
end
end
end

def test_type_case_case_when
with_checker do |checker|
source = parse_ruby(<<EOF)
Expand Down

0 comments on commit c80cd43

Please sign in to comment.