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

Treat present? as a special method that guarantees the receiver is not nil #1130

Open
wants to merge 1 commit 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
14 changes: 14 additions & 0 deletions lib/steep/interface/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,20 @@ def replace_primitive_method(method_name, method_def, method_type)
end
end

when :present?
case defined_in
when RBS::BuiltinNames::Object.name,
AST::Builtin::NilClass.module_name,
RBS::BuiltinNames::Kernel.name
if member.instance?
return method_type.with(
type: method_type.type.with(
return_type: AST::Types::Logic::ReceiverIsNotNil.new(location: method_type.type.return_type.location)
)
)
end
end

when :!
case defined_in
when RBS::BuiltinNames::BasicObject.name,
Expand Down
23 changes: 23 additions & 0 deletions lib/steep/type_inference/logic_type_interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,29 @@ def evaluate_method_call(env:, type:, receiver:, arguments:)
[truthy_result, falsy_result]
end

when AST::Types::Logic::ReceiverIsNotNil
if receiver && arguments.size.zero?
receiver_type = typing.type_of(node: receiver)
unwrap = factory.unwrap_optional(receiver_type)
truthy_receiver = unwrap || receiver_type
falsy_receiver = AST::Builtin.nil_type

truthy_env, falsy_env = refine_node_type(
env: env,
node: receiver,
truthy_type: truthy_receiver,
falsy_type: falsy_receiver
)

truthy_result = Result.new(type: TRUE, env: truthy_env, unreachable: false)
truthy_result.unreachable! unless unwrap

falsy_result = Result.new(type: FALSE, env: falsy_env, unreachable: false)
falsy_result.unreachable! if no_subtyping?(sub_type: AST::Builtin.nil_type, super_type: receiver_type)

[truthy_result, falsy_result]
end

Comment on lines +303 to +325
Copy link
Author

Choose a reason for hiding this comment

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

This part would be the exactly the same as https://github.com/soutaro/steep/pull/905/files#diff-56dab2e481abd1ca9efdcfd898234599b5f7ad222e557cf242db1d3c6904516a

@tk0miya Could you extract this part as different PR?

In either way we need this patch.

when AST::Types::Logic::ReceiverIsArg
if receiver && (arg = arguments[0])
receiver_type = typing.type_of(node: receiver)
Expand Down
35 changes: 35 additions & 0 deletions test/type_construction_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6195,6 +6195,41 @@ def test_logic_receiver_is_nil
end
end

def test_logic_receiver_is_not_nil
with_checker(<<-RBS) do |checker|
class Object
def present?: () -> bool
end
RBS
source = parse_ruby(<<-RUBY)
a = [1].first
return unless a.present?
a + 1

b = [1].first
return unless (x = b).present?
x + 1

c = [1].first
return unless (y = c.present?)
c + 1

d = [1].first
puts "nil!" and return unless d.present?
d + 1

e = [1].first
nil or return unless e.present?
e + 1
RUBY

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

def test_logic_receiver_is_arg
with_checker(<<-RBS) do |checker|
RBS
Expand Down