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

WIP: Properly attribute dynamically-generated methods #1984

Draft
wants to merge 3 commits into
base: uk-method-generation-improvements
Choose a base branch
from
Draft
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
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?(constant, 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
43 changes: 33 additions & 10 deletions lib/tapioca/gem/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,48 @@ 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) }
def method_in_gem?(method)
source_location = method.source_location&.first
return false if source_location.nil?
sig { params(constant: Module, method: UnboundMethod).returns(T.nilable(T::Boolean)) }
def method_in_gem?(constant, method)
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)"

return true if @gem.contains_path?(source_file)

method_definitions = Tapioca::Runtime::Trackers::MethodDefinition.method_definitions_for(constant)

source_file = method_definitions[method.name]

# If the source location of the method isn't available, signal that by returning nil.
return unless source_file

# 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)"

source_file = source_file&.sub(EVAL_SOURCE_FILE_PATTERN, "\\1")

@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)

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
1 change: 1 addition & 0 deletions lib/tapioca/runtime/trackers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ def register_tracker(tracker)
require "tapioca/runtime/trackers/constant_definition"
require "tapioca/runtime/trackers/autoload"
require "tapioca/runtime/trackers/required_ancestor"
require "tapioca/runtime/trackers/method_definition"
43 changes: 43 additions & 0 deletions lib/tapioca/runtime/trackers/method_definition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# typed: true
# frozen_string_literal: true

module Tapioca
module Runtime
module Trackers
module MethodDefinition
extend Tracker
extend T::Sig

@method_definitions = {}.compare_by_identity

class << self
extend T::Sig

sig { params(constant: Module, method_name: Symbol).void }
def register(constant, method_name)
return unless enabled?

@method_definitions[constant] ||= {}
@method_definitions[constant][method_name] = Reflection.resolve_loc(caller_locations)
end

sig { params(constant: Module).returns(T::Hash[Symbol, T::Array[T::Hash[Symbol, String]]]) }
def method_definitions_for(constant)
@method_definitions[constant] || {}
end
end
end
end
end
end

class Module
prepend(Module.new do
def method_added(method_name)
Tapioca::Runtime::Trackers::MethodDefinition.register(self, method_name)
super
end
end)
end

# TODO: are there other methods I have to override here?
Loading
Loading