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 type inference problem #843

Merged
merged 2 commits into from
Jul 7, 2023
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
12 changes: 7 additions & 5 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3913,13 +3913,15 @@ def try_method_type(node, receiver_type:, method_name:, method_type:, arguments:
end

if hint && !fvs.empty?
if check_relation(sub_type: method_type.type.return_type, super_type: hint, constraints: constraints).success?
method_type, solved, s = apply_solution(errors, node: node, method_type: method_type) do
constraints.solution(checker, variables: fvs, context: ccontext)
if hint.free_variables.subset?(self_type.free_variables)
if check_relation(sub_type: method_type.type.return_type, super_type: hint, constraints: constraints).success?
method_type, solved, s = apply_solution(errors, node: node, method_type: method_type) do
constraints.solution(checker, variables: fvs, context: ccontext)
end
end
end

method_type.block or raise
method_type.block or raise
end
end

# Method accepts block
Expand Down
4 changes: 3 additions & 1 deletion lib/steep/type_inference/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def [](name)

def upper_bounds
table.each_value.with_object({}) do |type_param, bounds|
bounds[type_param.name] = type_param.upper_bound
if type_param.upper_bound
bounds[type_param.name] = type_param.upper_bound
end
end
end

Expand Down
76 changes: 76 additions & 0 deletions test/type_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,80 @@ def test_case_when__bool_value
YAML
)
end

def test_type_inference__nested_block
run_type_check_test(
signatures: {
},
code: {
"a.rb" => <<~RUBY
a = 123.yield_self do
"abc".yield_self do
:hogehoge
end
end

a.is_symbol
RUBY
},
expectations: <<~YAML
---
- file: a.rb
diagnostics:
- range:
start:
line: 7
character: 2
end:
line: 7
character: 11
severity: ERROR
message: Type `::Symbol` does not have method `is_symbol`
code: Ruby::NoMethod
YAML
)
end

def test_type_inference__nested_block_free_variable
run_type_check_test(
signatures: {
"a.rbs" => <<~RBS
class Foo[T]
def foo: () -> T
end
RBS
},
code: {
"a.rb" => <<~RUBY
# Type error is reported because `::Symbol`` cannot be `T`
class Foo
def foo
"".yield_self do
:symbol
end
end
end
RUBY
},
expectations: <<~YAML
---
- file: a.rb
diagnostics:
- range:
start:
line: 4
character: 4
end:
line: 6
character: 7
severity: ERROR
message: |-
Cannot find compatible overloading of method `yield_self` of type `::String`
Method types:
def yield_self: [X] () { (::String) -> X } -> X
| () -> ::Enumerator[::String, untyped]
code: Ruby::UnresolvedOverloading
YAML
)
end
end