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

TypeError: bind argument must be an instance of ... #5131

Closed
amarkowitz opened this issue Apr 6, 2018 · 3 comments
Closed

TypeError: bind argument must be an instance of ... #5131

amarkowitz opened this issue Apr 6, 2018 · 3 comments

Comments

@amarkowitz
Copy link

Environment

$> ruby -v
jruby 9.1.16.0 (2.3.3) 2018-02-21 8f3f95a Java HotSpot(TM) 64-Bit Server VM 25.131-b11 on 1.8.0_131-b11 +jit [darwin-x86_64]
$> uname -a
Darwin <computer_name> 16.7.0 Darwin Kernel Version 16.7.0: Tue Jan 30 11:27:06 PST 2018; root:xnu-3789.73.11~1/RELEASE_X86_64 x86_64

failure test case:

# Gemfile
source 'https://rubygems.org'    

    gem 'mocha',            '1.4.0'
    gem 'minitest',         '5.11.3'
    gem 'minitest-stub_any_instance', '1.0.2'
# test_foo.rb
require "minitest/autorun"
require 'minitest/stub_any_instance'
require 'mocha/minitest'

class Stubber; def foo; 'bar'; end; end

class FooTest < MiniTest::Spec
  i_suck_and_my_tests_are_order_dependent!

  it 'sets us up for failure' do
    Stubber.any_instance.stubs(:foo)
  end

  def bad_stuff
    original_define = Stubber.instance_method(:foo)

    Stubber.stub_any_instance(:foo, proc { |*args| original_define.bind(self).call(*args) }) { Stubber.new.foo }
  end

  it "does not fail with an error" do
    bad_stuff
  end

  it "fails with an error" do
    bad_stuff
  end
end
# run with the following command
$> gem install bundler && bundle install && ruby test_foo.rb

Expected Behavior

No errors.

Actual Behavior

TypeError: bind argument must be an instance of Stubber
    org/jruby/RubyUnboundMethod.java:105:in `bind'
    test_foo.rb:17:in `block in bad_stuff'
    org/jruby/RubyBasicObject.java:1728:in `instance_exec'

Behavior from various versions of jruby and MRI ruby

# jruby-9.1.16.0
$> ruby test_foo.rb
Run options: --seed 51722

# Running:

..E

Finished in 0.021569s, 139.0873 runs/s, 0.0000 assertions/s.

  1) Error:
FooTest#test_0003_fails with an error:
TypeError: bind argument must be an instance of Stubber
    org/jruby/RubyUnboundMethod.java:105:in `bind'
    test_foo.rb:17:in `block in bad_stuff'
    org/jruby/RubyBasicObject.java:1728:in `instance_exec'

3 runs, 0 assertions, 0 failures, 1 errors, 0 skips
# jruby-9.1.13.0
$> ruby test_foo.rb
Run options: --seed 38186

# Running:

..E

Finished in 0.060058s, 49.9516 runs/s, 0.0000 assertions/s.

  1) Error:
FooTest#test_0003_fails with an error:
TypeError: bind argument must be an instance of Stubber
    org/jruby/RubyUnboundMethod.java:105:in `bind'
    test_foo.rb:17:in `block in bad_stuff'
    org/jruby/RubyBasicObject.java:1728:in `instance_exec'

3 runs, 0 assertions, 0 failures, 1 errors, 0 skips
# MRI Ruby 2.4.4
$> ruby test_foo.rb
Run options: --seed 46128

# Running:

...

Finished in 0.001754s, 1710.3763 runs/s, 0.0000 assertions/s.

3 runs, 0 assertions, 0 failures, 0 errors, 0 skips
# MRI Ruby 2.3.3
$> ruby test_foo.rb
Run options: --seed 31182

# Running:

...

Finished in 0.001718s, 1746.2165 runs/s, 0.0000 assertions/s.

3 runs, 0 assertions, 0 failures, 0 errors, 0 skips

possibly related to #2519

@amarkowitz amarkowitz changed the title TypeError: bind argument must be an instance of NNN TypeError: bind argument must be an instance of ... Apr 6, 2018
@headius
Copy link
Member

headius commented Apr 12, 2018

Thanks for the test case! Looking into it for a few minutes this evening...

@headius
Copy link
Member

headius commented Apr 12, 2018

Oddly enough, the following change appears to fix the problem. It seems to match similar logic in MRI that accesses owner, as well as logic in our own RubyModule that does the same.

I'll push the change and hopefully you can confirm it's working for you tonight.

If we can narrow this down to a piece of code that does not depend on minitest, it would be great to add a spec to https://github.com/ruby/spec!

headius added a commit that referenced this issue Apr 12, 2018
@headius headius closed this as completed Apr 12, 2018
@headius headius added this to the JRuby 9.1.17.0 milestone Apr 12, 2018
@amarkowitz
Copy link
Author

amarkowitz commented Apr 12, 2018

Heya, @headius! I spent a little time this evening and I think I was able to boil down the test case a bit. The initial test case for some reason doesn't fail until the second time the instance_method is attempted to be bound, however here's what I'm hoping is a simpler test case that exhibits the problem more concisely. Does this failing example line up with the fix you made?:

PrependedModule = Class.new(Module)

class Stubber; def foo; 'bar'; end; end

original_define = Stubber.instance_method(:foo)
original_define.bind(Stubber.new).call # this call works fine

Stubber.__send__(:prepend, PrependedModule.new)

original_define = Stubber.instance_method(:foo)
original_define.bind(Stubber.new).call # this call fails

output

Uncaught exception: bind argument must be an instance of Stubber
	org/jruby/RubyUnboundMethod.java:105:in `bind'
	/Users/user/dev/helltool/test/test_foo.rb:11:in `<main>'
	org/jruby/debug/RubyDebugger.java:209:in `debug_load'
	org/jruby/RubyKernel.java:973:in `load'

EDIT: I'm running under intellij and for some reason I get the above error in my output console when executing under the integrated debugger.

If I run via intellij without the integrated debugger I receive the following output:

TypeError: bind argument must be an instance of Stubber
    bind at org/jruby/RubyUnboundMethod.java:105
  <main> at /Users/user/dev/helltool/test/test_foo.rb:11
    load at org/jruby/RubyKernel.java:973
  <main> at -e:1

Process finished with exit code 1

Top 2 frames are the same for both failure runs for me (debugger and no debugger, but running through the IDE).

That said, It is unclear to me that when running the above code example via command line I receive no output. I would expect the same exception in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants