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

Stop generating more methods than necessary #293

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
38 changes: 23 additions & 15 deletions lib/tapioca/gem/listeners/methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,30 @@ def compile_directly_owned_methods(
def compile_method(tree, symbol_name, constant, method, visibility = RBI::Public.new)
return unless method
return unless method_owned_by_constant?(method, constant)
return if @pipeline.symbol_in_payload?(symbol_name) && [email protected]_in_gem?(method)

signature = lookup_signature_of(method)
method = T.let(signature.method, UnboundMethod) if signature
begin
signature = signature_of!(method)
method = T.let(signature.method, UnboundMethod) if signature

case @pipeline.method_in_gem?(method)
when nil
# This means that this is a C-method. Thus, we want to
# skip it only if the constant is an ignored one, since
# that probably means that we've hit a C-method for a
# core type.
return if @pipeline.symbol_in_payload?(symbol_name)
when false
# Do not process this method, if it is not defined by the current gem
return
end
rescue SignatureBlockError => error
@pipeline.error_handler.call(<<~MSG)
Unable to compile signature for method: #{method.owner}##{method.name}
Exception raised when loading signature: #{error.cause.inspect}
MSG

signature = nil
end

method_name = method.name.to_s
return unless valid_method_name?(method_name)
Expand Down Expand Up @@ -211,18 +231,6 @@ def initialize_method_for(constant)
def ignore?(event)
event.is_a?(Tapioca::Gem::ForeignScopeNodeAdded)
end

sig { params(method: UnboundMethod).returns(T.untyped) }
def lookup_signature_of(method)
signature_of!(method)
rescue LoadError, StandardError => error
@pipeline.error_handler.call(<<~MSG)
Unable to compile signature for method: #{method.owner}##{method.name}
Exception raised when loading signature: #{error.inspect}
MSG

nil
end
end
end
end
Expand Down
27 changes: 18 additions & 9 deletions lib/tapioca/gem/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,34 @@ def constant_in_gem?(name)
return true unless Object.respond_to?(:const_source_location)

source_file, _ = Object.const_source_location(name)
return true unless source_file
# If the source location of the constant is "(eval)", all bets are off.
return true if source_file == "(eval)"

# Ruby 3.3 adds automatic definition of source location for evals if
# `file` and `line` arguments are not provided. This results in the source
# file being something like `(eval at /path/to/file.rb:123)`. We try to parse
# this string to get the actual source file.
source_file = source_file.sub(EVAL_SOURCE_FILE_PATTERN, "\\1")
source_file = source_file&.sub(EVAL_SOURCE_FILE_PATTERN, "\\1")

# If the source location of the constant isn't available or is "(eval)", all bets are off.
return true if source_file.nil? || source_file == "(eval)"

gem.contains_path?(source_file)
end

sig { params(method: UnboundMethod).returns(T::Boolean) }
sig { params(method: UnboundMethod).returns(T.nilable(T::Boolean)) }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's add a comment about nil meaning no source location

def method_in_gem?(method)
source_location = method.source_location&.first
return false if source_location.nil?
source_file, _ = method.source_location
# Ruby 3.3 adds automatic definition of source location for evals if
# `file` and `line` arguments are not provided. This results in the source
# file being something like `(eval at /path/to/file.rb:123)`. We try to parse
# this string to get the actual source file.
source_file = source_file&.sub(EVAL_SOURCE_FILE_PATTERN, "\\1")

# If the source location of the method isn't available, signal that by returning nil.
return unless source_file # rubocop:disable Style/ReturnNilInPredicateMethodDefinition

# If the source location of the method is "(eval)", err on the side of caution and include the method.
return true if source_file == "(eval)"

@gem.contains_path?(source_location)
@gem.contains_path?(source_file)
end

# Helpers
Expand Down
6 changes: 5 additions & 1 deletion lib/tapioca/runtime/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,19 @@ def qualified_name_of(constant)
end
end

SignatureBlockError = Class.new(StandardError)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be a Tapioca::Error?


sig { params(method: T.any(UnboundMethod, Method)).returns(T.untyped) }
def signature_of!(method)
T::Utils.signature_for_method(method)
rescue LoadError, StandardError
Kernel.raise SignatureBlockError
end

sig { params(method: T.any(UnboundMethod, Method)).returns(T.untyped) }
def signature_of(method)
signature_of!(method)
rescue LoadError, StandardError
rescue SignatureBlockError
nil
end

Expand Down
150 changes: 1 addition & 149 deletions sorbet/rbi/gems/[email protected]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading