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 the8217+
# compile-time dispatch path stops finding it. cls_find_method8218+
# then walks to the parent (or returns -1), so `c.meth` after8219+
# `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 = -18223+
ki_u = 08224+
while ki_u < cur_names_u.length8225+
if cur_names_u[ki_u] == name8226+
hit_u = ki_u8227+
ki_u = cur_names_u.length8228+
else8229+
ki_u = ki_u + 18230+
end8231+
end8232+
if hit_u >= 08233+
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.length8240+
cur_pempty_u.push("")8241+
end8242+
cur_names_u.delete_at(hit_u)8243+
if hit_u < cur_params_u.length8244+
cur_params_u.delete_at(hit_u)8245+
end8246+
if hit_u < cur_ptypes_u.length8247+
cur_ptypes_u.delete_at(hit_u)8248+
end8249+
if hit_u < cur_returns_u.length8250+
cur_returns_u.delete_at(hit_u)8251+
end8252+
if hit_u < cur_bodies_u.length8253+
cur_bodies_u.delete_at(hit_u)8254+
end8255+
if hit_u < cur_defaults_u.length8256+
cur_defaults_u.delete_at(hit_u)8257+
end8258+
if hit_u < cur_pempty_u.length8259+
cur_pempty_u.delete_at(hit_u)8260+
end8261+
@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 + 18264+
@cls_meth_ptypes[ci] = cur_ptypes_u.join("|")8265+
@cls_meth_ptypes_version = @cls_meth_ptypes_version + 18266+
@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+
end8273+
end82168274
end82178275
82188276
spinel_codegen.rbmodifiedView file
Loading diff…
test/undef_method_raises.rbaddedView file
Loading diff…
test/undef_method_raises.rb.expectedaddedView file
Loading diff…