module T::Private::Methods
def self._on_method_added(hook_mod, method_name, is_singleton_method: false)
def self._on_method_added(hook_mod, method_name, is_singleton_method: false) if T::Private::DeclState.current.skip_on_method_added return end current_declaration = T::Private::DeclState.current.active_declaration mod = is_singleton_method ? hook_mod.singleton_class : hook_mod if T::Private::Final.final_module?(mod) && (current_declaration.nil? || !current_declaration.decl_builder.final?) raise "#{mod} was declared as final but its method `#{method_name}` was not declared as final" end # Don't compute mod.ancestors if we don't need to bother checking final-ness. if @was_ever_final_names.include?(method_name) && @modules_with_final.include?(mod.object_id) _check_final_ancestors(mod, mod.ancestors, [method_name], nil) # We need to fetch the active declaration again, as _check_final_ancestors # may have reset it (see the comment in that method for details). current_declaration = T::Private::DeclState.current.active_declaration end if current_declaration.nil? return end T::Private::DeclState.current.reset! if method_name == :method_added || method_name == :singleton_method_added raise( "Putting a `sig` on `#{method_name}` is not supported" \ " (sorbet-runtime uses this method internally to perform `sig` validation logic)" ) end original_method = mod.instance_method(method_name) sig_block = lambda do T::Private::Methods.run_sig(hook_mod, method_name, original_method, current_declaration) end # Always replace the original method with this wrapper, # which is called only on the *first* invocation. # This wrapper is very slow, so it will subsequently re-wrap with a much faster wrapper # (or unwrap back to the original method). key = method_owner_and_name_to_key(mod, method_name) unless current_declaration.raw T::Private::ClassUtils.replace_method(mod, method_name) do |*args, &blk| method_sig = T::Private::Methods.maybe_run_sig_block_for_key(key) method_sig ||= T::Private::Methods._handle_missing_method_signature( self, original_method, __callee__, ) # Should be the same logic as CallValidation.wrap_method_if_needed but we # don't want that extra layer of indirection in the callstack if method_sig.mode == T::Private::Methods::Modes.abstract # We're in an interface method, keep going up the chain if defined?(super) super(*args, &blk) else raise NotImplementedError.new("The method `#{method_sig.method_name}` on #{mod} is declared as `abstract`. It does not have an implementation.") end # Note, this logic is duplicated (intentionally, for micro-perf) at `CallValidation.wrap_method_if_needed`, # make sure to keep changes in sync. elsif method_sig.check_level == :always || (method_sig.check_level == :tests && T::Private::RuntimeLevels.check_tests?) CallValidation.validate_call(self, original_method, method_sig, args, blk) elsif T::Configuration::AT_LEAST_RUBY_2_7 original_method.bind_call(self, *args, &blk) else original_method.bind(self).call(*args, &blk) end end end @sig_wrappers[key] = sig_block if current_declaration.decl_builder.final? @was_ever_final_names[method_name] = true # use hook_mod, not mod, because for example, we want class C to be marked as having final if we def C.foo as # final. change this to mod to see some final_method tests fail. note_module_deals_with_final(hook_mod) add_module_with_final_method(hook_mod, method_name, is_singleton_method) end end