diff --git a/test/ruby/test_super.rb b/test/ruby/test_super.rb index 8e973b0f7f..25bad2242a 100644 --- a/test/ruby/test_super.rb +++ b/test/ruby/test_super.rb @@ -759,4 +759,19 @@ class TestSuper < Test::Unit::TestCase inherited = inherited_class.new assert_equal 2, inherited.test # it may read index=1 while it should be index=2 end + + def test_super_in_basic_object + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + class ::BasicObject + def no_super + super() + rescue ::NameError + :ok + end + end + + assert_equal :ok, "[Bug #21694]".no_super + end; + end end diff --git a/vm_callinfo.h b/vm_callinfo.h index d85261aaf9..2cea9c5f02 100644 --- a/vm_callinfo.h +++ b/vm_callinfo.h @@ -305,6 +305,7 @@ struct rb_callcache { #define VM_CALLCACHE_BF IMEMO_FL_USER1 #define VM_CALLCACHE_SUPER IMEMO_FL_USER2 #define VM_CALLCACHE_REFINEMENT IMEMO_FL_USER3 +#define VM_CALLCACHE_INVALID_SUPER IMEMO_FL_USER6 enum vm_cc_type { cc_type_normal, // chained from ccs @@ -335,7 +336,7 @@ vm_cc_new(VALUE klass, *((struct rb_callable_method_entry_struct **)&cc->cme_) = (struct rb_callable_method_entry_struct *)cme; *((vm_call_handler *)&cc->call_) = call; - VM_ASSERT(RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS)); + VM_ASSERT(UNDEF_P(klass) || RB_TYPE_P(klass, T_CLASS) || RB_TYPE_P(klass, T_ICLASS)); switch (type) { case cc_type_normal: @@ -348,8 +349,10 @@ vm_cc_new(VALUE klass, break; } - if (cme->def->type == VM_METHOD_TYPE_ATTRSET || cme->def->type == VM_METHOD_TYPE_IVAR) { - vm_cc_attr_index_initialize(cc, INVALID_SHAPE_ID); + if (cme) { + if (cme->def->type == VM_METHOD_TYPE_ATTRSET || cme->def->type == VM_METHOD_TYPE_IVAR) { + vm_cc_attr_index_initialize(cc, INVALID_SHAPE_ID); + } } RB_DEBUG_COUNTER_INC(cc_new); @@ -401,6 +404,7 @@ vm_cc_cme(const struct rb_callcache *cc) { VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache)); VM_ASSERT(cc->call_ == NULL || // not initialized yet + UNDEF_P(cc->klass) || !vm_cc_markable(cc) || cc->cme_ != NULL); diff --git a/vm_insnhelper.c b/vm_insnhelper.c index e056b7889c..959a310401 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -5048,7 +5048,7 @@ vm_search_super_method(const rb_control_frame_t *reg_cfp, struct rb_call_data *c if (!klass) { /* bound instance method of module */ - cc = vm_cc_new(klass, NULL, vm_call_method_missing, cc_type_super); + cc = vm_cc_new(Qundef, NULL, vm_call_method_missing, cc_type_super); RB_OBJ_WRITE(reg_cfp->iseq, &cd->cc, cc); } else {