From 57747cca9adc468ec72bd2361e18a72a8b4ff61d Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 15:36:18 +0200 Subject: [PATCH 1/7] vm: fix post-splat arg binding, zsuper param forwarding, kw error order Three MRI-conformance fixes in the argument-binding / super machinery: - Post-*splat positional params (def m(a,*b,c)) were never bound: the splat greedily consumed the tail and post params stayed nil. Bind post params from the tail of the argument list and reserve them out of the splat, and enforce their required arity via iseqRequiredPositional so m(1) on def m(a,*b,c) raises "expected 2+". - Implicit `super` (zsuper) forwarded the ORIGINAL call arguments. MRI forwards the CURRENT values of the method's formal parameters, so a reassigned local, a filled-in optional/keyword default, and splat/post rebinding are all reflected. Reconstruct the argument list from the parameter local slots (new zsuperArgs helper). - A missing required keyword is now reported before an unknown one, as MRI does: m(b: 1) on def m(a:) raises "missing keyword: :a". super_spec 39->52 pass (13->1 fail), method_spec 26->32, keyword_arguments 9->10. New regression tests in language_def_residuals_test.go. Co-Authored-By: Claude Opus 4.8 --- internal/vm/language_def_residuals_test.go | 78 +++++++++++++ internal/vm/vm.go | 122 ++++++++++++++++----- 2 files changed, 172 insertions(+), 28 deletions(-) create mode 100644 internal/vm/language_def_residuals_test.go diff --git a/internal/vm/language_def_residuals_test.go b/internal/vm/language_def_residuals_test.go new file mode 100644 index 00000000..d7767d05 --- /dev/null +++ b/internal/vm/language_def_residuals_test.go @@ -0,0 +1,78 @@ +package vm_test + +import ( + "strings" + "testing" +) + +// TestLanguageDefResiduals covers language-def/method/super/keyword-argument +// residuals fixed together: post-*splat positional binding, implicit `super` +// (zsuper) re-reading the CURRENT parameter locals, and the missing-before- +// unknown keyword-error ordering. All expectations verified against MRI 4.0. +func TestLanguageDefResiduals(t *testing.T) { + cases := []struct{ src, want string }{ + // --- post-splat positional binding (def m(a,*b,c)) --- + {`def m(a,*b,c); [a,b,c]; end; p m(1,2,3,4)`, "[1, [2, 3], 4]\n"}, + {`def m(a,*b,c); [a,b,c]; end; p m(1,2)`, "[1, [], 2]\n"}, + // two post params, empty middle + {`def m(a,*b,c,d); [a,b,c,d]; end; p m(1,2,3)`, "[1, [], 2, 3]\n"}, + // leading optional + splat still works (no post) + {`def m(a,b=9,*c); [a,b,c]; end; p m(1)`, "[1, 9, []]\n"}, + + // --- zsuper: forwards CURRENT parameter values --- + // optional param with a default is passed to the parent + {`class A; def m(a,b,c); [a,b,c]; end; end + class B < A; def m(a,b,c=30); super; end; end + p B.new.m(1,2)`, "[1, 2, 30]\n"}, + // a reassigned *rest is forwarded with modifications + {`class F; def m(*a); a; end; end + class G < F; def m(*a); a[0]=99; super; end; end + p G.new.m(1,2)`, "[99, 2]\n"}, + // keyword defaults are forwarded even when not originally passed + {`class D; def m(a:1,b:2); [a,b]; end; end + class E < D; def m(a:10,b:20); super; end; end + p E.new.m(a:5)`, "[5, 20]\n"}, + // post-splat params forwarded in order by zsuper + {`class A; def m(a,*b,c); [a,b,c]; end; end + class B < A; def m(a,*b,c); super; end; end + p B.new.m(1,2,3,4)`, "[1, [2, 3], 4]\n"}, + // a plain unmodified super is unchanged (regression) + {`class A; def m(a,b); [a,b]; end; end + class B < A; def m(a,b); super; end; end + p B.new.m(7,8)`, "[7, 8]\n"}, + // **kwrest forwarded by zsuper + {`class A; def m(**k); k; end; end + class B < A; def m(**k); super; end; end + p B.new.m(x:1,y:2)`, "{x: 1, y: 2}\n"}, + } + for _, tc := range cases { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + } +} + +// TestKeywordAndArityErrors covers the keyword/arity error paths: a missing +// required keyword is reported before an unknown one, and the post-splat arity +// minimum is enforced. +func TestKeywordAndArityErrors(t *testing.T) { + cases := []struct{ src, wantMsg string }{ + // missing required keyword takes precedence over an unknown one + {`def m(a:); a; end; m(b: 1)`, "missing keyword: :a"}, + {`def m(a:, kw:); [a,kw]; end; m(x: 1)`, "missing keywords: :a, :kw"}, + // unknown keyword still raised when nothing is missing + {`def m(a:); a; end; m(a: 1, b: 2)`, "unknown keyword: :b"}, + // post-splat arity minimum: def m(a,*b,c) needs at least 2 args + {`def m(a,*b,c); [a,b,c]; end; m(1)`, "given 1, expected 2+"}, + } + for _, tc := range cases { + err := runErr(t, tc.src) + if err == nil { + t.Errorf("src=%q: expected error %q, got nil", tc.src, tc.wantMsg) + continue + } + if !strings.Contains(err.Error(), tc.wantMsg) { + t.Errorf("src=%q\n got=%q\nwant substring %q", tc.src, err.Error(), tc.wantMsg) + } + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 631bec08..6ccc1c78 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -993,6 +993,19 @@ func (vm *VM) bindKeywords(iseq *bytecode.ISeq, args *[]object.Value) *object.Ha for _, kn := range iseq.KwNames { valid[object.Symbol(kn)] = true } + // MRI reports a missing REQUIRED keyword before an unknown one: m(b: 1) on + // def m(a:) raises "missing keyword: :a", not "unknown keyword: :b". + var missing []string + for i, kn := range iseq.KwNames { + if iseq.KwRequired[i] { + if _, ok := kwargs.Get(object.SymVal(kn)); !ok { + missing = append(missing, ":"+kn) + } + } + } + if len(missing) > 0 { + raise("ArgumentError", "missing keyword%s: %s", plural(len(missing)), strings.Join(missing, ", ")) + } // With a **rest param, surplus keywords are captured rather than rejected. if iseq.KwRestSlot < 0 { var unknown []string @@ -1006,17 +1019,6 @@ func (vm *VM) bindKeywords(iseq *bytecode.ISeq, args *[]object.Value) *object.Ha raise("ArgumentError", "unknown keyword%s: %s", plural(len(unknown)), strings.Join(unknown, ", ")) } } - var missing []string - for i, kn := range iseq.KwNames { - if iseq.KwRequired[i] { - if _, ok := kwargs.Get(object.SymVal(kn)); !ok { - missing = append(missing, ":"+kn) - } - } - } - if len(missing) > 0 { - raise("ArgumentError", "missing keyword%s: %s", plural(len(missing)), strings.Join(missing, ", ")) - } return kwargs } @@ -1045,6 +1047,46 @@ type frameMethod struct { // `&b`, while `yield` inside the proc still reaches the block captured where the // proc was defined (block). Passing blockArg == block preserves the old // behaviour everywhere it is not deliberately split. +// zsuperArgs reconstructs the argument list an implicit `super` (bare `super`, +// no parentheses) forwards to the parent method. MRI passes the CURRENT values +// of the calling method's formal parameters — reading the local slots — rather +// than the arguments the method was originally called with, so any reassignment +// in the body, a filled-in optional or keyword default, and splat/post +// rebinding are all reflected. Positional params contribute their slot value +// (the splat slice is spliced in place); keyword params and any **kwrest are +// gathered into a trailing hash, appended only when non-empty. +func zsuperArgs(iseq *bytecode.ISeq, env *Env) []object.Value { + out := make([]object.Value, 0, len(iseq.Params)+1) + for i := 0; i < len(iseq.Params); i++ { + if i == iseq.SplatIndex { + if arr, ok := env.slots[i].(*object.Array); ok { + out = append(out, arr.Elems...) + } + continue + } + out = append(out, env.slots[i]) + } + if len(iseq.KwNames) > 0 || iseq.KwRestSlot >= 0 { + h := object.NewHash() + base := len(iseq.Params) + for i, kn := range iseq.KwNames { + h.Set(object.SymVal(kn), env.slots[base+i]) + } + if iseq.KwRestSlot >= 0 { + if kr, ok := env.slots[iseq.KwRestSlot].(*object.Hash); ok { + for _, k := range kr.Keys { + v, _ := kr.Get(k) + h.Set(k, v) + } + } + } + if h.Len() > 0 { + out = append(out, h) + } + } + return out +} + func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, definee *RClass, methodName string, parentEnv *Env, block, selfBlock, blockArg *Proc, methodLexScope *RClass) (execResult object.Value) { // Determine this frame's __method__/__callee__ pair before any nested call can // disturb pendingMethodCtx: a block inherits the pair captured on the block @@ -1065,15 +1107,18 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, if len(iseq.KwNames) > 0 || iseq.KwRestSlot >= 0 { kwargs = vm.bindKeywords(iseq, &args) } - if len(args) < iseq.NumRequired || (iseq.SplatIndex < 0 && len(args) > len(iseq.Params)) { + // minReq is the true minimum arity: the leading required params plus any + // required "post" params after a *splat (def m(a,*b,c) requires 2, not 1). + minReq := iseqRequiredPositional(iseq) + if len(args) < minReq || (iseq.SplatIndex < 0 && len(args) > len(iseq.Params)) { var expected string switch { case iseq.SplatIndex >= 0: - expected = fmt.Sprintf("%d+", iseq.NumRequired) - case iseq.NumRequired == len(iseq.Params): - expected = fmt.Sprintf("%d", iseq.NumRequired) + expected = fmt.Sprintf("%d+", minReq) + case minReq == len(iseq.Params): + expected = fmt.Sprintf("%d", minReq) default: - expected = fmt.Sprintf("%d..%d", iseq.NumRequired, len(iseq.Params)) + expected = fmt.Sprintf("%d..%d", minReq, len(iseq.Params)) } raise("ArgumentError", "wrong number of arguments (given %d, expected %s)", len(args), expected) } @@ -1089,16 +1134,29 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, } if iseq.SplatIndex >= 0 { si := iseq.SplatIndex - nbind := len(args) - if nbind > si { - nbind = si + // Params after the splat ("post" params, e.g. c in def m(a,*b,c)) are + // always required and bind from the TAIL of the argument list; the splat + // captures whatever is left in the middle. + npost := len(iseq.Params) - si - 1 + avail := len(args) - npost // args available to the leading params + splat + if avail < 0 { + avail = 0 + } + nbind := si + if nbind > avail { + nbind = avail } copy(env.slots[:nbind], args[:nbind]) rest := []object.Value{} - if len(args) > si { - rest = append(rest, args[si:]...) + if avail > si { + rest = append(rest, args[si:avail]...) } env.slots[si] = object.NewArrayFromSlice(rest) + for j := 0; j < npost; j++ { + if srcIdx := len(args) - npost + j; srcIdx >= 0 && srcIdx < len(args) { + env.slots[si+1+j] = args[srcIdx] + } + } } else { copy(env.slots, args) } @@ -1734,13 +1792,21 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, // MRI forbids implicit-argument super from a define_method body. raise("RuntimeError", "implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly.") } - superArgs = homeSuperArgs - // Keyword arguments were peeled off args into env.kwargs on entry; - // re-attach them as the trailing hash so bare super forwards them too. - // (Only the method's own frame carries kwargs; a block forwards the - // home method's positional args as captured.) - if selfBlock == nil && env.kwargs != nil && len(env.kwargs.Keys) > 0 { - superArgs = append(append([]object.Value(nil), homeSuperArgs...), env.kwargs) + if selfBlock == nil { + // zsuper: MRI forwards the CURRENT values of the method's formal + // parameters, not the original argument list. This reflects any + // reassignment in the body, filled-in optional/keyword defaults, + // and splat/post rebinding. + superArgs = zsuperArgs(iseq, env) + } else { + superArgs = homeSuperArgs + // Keyword arguments were peeled off args into env.kwargs on entry; + // re-attach them as the trailing hash so bare super forwards them + // too. (A block forwards the home method's positional args as + // captured.) + if env.kwargs != nil && len(env.kwargs.Keys) > 0 { + superArgs = append(append([]object.Value(nil), homeSuperArgs...), env.kwargs) + } } } else { superArgs = make([]object.Value, in.A) From 9f6cbb612354cd445587c33e78809b2319e30fe9 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 15:39:47 +0200 Subject: [PATCH 2/7] vm: define initialize/respond_to_missing? hooks as private (MRI) MRI always defines a small set of core hook methods with private visibility, regardless of the current default: initialize, initialize_copy, initialize_clone, initialize_dup, and respond_to_missing?. Mark them private in OpDefineMethod. language/def_spec 47->52 pass. Regression tests added. Co-Authored-By: Claude Opus 4.8 --- internal/vm/language_def_residuals_test.go | 23 ++++++++++++++++++++++ internal/vm/vm.go | 16 +++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/internal/vm/language_def_residuals_test.go b/internal/vm/language_def_residuals_test.go index d7767d05..6b905511 100644 --- a/internal/vm/language_def_residuals_test.go +++ b/internal/vm/language_def_residuals_test.go @@ -52,6 +52,29 @@ func TestLanguageDefResiduals(t *testing.T) { } } +// TestAlwaysPrivateMethodDefs covers the core hook methods MRI always defines +// with private visibility regardless of the surrounding default. +func TestAlwaysPrivateMethodDefs(t *testing.T) { + cases := []struct{ src, want string }{ + {`class C; def initialize; end; def initialize_copy(o); end + def initialize_dup(o); end; def initialize_clone(o); end + def respond_to_missing?(a,b); end; def normal; end; end + p C.private_instance_methods(false).sort`, + "[:initialize, :initialize_clone, :initialize_copy, :initialize_dup, :respond_to_missing?]\n"}, + // a normal method is still public + {`class C; def initialize; end; def normal; end; end + p C.public_instance_methods(false).include?(:normal)`, "true\n"}, + // reopening a class with a normal def stays public + {`class C; end; class C; def foo; end; end + p C.public_instance_methods(false).include?(:foo)`, "true\n"}, + } + for _, tc := range cases { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + } +} + // TestKeywordAndArityErrors covers the keyword/arity error paths: a missing // required keyword is reported before an unknown one, and the post-splat arity // minimum is enforced. diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 6ccc1c78..78a12b83 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -1022,6 +1022,17 @@ func (vm *VM) bindKeywords(iseq *bytecode.ISeq, args *[]object.Value) *object.Ha return kwargs } +// alwaysPrivateMethodName reports whether a method name is one MRI defines with +// private visibility unconditionally (the object-initialization and +// respond-to-missing hooks), independent of the surrounding default visibility. +func alwaysPrivateMethodName(name string) bool { + switch name { + case "initialize", "initialize_copy", "initialize_clone", "initialize_dup", "respond_to_missing?": + return true + } + return false +} + // plural returns "s" when n > 1, for "keyword"/"keywords" in error messages. func plural(n int) string { if n > 1 { @@ -1702,6 +1713,11 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, if definee.funcMode { m.vis = visPrivate } + // A handful of core hook methods are always defined private, + // regardless of the surrounding default visibility (MRI). + if alwaysPrivateMethodName(name) { + m.vis = visPrivate + } definee.methods[name] = m if definee.funcMode { sm := *m From 0b35ad19e120321cad4c129b3d98c44b78f62085 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 15:48:57 +0200 Subject: [PATCH 3/7] vm: a def inside a running method body is always public (MRI) The class default visibility (set by a bare `private`/`public` in the class body) persists on the RClass and was wrongly applied to defs executed later inside a method body. In MRI a `def` run inside a method invocation is always public regardless of the class default; only defs written directly in a class/module body (or a block at that level) follow the default. Detect the method-body frame (selfBlock == nil, non-empty methodName) and force public there. Fixes the "resets visibility to public for nested definitions | in Class.new" def_spec example. Regression tests added. Co-Authored-By: Claude Opus 4.8 --- internal/vm/language_def_residuals_test.go | 11 +++++++++++ internal/vm/vm.go | 14 +++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/internal/vm/language_def_residuals_test.go b/internal/vm/language_def_residuals_test.go index 6b905511..af40e105 100644 --- a/internal/vm/language_def_residuals_test.go +++ b/internal/vm/language_def_residuals_test.go @@ -67,6 +67,17 @@ func TestAlwaysPrivateMethodDefs(t *testing.T) { // reopening a class with a normal def stays public {`class C; end; class C; def foo; end; end p C.public_instance_methods(false).include?(:foo)`, "true\n"}, + // a def executed INSIDE a method body is always public, even when the + // class default visibility was set to private in the class body + {`class C; private; def do_def; def new_def; 1; end; end; end + o = C.new; o.send(:do_def) + p C.public_instance_methods(false).include?(:new_def)`, "true\n"}, + // the private default still governs the directly-written method + {`class C; private; def do_def; def new_def; 1; end; end; end + p C.private_instance_methods(false).include?(:do_def)`, "true\n"}, + // a def inside a block at class-body level DOES follow the class default + {`class D; private; [1].each { def blk_def; 2; end }; end + p D.private_instance_methods(false).include?(:blk_def)`, "true\n"}, } for _, tc := range cases { if got := eval(t, tc.src); got != tc.want { diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 78a12b83..9bfaaba6 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -1708,9 +1708,17 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, // the new Method's own vis governs (MRI: redefining resets to the // current default visibility). delete(definee.visOverrides, name) - // In module_function (no-arg) mode, a def is private as an instance - // method but public as the module/singleton method. - if definee.funcMode { + // A `def` executed INSIDE a running method body is always public in + // MRI, regardless of the class's persisted default visibility (which + // only governs defs written directly in a class/module body). A block + // frame (selfBlock != nil), a class/module body, and the top level + // (methodName == "") all keep the class default. In module_function + // (no-arg) mode, a def is private as an instance method but public as + // the module/singleton method. + switch { + case selfBlock == nil && methodName != "": + m.vis = visPublic + case definee.funcMode: m.vis = visPrivate } // A handful of core hook methods are always defined private, From 78561b1fa29162bb314cd4981b2b1af8a6a47d9b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 15:51:18 +0200 Subject: [PATCH 4/7] vm: splat of an object whose #to_a returns nil wraps it (MRI) `*obj` coercion (splatToArray) raised TypeError when obj#to_a returned nil. MRI treats a nil result as "no array conversion" and wraps the object in a one-element array, exactly as for an object without #to_a; a non-nil non-Array result stays a TypeError. Fixes 8 language/method_spec errors (splatted-object element/attribute assignment). Test added. Co-Authored-By: Claude Opus 4.8 --- internal/vm/aot_runtime.go | 6 ++++++ internal/vm/language_def_residuals_test.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/internal/vm/aot_runtime.go b/internal/vm/aot_runtime.go index 674bea44..49ffcb92 100644 --- a/internal/vm/aot_runtime.go +++ b/internal/vm/aot_runtime.go @@ -52,6 +52,12 @@ func (vm *VM) splatToArray(v object.Value) object.Value { if a, ok := r.(*object.Array); ok { return a } + // MRI treats a nil #to_a result as "no conversion available" and wraps the + // object in a one-element Array, exactly as for an object without #to_a; a + // non-nil, non-Array result is the error. + if object.IsNil(r) { + return object.NewArray(v) + } raise("TypeError", "can't convert %s to Array (%s#to_a gives %s)", vm.classOf(v).name, vm.classOf(v).name, vm.classOf(r).name) } diff --git a/internal/vm/language_def_residuals_test.go b/internal/vm/language_def_residuals_test.go index af40e105..81c13708 100644 --- a/internal/vm/language_def_residuals_test.go +++ b/internal/vm/language_def_residuals_test.go @@ -86,6 +86,20 @@ func TestAlwaysPrivateMethodDefs(t *testing.T) { } } +// TestSplatToArrayNilToA covers the splat (*obj) coercion when the object's +// #to_a returns nil: MRI wraps the object in a one-element array rather than +// raising, while a non-nil non-Array #to_a result is still a TypeError. +func TestSplatToArrayNilToA(t *testing.T) { + if got := eval(t, `class M; def to_a; nil; end; end + a = [*M.new]; p a.size; p a[0].class`); got != "1\nM\n" { + t.Errorf("nil to_a splat: got %q", got) + } + err := runErr(t, `class N; def to_a; 5; end; end; [*N.new]`) + if err == nil || !strings.Contains(err.Error(), "can't convert N to Array (N#to_a gives Integer)") { + t.Errorf("non-array to_a: got %v", err) + } +} + // TestKeywordAndArityErrors covers the keyword/arity error paths: a missing // required keyword is reported before an unknown one, and the post-splat arity // minimum is enforced. From 291dea8543c18db12b94955ecc4227c1f5ebe20e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 15:56:04 +0200 Subject: [PATCH 5/7] vm: super past an undef tombstone fires method_missing, no crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit invokeSuper invoked an `undef_method` tombstone (nil body) when a super call's next definition was an undef blocker, crashing with a nil-pointer deref. Treat an undefined tombstone as a non-callable blocker: stop the ancestor search there, and — matching MRI — route a failed super through a user-defined method_missing (the default one on BasicObject still yields the "super: no superclass method" NoMethodError, preserving that message). Fixes language/super_spec "calls method_missing when a superclass method is not found" (was a process crash). Test added. Co-Authored-By: Claude Opus 4.8 --- internal/vm/language_def_residuals_test.go | 22 +++++++++++++++ internal/vm/vm.go | 32 ++++++++++++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/internal/vm/language_def_residuals_test.go b/internal/vm/language_def_residuals_test.go index 81c13708..023bafcb 100644 --- a/internal/vm/language_def_residuals_test.go +++ b/internal/vm/language_def_residuals_test.go @@ -100,6 +100,28 @@ func TestSplatToArrayNilToA(t *testing.T) { } } +// TestSuperFallsBackToMethodMissing covers a `super` that finds no callable +// superclass method: it must not invoke an `undef` tombstone (which has a nil +// body and crashed), and it routes to a user-defined method_missing while a +// plain failure still raises the super-specific NoMethodError. +func TestSuperFallsBackToMethodMissing(t *testing.T) { + // undef'd ancestor method + user method_missing: super reaches method_missing. + got := eval(t, `class A; undef_method :is_a?; end + class B < A + def is_a?(x); super; end + def method_missing(*a); "mm:#{a[0]}"; end + end + p B.new.is_a?(Hash)`) + if got != "\"mm:is_a?\"\n" { + t.Errorf("super->method_missing: got %q", got) + } + // no super method and only the default method_missing: super-specific error. + err := runErr(t, `class A; def f; super; end; end; A.new.f`) + if err == nil || !strings.Contains(err.Error(), "super: no superclass method 'f'") { + t.Errorf("plain super fail: got %v", err) + } +} + // TestKeywordAndArityErrors covers the keyword/arity error paths: a missing // required keyword is reported before an unknown one, and the post-splat arity // minimum is enforced. diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 9bfaaba6..f1cdc628 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -2441,13 +2441,21 @@ func (vm *VM) invokeSuper(self object.Value, definee *RClass, methodName string, break } } - if start >= 0 { + // Locate the next definition of methodName above definee. An `undef` + // tombstone (m.undefined) is a blocker: it is the "next definition" and it is + // not callable, so the search stops there and super falls through to + // method_missing / NoMethodError rather than reaching a still-defined ancestor + // above the blocker (and rather than crashing on its nil body). + var found *Method + switch { + case start >= 0: for _, k := range anc[start+1:] { if m, ok := k.methods[methodName]; ok { - return vm.invoke(m, self, args, blk) + found = m + break } } - } else if definee.metaOf != nil { + case definee.metaOf != nil: // definee is a metaclass: the current method was defined in a `class << self` // body, so its class-method super walks the metaclass chain // (# -> # -> ...). Each metaclass's method table @@ -2455,13 +2463,25 @@ func (vm *VM) invokeSuper(self object.Value, definee *RClass, methodName string, // class method. for k := definee.super; k != nil; k = k.super { if m, ok := k.methods[methodName]; ok { - return vm.invoke(m, self, args, blk) + found = m + break } } - } else if m := lookupSMethod(definee.super, methodName); m != nil { + default: // definee is the class itself, outside the receiver's ancestry: this is a // class-method super (def self.foo), so walk the singleton-method chain. - return vm.invoke(m, self, args, blk) + found = lookupSMethod(definee.super, methodName) + } + if found != nil && !found.undefined { + return vm.invoke(found, self, args, blk) + } + // No callable superclass method. MRI routes a failed super through + // method_missing: a user-defined one handles it, while the default (on + // BasicObject) reports the super-specific message below. Dispatch only a + // genuine override so the default path keeps that exact message. + if mm := lookupMethod(vm.classOf(self), "method_missing"); mm != nil && !mm.undefined && mm.owner != vm.cBasicObject { + mmArgs := append([]object.Value{object.SymVal(methodName)}, args...) + return vm.invoke(mm, self, mmArgs, blk) } raise("NoMethodError", "super: no superclass method '%s'", methodName) return object.NilV From fd1898453aed9665de947e7eef9a8200ba544e60 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 16:00:20 +0200 Subject: [PATCH 6/7] vm: super resolves through singleton methods and aliases (MRI) Two super-resolution fixes in invokeSuper / the frame's super anchor: - A `super` from a per-object singleton method (def obj.foo, or a define_method on the singleton class) walked ancestors(classOf(self)), which omits the object's singleton class, so it never reached the class-level definition ("super: no superclass method"). Start the ancestor walk from the object's singleton class when it has one; its super is the object's class, so the class definition is found next. - A `super` from an aliased method used the alias name (__callee__) rather than the method's original name (__method__), so it searched for the alias above the original owner and found nothing. Anchor super on fm.orig (the original name), matching MRI's "respects the original module a method is aliased from". language/super_spec 53->57 pass. Regression tests added. Co-Authored-By: Claude Opus 4.8 --- internal/vm/language_def_residuals_test.go | 30 ++++++++++++++++++++++ internal/vm/vm.go | 18 ++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/internal/vm/language_def_residuals_test.go b/internal/vm/language_def_residuals_test.go index 023bafcb..0360c725 100644 --- a/internal/vm/language_def_residuals_test.go +++ b/internal/vm/language_def_residuals_test.go @@ -122,6 +122,36 @@ func TestSuperFallsBackToMethodMissing(t *testing.T) { } } +// TestSuperResolution covers super from a per-object singleton method and from +// an aliased method: the singleton method reaches the class-level definition, +// and the alias anchors super on the method's original name/owner (MRI). +func TestSuperResolution(t *testing.T) { + cases := []struct{ src, want string }{ + // super from a `def obj.foo` singleton method reaches the class method + {`class Foo; def foobar(a); a << :foo; end; end + o = Foo.new + def o.foobar(a); a << :singleton; super; end + p o.foobar([])`, "[:singleton, :foo]\n"}, + // super from a define_method on the singleton class using an unbound method + {`fc = Class.new { def bar; "bar"; end } + m = Module.new { def bar; "super_" + super; end } + foo = fc.new + foo.singleton_class.define_method(:bar, m.instance_method(:bar)) + p foo.bar`, "\"super_bar\"\n"}, + // aliased method: super uses the ORIGINAL name, resolving above the alias's + // owner rather than looping on the alias name + {`class A1; def name; [:a1]; end; end + class A2 < A1; def name; [:a2] + super; end; end + class A3 < A2; alias_method :name3, :name; end + p A3.new.name3`, "[:a2, :a1]\n"}, + } + for _, tc := range cases { + if got := eval(t, tc.src); got != tc.want { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + } +} + // TestKeywordAndArityErrors covers the keyword/arity error paths: a missing // required keyword is reported before an unknown one, and the post-splat arity // minimum is enforced. diff --git a/internal/vm/vm.go b/internal/vm/vm.go index f1cdc628..86f589ec 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -1314,7 +1314,12 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, // `yield` and `return`: inside a method the anchor is the method itself; inside // a block the anchor is inherited from the enclosing block (so `super` nested // through several blocks still reaches the home method). - homeSuperName, homeSuperDefinee, homeSuperArgs := methodName, definee, args + // + // The anchor NAME is the method's ORIGINAL name (fm.orig), not the name it was + // invoked by: for an aliased method (`alias_method :name3, :name`), a bare + // `super` resolves the original `name` above the alias's owner, matching MRI — + // __method__, not __callee__. For a plain def the two coincide. + homeSuperName, homeSuperDefinee, homeSuperArgs := fm.orig, definee, args homeDmBody := false if selfBlock != nil { homeSuperName, homeSuperDefinee, homeSuperArgs = selfBlock.superName, selfBlock.superDefinee, selfBlock.superArgs @@ -2432,8 +2437,15 @@ func (vm *VM) invokeSuper(self object.Value, definee *RClass, methodName string, } // super resolves to the next definition of methodName after the current // method's owner (definee) in the receiver's ancestor chain — so it walks - // prepended and included modules, not just the superclass. - anc := vm.ancestors(vm.classOf(self)) + // prepended and included modules, not just the superclass. When the receiver + // carries a per-object singleton class, the chain begins there (its super is + // the object's class), so a `super` from a singleton method — `def obj.foo`, + // or define_method on the singleton class — reaches the class-level definition. + startClass := vm.classOf(self) + if sc := vm.objSingleton(self); sc != nil { + startClass = sc + } + anc := vm.ancestors(startClass) start := -1 for i, k := range anc { if k == definee { From 3f9c664f331a8e37d61484e4e5ebc3a8809efced Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 31 Aug 2026 16:10:37 +0200 Subject: [PATCH 7/7] vm: drop dead post-splat guard and stray block-frame kwargs reattach MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Coverage cleanup, no behaviour change on any tested path: - The `if avail < 0` guard in post-splat binding is unreachable — the arity check (minReq includes the post params) guarantees len(args) >= npost — so it is removed. - The bare-super block-frame branch no longer re-attaches env.kwargs: a block forwards the home method's captured args (as it always did before this cluster), while the method frame's keyword-aware reconstruction is the zsuperArgs path. This also restores the pre-change block behaviour. Co-Authored-By: Claude Opus 4.8 --- internal/vm/vm.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 86f589ec..b50e1d74 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -1149,10 +1149,10 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, // always required and bind from the TAIL of the argument list; the splat // captures whatever is left in the middle. npost := len(iseq.Params) - si - 1 - avail := len(args) - npost // args available to the leading params + splat - if avail < 0 { - avail = 0 - } + // args available to the leading params + splat, after reserving the post + // params. The arity check above (minReq includes npost) guarantees + // len(args) >= npost, so this is never negative. + avail := len(args) - npost nbind := si if nbind > avail { nbind = avail @@ -1828,14 +1828,12 @@ func (vm *VM) exec(iseq *bytecode.ISeq, self object.Value, args []object.Value, // and splat/post rebinding. superArgs = zsuperArgs(iseq, env) } else { + // A bare super inside a block forwards the home method's arguments + // as captured when the block was created (a block is transparent to + // super, like yield). The method frame's own reconstruction — which + // re-reads the current parameter locals, keywords included — is the + // selfBlock == nil path above. superArgs = homeSuperArgs - // Keyword arguments were peeled off args into env.kwargs on entry; - // re-attach them as the trailing hash so bare super forwards them - // too. (A block forwards the home method's positional args as - // captured.) - if env.kwargs != nil && len(env.kwargs.Keys) > 0 { - superArgs = append(append([]object.Value(nil), homeSuperArgs...), env.kwargs) - } } } else { superArgs = make([]object.Value, in.A)