diff --git a/internal/vm/exception.go b/internal/vm/exception.go index 8f17e200..b5f698b7 100644 --- a/internal/vm/exception.go +++ b/internal/vm/exception.go @@ -375,6 +375,15 @@ func (vm *VM) raiseWithIvars(class, msg string, ivars map[string]object.Value) { } } obj := &RObject{class: cls, ivars: iv, ivarOrder: order} + // ⛔ The cause is stamped HERE, at the raise, which is where MRI takes it + // (exc_setup_message reads get_ec_errinfo(ec) when no cause was given, + // eval.c at v4.0.9). Kernel#raise reaches applyRaiseCause with the object it + // was handed; this is the other door — every internal raise that carries + // data, so NoMethodError and NameError from the dispatcher included, and + // language/send_spec.rb asserts $! as the cause of both. + // + // autoCause already refuses a self-cause and leaves an explicit cause alone. + vm.autoCause(obj) panic(vm.excError(vm.captureBacktrace(obj))) } diff --git a/internal/vm/internal_raise_cause_test.go b/internal/vm/internal_raise_cause_test.go new file mode 100644 index 00000000..08dcfc44 --- /dev/null +++ b/internal/vm/internal_raise_cause_test.go @@ -0,0 +1,148 @@ +// Copyright (c) the go-embedded-ruby/ruby authors +// +// SPDX-License-Identifier: BSD-3-Clause + +package vm + +import "testing" + +const causePre = `def show(label) + yield +rescue Exception => e + puts "#{label}: #{e.class} cause=#{e.cause.inspect}" +end +` + +// An exception raised while another is being handled carries that other one as +// its #cause. MRI takes it from get_ec_errinfo(ec) in exc_setup_message when no +// cause was given, and exc_setup_cause declines a self-cause (eval.c at v4.0.9). +// +// ⛔ Kernel#raise already did this here (applyRaiseCause). An INTERNAL raise did +// not, and there are TWO doors: raiseWithIvars builds its object up front to +// carry @name/@receiver/@args and is stamped at the raise; the objectless +// raise() helper panics with a RubyError struct and is stamped when the object +// is built in exceptionObject. NameError and NoMethodError from the dispatcher +// come through the first, which is why a fix in the second alone changed +// nothing — measured, not reasoned. +// +// Every expectation was measured on +// `ruby 4.0.7 (2026-09-15 revision 229531a6cf) +PRISM [arm64-darwin25]` +// through scripts/conformance/rubyspec/cmd/oracle; the control (main at +// ac87a757) differs on exactly the three positive rows. +func TestAnInternalRaiseCarriesTheHandledExceptionAsItsCause(t *testing.T) { + for _, tc := range []struct{ name, src, want string }{ + // ⛔ The two rows language/send_spec.rb asserts: "raises NameError with + // $! as a cause" and "raises NoMethodError with $! as a cause". + {"a NoMethodError raised inside a rescue names the handled exception", + causePre + `show("x") do + begin + raise "orig" + rescue + nil.no_such_method + end +end`, `x: NoMethodError cause=#`}, + + {"a NameError from a vcall miss names it too", + causePre + `show("x") do + begin + raise "orig" + rescue + no_such_bare_name + end +end`, `x: NameError cause=#`}, + + {"the cause is the innermost handled exception, not the outermost", + causePre + `show("x") do + begin + begin + raise "first" + rescue + raise "second" + end + rescue + nil.no_such_method + end +end`, `x: NoMethodError cause=#`}, + + {"an internal raise inside an ensure sees the exception in flight", + causePre + `show("x") do + begin + begin + raise "orig-8" + ensure + nil.no_such_method + end + rescue Exception + raise + end +end`, `x: NoMethodError cause=#`}, + + // ⛔ THE NEGATIVE ROWS. Stamping a cause on every internal raise is the + // way this change could be wrong, and nothing above would notice. + {"no enclosing rescue means no cause", + causePre + `show("x") { nil.no_such_method }`, + `x: NoMethodError cause=nil`}, + + {"after the rescue has finished there is nothing to be caused by", + causePre + `begin + raise "orig" +rescue + nil +end +show("x") { nil.no_such_method }`, `x: NoMethodError cause=nil`}, + + {"a bare re-raise is the same exception and is not its own cause", + causePre + `show("x") do + begin + raise "orig" + rescue + raise + end +end`, `x: RuntimeError cause=nil`}, + + {"an explicit cause: wins over the handled exception", + causePre + `show("x") do + begin + raise "orig" + rescue + raise RuntimeError, "chosen", cause: ArgumentError.new("picked") + end +end`, `x: RuntimeError cause=#`}, + + {"an explicit nil cause suppresses the automatic one", + causePre + `show("x") do + begin + raise "orig" + rescue + raise RuntimeError, "no-cause", cause: nil + end +end`, `x: RuntimeError cause=nil`}, + + // ⛔ The OTHER door, and the reason exceptionObject is stamped as well as + // raiseWithIvars: these two reach the objectless raise() helper, which + // panics with a RubyError struct carrying no exception object. + {"an objectless internal raise is stamped too", + causePre + `show("x") do + begin + raise "orig" + rescue + Object.new.instance_variable_get("bad name") + end +end`, `x: NameError cause=#`}, + + {"a conversion failure carries the cause as well", + causePre + `show("x") do + begin + raise "orig" + rescue + Integer("zz") + end +end`, `x: ArgumentError cause=#`}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := runSrc(t, tc.src); got != tc.want { + t.Errorf("%s\n got %q\nwant %q", tc.src, got, tc.want) + } + }) + } +} diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 75602786..d9f8e764 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -195,6 +195,39 @@ func (vm *VM) exceptionObject(e RubyError) object.Value { cls = vm.consts["StandardError"].(*RClass) } obj := &RObject{class: cls, ivars: map[string]object.Value{"@message": object.NewString(e.Message)}} + // ⛔ The cause of an OBJECTLESS internal raise — `raise("TypeError", ...)`, + // the Go helper at vm.go:55, which panics with a RubyError STRUCT and so has + // nothing to stamp until here. An internal raise that carries data builds its + // object up front and is stamped at the raise instead (raiseWithIvars). + // + // MRI's exc_setup_message (eval.c at v4.0.9) takes the cause from + // get_ec_errinfo(ec) when none was given, and exc_setup_cause skips a + // self-cause; autoCause already encodes both, plus "an explicit cause wins". + // + // ⛔ THE CALLER'S ORDER IS WHAT MAKES THIS $! AND NOT THIS EXCEPTION. The + // rescue path reads + // + // exc := vm.exceptionObject(rerr) + // vm.curExc = exc + // + // so curExc here is still the exception whose rescue body raised this one. + // Moving the assignment above the call would make every such exception its + // own cause, which autoCause would then correctly refuse — and the chain + // would silently be nil again. + // + // ⛔ Known limit, stated rather than hidden: on THIS path the object is built + // when the exception is RESCUED, not when it is raised, so the cause is $! as + // of the rescue. The two differ only for an exception that unwinds past a + // frame restoring $! before any handler builds the object; for every case a + // program can observe #cause on, it has been rescued and the two agree. + // + // ⛔ BOTH STAMPS ARE LOAD-BEARING AND THE PARTITION IS EXACT, which is the + // check a two-site fix needs — a second stamp that changed nothing would be + // a rule nobody authorised. Measured by disabling each in turn against + // TestAnInternalRaiseCarriesTheHandledExceptionAsItsCause: without + // raiseWithIvars' stamp the four object-carrying rows fail and the two + // objectless ones pass; without this one, exactly the other two fail. + vm.autoCause(obj) return vm.captureBacktrace(obj) }