Description
A bare receiverless identifier raises the wrong error class, with the wrong message,
and renders top-level self differently. Three divergences on one line — the first
thing a beginner sees after a typo.
main @f1af122 against MRI 4.0.5, same file:
wMRI : /tmp/bare.rb:1:in '<main>': undefined local variable or method 'w' for main (NameError)
rbgo: /tmp/bare.rb:1:in '<main>': undefined method 'w' for #<Object:0x0000000000000008> (NoMethodError)
| MRI 4.0.5 | rbgo | |
|---|---|---|
| class | NameError |
NoMethodError |
| wording | "undefined local variable or method" | "undefined method" |
top-level self |
main |
#<Object:0x…> |
The class matters beyond the text: NameError is the superclass of
NoMethodError, so rescue NameError catches both and rescue NoMethodError does
not catch MRI's. A program that distinguishes a typo'd variable from a missing
method behaves differently on the two engines.
MRI decides this in vm_insnhelper.c: a VM_CALL_VCALL (a bareword with no
receiver and no arguments) raises NameError through rb_name_err_raise, while a
call with a receiver or arguments raises NoMethodError. So the distinction is
carried by the call site's shape, not by what the lookup found — which is why it
cannot be fixed at the point of failure alone.
The third row is separate and wider: top-level self should print as main
(Init_Object gives the top-level object a singleton to_s returning "main").
Ours falls through to the default #<Object:0x…>, so every message naming
top-level self differs.
Costs no measured corpus examples — reported because it is on the first line of
output a user sees when they make the commonest possible mistake. Found by the
front-end wave (#716).
Activity
tannevaled commentedon Oct 3, 2026
All three rows now match
ruby 4.0.5byte for byte onmain@325bea5, samefile, same invocation as the report:
325bea5NameErrorNameErrorselfmainmainAnd the discriminating controls, which is what makes this more than a
message comparison — the issue's own point was that the distinction has to be
carried by the call site's shape rather than by what the lookup found, so a fix
that simply renamed the error would also have made these two wrong:
A receiver or an argument still gives
NoMethodErroron both sides, so theVM_CALL_VCALLshape is what decides it here as in MRI.Closing. MRI's source-excerpt-with-caret in the error report is a separate
surface and is not part of any row above.