Commit 662525bb

662525bbe536Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(analyze,codegen): undef removes the method and raises NoMethodError (#717)

undef <m> was recorded in @undef_class_idx / @undef_method but the method stayed in @cls_meth_names; subsequent c.m calls dispatched to the original body, so class C; def m; "x"; end; undef m; end; C.new.m printed "x" instead of raising.

Two changes:

  • collect_class_method_undef splices the method out of all parallel @cls_meth_* arrays (params / ptypes / returns / bodies / defaults / ptypes_empty) so cls_find_method walks past it.
  • The obj-recv dispatch arm in compile_call walks the receiver's class + parent chain against (@undef_class_idx, @undef_method) before any other resolution. When a match is found, the call site emits sp_raise_cls("NoMethodError", "undefined method 'X' for "), letting a surrounding rescue catch it.

Test: test/undef_method_raises.rb covers the same-class undef and the child-class shadowing-parent shape (Child#hello undef'd while P#hello is still callable on a P instance).

461 tests pass; optcarrot checksum 59662.

Closes #717

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
662525bbe536
Parent
bb63828682c8

spinel_analyze.rbmodifiedView file

@@ -8213,6 +8213,64 @@
82138213 def collect_class_method_undef(ci, name)
82148214 @undef_class_idx.push(ci)
82158215 @undef_method.push(name)
8216+ # Remove the method from this class's method tables so the
8217+ # compile-time dispatch path stops finding it. cls_find_method
8218+ # then walks to the parent (or returns -1), so `c.meth` after
8219+ # `undef meth` raises NoMethodError just like CRuby. Issue #717.
8220+ if @cls_meth_names[ci] != ""
8221+ cur_names_u = @cls_meth_names[ci].split(";")
8222+ hit_u = -1
8223+ ki_u = 0
8224+ while ki_u < cur_names_u.length
8225+ if cur_names_u[ki_u] == name
8226+ hit_u = ki_u
8227+ ki_u = cur_names_u.length
8228+ else
8229+ ki_u = ki_u + 1
8230+ end
8231+ end
8232+ if hit_u >= 0
8233+ cur_params_u = @cls_meth_params[ci].split("|")
8234+ cur_ptypes_u = @cls_meth_ptypes[ci].split("|")
8235+ cur_returns_u = @cls_meth_returns[ci].split(";")
8236+ cur_bodies_u = @cls_meth_bodies[ci].split(";")
8237+ cur_defaults_u = @cls_meth_defaults[ci].split("|")
8238+ cur_pempty_u = @cls_meth_ptypes_empty[ci].split("|")
8239+ while cur_pempty_u.length < cur_names_u.length
8240+ cur_pempty_u.push("")
8241+ end
8242+ cur_names_u.delete_at(hit_u)
8243+ if hit_u < cur_params_u.length
8244+ cur_params_u.delete_at(hit_u)
8245+ end
8246+ if hit_u < cur_ptypes_u.length
8247+ cur_ptypes_u.delete_at(hit_u)
8248+ end
8249+ if hit_u < cur_returns_u.length
8250+ cur_returns_u.delete_at(hit_u)
8251+ end
8252+ if hit_u < cur_bodies_u.length
8253+ cur_bodies_u.delete_at(hit_u)
8254+ end
8255+ if hit_u < cur_defaults_u.length
8256+ cur_defaults_u.delete_at(hit_u)
8257+ end
8258+ if hit_u < cur_pempty_u.length
8259+ cur_pempty_u.delete_at(hit_u)
8260+ end
8261+ @cls_meth_names[ci] = cur_names_u.join(";")
8262+ @cls_meth_params[ci] = cur_params_u.join("|")
8263+ @cls_meth_params_version = @cls_meth_params_version + 1
8264+ @cls_meth_ptypes[ci] = cur_ptypes_u.join("|")
8265+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
8266+ @cls_meth_returns[ci] = cur_returns_u.join(";")
8267+ @cls_meth_bodies[ci] = cur_bodies_u.join(";")
8268+ @cls_meth_defaults[ci] = cur_defaults_u.join("|")
8269+ @cls_meth_ptypes_empty[ci] = cur_pempty_u.join("|")
8270+ @cls_meth_idx_cache = {}
8271+ @cls_meth_return_cache = {}
8272+ end
8273+ end
82168274 end
82178275
82188276