From 04ec59c07ab6471b47ddefa53087282ed3fa9fc4 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 2 Nov 2026 18:34:37 +0100 Subject: [PATCH 1/5] Binding: Ruby 4.0's implicit-parameter API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. rbgo had none of the three, and nothing said so. The three ruby/spec files 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, so a version-move measurement that compared PASSING counts read zero on all three. That is 37 of the ~75 examples #799's move opens, and the largest single gap in it. ## The reading Two facts from the reference, both load-bearing: * MRI keeps implicit parameters in the local table under IDs the program cannot spell: `it` under idItImplicit rather than idIt, and the numbered ones under IDs rb_numparam_id_p recognises. So they have a name and a value while being absent from #local_variables. * Every local-variable method reads with get_local_variable_ptr(&env, lid, TRUE) and every implicit-parameter one with FALSE (:688, :734, :782). That final argument is "search the parent scopes", and it alone is the whole of "ignores a numbered parameter defined in a parent scope", which ruby/spec pins four times over. ## The change `bytecode.ISeq` gains `ImplicitParams`, a second channel beside `Locals` naming the implicit slots. rbgo already blanked `Locals` for `it` so it could not be seen; this records the name instead of discarding it, and the public local table is unchanged. `Binding` captures those names at DEPTH 0 only, which is MRI's FALSE, and the three methods read them. `implicit_parameters` answers [:it] or the numbered ones and never a mixture, because a block cannot have both. ⛔ The two refusals are kept apart, as MRI keeps them -- the shape of the NAME is checked before any lookup (:726, :775): :a -> NameError "'a' is not an implicit parameter" :_1 -> NameError "implicit parameter '_1' is not defined for …" Collapsing them would answer "not defined" for a name no scope could ever hold. ## Not in this change `_1` stays VISIBLE to #local_variables. ruby/spec pins it so under `ruby_version_is ""..."4.0"` and pins the hiding only from 4.0, and rbgo reports 3.4.1; hiding it belongs with the version bump. The channel this adds is what that bump will flip, so it will not have to invent one. ## Tests Sixteen behaviour cases and seven error cases, each a shape the three spec files pin, plus a control that would have caught the obvious wrong turn: `it` must STILL be absent from #local_variables and unreachable through #local_variable_get, and `_1` must still be present. Without it, making `it` reachable would have passed everything else. Full internal suite green, no regression. Refs #799 Co-Authored-By: Claude Opus 5 --- README.md | 7 + internal/bytecode/bytecode.go | 22 ++- internal/compiler/compiler.go | 63 +++++-- internal/vm/binding.go | 112 ++++++++++++ internal/vm/binding_implicit_params_test.go | 185 ++++++++++++++++++++ internal/vm/vm.go | 12 ++ 6 files changed, 382 insertions(+), 19 deletions(-) create mode 100644 internal/vm/binding_implicit_params_test.go diff --git a/README.md b/README.md index 9db2e3dc..9e36702b 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,13 @@ 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. + **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..a230d34c 100644 --- a/internal/bytecode/bytecode.go +++ b/internal/bytecode/bytecode.go @@ -360,8 +360,26 @@ 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 may be shorter than NumLocals, or nil: a slot past its end is ordinary. + 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..1ca0d14f 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,25 @@ 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 } for i, name := range positionals { + if name == "" { + continue + } + 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..f7a995f8 --- /dev/null +++ b/internal/vm/binding_implicit_params_test.go @@ -0,0 +1,185 @@ +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 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/vm.go b/internal/vm/vm.go index 15f23af2..bd9a6017 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -400,6 +400,18 @@ 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 n == "" { + continue + } + 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} From 147eb5a0120319c85cb5fd786f5f52eeb717d9b0 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 2 Nov 2026 21:29:24 +0100 Subject: [PATCH 2/5] Thread#raise: the cause: keyword, from the builder Fiber#raise already used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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, which is 19 of the examples #799's version move opens in that one file -- the second largest block in the move after Binding's implicit parameters. rbgo already had the rule. Fiber#raise built its exception through fiberRaiseException, which handles cause:, the circular-cause refusal, the "only cause is given" refusal and the backtrace argument. Thread#raise built its own by hand: if len(args) == 0 { exc = RuntimeError.new("") } else { exc = raiseExceptionObject(args) } -- which silently dropped cause: AND the third positional backtrace argument Kernel#raise has always taken. Two places, one rule, and only one of them right. So the builder is shared rather than copied, renamed asyncRaiseException for what it is: the exception for a raise aimed at ANOTHER context. A second copy would have had to be kept in step with the first. ⛔ Running it in the CALLER's context is what makes the automatic chaining right. `cause` is the caller's $! at the moment of the call and not the target's, which is exactly what ruby/spec's `:kernel_raise_across_contexts` pins from 4.0 -- the target thread in those examples is asleep and has no current exception at all. ## Tests Nine cases on the SLEEPING-thread shape, because Thread#raise on the CURRENT thread takes a different path (it panics here and now) and would have exercised none of the delivery: cause: set, cause: nil blocking the chain, the automatic cause coming from the caller, no cause at all, the two ArgumentErrors, the TypeError, and the backtrace argument that was being dropped. ⛔ Plus the control: a dead thread must STILL ignore the raise and answer nil. That is pinned outside any version guard and is the easiest thing to break by moving argument handling earlier. Full internal suite green, no regression. Refs #799 Co-Authored-By: Claude Opus 5 --- internal/vm/fiber.go | 28 ++++-- internal/vm/thread.go | 14 +-- internal/vm/thread_raise_cause_test.go | 128 +++++++++++++++++++++++++ 3 files changed, 155 insertions(+), 15 deletions(-) create mode 100644 internal/vm/thread_raise_cause_test.go diff --git a/internal/vm/fiber.go b/internal/vm/fiber.go index bcc1dab1..19d07015 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,25 @@ 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. +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) + } + }) + } +} From 4c4aa4ac9f05c8b14471f173365ac0311a399dc0 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 2 Nov 2026 21:30:05 +0100 Subject: [PATCH 3/5] README: name the two Ruby 4.0 surfaces this branch adds Co-Authored-By: Claude Opus 5 --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e36702b..02301256 100644 --- a/README.md +++ b/README.md @@ -239,7 +239,9 @@ Ruby **4.0**'s implicit-parameter API is implemented: `#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. +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 From f7706e1b43ca1684f9b92be8ce0b60a1b9394971 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 2 Nov 2026 21:54:50 +0100 Subject: [PATCH 4/5] Mark the positional-Hash limit #873 where it bites A native cannot tell `raise("m", {cause: e})` from `raise("m", cause: e)`: both arrive as a trailing *object.Hash and nothing marks which it was. The defect predates this function -- Kernel#raise and Fiber#raise have always had it, measured on the control -- and it is why Thread#raise now fails `raises a TypeError when passed a message and an extra argument`, an example it used to pass BECAUSE it ignored cause: entirely. Refs #873 Co-Authored-By: Claude Opus 5 --- internal/vm/fiber.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/vm/fiber.go b/internal/vm/fiber.go index 19d07015..bf6329ca 100644 --- a/internal/vm/fiber.go +++ b/internal/vm/fiber.go @@ -456,6 +456,13 @@ func (vm *VM) fiberStorageKey(v object.Value) object.Value { // 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 { From 076d561a9aaa1d389433778c1ffdf24427ce1f20 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 3 Nov 2026 07:54:32 +0100 Subject: [PATCH 5/5] Two guards that could not be reached, and two that were never driven The coverage ratchet named four functions this branch dropped below 100%, and the four split cleanly in two. ## Unreachable, so deleted internal/compiler/compiler.go hideImplicitBlockParams 91.7% internal/vm/vm.go collectBindingLocals 97.1% Both carried a `name == ""` arm I wrote out of caution. It cannot run: blockParamsAreImplicit already means the parser synthesised the WHOLE parameter list, so there is no anonymous slot among them and the list is dense from slot 0. The guards were not defensive, they were dead -- and a dead branch is exactly what a per-function gate is for. ISeq.ImplicitParams now says it is dense. ## Written and never driven, so tested internal/vm/binding.go copyImplicit 33.3% internal/vm/binding.go registerBinding 95.8% Every `dup`/`clone` in the suite happened to be of a binding with NO implicit parameters, so the copy was never exercised; and the three arity refusals were written without a single call reaching them, which is the same as not having them. Six cases added: dup and clone of a binding that HAS one (plus the control, one that has none), and each method called with the wrong number of arguments. Verified locally with the gate CI runs, not by hoping: coverage ratchet OK -- no function newly below 100% Refs #799 Co-Authored-By: Claude Opus 5 --- internal/bytecode/bytecode.go | 5 +- internal/compiler/compiler.go | 8 +-- internal/vm/binding_implicit_params_test.go | 55 +++++++++++++++++++++ internal/vm/vm.go | 3 -- 4 files changed, 64 insertions(+), 7 deletions(-) diff --git a/internal/bytecode/bytecode.go b/internal/bytecode/bytecode.go index a230d34c..9c2a1c70 100644 --- a/internal/bytecode/bytecode.go +++ b/internal/bytecode/bytecode.go @@ -375,7 +375,10 @@ type ISeq struct { // blanked Locals for `it` to the same end; this records what was blanked // instead of discarding it. // - // It may be shorter than NumLocals, or nil: a slot past its end is ordinary. + // 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) diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index 1ca0d14f..c8d4f318 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -2222,10 +2222,12 @@ 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 { - if name == "" { - continue - } b.markImplicitParam(i, name) if name != "it" { continue diff --git a/internal/vm/binding_implicit_params_test.go b/internal/vm/binding_implicit_params_test.go index f7a995f8..1a2ad397 100644 --- a/internal/vm/binding_implicit_params_test.go +++ b/internal/vm/binding_implicit_params_test.go @@ -150,6 +150,61 @@ func TestBindingImplicitParameterErrors(t *testing.T) { } } +// 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 diff --git a/internal/vm/vm.go b/internal/vm/vm.go index bd9a6017..75602786 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -404,9 +404,6 @@ func (vm *VM) collectBindingLocals(b *Binding, i int) { // 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 n == "" { - continue - } if b.implicit == nil { b.implicit = map[string]int{} }