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

Prepending a module to a module extended by a class gives NPE when calling super #4678

Closed
donv opened this issue Jun 19, 2017 · 5 comments
Closed
Milestone

Comments

@donv
Copy link
Member

donv commented Jun 19, 2017

jruby 9.1.12.0 (2.3.3) 2017-06-15 33c6439 Java HotSpot(TM) 64-Bit Server VM 25.112-b16 on 1.8.0_112-b16 +jit [darwin-x86_64]
Darwin Gratass.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64

Given the following code:

module A
  def self.included(clazz)
    clazz.extend ClassMethods
  end

  module ClassMethods
    def foo
      puts 'super'
    end
  end
end

class B
  include A
end

module C
  def foo
    super
  end
end
A::ClassMethods.prepend C

B.foo

JRuby 9.1.12.0 gives the following stack trace:

Unhandled Java exception: java.lang.NullPointerException
java.lang.NullPointerException: null
                      unresolvedSuper at org/jruby/ir/runtime/IRRuntimeHelpers.java:1014
  invokeSuper6:-unknown-super-target- at jruby_example.rb:19
                                  foo at jruby_example.rb:19
                                 call at org/jruby/internal/runtime/methods/CompiledIRMethod.java:155
                         cacheAndCall at org/jruby/runtime/callsite/CachingCallSite.java:318
                                 call at org/jruby/runtime/callsite/CachingCallSite.java:131
                    invokeOther10:foo at jruby_example.rb:24
                               <main> at jruby_example.rb:24
                  invokeWithArguments at java/lang/invoke/MethodHandle.java:627
                                 load at org/jruby/ir/Compiler.java:95
                            runScript at org/jruby/Ruby.java:827
                          runNormally at org/jruby/Ruby.java:746
                          runNormally at org/jruby/Ruby.java:764
                          runFromMain at org/jruby/Ruby.java:577
                        doRunFromMain at org/jruby/Main.java:417
                          internalRun at org/jruby/Main.java:305
                                  run at org/jruby/Main.java:232
                                 main at org/jruby/Main.java:204

With MRI 2.4.1 it prints "super".

@donv donv changed the title Prepending a module to a module extended by a class gives NPE Prepending a module to a module extended by a class gives NPE when calling super Jun 19, 2017
@donv
Copy link
Member Author

donv commented Jun 19, 2017

Workaround is to postpone the include call to after the prependcall.

Possible duplicate of #4531.

@headius
Copy link
Member

headius commented Mar 13, 2019

This no longer NPEs but still fails:

$ jruby blah.rb
NoMethodError: super: no superclass method `foo' for B:Class
Did you mean?  fork
  method_missing at org/jruby/RubyBasicObject.java:1708
             foo at blah.rb:19
          <main> at blah.rb:24

@headius headius added this to the JRuby 9.2.8.0 milestone Apr 9, 2019
@headius headius removed this from the JRuby 9.2.8.0 milestone Aug 1, 2019
@headius headius added this to the JRuby 9.2.10.0 milestone Oct 29, 2019
@headius
Copy link
Member

headius commented Feb 16, 2020

Slightly simpler example:

module X
  def foo
    puts "super"
  end
end

class B
  class << self
    include X
  end
end

module C
  def foo
    super
  end
end

X.prepend C

B.foo

@headius
Copy link
Member

headius commented Mar 26, 2020

doudou/flexmock#16 is another case of this; FlexMock prepends a proxy module onto the target class, which in that case is a class's class.

@enebo
Copy link
Member

enebo commented Aug 20, 2021

Fixed in PR #6777

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

3 participants