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

Proposal: Add method annotations: primitive:nil? and primitive:not_nil? #905

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 @@ -767,6 +767,20 @@ def replace_primitive_method(method_name, method_def, method_type)
)
end
end

if method_def.annotations.any? {|annotation| annotation.string == "primitive:nil?" }
return method_type.with(
type: method_type.type.with(
return_type: AST::Types::Logic::ReceiverIsNil.new(location: method_type.type.return_type.location)
)
)
elsif method_def.annotations.any? {|annotation| annotation.string == "primitive:not_nil?" }
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

method_type
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

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

def test_logic_receiver_is_nil_via_annotation
with_checker(<<-RBS) do |checker|
class Object
%a{primitive:nil?}
Copy link

Choose a reason for hiding this comment

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

Unlike nil?, blank? doesn't guarantee the receiver is nil (e.g. it returns true for empty string)

def blank?: () -> bool
end
RBS
source = parse_ruby(<<-RUBY)
a = [1].first
return if a.blank?
a + 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_not_nil_via_annotation
with_checker(<<-RBS) do |checker|
class Object
%a{primitive:not_nil?}
def present?: () -> bool
end
RBS
source = parse_ruby(<<-RUBY)
a = [1].first
return unless a.present?
a + 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