Skip to content

Commit c2f66f2

Browse files
committed
Fix edge case in findMethodInstanceContainer codeo
* Looks like we can have scenarios where 'self' for a closure executed in a module_eval context need not be the module but a different object. In that case, we still need to add the method to self => we need to get its metaclass first. * This fixes crasher in this case: ------------- require "minitest/autorun" describe "A" do it "should do something" do def foo end end end -------------
1 parent e5e5198 commit c2f66f2

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

core/src/main/java/org/jruby/ir/runtime/IRRuntimeHelpers.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,14 @@ public static RubyModule findInstanceMethodContainer(ThreadContext context, Dyna
735735
for (DynamicScope ds = currDynScope; ds != null; ) {
736736
IRScopeType scopeType = ds.getStaticScope().getScopeType();
737737
switch (ds.getEvalType()) {
738-
case MODULE_EVAL : return (RubyModule) self;
738+
// The most common use case here is where :
739+
// - a method is defined inside a closure
740+
// that is nested inside a module_eval.
741+
// here self = the module
742+
// - in the rare case where it is not (looks like it can
743+
// happen in some testing frameworks), we have to add
744+
// the method to self itself => its metaclass.
745+
case MODULE_EVAL : return self instanceof RubyModule ? (RubyModule) self : self.getMetaClass();
739746
case INSTANCE_EVAL: return self.getSingletonClass();
740747
case BINDING_EVAL : ds = ds.getParentScope(); break;
741748
case NONE:

0 commit comments

Comments
 (0)