Skip to content

vm: singleton_method and public_method raise MRI's NameError - #985

Merged
tannevaled merged 2 commits into
mainfrom
name-error-receivers
Nov 29, 2026
Merged

tannevaled merged 2 commits into
mainfrom
name-error-receivers

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

What

Object#singleton_method and Object#public_method now raise the NameError MRI 4.0.10 raises.

call before (rbgo) after = MRI 4.0.10
Foo.new.singleton_method(:nope) (inspect → FOO!) undefined singleton method 'nope' for Foo, no #name / #receiver undefined singleton method 'nope' for 'FOO!', name == :nope, receiver.equal?(obj)
Foo.new.public_method(:priv) ... for Foo is not public method 'priv' for class 'Foo' is private, receiver == Foo
Foo.public_method(:nope) ... for Foo undefined method 'nope' for class '#<Class:Foo>'

How

  • singleton_method follows rb_obj_singleton_method (proc.c v4.0.9:2362): the message names the receiver through '%2$s', and #name / #receiver are set.
  • public_method follows rb_print_undef / rb_print_inaccessible on CLASS_OF(receiver): "for class '…'", "is private" / "is protected", with that class as #receiver. For a class receiver, or an object with a singleton class, that class is the singleton class.
  • nameErrMesgRecv renders '%2$s' the way name_err_mesg_to_str does (error.c v4.0.10:2633-2655):
    • nil, true and false print literally;
    • a class or module prints its #name, not its #inspect;
    • anything else prints #inspect through #to_s, so an #inspect returning 42 shows as '42';
    • when #name / #inspect raises, it falls back to rb_any_to_s.

Evidence

  • rubydiff -a rbgo -b ruby 4.0.10, using three probe programs that cover 17 receivers:
    • this branch: AGREE on all three;
    • origin/main (ed535866): DIVERGE.
  • The tests in internal/vm/name_error_receiver_test.go take their expected text from MRI 4.0.10's own output.
  • mutsweep: 4 of the 4 changed guards are caught by these tests. A first pass found 3 survivors (no true receiver, no class whose #inspect differs from #name, and a redundant class branch in the fallback). Two of them got test cases; the redundant branch was removed.
  • gofmt -l: clean.

The full local go test ./internal/vm/ hit the 10-minute package timeout at load average 145, with no test failing. CI is the measurement here.

🤖 Generated with Claude Code

tannevaled and others added 2 commits November 29, 2026 13:48
Object#singleton_method named the receiver's class ("for Foo") where MRI
names the receiver itself ("for 'FOO!'", proc.c rb_obj_singleton_method
with '%2$s'), and set neither NameError#name nor #receiver.

Object#public_method said "for Foo" / "is not public" where MRI raises
through rb_print_undef / rb_print_inaccessible on CLASS_OF(receiver):
"for class 'Foo'", "is private" / "is protected", with that class -- the
singleton class when there is one -- as #receiver.

nameErrMesgRecv renders '%2$s' as error.c name_err_mesg_to_str does:
nil/true/false literally, a class or module by #name, anything else by
#inspect through #to_s, and rb_any_to_s when #inspect raises.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The coverage ratchet on ubuntu read nameErrMesgRecv at 92.9%: the
re-panic of a non-RubyError value in its recover was never reached.
The new whitebox test gives the class a native #name that panics with a
Go value and asserts the panic reaches the caller unchanged instead of
being rendered as "#<Class:0x...>". Control (same -run, the new test
skipped): 92.9%; with it: 100.0%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 5aa36b8 into main Nov 29, 2026
26 checks passed
@tannevaled
tannevaled deleted the name-error-receivers branch November 29, 2026 17:19
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