diff --git a/README.md b/README.md index 9db2e3dc..02301256 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,15 @@ block keyword params, begin-less rescue/ensure, and `!~` — on top of the pre-existing core (full object model, metaprogramming, Fiber/Thread, Marshal, regexp, the scientific bindings, and the js/wasm target) detailed below. +Ruby **4.0**'s implicit-parameter API is implemented: +`Binding#implicit_parameters`, `#implicit_parameter_get` and +`#implicit_parameter_defined?` read a block's `it` or `_1`..`_N` — the current +scope's only, never a parent's or a nested one's, which is the single flag MRI +distinguishes them by (`proc.c` v4.0.9:688). An implicit parameter is not a +local variable, so these are the way to reach one. `Thread#raise` takes 4.0's +`cause:` keyword too, chaining from the **caller's** `$!` rather than the +target thread's. + **Performance:** a 6-runtime comparative suite ([BENCHMARKS.md](BENCHMARKS.md)) pits rbgo and rbgo+AOT against MRI, MRI+YJIT, JRuby and TruffleRuby. The rbgo interpreter runs ~3–6× MRI on compute and at parity on I/O-bound work; diff --git a/internal/bytecode/bytecode.go b/internal/bytecode/bytecode.go index 06eb9943..9c2a1c70 100644 --- a/internal/bytecode/bytecode.go +++ b/internal/bytecode/bytecode.go @@ -360,8 +360,29 @@ type ISeq struct { BlockSlot int // slot of the &block param, or -1 NumLocals int // total local slots (params first, then assigns) Locals []string // local-variable names by slot (for Binding); "" for anonymous slots - Children []*ISeq // nested ISeqs (method bodies / class bodies defined here) - Super string // for a class body: the superclass name ("" → Object) + + // ImplicitParams names the slots holding a block's IMPLICIT parameters — + // `it`, or `_1`..`_N` — by slot, "" elsewhere. It is a SECOND channel beside + // Locals because the two answer different questions and 4.0 separated them: + // an implicit parameter is not a local variable (it is absent from + // Binding#local_variables and unreachable by #local_variable_get) and yet it + // has a name and a value, which Binding#implicit_parameters, + // #implicit_parameter_get and #implicit_parameter_defined? return. + // + // MRI keeps them in the same local table under IDs the program cannot spell: + // `it` under idItImplicit rather than idIt, and the numbered parameters under + // IDs rb_numparam_id_p recognises (proc.c v4.0.9:680-783). rbgo already + // blanked Locals for `it` to the same end; this records what was blanked + // instead of discarding it. + // + // It is DENSE and starts at slot 0: a block whose parameters the parser + // synthesised has no anonymous slot among them, so there is no gap to + // represent. Empty or nil for every other scope, and a slot past its end is + // an ordinary local. + ImplicitParams []string + + Children []*ISeq // nested ISeqs (method bodies / class bodies defined here) + Super string // for a class body: the superclass name ("" → Object) // Lines is the source map: which source line each instruction came from, // COMPRESSED to one entry per line CHANGE and kept sorted by PC. diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 63f46471..c8d4f318 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -77,6 +77,20 @@ type builder struct { curLine int lastLine int firstLine int + + // implicitParams records, by slot, the name of a block's IMPLICIT parameter + // — `it` or `_1`..`_N` — and "" elsewhere. See bytecode.ISeq.ImplicitParams: + // it keeps the names the public local table must not carry, instead of + // discarding them as blanking alone did. + implicitParams []string +} + +// markImplicitParam records slot i as holding the implicit parameter `name`. +func (b *builder) markImplicitParam(i int, name string) { + for len(b.implicitParams) <= i { + b.implicitParams = append(b.implicitParams, "") + } + b.implicitParams[i] = name } func newBuilder(name string, params []string) *builder { @@ -232,23 +246,24 @@ func (b *builder) localLookup(name string) (int, bool) { func (b *builder) build() *bytecode.ISeq { return &bytecode.ISeq{ - Name: b.name, - Insns: b.insns, - Consts: b.consts, - Names: b.names, - Params: b.params, - NumRequired: b.numRequired, - SplatIndex: b.splatIndex, - PostCount: b.postCount, - KwNames: b.kwNames, - KwRequired: b.kwRequired, - KwRestSlot: b.kwRestSlot, - BlockSlot: b.blockSlot, - NumLocals: len(b.locals), - Locals: b.locals, - Children: b.children, - Lines: b.lines, - FirstLine: b.firstLine, + Name: b.name, + Insns: b.insns, + Consts: b.consts, + Names: b.names, + Params: b.params, + NumRequired: b.numRequired, + SplatIndex: b.splatIndex, + PostCount: b.postCount, + KwNames: b.kwNames, + KwRequired: b.kwRequired, + KwRestSlot: b.kwRestSlot, + BlockSlot: b.blockSlot, + NumLocals: len(b.locals), + Locals: b.locals, + ImplicitParams: b.implicitParams, + Children: b.children, + Lines: b.lines, + FirstLine: b.firstLine, } } @@ -2193,11 +2208,27 @@ func blockParamsAreImplicit(blk *ast.Block) bool { // under `ruby_version_is ""..."4.0"` and pins the hiding only from 4.0. Hiding // them here would be answering a question rbgo has not yet said it is answering; // it belongs with the RUBY_VERSION bump. +// +// ⛔ BOTH KINDS ARE RECORDED, hidden or not. Ruby 4.0 adds +// Binding#implicit_parameters, #implicit_parameter_get and +// #implicit_parameter_defined? (proc.c v4.0.9:680-783), which answer for `it` +// AND for `_1`..`_N`. That is the other half of taking `_1` out of +// #local_variables: the implicit parameters do not disappear, they move to an +// API of their own. Blanking alone threw the name away and left nothing for the +// new methods to read, so the name goes into ImplicitParams first — a channel +// the public local table does not expose and the version bump does not have to +// invent. func hideImplicitBlockParams(b *builder, blk *ast.Block, positionals []string) { if !blockParamsAreImplicit(blk) { return } + // ⛔ EVERY positional here is implicit, so the list is DENSE and recorded + // without a guard. A `name == ""` arm looked prudent and was unreachable: + // blockParamsAreImplicit already means the parser synthesised the whole + // parameter list, so there is no anonymous slot among them. The coverage + // gate named it, which is the right way to find out. for i, name := range positionals { + b.markImplicitParam(i, name) if name != "it" { continue } diff --git a/internal/vm/binding.go b/internal/vm/binding.go index 56deff6e..fc64cf71 100644 --- a/internal/vm/binding.go +++ b/internal/vm/binding.go @@ -41,6 +41,18 @@ type Binding struct { // fresh block-less scope, it continues the captured one. block *Proc + // implicit maps an IMPLICIT parameter's name — `it`, or `_1`..`_N` — to its + // slot in env, the binding's own (depth 0) scope. Only depth 0: MRI reads + // these with get_local_variable_ptr(&env, lid, FALSE), where the final + // argument is "search the parent scopes" and is TRUE for every local-variable + // method and FALSE for every implicit-parameter one (proc.c v4.0.9:688, 734, + // 782). That single flag is the whole of "ignores a numbered parameter + // defined in a parent scope", which ruby/spec pins four times over. + // + // nil for a scope with none, which is every scope that is not a block whose + // parameters the parser synthesised. + implicit map[string]int + // method is the __method__/__callee__ pair of the frame the binding was // captured in, so eval'd code reports the enclosing method. MRI reaches the // same answer from the binding's own frame (rb_vm_frame_method_entry on the @@ -267,6 +279,9 @@ func (vm *VM) registerBinding() { names: append([]string(nil), b.names...), locs: append([]bindLoc(nil), b.locs...), added: append([]string(nil), b.added...), + // Shared by value, not by reference: nothing mutates the map after + // capture, but a copy keeps that true if anything ever does. + implicit: copyImplicit(b.implicit), } } cBinding.define("dup", bindingDup) @@ -305,6 +320,103 @@ func (vm *VM) registerBinding() { cBinding.define("local_variable_defined?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return object.Bool(self.(*Binding).slotOf(vm.bindingVarName(args[0])) >= 0) }) + + // Ruby 4.0's implicit-parameter trio (proc.c v4.0.9:680-783). They are the + // other half of taking `_1` out of #local_variables: the implicit parameters + // do not vanish, they move to an API of their own. + // + // bind_implicit_parameters answers [:it] when `it` is in scope and the + // numbered parameters otherwise — never a mixture, because a block cannot + // have both. + cBinding.define("implicit_parameters", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) != 0 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 0)", len(args)) + } + return self.(*Binding).implicitParameterNames() + }) + cBinding.define("implicit_parameter_get", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) != 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1)", len(args)) + } + b := self.(*Binding) + name := vm.implicitParamName(args[0]) + slot, ok := b.implicit[name] + if !ok { + raise("NameError", "implicit parameter '%s' is not defined for %s", name, b.ToS()) + } + return b.env.slots[slot] + }) + cBinding.define("implicit_parameter_defined?", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) != 1 { + raise("ArgumentError", "wrong number of arguments (given %d, expected 1)", len(args)) + } + b := self.(*Binding) + _, ok := b.implicit[vm.implicitParamName(args[0])] + return object.Bool(ok) + }) +} + +// implicitParamName coerces the argument of #implicit_parameter_get / +// #implicit_parameter_defined? and REFUSES a name that is not an implicit +// parameter's at all. +// +// ⛔ THE TWO REFUSALS ARE DIFFERENT AND BOTH ARE PINNED. MRI checks the shape of +// the NAME before it looks for a value (proc.c v4.0.9:726, 775): +// +// :a -> NameError "'a' is not an implicit parameter" — never could be +// :_1 -> NameError "implicit parameter '_1' is not defined for …" — could be, is not +// +// Collapsing them would pass "not defined" for a name no scope could ever hold. +// The TypeError for a non-String/Symbol is bindingVarName's, which is already +// MRI's message. +func (vm *VM) implicitParamName(v object.Value) string { + name := vm.bindingVarName(v) + if !isImplicitParamName(name) { + raise("NameError", "'%s' is not an implicit parameter", name) + } + return name +} + +func copyImplicit(m map[string]int) map[string]int { + if m == nil { + return nil + } + out := make(map[string]int, len(m)) + for k, v := range m { + out[k] = v + } + return out +} + +// isImplicitParamName is MRI's rb_implicit_param_p: `it`, or `_` followed by a +// single digit 1..9. `_0` and `_10` are ordinary locals. +func isImplicitParamName(name string) bool { + if name == "it" { + return true + } + return len(name) == 2 && name[0] == '_' && name[1] >= '1' && name[1] <= '9' +} + +// implicitParameterNames is bind_implicit_parameters (proc.c v4.0.9:680): [:it] +// when `it` is this scope's implicit parameter, otherwise the numbered ones in +// order. A block has one kind or the other and never both — `proc { |x| _1 }` is +// a syntax error — so the `it` arm returns before the numbered ones are looked +// at, as MRI's does. +func (b *Binding) implicitParameterNames() object.Value { + if _, ok := b.implicit["it"]; ok { + return object.NewArray(object.Symbol("it")) + } + var elems []object.Value + // Ascending by name, which for `_1`..`_9` is ascending by slot too. MRI + // builds the list from the local table in order for the same reason. + for d := byte('1'); d <= '9'; d++ { + n := string([]byte{'_', d}) + if _, ok := b.implicit[n]; !ok { + break + } + elems = append(elems, object.Symbol(n)) + } + return object.NewArrayFromSlice(elems) } // localVariableNames is the Symbol array Binding#local_variables and diff --git a/internal/vm/binding_implicit_params_test.go b/internal/vm/binding_implicit_params_test.go new file mode 100644 index 00000000..1a2ad397 --- /dev/null +++ b/internal/vm/binding_implicit_params_test.go @@ -0,0 +1,240 @@ +package vm + +import "testing" + +// evalString runs a fragment and returns the value of its last expression, so a +// case reads as the Ruby it is testing rather than as a program that prints. +func evalString(t *testing.T, src string) string { + t.Helper() + return runSrc(t, "__v = begin\n"+src+"\nend\nputs __v") +} + +// Ruby 4.0 adds Binding#implicit_parameters, #implicit_parameter_get and +// #implicit_parameter_defined? (proc.c v4.0.9:680-783). They are the other half +// of taking `_1` out of #local_variables: the implicit parameters do not +// vanish, they move to an API of their own. +// +// ⛔ WHY THIS FEATURE WAS INVISIBLE. The three ruby/spec files for it are +// `pass=0 fail=0 error=0` below 4.0 — every example behind a +// `ruby_version_is "4.0"` guard — and 37 failures above it. A version-move +// measurement that compared PASSING counts read zero on all three and a plan +// was published without them. The cases below are those specs' shapes, run here +// at whatever version rbgo reports, because a guard is not a reason to leave a +// method unimplemented. +func TestBindingImplicitParameters(t *testing.T) { + for _, tc := range []struct { + name, src, want string + }{{ + name: "it is reported alone", + src: `r = nil; [42].each { it; r = binding.implicit_parameters }; r.inspect`, + want: `[:it]`, + }, { + name: "a numbered parameter is reported", + src: `r = nil; [42].each { _1; r = binding.implicit_parameters }; r.inspect`, + want: `[:_1]`, + }, { + // ⛔ Every name UP TO the maximum referenced one, not just the one + // written: `_3` alone makes the block take three. + name: "every numbered parameter up to the maximum referenced one", + src: `proc { _3; binding.implicit_parameters }.call(:a, :b, :c, :d).inspect`, + want: `[:_1, :_2, :_3]`, + }, { + name: "a scope with neither reports nothing", + src: `a = 1; binding.implicit_parameters.inspect`, + want: `[]`, + }, { + // The parameter list is a property of the BLOCK, fixed when it is + // compiled — not of the point the binding is taken at. + name: "an implicit parameter referenced AFTER the call is still reported", + src: `proc { r = binding.implicit_parameters; a = _1; r }.call.inspect`, + want: `[:_1]`, + }, { + name: "it from a parent scope is not this scope's", + src: `r = nil; [1].each { it; proc { r = binding.implicit_parameters }.call }; r.inspect`, + want: `[]`, + }, { + name: "a numbered parameter from a parent scope is not this scope's", + src: `r = nil; [1].each { _1; proc { r = binding.implicit_parameters }.call }; r.inspect`, + want: `[]`, + }, { + name: "one in a NESTED scope is not this scope's either", + src: `foo = -> { _1 }; binding.implicit_parameters.inspect`, + want: `[]`, + }, { + name: "get reads the value", + src: `-> { _1; binding.implicit_parameter_get(:_1) }.call(:a).inspect`, + want: `:a`, + }, { + name: "get reads every numbered parameter up to the maximum", + src: `proc { _3; [binding.implicit_parameter_get(:_1), ` + + `binding.implicit_parameter_get(:_2), binding.implicit_parameter_get(:_3)] }` + + `.call(:a, :b, :c, :d).inspect`, + want: `[:a, :b, :c]`, + }, { + name: "get reads it", + src: `-> { it; binding.implicit_parameter_get(:it) }.call(:a).inspect`, + want: `:a`, + }, { + name: "get accepts a String", + src: `-> { _1; binding.implicit_parameter_get('_1') }.call(:a).inspect`, + want: `:a`, + }, { + name: "defined? is false where there is none", + src: `binding.implicit_parameter_defined?(:_1).inspect`, + want: `false`, + }, { + name: "defined? is true where there is one", + src: `proc { _1; binding.implicit_parameter_defined?(:_1) }.call.inspect`, + want: `true`, + }, { + // ⛔ The boundary: `_3` defines three, and the fourth is false rather + // than an error. A rule that answered "defined" for any `_N` would pass + // every other case here. + name: "defined? stops at the maximum referenced one", + src: `proc { _3; [binding.implicit_parameter_defined?(:_3), ` + + `binding.implicit_parameter_defined?(:_4)] }.call.inspect`, + want: `[true, false]`, + }, { + name: "defined? is false for a parent scope's", + src: `r = nil; [1].each { _1; proc { r = binding.implicit_parameter_defined?(:_1) }.call }; r.inspect`, + want: `false`, + }} { + t.Run(tc.name, func(t *testing.T) { + if got := evalString(t, tc.src); got != tc.want { + t.Errorf("%s\n got %s\nwant %s", tc.src, got, tc.want) + } + }) + } +} + +// ⛔ THE TWO REFUSALS ARE DIFFERENT AND BOTH ARE PINNED. MRI checks the SHAPE of +// the name before it looks for a value (proc.c v4.0.9:726, 775): a name that +// could never be an implicit parameter is refused differently from one that +// could be and is not. Collapsing them would answer "not defined" for `:a`. +func TestBindingImplicitParameterErrors(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{{ + name: "a name no scope could ever hold", + src: `begin; binding.implicit_parameter_get(:a); rescue NameError => e; e.message; end`, + want: `'a' is not an implicit parameter`, + }, { + name: "the same refusal from defined?, which does NOT answer false", + src: `begin; binding.implicit_parameter_defined?(:a); rescue NameError => e; e.message; end`, + want: `'a' is not an implicit parameter`, + }, { + name: "a well-formed name that this scope does not hold", + src: `begin; binding.implicit_parameter_get(:_1); rescue NameError => e; e.message; end`, + want: `implicit parameter '_1' is not defined for #`, + }, { + name: "it, when this scope has none", + src: `begin; binding.implicit_parameter_get(:it); rescue NameError => e; e.message; end`, + want: `implicit parameter 'it' is not defined for #`, + }, { + name: "a parent scope's is not defined HERE", + src: `r = nil; [1].each { _1; proc { begin; binding.implicit_parameter_get(:_1); ` + + `rescue NameError => e; r = e.message; end }.call }; r`, + want: `implicit parameter '_1' is not defined for #`, + }, { + name: "neither a String nor a Symbol", + src: `begin; binding.implicit_parameter_get(1); rescue TypeError => e; e.message; end`, + want: `1 is not a symbol nor a string`, + }, { + name: "and the same from defined?", + src: `begin; binding.implicit_parameter_defined?(1); rescue TypeError => e; e.message; end`, + want: `1 is not a symbol nor a string`, + }} { + t.Run(tc.name, func(t *testing.T) { + if got := evalString(t, tc.src); got != tc.want { + t.Errorf("%s\n got %q\nwant %q", tc.src, got, tc.want) + } + }) + } +} + +// The arity of all three is fixed in MRI (0, 1 and 1), so a wrong count is an +// ArgumentError and not a silently ignored argument. +// +// ⛔ Named by the coverage gate rather than thought of: the three refusals were +// written and never driven, which is the same as not having them. +func TestBindingImplicitParameterArity(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{{ + name: "implicit_parameters takes none", + src: `begin; binding.implicit_parameters(1); rescue ArgumentError => e; e.message; end`, + want: `wrong number of arguments (given 1, expected 0)`, + }, { + name: "implicit_parameter_get takes one", + src: `begin; binding.implicit_parameter_get(:_1, 2); rescue ArgumentError => e; e.message; end`, + want: `wrong number of arguments (given 2, expected 1)`, + }, { + name: "implicit_parameter_defined? takes one", + src: `begin; binding.implicit_parameter_defined?; rescue ArgumentError => e; e.message; end`, + want: `wrong number of arguments (given 0, expected 1)`, + }} { + t.Run(tc.name, func(t *testing.T) { + if got := evalString(t, tc.src); got != tc.want { + t.Errorf("%s\n got %q\nwant %q", tc.src, got, tc.want) + } + }) + } +} + +// Binding#dup and #clone copy the implicit-parameter map rather than sharing +// it, the same way they copy the name list — so the copy answers for the same +// scope and a later change to one cannot reach the other. +// +// ⛔ Also named by the coverage gate: every dup in the suite happened to be of +// a binding with NO implicit parameters, so the copy was never exercised. +func TestADuplicatedBindingKeepsItsImplicitParameters(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{{ + name: "dup of a binding that has one", + src: `r = nil; [7].each { _1; r = binding.dup.implicit_parameter_get(:_1) }; r.inspect`, + want: `7`, + }, { + name: "clone of a binding that has one", + src: `r = nil; [7].each { it; r = binding.clone.implicit_parameters }; r.inspect`, + want: `[:it]`, + }, { + name: "dup of a binding that has none keeps having none", + src: `a = 1; binding.dup.implicit_parameters.inspect`, + want: `[]`, + }} { + t.Run(tc.name, func(t *testing.T) { + if got := evalString(t, tc.src); got != tc.want { + t.Errorf("%s\n got %s\nwant %s", tc.src, got, tc.want) + } + }) + } +} + +// ⛔ THE CONTROL. An implicit parameter is not a local variable, and `it` has +// never been one — that part is already rbgo's behaviour and must stay. Without +// this, the change above could have made `it` visible to #local_variables and +// every test here would still pass. +// +// `_1` is a different case and deliberately untouched: ruby/spec pins it as +// VISIBLE under `ruby_version_is ""..."4.0"` and hidden only from 4.0, and rbgo +// reports 3.4.1. Hiding it belongs with the version bump, not here. +func TestImplicitParametersAreNotLocalVariables(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{{ + // Membership, not the whole list: local_variables inside a block also + // names the enclosing scope's locals, which is correct and not what this + // is about. + name: "it is absent from local_variables", + src: `r = nil; [1].each { it; r = binding.local_variables.include?(:it) }; r.inspect`, + want: `false`, + }, { + name: "and unreachable through local_variable_get", + src: `r = nil; [1].each { it; begin; binding.local_variable_get(:it); ` + + `rescue NameError => e; r = e.message; end }; r`, + want: `local variable 'it' is not defined for #`, + }, { + name: "while _1 stays visible at the version rbgo reports", + src: `r = nil; [1].each { _1; r = binding.local_variables.include?(:_1) }; r.inspect`, + want: `true`, + }} { + t.Run(tc.name, func(t *testing.T) { + if got := evalString(t, tc.src); got != tc.want { + t.Errorf("%s\n got %s\nwant %s", tc.src, got, tc.want) + } + }) + } +} diff --git a/internal/vm/fiber.go b/internal/vm/fiber.go index bcc1dab1..bf6329ca 100644 --- a/internal/vm/fiber.go +++ b/internal/vm/fiber.go @@ -271,7 +271,7 @@ func (vm *VM) registerFiber() { // caller's — while the backtrace is stamped in the target, where it is // actually raised. cFiber.define("raise", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { - return vm.fiberRaise(self.(*Fiber), vm.fiberRaiseException(args)) + return vm.fiberRaise(self.(*Fiber), vm.asyncRaiseException(args)) }) // Fiber#kill terminates the fiber with an uncatchable unwind: ensure blocks // run, rescue clauses do not (rb_fiber_m_kill, cont.c). An unborn fiber goes @@ -438,13 +438,32 @@ func (vm *VM) fiberStorageKey(v object.Value) object.Value { return raise("TypeError", "%s is not a symbol nor a string", v.Inspect()) } -// fiberRaiseException builds the exception object for Fiber#raise from its -// arguments, exactly as Kernel#raise does (rb_make_exception plus, since Ruby -// 4.0, the cause: keyword): a bare call is a RuntimeError with an empty message, -// a third positional argument is the backtrace, and cause: overrides the -// automatic chaining from the caller's $!. It does not panic — the exception -// travels to the target fiber, which raises it. -func (vm *VM) fiberRaiseException(args []object.Value) object.Value { +// asyncRaiseException builds the exception object for a raise aimed at ANOTHER +// context — Fiber#raise and Thread#raise — from its arguments, exactly as +// Kernel#raise does (rb_make_exception plus, since Ruby 4.0, the cause: +// keyword): a bare call is a RuntimeError with an empty message, a third +// positional argument is the backtrace, and cause: overrides the automatic +// chaining from the caller's $!. It does not panic — the exception travels to +// the target, which raises it. +// +// ⛔ It is shared because the two are the SAME rule and were not the same code. +// Ruby 4.0 gives both of them the cause: keyword (ruby/spec's +// `:kernel_raise_with_cause`, included by core/thread/raise_spec.rb and +// core/fiber/raise_spec.rb alike). Fiber#raise had it; Thread#raise built its +// exception by hand and silently dropped both cause: and the backtrace +// argument. A second copy would have had to be kept in step with this one. +// +// Running in the CALLER's context is what makes the automatic chaining right: +// `cause` is the caller's $! at the moment of the call, not the target's, which +// is exactly what ruby/spec's `:kernel_raise_across_contexts` pins from 4.0. +// +// ⛔ KNOWN LIMIT, #873: popCauseKwarg cannot tell a POSITIONAL Hash from +// keyword arguments, because a native receives both as a trailing *object.Hash +// and nothing marks which it was. So `raise("m", {cause: e})` — two positional +// arguments, a TypeError in MRI — is read here as `raise("m", cause: e)`. The +// defect is older than this function (Kernel#raise and Fiber#raise have always +// had it) and wider than raise; it is filed rather than patched locally. +func (vm *VM) asyncRaiseException(args []object.Value) object.Value { args, causeGiven, causeVal := popCauseKwarg(args) if len(args) == 0 { if causeGiven { diff --git a/internal/vm/thread.go b/internal/vm/thread.go index 3ddbd424..c3d54951 100644 --- a/internal/vm/thread.go +++ b/internal/vm/thread.go @@ -921,7 +921,7 @@ func (vm *VM) registerThreadClass() { }) return object.NewArrayFromSlice(keys) }) - // raise([exc[, msg]]) delivers an exception to the thread asynchronously: it is + // raise([exc[, msg[, backtrace]]][, cause:]) delivers an exception to the thread asynchronously: it is // picked up at the target's next interpreter safepoint (a backward jump or a // method/block entry) and raised in the target's own context. With no arguments // it raises a RuntimeError with an empty message (MRI); otherwise the arguments @@ -935,12 +935,12 @@ func (vm *VM) registerThreadClass() { if t.isDone() { return object.NilV } - var exc object.Value - if len(args) == 0 { - exc = vm.send(vm.consts["RuntimeError"].(*RClass), "new", []object.Value{object.NewString("")}, nil) - } else { - exc = vm.raiseExceptionObject(args) - } + // Built by the SHARED builder Fiber#raise uses: a hand-rolled copy here + // dropped the cause: keyword Ruby 4.0 adds and the backtrace argument + // Kernel#raise has always taken. It runs in the CALLER's context, so the + // automatic chaining reads the caller's $! rather than the target's — + // which is what ruby/spec's :kernel_raise_across_contexts pins. + exc := vm.asyncRaiseException(args) if t == vm.currentThread { panic(vm.excError(vm.captureBacktrace(exc))) } diff --git a/internal/vm/thread_raise_cause_test.go b/internal/vm/thread_raise_cause_test.go new file mode 100644 index 00000000..3e5bc0b4 --- /dev/null +++ b/internal/vm/thread_raise_cause_test.go @@ -0,0 +1,128 @@ +package vm + +import "testing" + +// evalInspect is evalString's sibling for cases whose answer is a structure +// rather than a message: `puts` on an Array prints one element per line, which +// would compare two different things. +func evalInspect(t *testing.T, src string) string { + t.Helper() + return runSrc(t, "__v = begin\n"+src+"\nend\nputs __v.inspect") +} + +// Ruby 4.0 gives Thread#raise the cause: keyword — ruby/spec includes the +// shared `:kernel_raise_with_cause` examples into core/thread/raise_spec.rb +// from 4.0 and not before. rbgo's Fiber#raise already had it; Thread#raise +// built its exception by hand and dropped both cause: and the backtrace +// argument, so the two were the same rule in two places and only one of them +// was right. +// +// ⛔ These ran on the SLEEPING-thread shape rather than the current one, +// because Thread#raise on the current thread takes a different path (it panics +// here and now) and would have exercised none of the delivery. +func TestThreadRaiseCause(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{{ + name: "cause: sets the raised exception's cause", + src: `c = StandardError.new("original") +t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +t.raise("new error", cause: c) +begin; t.value; rescue => e; [e.message, e.cause.message]; end`, + want: `["new error", "original"]`, + }, { + name: "cause: nil prevents the automatic chaining", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +begin + raise "first error" +rescue + t.raise("second error", cause: nil) +end +begin; t.value; rescue => e; [e.message, e.cause]; end`, + want: `["second error", nil]`, + }, { + // ⛔ The chaining reads the CALLER's $!, not the target thread's, which + // is what :kernel_raise_across_contexts pins from 4.0. The target here + // is asleep and has no current exception at all. + name: "the automatic cause comes from the CALLING context", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +begin + raise "caller's error" +rescue + t.raise("delivered") +end +begin; t.value; rescue => e; [e.message, e.cause.message]; end`, + want: `["delivered", "caller's error"]`, + }, { + name: "no previous exception leaves the cause nil", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +t.raise("error without a cause") +begin; t.value; rescue => e; [e.message, e.cause]; end`, + want: `["error without a cause", nil]`, + }, { + name: "only cause: and nothing else is an ArgumentError", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +r = begin; t.raise(cause: StandardError.new("c")); rescue => e; [e.class, e.message]; end +t.kill; t.join; r`, + want: `[ArgumentError, "only cause is given with no arguments"]`, + }, { + name: "a cause that is neither an Exception nor nil is a TypeError", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +r = begin; t.raise("message", cause: Object.new); rescue => e; [e.class, e.message]; end +t.kill; t.join; r`, + want: `[TypeError, "exception object expected"]`, + }, { + name: "a cause that would close a cycle is an ArgumentError", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +r = begin + begin + raise "Error 1" + rescue => e1 + begin + raise "Error 2" + rescue + begin + raise "Error 3" + rescue => e3 + t.raise(e1, cause: e3) + end + end + end +rescue => e + [e.class, e.message] +end +t.kill; t.join; r`, + want: `[ArgumentError, "circular causes"]`, + }, { + // The third positional argument was dropped outright before this change; + // Kernel#raise has always taken it. + name: "the backtrace argument is honoured", + src: `t = Thread.new { Thread.current.report_on_exception = false; sleep } +Thread.pass while t.status != "sleep" +t.raise(ArgumentError, "with a backtrace", ["line1", "line2"]) +begin; t.value; rescue => e; [e.class, e.message, e.backtrace]; end`, + want: `[ArgumentError, "with a backtrace", ["line1", "line2"]]`, + }, { + // ⛔ THE CONTROL. A dead thread still ignores the raise and answers nil + // — pinned by core/thread/raise_spec.rb outside any version guard, and + // the easiest thing to break by moving the argument handling earlier. + name: "a dead thread still ignores it and answers nil", + src: `t = Thread.new { :dead } +Thread.pass while t.alive? +r = t.raise("Kill the thread") +t.join +r`, + want: `nil`, + }} { + t.Run(tc.name, func(t *testing.T) { + if got := evalInspect(t, tc.src); got != tc.want { + t.Errorf("\n got %s\nwant %s", got, tc.want) + } + }) + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 15f23af2..75602786 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -400,6 +400,15 @@ func (vm *VM) collectBindingLocals(b *Binding, i int) { // more — locs carries the slot — but a binding with no enclosing scope then // has the identity map it always had, and reads the same in a dump. b.names = append(b.names, fc.iseq.Locals...) + // The implicit parameters of THIS scope only — depth 0 — which is MRI's + // get_local_variable_ptr(..., FALSE) and the reason a numbered parameter in a + // parent or a nested scope is invisible here. + for slot, n := range fc.iseq.ImplicitParams { + if b.implicit == nil { + b.implicit = map[string]int{} + } + b.implicit[n] = slot + } b.locs = make([]bindLoc, len(b.names)) for k := range b.locs { b.locs[k] = bindLoc{slot: k}