Skip to content

kernel: implement caller_locations, add caller start/length/range args, fix raise #exception protocol - #575

Merged
tannevaled merged 3 commits into
mainfrom
wave15/kernel
Sep 6, 2026
Merged

tannevaled merged 3 commits into
mainfrom
wave15/kernel

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Summary

Wave-15 Kernel conformance against MRI 4.0.6. Raises core/kernel pass 1767 → 1796 (+29), fail 230 → 215, error 154 → 140, with no regressions in any other spec.

Per-spec:

  • caller_locations_spec: 0 → 16 pass (was 4 fail + 13 error → 1 fail, 0 error)
  • caller_spec: 7 → 16 pass (was 11 fail → 2 fail)
  • raise_spec: 32 → 36 pass (was 11 fail + 2 error → 8 fail, 1 error)

What changed

Kernel#caller_locations — implemented (was NoMethodError)

It was entirely missing, which additionally poisoned raise_spec: the shared :kernel_raise group evaluates caller_locations(1, 2) at collection time, so the whole group failed to load. Implementing it recovers that cascade. Backed by the existing Thread::Backtrace::Location class and backtraceLocation helper.

Kernel#caller — argument handling (start / length / range)

caller ignored all arguments. caller and caller_locations now share callerSlice(args), applying MRI's semantics over caller(0):

  • a Range slices caller(0) via Array#[] (endless / beginless / negative bounds);
  • an Integer start (default 1) omits that many innermost levels, with an optional non-negative length cap;
  • negative start/length → ArgumentError "negative level/size (N)";
  • a start past the top returns nil (an exactly-at-the-end start returns []);
  • start/length coerced through #to_int (Float truncates).

Kernel#raise — MRI make_exception fixes

  • Bare raise with nothing being handled now yields RuntimeError with an empty message (was "unhandled exception").
  • raiseExceptionObject now implements MRI's #exception protocol: a lone String → RuntimeError; otherwise the object must respond_to?(:exception) (else TypeError "exception class/object expected"), obj.exception(msg?) is called, and its result must be an Exception (else TypeError "exception object expected"). This fixes duck-typed exception objects and both TypeError messages.
  • A cause: chain that loops back to the raised exception is rejected with ArgumentError "circular causes"; a cause: equal to the exception itself is not treated as circular.

Bibliography

ruby/spec core/kernel/{caller,caller_locations,raise}_spec.rb and shared/kernel/raise.rb; MRI eval.c make_exception; existing rbgo backtraceFrames / backtraceLocation.

Tests / gates

  • New whitebox internal/vm/kernel_caller_raise_internal_test.go covers every new branch; callerSlice, raiseExceptionObject, nativeRaise are at 100% statement coverage (registerKernelIntrospection / registerKernelModuleFunctions also 100%).
  • go build ./..., gofmt -l internal/vm/ clean, wasip1 (GOOS=wasip1 GOARCH=wasm) build all green. The vm suite's only failures are pre-existing network-dependent tests (redis/etcd/excon dialing the unreachable 127.0.0.1:1), unrelated to this change.
  • Byte-for-byte vs MRI verified for the changed behaviors.

Remaining caller/caller_locations residuals require real source line numbers (systemically 0 in this VM) and are out of scope; remaining raise residuals are $!-restoration in nested rescue (VM rescue handling) and shim artifacts.

🤖 Generated with Claude Code

tannevaled and others added 3 commits September 6, 2026 13:54
…s, fix raise

Kernel#caller_locations was entirely unimplemented (NoMethodError), which also
poisoned raise_spec: its shared group evaluates caller_locations(1, 2) at
collection time, so the whole group failed to load. Kernel#caller ignored all
arguments.

Bibliography read: ruby/spec core/kernel/{caller,caller_locations,raise}_spec.rb
and shared/kernel/raise.rb; MRI eval.c make_exception; existing rbgo
backtraceFrames / backtraceLocation (Thread::Backtrace::Location already present).

- caller / caller_locations now share callerSlice(args): a Range slices caller(0)
  via Array#[] (endless/beginless/negative bounds); an Integer start (default 1)
  omits that many innermost levels with an optional non-negative length cap;
  negative start/length -> ArgumentError "negative level/size (N)"; a start past
  the top returns nil (== end returns []); start/length coerced through #to_int.
  caller_locations maps each frame string through backtraceLocation. Registered
  as a Kernel module function (private instance + public module method).
- raise: bare raise with nothing handled now yields RuntimeError with an empty
  message (was "unhandled exception"); raiseExceptionObject implements MRI's
  #exception protocol (single String -> RuntimeError; else require
  respond_to?(:exception) else TypeError "exception class/object expected"; call
  obj.exception(msg?); result must be an Exception else TypeError "exception
  object expected"); Kernel#raise now rejects a circular cause: chain with
  ArgumentError "circular causes".

core/kernel: pass 1767->1796 (+29), fail 230->215, error 154->140, no regressions.
caller 7->16, caller_locations 0->16, raise 32->36.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…nged funcs)

Cover every new branch of callerSlice (default/start/length/nil-length/Range/
Float coercion/negative-arg ArgumentError/overshoot-nil vs empty-at-end),
caller_locations (Location shape, Range, overshoot, Kernel module-function
visibility) and the raise changes (make_exception #exception protocol incl. both
TypeError messages, bare-raise empty message, circular-cause ArgumentError,
cause==self non-circular, bad-cause TypeError). callerSlice, raiseExceptionObject
and nativeRaise are at 100% statement coverage.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The bare-raise fix (message "unhandled exception" -> "") is correct per MRI
4.0 (ruby -e 'begin;raise;rescue=>e;p e.message;end' => ""). The pre-existing
TestRaise/bare pinned the old buggy string; the arch lanes run the full
go test ./... and caught it (the internal/vm-only run in the wave missed it).
"unhandled exception" survives only as RuntimeError#detailed_message's display
default (exception_residuals_test.go still asserts that, unchanged).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 3d9c764 into main Sep 6, 2026
7 checks passed
@tannevaled
tannevaled deleted the wave15/kernel branch September 6, 2026 13:31
tannevaled added a commit that referenced this pull request Sep 6, 2026
…578)

Wave-15 merged three disjoint-file, bibliography-first conformance PRs:
- #576 core/io: fd-based IO constructors (IO.open/new/for_fd + mode/encoding/
  binmode/autoclose resolution per io.c) — core/io +107.
- #575 core/kernel: Kernel#caller_locations (unblocks the raise_spec collection
  cascade), caller start/length/range args, raise #exception protocol + empty
  bare-raise message per MRI — core/kernel +29.
- #577 core/regexp+enumerator: Regexp.linear_time?, quote/escape result
  encoding, \u-escape fixed_encoding, named_captures dup names, Lazy value
  packing — core/regexp +17, enumerator +2.

Measured total on merged main: 19179 passing (files loaded 2171, +7 that
previously failed to load). Local measure at TIMEOUT=25; CI runs TIMEOUT=60 so
its total is >= this floor.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant