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 new file mode 100644 index 00000000..0360c725 --- /dev/null +++ b/internal/vm/language_def_residuals_test.go @@ -0,0 +1,178 @@ +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) + } + } +} + +// 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"}, + // 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 { + t.Errorf("src=%q\n got=%q\nwant=%q", tc.src, got, tc.want) + } + } +} + +// 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) + } +} + +// 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) + } +} + +// 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. +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..b50e1d74 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,20 +1019,20 @@ 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 } +// 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 { @@ -1045,6 +1058,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 +1118,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 +1145,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 + // 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 } 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) } @@ -1245,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 @@ -1639,9 +1713,22 @@ 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, + // regardless of the surrounding default visibility (MRI). + if alwaysPrivateMethodName(name) { m.vis = visPrivate } definee.methods[name] = m @@ -1734,13 +1821,19 @@ 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 { + // 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 } } else { superArgs = make([]object.Value, in.A) @@ -2342,8 +2435,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 { @@ -2351,13 +2451,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 @@ -2365,13 +2473,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