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

Assign different error code to break without value #387

Merged
merged 1 commit into from
Jun 26, 2021
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
17 changes: 17 additions & 0 deletions lib/steep/diagnostic/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,23 @@ def header_line
end
end

class ImplicitBreakValueMismatch < Base
attr_reader :jump_type
attr_reader :result

include ResultPrinter

def initialize(node:, jump_type:, result:)
super(node: node)
@jump_type = jump_type
@result = result
end

def header_line
"Breaking without a value may result an error because a value of type `#{jump_type}` is expected"
end
end

class UnexpectedJump < Base
def header_line
"Cannot jump from here"
Expand Down
5 changes: 2 additions & 3 deletions lib/steep/type_construction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1154,10 +1154,9 @@ def synthesize(node, hint: nil, condition: false)
else
check_relation(sub_type: AST::Builtin.nil_type, super_type: break_type).else do |result|
typing.add_error(
Diagnostic::Ruby::BreakTypeMismatch.new(
Diagnostic::Ruby::ImplicitBreakValueMismatch.new(
node: node,
expected: break_type,
actual: AST::Builtin.nil_type,
jump_type: break_type,
result: result
)
)
Expand Down
30 changes: 27 additions & 3 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6997,9 +6997,8 @@ def foo: () { (String) -> Integer } -> String
end

assert_any!(errors) do |error|
assert_instance_of Diagnostic::Ruby::BreakTypeMismatch, error
assert_equal parse_type("nil"), error.actual
assert_equal parse_type("::String"), error.expected
assert_instance_of Diagnostic::Ruby::ImplicitBreakValueMismatch, error
assert_equal parse_type("::String"), error.jump_type
end
end
end
Expand Down Expand Up @@ -7688,4 +7687,29 @@ def HelloWorld(x, y = 1, *z, a:, b: false, **c, &block)
end
end
end

def test_break_without_value_to_block
with_checker(<<RBS) do |checker|
class NextTest
def foo: () { (String) -> Integer } -> String
end
RBS
source = parse_ruby(<<RUBY)
NextTest.new.foo do |x|
break
end
RUBY

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

assert_typing_error(typing, size: 1) do |errors|
assert_any!(errors) do |error|
assert_instance_of Diagnostic::Ruby::ImplicitBreakValueMismatch, error
assert_equal parse_type("::String"), error.jump_type
end
end
end
end
end
end