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

Fix self union type checking #1467

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 5 additions & 6 deletions lib/steep/subtyping/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ def check_type0(relation)
)
end

when relation.sub_type.is_a?(AST::Types::Self) && !self_type.is_a?(AST::Types::Self)
Expand(relation) do
check_type(Relation.new(sub_type: self_type, super_type: relation.super_type))
end

when relation.sub_type.is_a?(AST::Types::Instance) && !instance_type.is_a?(AST::Types::Instance)
Expand(relation) do
check_type(Relation.new(sub_type: instance_type, super_type: relation.super_type))
Expand Down Expand Up @@ -419,6 +414,11 @@ def check_type0(relation)
end
end

when relation.sub_type.is_a?(AST::Types::Self) && !self_type.is_a?(AST::Types::Self)
Expand(relation) do
check_type(Relation.new(sub_type: self_type, super_type: relation.super_type))
end

when relation.super_type.is_a?(AST::Types::Name::Interface)
Expand(relation) do
check_interface(
Expand Down Expand Up @@ -608,7 +608,6 @@ def check_type0(relation)
Expand(relation) do
check_type(Relation.new(sub_type: relation.sub_type.back_type, super_type: relation.super_type))
end

else
Failure(relation, Result::Failure::UnknownPairError.new(relation: relation))
end
Expand Down
27 changes: 27 additions & 0 deletions test/type_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2927,4 +2927,31 @@ def test_tuple_type_assertion
YAML
)
end

def test_self_type_union_assertion
run_type_check_test(
signatures: {
"a.rbs" => <<~RBS
class Foo
def bar: (bool) -> self?
end
RBS
},
code: {
"a.rb" => <<~RUBY
class Foo
def bar(var)
return nil if var
self
end
end
RUBY
},
expectations: <<~YAML
---
- file: a.rb
diagnostics: []
YAML
)
end
end