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

rbs prototype rb guess return value type even if "def" has multiple statements #303

Merged
merged 2 commits into from
Jun 14, 2020

Conversation

pocke
Copy link
Member

@pocke pocke commented Jun 14, 2020

Background

Since #209, rbs prototype rb has been aware of returned type. But it only can be aware of the method body has only one statement and it is literal.

For example:

# It will be: def good: () -> "foo"
def good
  "foo"
end

# But it will be: def bad: () -> untyped
def bad
  something
  "foo"
end

What is this pull request

This pull request solves the bad: () -> untyped. It will be bad: () -> "foo".

Previously rbs prototype rb only guessed returned types for methods that contain only one literal.
But it will guess types for return statements and the last statement of the method.

@@ -408,9 +420,11 @@ def literal_to_type(node)

def types_to_union_type(types)
return untyped if types.empty?
return untyped if types.include?(untyped)
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously Something | untyped is squashed to untyped. But I think Something | untyped is useful for humans when reviewing the generated types. (The original idea is the type profiler.)
So it will not be squashed by this pull request.

@@ -393,7 +405,7 @@ def literal_to_type(node)
value_types << literal_to_type(v)
end

if key_types.all? { |t| t.is_a?(Types::Literal) }
if !key_types.empty? && key_types.all? { |t| t.is_a?(Types::Literal) }
Copy link
Member Author

Choose a reason for hiding this comment

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

An empty record is syntax error, so it should be Hash[untyped, untyped] instead.

@@ -128,6 +128,9 @@ def assert_write(decls, string)
writer.write(decls)

assert_equal string, writer.out.string

# Check syntax error
RBS::Parser.parse_signature(writer.out.string)
Copy link
Member

Choose a reason for hiding this comment

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

👍

@soutaro soutaro merged commit 06e4db7 into ruby:master Jun 14, 2020
@pocke pocke deleted the guess-multi-stmts branch June 14, 2020 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants