Skip to content
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
2 changes: 2 additions & 0 deletions lib/steep/ast/types/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ def partition_union(type)
]
when AST::Types::Any, AST::Types::Boolean, AST::Types::Top, AST::Types::Logic::Base
[type, type]
when AST::Types::Bot, AST::Types::Void
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

[nil, nil]
when AST::Types::Nil
[nil, type]
when AST::Types::Literal
Expand Down
34 changes: 34 additions & 0 deletions test/logic_type_interpreter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,40 @@ def test_type_case_select
end
end

def test_call_void
with_checker(<<-RBS) do |checker|
RBS
source = parse_ruby("email = foo; email.void!")

node = source.node.children[1]

call = TypeInference::MethodCall::Typed.new(
node: node,
context: TypeInference::MethodCall::TopLevelContext.new,
method_name: :void!,
receiver_type: parse_type("::String"),
actual_method_type: parse_method_type("() -> void"),
method_decls: [],
return_type: AST::Types::Void.new()
)

typing = Typing.new(source: source, root_context: nil, cursor: nil)
typing.add_typing(dig(node), AST::Types::Void.new(), nil)
typing.add_typing(dig(node, 0), parse_type("::String"), nil)

env = type_env
.assign_local_variable(:email, parse_type("::String"), nil)

interpreter = LogicTypeInterpreter.new(subtyping: checker, typing: typing, config: config)
truthy_result, falsy_result = interpreter.eval(env: env, node: node)

assert_equal parse_type("bot"), truthy_result.type
assert_equal parse_type("bot"), falsy_result.type
assert_equal true, truthy_result.unreachable
assert_equal true, falsy_result.unreachable
end
end

def test_type_case_select_untyped
with_checker do |checker|
interpreter = LogicTypeInterpreter.new(subtyping: checker, typing: nil, config: config)
Expand Down
93 changes: 93 additions & 0 deletions test/type_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,99 @@ def test_if_unreachable__unless_else
)
end

def test_if_unreachable__if_void
run_type_check_test(
signatures: {
"a.rbs" => <<~RBS
class Foo
def void: () -> void
end
RBS
},
code: {
"a.rb" => <<~RUBY
# Both branches are unreachable
if Foo.new.void
123
else
123
end
RUBY
},
expectations: <<~YAML
---
- file: a.rb
diagnostics:
- range:
start:
line: 2
character: 0
end:
line: 2
character: 2
severity: ERROR
message: The branch is unreachable
code: Ruby::UnreachableBranch
- range:
start:
line: 4
character: 0
end:
line: 4
character: 4
severity: ERROR
message: The branch is unreachable
code: Ruby::UnreachableBranch
YAML
)
end
def test_if_unreachable__if_bot
run_type_check_test(
signatures: {
"a.rbs" => <<~RBS
class Foo
def bot: () -> bot
end
RBS
},
code: {
"a.rb" => <<~RUBY
# Both branches are unreachable
if Foo.new.bot
123
else
123
end
RUBY
},
expectations: <<~YAML
---
- file: a.rb
diagnostics:
- range:
start:
line: 2
character: 0
end:
line: 2
character: 2
severity: ERROR
message: The branch is unreachable
code: Ruby::UnreachableBranch
- range:
start:
line: 4
character: 0
end:
line: 4
character: 4
severity: ERROR
message: The branch is unreachable
code: Ruby::UnreachableBranch
YAML
)
end

def test_case_unreachable_1
run_type_check_test(
signatures: {
Expand Down
9 changes: 9 additions & 0 deletions test/type_factory_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,13 @@ def test_partition_union__bool_union
end
end
end

def test_partition_union__void
with_factory() do |factory|
factory.partition_union(factory.type(parse_type("void"))).tap do |truthy, falsy|
assert_equal nil, truthy
assert_equal nil, falsy
end
end
end
end